sparse: Add '__vector_size__' to ignored attributes
[smatch.git] / parse.c
blob2dcd488a78d1e55842fc3aaa6f03b8d212b0e00f
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},
423 /* Storage class */
424 { "auto", NS_TYPEDEF, .op = &auto_op },
425 { "register", NS_TYPEDEF, .op = &register_op },
426 { "static", NS_TYPEDEF, .op = &static_op },
427 { "extern", NS_TYPEDEF, .op = &extern_op },
428 { "__thread", NS_TYPEDEF, .op = &thread_op },
430 /* Statement */
431 { "if", NS_KEYWORD, .op = &if_op },
432 { "return", NS_KEYWORD, .op = &return_op },
433 { "break", NS_KEYWORD, .op = &loop_iter_op },
434 { "continue", NS_KEYWORD, .op = &loop_iter_op },
435 { "default", NS_KEYWORD, .op = &default_op },
436 { "case", NS_KEYWORD, .op = &case_op },
437 { "switch", NS_KEYWORD, .op = &switch_op },
438 { "for", NS_KEYWORD, .op = &for_op },
439 { "while", NS_KEYWORD, .op = &while_op },
440 { "do", NS_KEYWORD, .op = &do_op },
441 { "goto", NS_KEYWORD, .op = &goto_op },
442 { "__context__",NS_KEYWORD, .op = &__context___op },
443 { "__range__", NS_KEYWORD, .op = &range_op },
444 { "asm", NS_KEYWORD, .op = &asm_op },
445 { "__asm", NS_KEYWORD, .op = &asm_op },
446 { "__asm__", NS_KEYWORD, .op = &asm_op },
448 /* Attribute */
449 { "packed", NS_KEYWORD, .op = &packed_op },
450 { "__packed__", NS_KEYWORD, .op = &packed_op },
451 { "aligned", NS_KEYWORD, .op = &aligned_op },
452 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
453 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
454 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
455 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
456 { "force", NS_KEYWORD, .op = &attr_force_op },
457 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
458 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
459 { "address_space",NS_KEYWORD, .op = &address_space_op },
460 { "mode", NS_KEYWORD, .op = &mode_op },
461 { "context", NS_KEYWORD, .op = &context_op },
462 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
463 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
464 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
465 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
466 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
467 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
468 {"const", 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 },
472 { "__mode__", NS_KEYWORD, .op = &mode_op },
473 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
474 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
475 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
476 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
477 { "SI", NS_KEYWORD, .op = &mode_SI_op },
478 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
479 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
480 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
481 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
482 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
483 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
484 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
487 const char *ignored_attributes[] = {
488 "alias",
489 "__alias__",
490 "alloc_size",
491 "__alloc_size__",
492 "always_inline",
493 "__always_inline__",
494 "artificial",
495 "__artificial__",
496 "bounded",
497 "__bounded__",
498 "cdecl",
499 "__cdecl__",
500 "cold",
501 "__cold__",
502 "constructor",
503 "__constructor__",
504 "deprecated",
505 "__deprecated__",
506 "destructor",
507 "__destructor__",
508 "dllexport",
509 "__dllexport__",
510 "dllimport",
511 "__dllimport__",
512 "externally_visible",
513 "__externally_visible__",
514 "fastcall",
515 "__fastcall__",
516 "format",
517 "__format__",
518 "format_arg",
519 "__format_arg__",
520 "hot",
521 "__hot__",
522 "l1_text",
523 "__l1_text__",
524 "l1_data",
525 "__l1_data__",
526 "l2",
527 "__l2__",
528 "may_alias",
529 "__may_alias__",
530 "malloc",
531 "__malloc__",
532 "may_alias",
533 "__may_alias__",
534 "model",
535 "__model__",
536 "ms_abi",
537 "__ms_abi__",
538 "ms_hook_prologue",
539 "__ms_hook_prologue__",
540 "naked",
541 "__naked__",
542 "no_instrument_function",
543 "__no_instrument_function__",
544 "noinline",
545 "__noinline__",
546 "nonnull",
547 "__nonnull",
548 "__nonnull__",
549 "nothrow",
550 "__nothrow",
551 "__nothrow__",
552 "regparm",
553 "__regparm__",
554 "section",
555 "__section__",
556 "sentinel",
557 "__sentinel__",
558 "signal",
559 "__signal__",
560 "stdcall",
561 "__stdcall__",
562 "syscall_linkage",
563 "__syscall_linkage__",
564 "sysv_abi",
565 "__sysv_abi__",
566 "unused",
567 "__unused__",
568 "used",
569 "__used__",
570 "vector_size",
571 "__vector_size__",
572 "visibility",
573 "__visibility__",
574 "warn_unused_result",
575 "__warn_unused_result__",
576 "warning",
577 "__warning__",
578 "weak",
579 "__weak__",
583 void init_parser(int stream)
585 int i;
586 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
587 struct init_keyword *ptr = keyword_table + i;
588 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
589 sym->ident->keyword = 1;
590 if (ptr->ns == NS_TYPEDEF)
591 sym->ident->reserved = 1;
592 sym->ctype.modifiers = ptr->modifiers;
593 sym->ctype.base_type = ptr->type;
594 sym->op = ptr->op;
597 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
598 const char * name = ignored_attributes[i];
599 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
600 NS_KEYWORD);
601 sym->ident->keyword = 1;
602 sym->op = &ignore_attr_op;
607 // Add a symbol to the list of function-local symbols
608 static void fn_local_symbol(struct symbol *sym)
610 if (function_symbol_list)
611 add_symbol(function_symbol_list, sym);
614 static int SENTINEL_ATTR match_idents(struct token *token, ...)
616 va_list args;
617 struct ident * next;
619 if (token_type(token) != TOKEN_IDENT)
620 return 0;
622 va_start(args, token);
623 do {
624 next = va_arg(args, struct ident *);
625 } while (next && token->ident != next);
626 va_end(args);
628 return next && token->ident == next;
632 struct statement *alloc_statement(struct position pos, int type)
634 struct statement *stmt = __alloc_statement(0);
635 stmt->type = type;
636 stmt->pos = pos;
637 return stmt;
640 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
642 static void apply_modifiers(struct position pos, struct decl_state *ctx)
644 struct symbol *ctype;
645 if (!ctx->mode)
646 return;
647 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
648 if (!ctype)
649 sparse_error(pos, "don't know how to apply mode to %s",
650 show_typename(ctx->ctype.base_type));
651 else
652 ctx->ctype.base_type = ctype;
656 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
658 struct symbol *sym = alloc_symbol(pos, type);
660 sym->ctype.base_type = ctype->base_type;
661 sym->ctype.modifiers = ctype->modifiers;
663 ctype->base_type = sym;
664 ctype->modifiers = 0;
665 return sym;
669 * NOTE! NS_LABEL is not just a different namespace,
670 * it also ends up using function scope instead of the
671 * regular symbol scope.
673 struct symbol *label_symbol(struct token *token)
675 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
676 if (!sym) {
677 sym = alloc_symbol(token->pos, SYM_LABEL);
678 bind_symbol(sym, token->ident, NS_LABEL);
679 fn_local_symbol(sym);
681 return sym;
684 static struct token *struct_union_enum_specifier(enum type type,
685 struct token *token, struct decl_state *ctx,
686 struct token *(*parse)(struct token *, struct symbol *))
688 struct symbol *sym;
689 struct position *repos;
691 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
692 if (token_type(token) == TOKEN_IDENT) {
693 sym = lookup_symbol(token->ident, NS_STRUCT);
694 if (!sym ||
695 (is_outer_scope(sym->scope) &&
696 (match_op(token->next,';') || match_op(token->next,'{')))) {
697 // Either a new symbol, or else an out-of-scope
698 // symbol being redefined.
699 sym = alloc_symbol(token->pos, type);
700 bind_symbol(sym, token->ident, NS_STRUCT);
702 if (sym->type != type)
703 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
704 ctx->ctype.base_type = sym;
705 repos = &token->pos;
706 token = token->next;
707 if (match_op(token, '{')) {
708 // The following test is actually wrong for empty
709 // structs, but (1) they are not C99, (2) gcc does
710 // the same thing, and (3) it's easier.
711 if (sym->symbol_list)
712 error_die(token->pos, "redefinition of %s", show_typename (sym));
713 sym->pos = *repos;
714 token = parse(token->next, sym);
715 token = expect(token, '}', "at end of struct-union-enum-specifier");
717 // Mark the structure as needing re-examination
718 sym->examined = 0;
719 sym->endpos = token->pos;
721 return token;
724 // private struct/union/enum type
725 if (!match_op(token, '{')) {
726 sparse_error(token->pos, "expected declaration");
727 ctx->ctype.base_type = &bad_ctype;
728 return token;
731 sym = alloc_symbol(token->pos, type);
732 token = parse(token->next, sym);
733 ctx->ctype.base_type = sym;
734 token = expect(token, '}', "at end of specifier");
735 sym->endpos = token->pos;
737 return token;
740 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
742 struct symbol *field, *last = NULL;
743 struct token *res;
744 res = struct_declaration_list(token, &sym->symbol_list);
745 FOR_EACH_PTR(sym->symbol_list, field) {
746 if (!field->ident) {
747 struct symbol *base = field->ctype.base_type;
748 if (base && base->type == SYM_BITFIELD)
749 continue;
751 if (last)
752 last->next_subobject = field;
753 last = field;
754 } END_FOR_EACH_PTR(field);
755 return res;
758 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
760 return struct_declaration_list(token, &sym->symbol_list);
763 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
765 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
768 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
770 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
774 typedef struct {
775 int x;
776 unsigned long long y;
777 } Num;
779 static void upper_boundary(Num *n, Num *v)
781 if (n->x > v->x)
782 return;
783 if (n->x < v->x) {
784 *n = *v;
785 return;
787 if (n->y < v->y)
788 n->y = v->y;
791 static void lower_boundary(Num *n, Num *v)
793 if (n->x < v->x)
794 return;
795 if (n->x > v->x) {
796 *n = *v;
797 return;
799 if (n->y > v->y)
800 n->y = v->y;
803 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
805 int shift = type->bit_size;
806 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
808 if (!is_unsigned)
809 shift--;
810 if (upper->x == 0 && upper->y >> shift)
811 return 0;
812 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
813 return 1;
814 return 0;
817 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
819 if (s1->bit_size < s2->bit_size) {
820 s1 = s2;
821 } else if (s1->bit_size == s2->bit_size) {
822 if (s2->ctype.modifiers & MOD_UNSIGNED)
823 s1 = s2;
825 if (s1->bit_size < bits_in_int)
826 return &int_ctype;
827 return s1;
830 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
832 struct symbol *sym;
834 FOR_EACH_PTR(list, sym) {
835 struct expression *expr = sym->initializer;
836 struct symbol *ctype;
837 if (expr->type != EXPR_VALUE)
838 continue;
839 ctype = expr->ctype;
840 if (ctype->bit_size == base_type->bit_size)
841 continue;
842 cast_value(expr, base_type, expr, ctype);
843 } END_FOR_EACH_PTR(sym);
846 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
848 unsigned long long lastval = 0;
849 struct symbol *ctype = NULL, *base_type = NULL;
850 Num upper = {-1, 0}, lower = {1, 0};
852 parent->examined = 1;
853 parent->ctype.base_type = &int_ctype;
854 while (token_type(token) == TOKEN_IDENT) {
855 struct expression *expr = NULL;
856 struct token *next = token->next;
857 struct symbol *sym;
859 if (match_op(next, '=')) {
860 next = constant_expression(next->next, &expr);
861 lastval = get_expression_value(expr);
862 ctype = &void_ctype;
863 if (expr && expr->ctype)
864 ctype = expr->ctype;
865 } else if (!ctype) {
866 ctype = &int_ctype;
867 } else if (is_int_type(ctype)) {
868 lastval++;
869 } else {
870 error_die(token->pos, "can't increment the last enum member");
873 if (!expr) {
874 expr = alloc_expression(token->pos, EXPR_VALUE);
875 expr->value = lastval;
876 expr->ctype = ctype;
879 sym = alloc_symbol(token->pos, SYM_NODE);
880 bind_symbol(sym, token->ident, NS_SYMBOL);
881 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
882 sym->initializer = expr;
883 sym->enum_member = 1;
884 sym->ctype.base_type = parent;
885 add_ptr_list(&parent->symbol_list, sym);
887 if (base_type != &bad_ctype) {
888 if (ctype->type == SYM_NODE)
889 ctype = ctype->ctype.base_type;
890 if (ctype->type == SYM_ENUM) {
891 if (ctype == parent)
892 ctype = base_type;
893 else
894 ctype = ctype->ctype.base_type;
897 * base_type rules:
898 * - if all enums are of the same type, then
899 * the base_type is that type (two first
900 * cases)
901 * - if enums are of different types, they
902 * all have to be integer types, and the
903 * base type is at least "int_ctype".
904 * - otherwise the base_type is "bad_ctype".
906 if (!base_type) {
907 base_type = ctype;
908 } else if (ctype == base_type) {
909 /* nothing */
910 } else if (is_int_type(base_type) && is_int_type(ctype)) {
911 base_type = bigger_enum_type(base_type, ctype);
912 } else
913 base_type = &bad_ctype;
914 parent->ctype.base_type = base_type;
916 if (is_int_type(base_type)) {
917 Num v = {.y = lastval};
918 if (ctype->ctype.modifiers & MOD_UNSIGNED)
919 v.x = 0;
920 else if ((long long)lastval >= 0)
921 v.x = 0;
922 else
923 v.x = -1;
924 upper_boundary(&upper, &v);
925 lower_boundary(&lower, &v);
927 token = next;
929 sym->endpos = token->pos;
931 if (!match_op(token, ','))
932 break;
933 token = token->next;
935 if (!base_type) {
936 sparse_error(token->pos, "bad enum definition");
937 base_type = &bad_ctype;
939 else if (!is_int_type(base_type))
940 base_type = base_type;
941 else if (type_is_ok(base_type, &upper, &lower))
942 base_type = base_type;
943 else if (type_is_ok(&int_ctype, &upper, &lower))
944 base_type = &int_ctype;
945 else if (type_is_ok(&uint_ctype, &upper, &lower))
946 base_type = &uint_ctype;
947 else if (type_is_ok(&long_ctype, &upper, &lower))
948 base_type = &long_ctype;
949 else if (type_is_ok(&ulong_ctype, &upper, &lower))
950 base_type = &ulong_ctype;
951 else if (type_is_ok(&llong_ctype, &upper, &lower))
952 base_type = &llong_ctype;
953 else if (type_is_ok(&ullong_ctype, &upper, &lower))
954 base_type = &ullong_ctype;
955 else
956 base_type = &bad_ctype;
957 parent->ctype.base_type = base_type;
958 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
959 parent->examined = 0;
961 cast_enum_list(parent->symbol_list, base_type);
963 return token;
966 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
968 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
969 struct ctype *ctype = &ctx->ctype.base_type->ctype;
971 if (!ctype->base_type)
972 ctype->base_type = &incomplete_ctype;
974 return ret;
977 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
979 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
981 struct symbol *sym;
983 if (!match_op(token, '(')) {
984 sparse_error(token->pos, "expected '(' after typeof");
985 return token;
987 if (lookup_type(token->next)) {
988 token = typename(token->next, &sym, NULL);
989 ctx->ctype.base_type = sym->ctype.base_type;
990 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
991 } else {
992 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
993 token = parse_expression(token->next, &typeof_sym->initializer);
995 typeof_sym->endpos = token->pos;
996 if (!typeof_sym->initializer) {
997 sparse_error(token->pos, "expected expression after the '(' token");
998 typeof_sym = &bad_ctype;
1000 ctx->ctype.base_type = typeof_sym;
1002 return expect(token, ')', "after typeof");
1005 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1007 struct expression *expr = NULL;
1008 if (match_op(token, '('))
1009 token = parens_expression(token, &expr, "in attribute");
1010 return token;
1013 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1015 if (!ctx->ctype.alignment)
1016 ctx->ctype.alignment = 1;
1017 return token;
1020 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1022 int alignment = max_alignment;
1023 struct expression *expr = NULL;
1025 if (match_op(token, '(')) {
1026 token = parens_expression(token, &expr, "in attribute");
1027 if (expr)
1028 alignment = const_expression_value(expr);
1030 if (alignment & (alignment-1)) {
1031 warning(token->pos, "I don't like non-power-of-2 alignments");
1032 return token;
1033 } else if (alignment > ctx->ctype.alignment)
1034 ctx->ctype.alignment = alignment;
1035 return token;
1038 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1040 if (ctx->modifiers & qual)
1041 warning(*pos, "duplicate %s", modifier_string(qual));
1042 ctx->modifiers |= qual;
1045 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1047 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1048 return token;
1051 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1053 struct expression *expr = NULL;
1054 int as;
1055 token = expect(token, '(', "after address_space attribute");
1056 token = conditional_expression(token, &expr);
1057 if (expr) {
1058 as = const_expression_value(expr);
1059 if (Waddress_space && as)
1060 ctx->ctype.as = as;
1062 token = expect(token, ')', "after address_space attribute");
1063 return token;
1066 static struct symbol *to_QI_mode(struct symbol *ctype)
1068 if (ctype->ctype.base_type != &int_type)
1069 return NULL;
1070 if (ctype == &char_ctype)
1071 return ctype;
1072 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1073 : &schar_ctype;
1076 static struct symbol *to_HI_mode(struct symbol *ctype)
1078 if (ctype->ctype.base_type != &int_type)
1079 return NULL;
1080 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1081 : &sshort_ctype;
1084 static struct symbol *to_SI_mode(struct symbol *ctype)
1086 if (ctype->ctype.base_type != &int_type)
1087 return NULL;
1088 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1089 : &sint_ctype;
1092 static struct symbol *to_DI_mode(struct symbol *ctype)
1094 if (ctype->ctype.base_type != &int_type)
1095 return NULL;
1096 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1097 : &sllong_ctype;
1100 static struct symbol *to_TI_mode(struct symbol *ctype)
1102 if (ctype->ctype.base_type != &int_type)
1103 return NULL;
1104 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1105 : &slllong_ctype;
1108 static struct symbol *to_word_mode(struct symbol *ctype)
1110 if (ctype->ctype.base_type != &int_type)
1111 return NULL;
1112 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1113 : &slong_ctype;
1116 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1118 token = expect(token, '(', "after mode attribute");
1119 if (token_type(token) == TOKEN_IDENT) {
1120 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1121 if (mode && mode->op->type == KW_MODE)
1122 ctx->mode = mode->op;
1123 else
1124 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1125 token = token->next;
1126 } else
1127 sparse_error(token->pos, "expect attribute mode symbol\n");
1128 token = expect(token, ')', "after mode attribute");
1129 return token;
1132 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1134 struct context *context = alloc_context();
1135 struct expression *args[3];
1136 int argc = 0;
1138 token = expect(token, '(', "after context attribute");
1139 while (!match_op(token, ')')) {
1140 struct expression *expr = NULL;
1141 token = conditional_expression(token, &expr);
1142 if (!expr)
1143 break;
1144 if (argc < 3)
1145 args[argc++] = expr;
1146 if (!match_op(token, ','))
1147 break;
1148 token = token->next;
1151 switch(argc) {
1152 case 0:
1153 sparse_error(token->pos, "expected context input/output values");
1154 break;
1155 case 1:
1156 context->in = get_expression_value(args[0]);
1157 break;
1158 case 2:
1159 context->in = get_expression_value(args[0]);
1160 context->out = get_expression_value(args[1]);
1161 break;
1162 case 3:
1163 context->context = args[0];
1164 context->in = get_expression_value(args[1]);
1165 context->out = get_expression_value(args[2]);
1166 break;
1169 if (argc)
1170 add_ptr_list(&ctx->ctype.contexts, context);
1172 token = expect(token, ')', "after context attribute");
1173 return token;
1176 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1178 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1179 ctx->ctype.base_type->designated_init = 1;
1180 else
1181 warning(token->pos, "attribute designated_init applied to non-structure type");
1182 return token;
1185 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1187 if (Wtransparent_union)
1188 warning(token->pos, "ignoring attribute __transparent_union__");
1189 return token;
1192 static struct token *recover_unknown_attribute(struct token *token)
1194 struct expression *expr = NULL;
1196 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1197 token = token->next;
1198 if (match_op(token, '('))
1199 token = parens_expression(token, &expr, "in attribute");
1200 return token;
1203 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1205 token = expect(token, '(', "after attribute");
1206 token = expect(token, '(', "after attribute");
1208 for (;;) {
1209 struct ident *attribute_name;
1210 struct symbol *attr;
1212 if (eof_token(token))
1213 break;
1214 if (match_op(token, ';'))
1215 break;
1216 if (token_type(token) != TOKEN_IDENT)
1217 break;
1218 attribute_name = token->ident;
1219 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1220 if (attr && attr->op->attribute)
1221 token = attr->op->attribute(token->next, attr, ctx);
1222 else
1223 token = recover_unknown_attribute(token);
1225 if (!match_op(token, ','))
1226 break;
1227 token = token->next;
1230 token = expect(token, ')', "after attribute");
1231 token = expect(token, ')', "after attribute");
1232 return token;
1235 static const char *storage_class[] =
1237 [STypedef] = "typedef",
1238 [SAuto] = "auto",
1239 [SExtern] = "extern",
1240 [SStatic] = "static",
1241 [SRegister] = "register",
1242 [SForced] = "[force]"
1245 static unsigned long storage_modifiers(struct decl_state *ctx)
1247 static unsigned long mod[] =
1249 [SAuto] = MOD_AUTO,
1250 [SExtern] = MOD_EXTERN,
1251 [SStatic] = MOD_STATIC,
1252 [SRegister] = MOD_REGISTER
1254 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1255 | (ctx->is_tls ? MOD_TLS : 0);
1258 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1260 /* __thread can be used alone, or with extern or static */
1261 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1262 sparse_error(*pos, "__thread can only be used alone, or with "
1263 "extern or static");
1264 return;
1267 if (!ctx->storage_class) {
1268 ctx->storage_class = class;
1269 return;
1271 if (ctx->storage_class == class)
1272 sparse_error(*pos, "duplicate %s", storage_class[class]);
1273 else
1274 sparse_error(*pos, "multiple storage classes");
1277 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1279 set_storage_class(&next->pos, ctx, STypedef);
1280 return next;
1283 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1285 set_storage_class(&next->pos, ctx, SAuto);
1286 return next;
1289 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1291 set_storage_class(&next->pos, ctx, SRegister);
1292 return next;
1295 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1297 set_storage_class(&next->pos, ctx, SStatic);
1298 return next;
1301 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1303 set_storage_class(&next->pos, ctx, SExtern);
1304 return next;
1307 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1309 /* This GCC extension can be used alone, or with extern or static */
1310 if (!ctx->storage_class || ctx->storage_class == SStatic
1311 || ctx->storage_class == SExtern) {
1312 ctx->is_tls = 1;
1313 } else {
1314 sparse_error(next->pos, "__thread can only be used alone, or "
1315 "with extern or static");
1318 return next;
1321 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1323 set_storage_class(&token->pos, ctx, SForced);
1324 return token;
1327 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1329 ctx->is_inline = 1;
1330 return next;
1333 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1335 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1336 return next;
1339 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1341 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1342 return next;
1345 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1347 unsigned long mod = thistype->modifiers;
1349 if (mod)
1350 apply_qualifier(&pos, ctype, mod);
1352 /* Context */
1353 concat_ptr_list((struct ptr_list *)thistype->contexts,
1354 (struct ptr_list **)&ctype->contexts);
1356 /* Alignment */
1357 if (thistype->alignment > ctype->alignment)
1358 ctype->alignment = thistype->alignment;
1360 /* Address space */
1361 if (thistype->as)
1362 ctype->as = thistype->as;
1365 static void specifier_conflict(struct position pos, int what, struct ident *new)
1367 const char *old;
1368 if (what & (Set_S | Set_T))
1369 goto Catch_all;
1370 if (what & Set_Char)
1371 old = "char";
1372 else if (what & Set_Double)
1373 old = "double";
1374 else if (what & Set_Float)
1375 old = "float";
1376 else if (what & Set_Signed)
1377 old = "signed";
1378 else if (what & Set_Unsigned)
1379 old = "unsigned";
1380 else if (what & Set_Short)
1381 old = "short";
1382 else if (what & Set_Long)
1383 old = "long";
1384 else
1385 old = "long long";
1386 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1387 old, show_ident(new));
1388 return;
1390 Catch_all:
1391 sparse_error(pos, "two or more data types in declaration specifiers");
1394 static struct symbol * const int_types[] =
1395 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1396 static struct symbol * const signed_types[] =
1397 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1398 &slllong_ctype};
1399 static struct symbol * const unsigned_types[] =
1400 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1401 &ulllong_ctype};
1402 static struct symbol * const real_types[] =
1403 {&float_ctype, &double_ctype, &ldouble_ctype};
1404 static struct symbol * const char_types[] =
1405 {&char_ctype, &schar_ctype, &uchar_ctype};
1406 static struct symbol * const * const types[] = {
1407 int_types + 1, signed_types + 1, unsigned_types + 1,
1408 real_types + 1, char_types, char_types + 1, char_types + 2
1411 struct symbol *ctype_integer(int size, int want_unsigned)
1413 return types[want_unsigned ? CUInt : CInt][size];
1416 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1418 while (token_type(t) == TOKEN_IDENT) {
1419 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1420 if (!s)
1421 break;
1422 if (s->type != SYM_KEYWORD)
1423 break;
1424 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1425 break;
1426 t = t->next;
1427 if (s->op->declarator)
1428 t = s->op->declarator(t, ctx);
1430 return t;
1433 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1435 int seen = 0;
1436 int class = CInt;
1437 int size = 0;
1439 while (token_type(token) == TOKEN_IDENT) {
1440 struct symbol *s = lookup_symbol(token->ident,
1441 NS_TYPEDEF | NS_SYMBOL);
1442 if (!s || !(s->namespace & NS_TYPEDEF))
1443 break;
1444 if (s->type != SYM_KEYWORD) {
1445 if (seen & Set_Any)
1446 break;
1447 seen |= Set_S | Set_T;
1448 ctx->ctype.base_type = s->ctype.base_type;
1449 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1450 token = token->next;
1451 continue;
1453 if (s->op->type & KW_SPECIFIER) {
1454 if (seen & s->op->test) {
1455 specifier_conflict(token->pos,
1456 seen & s->op->test,
1457 token->ident);
1458 break;
1460 seen |= s->op->set;
1461 class += s->op->class;
1462 if (s->op->type & KW_SHORT) {
1463 size = -1;
1464 } else if (s->op->type & KW_LONG && size++) {
1465 if (class == CReal) {
1466 specifier_conflict(token->pos,
1467 Set_Vlong,
1468 &double_ident);
1469 break;
1471 seen |= Set_Vlong;
1474 token = token->next;
1475 if (s->op->declarator)
1476 token = s->op->declarator(token, ctx);
1477 if (s->op->type & KW_EXACT) {
1478 ctx->ctype.base_type = s->ctype.base_type;
1479 ctx->ctype.modifiers |= s->ctype.modifiers;
1483 if (!(seen & Set_S)) { /* not set explicitly? */
1484 struct symbol *base = &incomplete_ctype;
1485 if (seen & Set_Any)
1486 base = types[class][size];
1487 ctx->ctype.base_type = base;
1490 if (ctx->ctype.modifiers & MOD_BITWISE) {
1491 struct symbol *type;
1492 ctx->ctype.modifiers &= ~MOD_BITWISE;
1493 if (!is_int_type(ctx->ctype.base_type)) {
1494 sparse_error(token->pos, "invalid modifier");
1495 return token;
1497 type = alloc_symbol(token->pos, SYM_BASETYPE);
1498 *type = *ctx->ctype.base_type;
1499 type->ctype.modifiers &= ~MOD_SPECIFIER;
1500 type->ctype.base_type = ctx->ctype.base_type;
1501 type->type = SYM_RESTRICT;
1502 ctx->ctype.base_type = type;
1503 create_fouled(type);
1505 return token;
1508 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1510 struct expression *expr = NULL;
1512 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1513 token = token->next;
1514 token = parse_expression(token, &expr);
1515 sym->array_size = expr;
1516 return token;
1519 static struct token *parameter_type_list(struct token *, struct symbol *);
1520 static struct token *identifier_list(struct token *, struct symbol *);
1521 static struct token *declarator(struct token *token, struct decl_state *ctx);
1523 static struct token *skip_attribute(struct token *token)
1525 token = token->next;
1526 if (match_op(token, '(')) {
1527 int depth = 1;
1528 token = token->next;
1529 while (depth && !eof_token(token)) {
1530 if (token_type(token) == TOKEN_SPECIAL) {
1531 if (token->special == '(')
1532 depth++;
1533 else if (token->special == ')')
1534 depth--;
1536 token = token->next;
1539 return token;
1542 static struct token *skip_attributes(struct token *token)
1544 struct symbol *keyword;
1545 for (;;) {
1546 if (token_type(token) != TOKEN_IDENT)
1547 break;
1548 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1549 if (!keyword || keyword->type != SYM_KEYWORD)
1550 break;
1551 if (!(keyword->op->type & KW_ATTRIBUTE))
1552 break;
1553 token = expect(token->next, '(', "after attribute");
1554 token = expect(token, '(', "after attribute");
1555 for (;;) {
1556 if (eof_token(token))
1557 break;
1558 if (match_op(token, ';'))
1559 break;
1560 if (token_type(token) != TOKEN_IDENT)
1561 break;
1562 token = skip_attribute(token);
1563 if (!match_op(token, ','))
1564 break;
1565 token = token->next;
1567 token = expect(token, ')', "after attribute");
1568 token = expect(token, ')', "after attribute");
1570 return token;
1573 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1575 struct symbol *keyword;
1576 for (;;) {
1577 if (token_type(token) != TOKEN_IDENT)
1578 break;
1579 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1580 if (!keyword || keyword->type != SYM_KEYWORD)
1581 break;
1582 if (!(keyword->op->type & keywords))
1583 break;
1584 token = keyword->op->declarator(token->next, ctx);
1585 keywords &= KW_ATTRIBUTE;
1587 return token;
1590 static int is_nested(struct token *token, struct token **p,
1591 int prefer_abstract)
1594 * This can be either a parameter list or a grouping.
1595 * For the direct (non-abstract) case, we know if must be
1596 * a parameter list if we already saw the identifier.
1597 * For the abstract case, we know if must be a parameter
1598 * list if it is empty or starts with a type.
1600 struct token *next = token->next;
1602 *p = next = skip_attributes(next);
1604 if (token_type(next) == TOKEN_IDENT) {
1605 if (lookup_type(next))
1606 return !prefer_abstract;
1607 return 1;
1610 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1611 return 0;
1613 return 1;
1616 enum kind {
1617 Empty, K_R, Proto, Bad_Func,
1620 static enum kind which_func(struct token *token,
1621 struct ident **n,
1622 int prefer_abstract)
1624 struct token *next = token->next;
1626 if (token_type(next) == TOKEN_IDENT) {
1627 if (lookup_type(next))
1628 return Proto;
1629 /* identifier list not in definition; complain */
1630 if (prefer_abstract)
1631 warning(token->pos,
1632 "identifier list not in definition");
1633 return K_R;
1636 if (token_type(next) != TOKEN_SPECIAL)
1637 return Bad_Func;
1639 if (next->special == ')') {
1640 /* don't complain about those */
1641 if (!n || match_op(next->next, ';'))
1642 return Empty;
1643 warning(next->pos,
1644 "non-ANSI function declaration of function '%s'",
1645 show_ident(*n));
1646 return Empty;
1649 if (next->special == SPECIAL_ELLIPSIS) {
1650 warning(next->pos,
1651 "variadic functions must have one named argument");
1652 return Proto;
1655 return Bad_Func;
1658 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1660 struct ctype *ctype = &ctx->ctype;
1661 struct token *next;
1662 struct ident **p = ctx->ident;
1664 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1665 *ctx->ident = token->ident;
1666 token = token->next;
1667 } else if (match_op(token, '(') &&
1668 is_nested(token, &next, ctx->prefer_abstract)) {
1669 struct symbol *base_type = ctype->base_type;
1670 if (token->next != next)
1671 next = handle_attributes(token->next, ctx,
1672 KW_ATTRIBUTE);
1673 token = declarator(next, ctx);
1674 token = expect(token, ')', "in nested declarator");
1675 while (ctype->base_type != base_type)
1676 ctype = &ctype->base_type->ctype;
1677 p = NULL;
1680 if (match_op(token, '(')) {
1681 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1682 struct symbol *fn;
1683 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1684 token = token->next;
1685 if (kind == K_R)
1686 token = identifier_list(token, fn);
1687 else if (kind == Proto)
1688 token = parameter_type_list(token, fn);
1689 token = expect(token, ')', "in function declarator");
1690 fn->endpos = token->pos;
1691 return token;
1694 while (match_op(token, '[')) {
1695 struct symbol *array;
1696 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1697 token = abstract_array_declarator(token->next, array);
1698 token = expect(token, ']', "in abstract_array_declarator");
1699 array->endpos = token->pos;
1700 ctype = &array->ctype;
1702 return token;
1705 static struct token *pointer(struct token *token, struct decl_state *ctx)
1707 while (match_op(token,'*')) {
1708 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1709 ptr->ctype.modifiers = ctx->ctype.modifiers;
1710 ptr->ctype.base_type = ctx->ctype.base_type;
1711 ptr->ctype.as = ctx->ctype.as;
1712 ptr->ctype.contexts = ctx->ctype.contexts;
1713 ctx->ctype.modifiers = 0;
1714 ctx->ctype.base_type = ptr;
1715 ctx->ctype.as = 0;
1716 ctx->ctype.contexts = NULL;
1717 ctx->ctype.alignment = 0;
1719 token = handle_qualifiers(token->next, ctx);
1720 ctx->ctype.base_type->endpos = token->pos;
1722 return token;
1725 static struct token *declarator(struct token *token, struct decl_state *ctx)
1727 token = pointer(token, ctx);
1728 return direct_declarator(token, ctx);
1731 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1733 struct ctype *ctype = &ctx->ctype;
1734 struct expression *expr;
1735 struct symbol *bitfield;
1736 long long width;
1738 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1739 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1740 show_typename(ctype->base_type));
1741 // Parse this to recover gracefully.
1742 return conditional_expression(token->next, &expr);
1745 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1746 token = conditional_expression(token->next, &expr);
1747 width = const_expression_value(expr);
1748 bitfield->bit_size = width;
1750 if (width < 0 || width > INT_MAX) {
1751 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1752 width = -1;
1753 } else if (*ctx->ident && width == 0) {
1754 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1755 show_ident(*ctx->ident));
1756 width = -1;
1757 } else if (*ctx->ident) {
1758 struct symbol *base_type = bitfield->ctype.base_type;
1759 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1760 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1761 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1762 // Valid values are either {-1;0} or {0}, depending on integer
1763 // representation. The latter makes for very efficient code...
1764 sparse_error(token->pos, "dubious one-bit signed bitfield");
1766 if (Wdefault_bitfield_sign &&
1767 bitfield_type->type != SYM_ENUM &&
1768 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1769 is_signed) {
1770 // The sign of bitfields is unspecified by default.
1771 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1774 bitfield->bit_size = width;
1775 bitfield->endpos = token->pos;
1776 return token;
1779 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1781 struct decl_state ctx = {.prefer_abstract = 0};
1782 struct ctype saved;
1783 unsigned long mod;
1785 token = declaration_specifiers(token, &ctx);
1786 mod = storage_modifiers(&ctx);
1787 saved = ctx.ctype;
1788 for (;;) {
1789 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1790 ctx.ident = &decl->ident;
1792 token = declarator(token, &ctx);
1793 if (match_op(token, ':'))
1794 token = handle_bitfield(token, &ctx);
1796 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1797 apply_modifiers(token->pos, &ctx);
1799 decl->ctype = ctx.ctype;
1800 decl->ctype.modifiers |= mod;
1801 decl->endpos = token->pos;
1802 add_symbol(list, decl);
1803 if (!match_op(token, ','))
1804 break;
1805 token = token->next;
1806 ctx.ctype = saved;
1808 return token;
1811 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1813 while (!match_op(token, '}')) {
1814 if (!match_op(token, ';'))
1815 token = declaration_list(token, list);
1816 if (!match_op(token, ';')) {
1817 sparse_error(token->pos, "expected ; at end of declaration");
1818 break;
1820 token = token->next;
1822 return token;
1825 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1827 struct decl_state ctx = {.prefer_abstract = 1};
1829 token = declaration_specifiers(token, &ctx);
1830 ctx.ident = &sym->ident;
1831 token = declarator(token, &ctx);
1832 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1833 apply_modifiers(token->pos, &ctx);
1834 sym->ctype = ctx.ctype;
1835 sym->ctype.modifiers |= storage_modifiers(&ctx);
1836 sym->endpos = token->pos;
1837 return token;
1840 struct token *typename(struct token *token, struct symbol **p, int *forced)
1842 struct decl_state ctx = {.prefer_abstract = 1};
1843 int class;
1844 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1845 *p = sym;
1846 token = declaration_specifiers(token, &ctx);
1847 token = declarator(token, &ctx);
1848 apply_modifiers(token->pos, &ctx);
1849 sym->ctype = ctx.ctype;
1850 sym->endpos = token->pos;
1851 class = ctx.storage_class;
1852 if (forced) {
1853 *forced = 0;
1854 if (class == SForced) {
1855 *forced = 1;
1856 class = 0;
1859 if (class)
1860 warning(sym->pos, "storage class in typename (%s %s)",
1861 storage_class[class], show_typename(sym));
1862 return token;
1865 static struct token *expression_statement(struct token *token, struct expression **tree)
1867 token = parse_expression(token, tree);
1868 return expect(token, ';', "at end of statement");
1871 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1872 struct expression_list **inout)
1874 struct expression *expr;
1876 /* Allow empty operands */
1877 if (match_op(token->next, ':') || match_op(token->next, ')'))
1878 return token->next;
1879 do {
1880 struct ident *ident = NULL;
1881 if (match_op(token->next, '[') &&
1882 token_type(token->next->next) == TOKEN_IDENT &&
1883 match_op(token->next->next->next, ']')) {
1884 ident = token->next->next->ident;
1885 token = token->next->next->next;
1887 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1888 token = primary_expression(token->next, &expr);
1889 add_expression(inout, expr);
1890 token = parens_expression(token, &expr, "in asm parameter");
1891 add_expression(inout, expr);
1892 } while (match_op(token, ','));
1893 return token;
1896 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1897 struct expression_list **clobbers)
1899 struct expression *expr;
1901 do {
1902 token = primary_expression(token->next, &expr);
1903 if (expr)
1904 add_expression(clobbers, expr);
1905 } while (match_op(token, ','));
1906 return token;
1909 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1910 struct symbol_list **labels)
1912 struct symbol *label;
1914 do {
1915 token = token->next; /* skip ':' and ',' */
1916 if (token_type(token) != TOKEN_IDENT)
1917 return token;
1918 label = label_symbol(token);
1919 add_symbol(labels, label);
1920 token = token->next;
1921 } while (match_op(token, ','));
1922 return token;
1925 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1927 int is_goto = 0;
1929 token = token->next;
1930 stmt->type = STMT_ASM;
1931 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1932 token = token->next;
1934 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1935 is_goto = 1;
1936 token = token->next;
1938 token = expect(token, '(', "after asm");
1939 token = parse_expression(token, &stmt->asm_string);
1940 if (match_op(token, ':'))
1941 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1942 if (match_op(token, ':'))
1943 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1944 if (match_op(token, ':'))
1945 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1946 if (is_goto && match_op(token, ':'))
1947 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1948 token = expect(token, ')', "after asm");
1949 return expect(token, ';', "at end of asm-statement");
1952 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1954 struct expression *expr;
1955 token = expect(token, '(', "after asm");
1956 token = parse_expression(token->next, &expr);
1957 token = expect(token, ')', "after asm");
1958 return token;
1961 /* Make a statement out of an expression */
1962 static struct statement *make_statement(struct expression *expr)
1964 struct statement *stmt;
1966 if (!expr)
1967 return NULL;
1968 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1969 stmt->expression = expr;
1970 return stmt;
1974 * All iterators have two symbols associated with them:
1975 * the "continue" and "break" symbols, which are targets
1976 * for continue and break statements respectively.
1978 * They are in a special name-space, but they follow
1979 * all the normal visibility rules, so nested iterators
1980 * automatically work right.
1982 static void start_iterator(struct statement *stmt)
1984 struct symbol *cont, *brk;
1986 start_symbol_scope(stmt->pos);
1987 cont = alloc_symbol(stmt->pos, SYM_NODE);
1988 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1989 brk = alloc_symbol(stmt->pos, SYM_NODE);
1990 bind_symbol(brk, &break_ident, NS_ITERATOR);
1992 stmt->type = STMT_ITERATOR;
1993 stmt->iterator_break = brk;
1994 stmt->iterator_continue = cont;
1995 fn_local_symbol(brk);
1996 fn_local_symbol(cont);
1999 static void end_iterator(struct statement *stmt)
2001 end_symbol_scope();
2004 static struct statement *start_function(struct symbol *sym)
2006 struct symbol *ret;
2007 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2009 start_function_scope(stmt->pos);
2010 ret = alloc_symbol(sym->pos, SYM_NODE);
2011 ret->ctype = sym->ctype.base_type->ctype;
2012 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2013 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2014 bind_symbol(ret, &return_ident, NS_ITERATOR);
2015 stmt->ret = ret;
2016 fn_local_symbol(ret);
2018 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2019 current_fn = sym;
2021 return stmt;
2024 static void end_function(struct symbol *sym)
2026 current_fn = NULL;
2027 end_function_scope();
2031 * A "switch()" statement, like an iterator, has a
2032 * the "break" symbol associated with it. It works
2033 * exactly like the iterator break - it's the target
2034 * for any break-statements in scope, and means that
2035 * "break" handling doesn't even need to know whether
2036 * it's breaking out of an iterator or a switch.
2038 * In addition, the "case" symbol is a marker for the
2039 * case/default statements to find the switch statement
2040 * that they are associated with.
2042 static void start_switch(struct statement *stmt)
2044 struct symbol *brk, *switch_case;
2046 start_symbol_scope(stmt->pos);
2047 brk = alloc_symbol(stmt->pos, SYM_NODE);
2048 bind_symbol(brk, &break_ident, NS_ITERATOR);
2050 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2051 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2052 switch_case->stmt = stmt;
2054 stmt->type = STMT_SWITCH;
2055 stmt->switch_break = brk;
2056 stmt->switch_case = switch_case;
2058 fn_local_symbol(brk);
2059 fn_local_symbol(switch_case);
2062 static void end_switch(struct statement *stmt)
2064 if (!stmt->switch_case->symbol_list)
2065 warning(stmt->pos, "switch with no cases");
2066 end_symbol_scope();
2069 static void add_case_statement(struct statement *stmt)
2071 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2072 struct symbol *sym;
2074 if (!target) {
2075 sparse_error(stmt->pos, "not in switch scope");
2076 stmt->type = STMT_NONE;
2077 return;
2079 sym = alloc_symbol(stmt->pos, SYM_NODE);
2080 add_symbol(&target->symbol_list, sym);
2081 sym->stmt = stmt;
2082 stmt->case_label = sym;
2083 fn_local_symbol(sym);
2086 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2088 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2090 if (!target)
2091 error_die(token->pos, "internal error: return without a function target");
2092 stmt->type = STMT_RETURN;
2093 stmt->ret_target = target;
2094 return expression_statement(token->next, &stmt->ret_value);
2097 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2099 struct symbol_list *syms;
2100 struct expression *e1, *e2, *e3;
2101 struct statement *iterator;
2103 start_iterator(stmt);
2104 token = expect(token->next, '(', "after 'for'");
2106 syms = NULL;
2107 e1 = NULL;
2108 /* C99 variable declaration? */
2109 if (lookup_type(token)) {
2110 token = external_declaration(token, &syms);
2111 } else {
2112 token = parse_expression(token, &e1);
2113 token = expect(token, ';', "in 'for'");
2115 token = parse_expression(token, &e2);
2116 token = expect(token, ';', "in 'for'");
2117 token = parse_expression(token, &e3);
2118 token = expect(token, ')', "in 'for'");
2119 token = statement(token, &iterator);
2121 stmt->iterator_syms = syms;
2122 stmt->iterator_pre_statement = make_statement(e1);
2123 stmt->iterator_pre_condition = e2;
2124 stmt->iterator_post_statement = make_statement(e3);
2125 stmt->iterator_post_condition = NULL;
2126 stmt->iterator_statement = iterator;
2127 end_iterator(stmt);
2129 return token;
2132 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2134 struct expression *expr;
2135 struct statement *iterator;
2137 start_iterator(stmt);
2138 token = parens_expression(token->next, &expr, "after 'while'");
2139 token = statement(token, &iterator);
2141 stmt->iterator_pre_condition = expr;
2142 stmt->iterator_post_condition = NULL;
2143 stmt->iterator_statement = iterator;
2144 end_iterator(stmt);
2146 return token;
2149 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2151 struct expression *expr;
2152 struct statement *iterator;
2154 start_iterator(stmt);
2155 token = statement(token->next, &iterator);
2156 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2157 token = token->next;
2158 else
2159 sparse_error(token->pos, "expected 'while' after 'do'");
2160 token = parens_expression(token, &expr, "after 'do-while'");
2162 stmt->iterator_post_condition = expr;
2163 stmt->iterator_statement = iterator;
2164 end_iterator(stmt);
2166 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2167 warning(iterator->pos, "do-while statement is not a compound statement");
2169 return expect(token, ';', "after statement");
2172 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2174 stmt->type = STMT_IF;
2175 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2176 token = statement(token, &stmt->if_true);
2177 if (token_type(token) != TOKEN_IDENT)
2178 return token;
2179 if (token->ident != &else_ident)
2180 return token;
2181 return statement(token->next, &stmt->if_false);
2184 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2186 stmt->type = STMT_CASE;
2187 token = expect(token, ':', "after default/case");
2188 add_case_statement(stmt);
2189 return statement(token, &stmt->case_statement);
2192 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2194 token = parse_expression(token->next, &stmt->case_expression);
2195 if (match_op(token, SPECIAL_ELLIPSIS))
2196 token = parse_expression(token->next, &stmt->case_to);
2197 return case_statement(token, stmt);
2200 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2202 return case_statement(token->next, stmt);
2205 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2207 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2208 stmt->type = STMT_GOTO;
2209 stmt->goto_label = target;
2210 if (!target)
2211 sparse_error(stmt->pos, "break/continue not in iterator scope");
2212 return expect(token->next, ';', "at end of statement");
2215 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2217 stmt->type = STMT_SWITCH;
2218 start_switch(stmt);
2219 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2220 token = statement(token, &stmt->switch_statement);
2221 end_switch(stmt);
2222 return token;
2225 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2227 stmt->type = STMT_GOTO;
2228 token = token->next;
2229 if (match_op(token, '*')) {
2230 token = parse_expression(token->next, &stmt->goto_expression);
2231 add_statement(&function_computed_goto_list, stmt);
2232 } else if (token_type(token) == TOKEN_IDENT) {
2233 stmt->goto_label = label_symbol(token);
2234 token = token->next;
2235 } else {
2236 sparse_error(token->pos, "Expected identifier or goto expression");
2238 return expect(token, ';', "at end of statement");
2241 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2243 stmt->type = STMT_CONTEXT;
2244 token = parse_expression(token->next, &stmt->expression);
2245 if (stmt->expression->type == EXPR_PREOP
2246 && stmt->expression->op == '('
2247 && stmt->expression->unop->type == EXPR_COMMA) {
2248 struct expression *expr;
2249 expr = stmt->expression->unop;
2250 stmt->context = expr->left;
2251 stmt->expression = expr->right;
2253 return expect(token, ';', "at end of statement");
2256 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2258 stmt->type = STMT_RANGE;
2259 token = assignment_expression(token->next, &stmt->range_expression);
2260 token = expect(token, ',', "after range expression");
2261 token = assignment_expression(token, &stmt->range_low);
2262 token = expect(token, ',', "after low range");
2263 token = assignment_expression(token, &stmt->range_high);
2264 return expect(token, ';', "after range statement");
2267 static struct token *statement(struct token *token, struct statement **tree)
2269 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2271 *tree = stmt;
2272 if (token_type(token) == TOKEN_IDENT) {
2273 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2274 if (s && s->op->statement)
2275 return s->op->statement(token, stmt);
2277 if (match_op(token->next, ':')) {
2278 stmt->type = STMT_LABEL;
2279 stmt->label_identifier = label_symbol(token);
2280 token = skip_attributes(token->next->next);
2281 return statement(token, &stmt->label_statement);
2285 if (match_op(token, '{')) {
2286 stmt->type = STMT_COMPOUND;
2287 start_symbol_scope(stmt->pos);
2288 token = compound_statement(token->next, stmt);
2289 end_symbol_scope();
2291 return expect(token, '}', "at end of compound statement");
2294 stmt->type = STMT_EXPRESSION;
2295 return expression_statement(token, &stmt->expression);
2298 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2299 static struct token *label_statement(struct token *token)
2301 while (token_type(token) == TOKEN_IDENT) {
2302 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2303 /* it's block-scope, but we want label namespace */
2304 bind_symbol(sym, token->ident, NS_SYMBOL);
2305 sym->namespace = NS_LABEL;
2306 fn_local_symbol(sym);
2307 token = token->next;
2308 if (!match_op(token, ','))
2309 break;
2310 token = token->next;
2312 return expect(token, ';', "at end of label declaration");
2315 static struct token * statement_list(struct token *token, struct statement_list **list)
2317 int seen_statement = 0;
2318 while (token_type(token) == TOKEN_IDENT &&
2319 token->ident == &__label___ident)
2320 token = label_statement(token->next);
2321 for (;;) {
2322 struct statement * stmt;
2323 if (eof_token(token))
2324 break;
2325 if (match_op(token, '}'))
2326 break;
2327 if (lookup_type(token)) {
2328 if (seen_statement) {
2329 warning(token->pos, "mixing declarations and code");
2330 seen_statement = 0;
2332 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2333 token = external_declaration(token, &stmt->declaration);
2334 } else {
2335 seen_statement = Wdeclarationafterstatement;
2336 token = statement(token, &stmt);
2338 add_statement(list, stmt);
2340 return token;
2343 static struct token *identifier_list(struct token *token, struct symbol *fn)
2345 struct symbol_list **list = &fn->arguments;
2346 for (;;) {
2347 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2348 sym->ident = token->ident;
2349 token = token->next;
2350 sym->endpos = token->pos;
2351 sym->ctype.base_type = &incomplete_ctype;
2352 add_symbol(list, sym);
2353 if (!match_op(token, ',') ||
2354 token_type(token->next) != TOKEN_IDENT ||
2355 lookup_type(token->next))
2356 break;
2357 token = token->next;
2359 return token;
2362 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2364 struct symbol_list **list = &fn->arguments;
2366 for (;;) {
2367 struct symbol *sym;
2369 if (match_op(token, SPECIAL_ELLIPSIS)) {
2370 fn->variadic = 1;
2371 token = token->next;
2372 break;
2375 sym = alloc_symbol(token->pos, SYM_NODE);
2376 token = parameter_declaration(token, sym);
2377 if (sym->ctype.base_type == &void_ctype) {
2378 /* Special case: (void) */
2379 if (!*list && !sym->ident)
2380 break;
2381 warning(token->pos, "void parameter");
2383 add_symbol(list, sym);
2384 if (!match_op(token, ','))
2385 break;
2386 token = token->next;
2388 return token;
2391 struct token *compound_statement(struct token *token, struct statement *stmt)
2393 token = statement_list(token, &stmt->stmts);
2394 return token;
2397 static struct expression *identifier_expression(struct token *token)
2399 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2400 expr->expr_ident = token->ident;
2401 return expr;
2404 static struct expression *index_expression(struct expression *from, struct expression *to)
2406 int idx_from, idx_to;
2407 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2409 idx_from = const_expression_value(from);
2410 idx_to = idx_from;
2411 if (to) {
2412 idx_to = const_expression_value(to);
2413 if (idx_to < idx_from || idx_from < 0)
2414 warning(from->pos, "nonsense array initializer index range");
2416 expr->idx_from = idx_from;
2417 expr->idx_to = idx_to;
2418 return expr;
2421 static struct token *single_initializer(struct expression **ep, struct token *token)
2423 int expect_equal = 0;
2424 struct token *next = token->next;
2425 struct expression **tail = ep;
2426 int nested;
2428 *ep = NULL;
2430 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2431 struct expression *expr = identifier_expression(token);
2432 if (Wold_initializer)
2433 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2434 token = initializer(&expr->ident_expression, next->next);
2435 if (expr->ident_expression)
2436 *ep = expr;
2437 return token;
2440 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2441 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2442 struct expression *expr = identifier_expression(next);
2443 *tail = expr;
2444 tail = &expr->ident_expression;
2445 expect_equal = 1;
2446 token = next->next;
2447 } else if (match_op(token, '[')) {
2448 struct expression *from = NULL, *to = NULL, *expr;
2449 token = constant_expression(token->next, &from);
2450 if (!from) {
2451 sparse_error(token->pos, "Expected constant expression");
2452 break;
2454 if (match_op(token, SPECIAL_ELLIPSIS))
2455 token = constant_expression(token->next, &to);
2456 expr = index_expression(from, to);
2457 *tail = expr;
2458 tail = &expr->idx_expression;
2459 token = expect(token, ']', "at end of initializer index");
2460 if (nested)
2461 expect_equal = 1;
2462 } else {
2463 break;
2466 if (nested && !expect_equal) {
2467 if (!match_op(token, '='))
2468 warning(token->pos, "obsolete array initializer, use C99 syntax");
2469 else
2470 expect_equal = 1;
2472 if (expect_equal)
2473 token = expect(token, '=', "at end of initializer index");
2475 token = initializer(tail, token);
2476 if (!*tail)
2477 *ep = NULL;
2478 return token;
2481 static struct token *initializer_list(struct expression_list **list, struct token *token)
2483 struct expression *expr;
2485 for (;;) {
2486 token = single_initializer(&expr, token);
2487 if (!expr)
2488 break;
2489 add_expression(list, expr);
2490 if (!match_op(token, ','))
2491 break;
2492 token = token->next;
2494 return token;
2497 struct token *initializer(struct expression **tree, struct token *token)
2499 if (match_op(token, '{')) {
2500 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2501 *tree = expr;
2502 token = initializer_list(&expr->expr_list, token->next);
2503 return expect(token, '}', "at end of initializer");
2505 return assignment_expression(token, tree);
2508 static void declare_argument(struct symbol *sym, struct symbol *fn)
2510 if (!sym->ident) {
2511 sparse_error(sym->pos, "no identifier for function argument");
2512 return;
2514 bind_symbol(sym, sym->ident, NS_SYMBOL);
2517 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2518 struct symbol_list **list)
2520 struct symbol_list **old_symbol_list;
2521 struct symbol *base_type = decl->ctype.base_type;
2522 struct statement *stmt, **p;
2523 struct symbol *prev;
2524 struct symbol *arg;
2526 old_symbol_list = function_symbol_list;
2527 if (decl->ctype.modifiers & MOD_INLINE) {
2528 function_symbol_list = &decl->inline_symbol_list;
2529 p = &base_type->inline_stmt;
2530 } else {
2531 function_symbol_list = &decl->symbol_list;
2532 p = &base_type->stmt;
2534 function_computed_target_list = NULL;
2535 function_computed_goto_list = NULL;
2537 if (decl->ctype.modifiers & MOD_EXTERN) {
2538 if (!(decl->ctype.modifiers & MOD_INLINE))
2539 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2541 if (!(decl->ctype.modifiers & MOD_STATIC))
2542 decl->ctype.modifiers |= MOD_EXTERN;
2544 stmt = start_function(decl);
2546 *p = stmt;
2547 FOR_EACH_PTR (base_type->arguments, arg) {
2548 declare_argument(arg, base_type);
2549 } END_FOR_EACH_PTR(arg);
2551 token = compound_statement(token->next, stmt);
2553 end_function(decl);
2554 if (!(decl->ctype.modifiers & MOD_INLINE))
2555 add_symbol(list, decl);
2556 check_declaration(decl);
2557 decl->definition = decl;
2558 prev = decl->same_symbol;
2559 if (prev && prev->definition) {
2560 warning(decl->pos, "multiple definitions for function '%s'",
2561 show_ident(decl->ident));
2562 info(prev->definition->pos, " the previous one is here");
2563 } else {
2564 while (prev) {
2565 prev->definition = decl;
2566 prev = prev->same_symbol;
2569 function_symbol_list = old_symbol_list;
2570 if (function_computed_goto_list) {
2571 if (!function_computed_target_list)
2572 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2573 else {
2574 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2575 stmt->target_list = function_computed_target_list;
2576 } END_FOR_EACH_PTR(stmt);
2579 return expect(token, '}', "at end of function");
2582 static void promote_k_r_types(struct symbol *arg)
2584 struct symbol *base = arg->ctype.base_type;
2585 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2586 arg->ctype.base_type = &int_ctype;
2590 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2592 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2593 struct symbol *arg;
2595 FOR_EACH_PTR(real_args, arg) {
2596 struct symbol *type;
2598 /* This is quadratic in the number of arguments. We _really_ don't care */
2599 FOR_EACH_PTR(argtypes, type) {
2600 if (type->ident == arg->ident)
2601 goto match;
2602 } END_FOR_EACH_PTR(type);
2603 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2604 continue;
2605 match:
2606 type->used = 1;
2607 /* "char" and "short" promote to "int" */
2608 promote_k_r_types(type);
2610 arg->ctype = type->ctype;
2611 } END_FOR_EACH_PTR(arg);
2613 FOR_EACH_PTR(argtypes, arg) {
2614 if (!arg->used)
2615 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2616 } END_FOR_EACH_PTR(arg);
2620 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2621 struct symbol_list **list)
2623 struct symbol_list *args = NULL;
2625 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2626 do {
2627 token = declaration_list(token, &args);
2628 if (!match_op(token, ';')) {
2629 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2630 break;
2632 token = token->next;
2633 } while (lookup_type(token));
2635 apply_k_r_types(args, decl);
2637 if (!match_op(token, '{')) {
2638 sparse_error(token->pos, "expected function body");
2639 return token;
2641 return parse_function_body(token, decl, list);
2644 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2646 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2647 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2648 struct statement *stmt;
2650 anon->ctype.base_type = fn;
2651 stmt = alloc_statement(token->pos, STMT_NONE);
2652 fn->stmt = stmt;
2654 token = parse_asm_statement(token, stmt);
2656 add_symbol(list, anon);
2657 return token;
2660 struct token *external_declaration(struct token *token, struct symbol_list **list)
2662 struct ident *ident = NULL;
2663 struct symbol *decl;
2664 struct decl_state ctx = { .ident = &ident };
2665 struct ctype saved;
2666 struct symbol *base_type;
2667 unsigned long mod;
2668 int is_typedef;
2670 /* Top-level inline asm? */
2671 if (token_type(token) == TOKEN_IDENT) {
2672 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2673 if (s && s->op->toplevel)
2674 return s->op->toplevel(token, list);
2677 /* Parse declaration-specifiers, if any */
2678 token = declaration_specifiers(token, &ctx);
2679 mod = storage_modifiers(&ctx);
2680 decl = alloc_symbol(token->pos, SYM_NODE);
2681 /* Just a type declaration? */
2682 if (match_op(token, ';')) {
2683 apply_modifiers(token->pos, &ctx);
2684 return token->next;
2687 saved = ctx.ctype;
2688 token = declarator(token, &ctx);
2689 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2690 apply_modifiers(token->pos, &ctx);
2692 decl->ctype = ctx.ctype;
2693 decl->ctype.modifiers |= mod;
2694 decl->endpos = token->pos;
2696 /* Just a type declaration? */
2697 if (!ident) {
2698 warning(token->pos, "missing identifier in declaration");
2699 return expect(token, ';', "at the end of type declaration");
2702 /* type define declaration? */
2703 is_typedef = ctx.storage_class == STypedef;
2705 /* Typedefs don't have meaningful storage */
2706 if (is_typedef)
2707 decl->ctype.modifiers |= MOD_USERTYPE;
2709 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2711 base_type = decl->ctype.base_type;
2713 if (is_typedef) {
2714 if (base_type && !base_type->ident) {
2715 switch (base_type->type) {
2716 case SYM_STRUCT:
2717 case SYM_UNION:
2718 case SYM_ENUM:
2719 case SYM_RESTRICT:
2720 base_type->ident = ident;
2723 } else if (base_type && base_type->type == SYM_FN) {
2724 /* K&R argument declaration? */
2725 if (lookup_type(token))
2726 return parse_k_r_arguments(token, decl, list);
2727 if (match_op(token, '{'))
2728 return parse_function_body(token, decl, list);
2730 if (!(decl->ctype.modifiers & MOD_STATIC))
2731 decl->ctype.modifiers |= MOD_EXTERN;
2732 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2733 sparse_error(token->pos, "void declaration");
2736 for (;;) {
2737 if (!is_typedef && match_op(token, '=')) {
2738 if (decl->ctype.modifiers & MOD_EXTERN) {
2739 warning(decl->pos, "symbol with external linkage has initializer");
2740 decl->ctype.modifiers &= ~MOD_EXTERN;
2742 token = initializer(&decl->initializer, token->next);
2744 if (!is_typedef) {
2745 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2746 add_symbol(list, decl);
2747 fn_local_symbol(decl);
2750 check_declaration(decl);
2751 if (decl->same_symbol)
2752 decl->definition = decl->same_symbol->definition;
2754 if (!match_op(token, ','))
2755 break;
2757 token = token->next;
2758 ident = NULL;
2759 decl = alloc_symbol(token->pos, SYM_NODE);
2760 ctx.ctype = saved;
2761 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2762 token = declarator(token, &ctx);
2763 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2764 apply_modifiers(token->pos, &ctx);
2765 decl->ctype = ctx.ctype;
2766 decl->ctype.modifiers |= mod;
2767 decl->endpos = token->pos;
2768 if (!ident) {
2769 sparse_error(token->pos, "expected identifier name in type definition");
2770 return token;
2773 if (is_typedef)
2774 decl->ctype.modifiers |= MOD_USERTYPE;
2776 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2778 /* Function declarations are automatically extern unless specifically static */
2779 base_type = decl->ctype.base_type;
2780 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2781 if (!(decl->ctype.modifiers & MOD_STATIC))
2782 decl->ctype.modifiers |= MOD_EXTERN;
2785 return expect(token, ';', "at end of declaration");