sval: change type_min/max() => sval_type_min/max()
[smatch.git] / parse.c
blob5dd9a061ecbeace1b37f9b356cb1c083812450d5
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 "error",
513 "__error__",
514 "externally_visible",
515 "__externally_visible__",
516 "fastcall",
517 "__fastcall__",
518 "format",
519 "__format__",
520 "format_arg",
521 "__format_arg__",
522 "hot",
523 "__hot__",
524 "leaf",
525 "__leaf__",
526 "l1_text",
527 "__l1_text__",
528 "l1_data",
529 "__l1_data__",
530 "l2",
531 "__l2__",
532 "may_alias",
533 "__may_alias__",
534 "malloc",
535 "__malloc__",
536 "may_alias",
537 "__may_alias__",
538 "model",
539 "__model__",
540 "ms_abi",
541 "__ms_abi__",
542 "ms_hook_prologue",
543 "__ms_hook_prologue__",
544 "naked",
545 "__naked__",
546 "no_instrument_function",
547 "__no_instrument_function__",
548 "noinline",
549 "__noinline__",
550 "nonnull",
551 "__nonnull",
552 "__nonnull__",
553 "nothrow",
554 "__nothrow",
555 "__nothrow__",
556 "regparm",
557 "__regparm__",
558 "section",
559 "__section__",
560 "sentinel",
561 "__sentinel__",
562 "signal",
563 "__signal__",
564 "stdcall",
565 "__stdcall__",
566 "syscall_linkage",
567 "__syscall_linkage__",
568 "sysv_abi",
569 "__sysv_abi__",
570 "unused",
571 "__unused__",
572 "used",
573 "__used__",
574 "vector_size",
575 "__vector_size__",
576 "visibility",
577 "__visibility__",
578 "warn_unused_result",
579 "__warn_unused_result__",
580 "warning",
581 "__warning__",
582 "weak",
583 "__weak__",
587 void init_parser(int stream)
589 int i;
590 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
591 struct init_keyword *ptr = keyword_table + i;
592 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
593 sym->ident->keyword = 1;
594 if (ptr->ns == NS_TYPEDEF)
595 sym->ident->reserved = 1;
596 sym->ctype.modifiers = ptr->modifiers;
597 sym->ctype.base_type = ptr->type;
598 sym->op = ptr->op;
601 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
602 const char * name = ignored_attributes[i];
603 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
604 NS_KEYWORD);
605 sym->ident->keyword = 1;
606 sym->op = &ignore_attr_op;
611 // Add a symbol to the list of function-local symbols
612 static void fn_local_symbol(struct symbol *sym)
614 if (function_symbol_list)
615 add_symbol(function_symbol_list, sym);
618 static int SENTINEL_ATTR match_idents(struct token *token, ...)
620 va_list args;
621 struct ident * next;
623 if (token_type(token) != TOKEN_IDENT)
624 return 0;
626 va_start(args, token);
627 do {
628 next = va_arg(args, struct ident *);
629 } while (next && token->ident != next);
630 va_end(args);
632 return next && token->ident == next;
636 struct statement *alloc_statement(struct position pos, int type)
638 struct statement *stmt = __alloc_statement(0);
639 stmt->type = type;
640 stmt->pos = pos;
641 return stmt;
644 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
646 static void apply_modifiers(struct position pos, struct decl_state *ctx)
648 struct symbol *ctype;
649 if (!ctx->mode)
650 return;
651 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
652 if (!ctype)
653 sparse_error(pos, "don't know how to apply mode to %s",
654 show_typename(ctx->ctype.base_type));
655 else
656 ctx->ctype.base_type = ctype;
660 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
662 struct symbol *sym = alloc_symbol(pos, type);
664 sym->ctype.base_type = ctype->base_type;
665 sym->ctype.modifiers = ctype->modifiers;
667 ctype->base_type = sym;
668 ctype->modifiers = 0;
669 return sym;
673 * NOTE! NS_LABEL is not just a different namespace,
674 * it also ends up using function scope instead of the
675 * regular symbol scope.
677 struct symbol *label_symbol(struct token *token)
679 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
680 if (!sym) {
681 sym = alloc_symbol(token->pos, SYM_LABEL);
682 bind_symbol(sym, token->ident, NS_LABEL);
683 fn_local_symbol(sym);
685 return sym;
688 static struct token *struct_union_enum_specifier(enum type type,
689 struct token *token, struct decl_state *ctx,
690 struct token *(*parse)(struct token *, struct symbol *))
692 struct symbol *sym;
693 struct position *repos;
695 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
696 if (token_type(token) == TOKEN_IDENT) {
697 sym = lookup_symbol(token->ident, NS_STRUCT);
698 if (!sym ||
699 (is_outer_scope(sym->scope) &&
700 (match_op(token->next,';') || match_op(token->next,'{')))) {
701 // Either a new symbol, or else an out-of-scope
702 // symbol being redefined.
703 sym = alloc_symbol(token->pos, type);
704 bind_symbol(sym, token->ident, NS_STRUCT);
706 if (sym->type != type)
707 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
708 ctx->ctype.base_type = sym;
709 repos = &token->pos;
710 token = token->next;
711 if (match_op(token, '{')) {
712 // The following test is actually wrong for empty
713 // structs, but (1) they are not C99, (2) gcc does
714 // the same thing, and (3) it's easier.
715 if (sym->symbol_list)
716 error_die(token->pos, "redefinition of %s", show_typename (sym));
717 sym->pos = *repos;
718 token = parse(token->next, sym);
719 token = expect(token, '}', "at end of struct-union-enum-specifier");
721 // Mark the structure as needing re-examination
722 sym->examined = 0;
723 sym->endpos = token->pos;
725 return token;
728 // private struct/union/enum type
729 if (!match_op(token, '{')) {
730 sparse_error(token->pos, "expected declaration");
731 ctx->ctype.base_type = &bad_ctype;
732 return token;
735 sym = alloc_symbol(token->pos, type);
736 token = parse(token->next, sym);
737 ctx->ctype.base_type = sym;
738 token = expect(token, '}', "at end of specifier");
739 sym->endpos = token->pos;
741 return token;
744 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
746 struct symbol *field, *last = NULL;
747 struct token *res;
748 res = struct_declaration_list(token, &sym->symbol_list);
749 FOR_EACH_PTR(sym->symbol_list, field) {
750 if (!field->ident) {
751 struct symbol *base = field->ctype.base_type;
752 if (base && base->type == SYM_BITFIELD)
753 continue;
755 if (last)
756 last->next_subobject = field;
757 last = field;
758 } END_FOR_EACH_PTR(field);
759 return res;
762 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
764 return struct_declaration_list(token, &sym->symbol_list);
767 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
769 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
772 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
774 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
778 typedef struct {
779 int x;
780 unsigned long long y;
781 } Num;
783 static void upper_boundary(Num *n, Num *v)
785 if (n->x > v->x)
786 return;
787 if (n->x < v->x) {
788 *n = *v;
789 return;
791 if (n->y < v->y)
792 n->y = v->y;
795 static void lower_boundary(Num *n, Num *v)
797 if (n->x < v->x)
798 return;
799 if (n->x > v->x) {
800 *n = *v;
801 return;
803 if (n->y > v->y)
804 n->y = v->y;
807 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
809 int shift = type->bit_size;
810 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
812 if (!is_unsigned)
813 shift--;
814 if (upper->x == 0 && upper->y >> shift)
815 return 0;
816 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
817 return 1;
818 return 0;
821 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
823 if (s1->bit_size < s2->bit_size) {
824 s1 = s2;
825 } else if (s1->bit_size == s2->bit_size) {
826 if (s2->ctype.modifiers & MOD_UNSIGNED)
827 s1 = s2;
829 if (s1->bit_size < bits_in_int)
830 return &int_ctype;
831 return s1;
834 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
836 struct symbol *sym;
838 FOR_EACH_PTR(list, sym) {
839 struct expression *expr = sym->initializer;
840 struct symbol *ctype;
841 if (expr->type != EXPR_VALUE)
842 continue;
843 ctype = expr->ctype;
844 if (ctype->bit_size == base_type->bit_size)
845 continue;
846 cast_value(expr, base_type, expr, ctype);
847 } END_FOR_EACH_PTR(sym);
850 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
852 unsigned long long lastval = 0;
853 struct symbol *ctype = NULL, *base_type = NULL;
854 Num upper = {-1, 0}, lower = {1, 0};
856 parent->examined = 1;
857 parent->ctype.base_type = &int_ctype;
858 while (token_type(token) == TOKEN_IDENT) {
859 struct expression *expr = NULL;
860 struct token *next = token->next;
861 struct symbol *sym;
863 if (match_op(next, '=')) {
864 next = constant_expression(next->next, &expr);
865 lastval = get_expression_value(expr);
866 ctype = &void_ctype;
867 if (expr && expr->ctype)
868 ctype = expr->ctype;
869 } else if (!ctype) {
870 ctype = &int_ctype;
871 } else if (is_int_type(ctype)) {
872 lastval++;
873 } else {
874 error_die(token->pos, "can't increment the last enum member");
877 if (!expr) {
878 expr = alloc_expression(token->pos, EXPR_VALUE);
879 expr->value = lastval;
880 expr->ctype = ctype;
883 sym = alloc_symbol(token->pos, SYM_NODE);
884 bind_symbol(sym, token->ident, NS_SYMBOL);
885 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
886 sym->initializer = expr;
887 sym->enum_member = 1;
888 sym->ctype.base_type = parent;
889 add_ptr_list(&parent->symbol_list, sym);
891 if (base_type != &bad_ctype) {
892 if (ctype->type == SYM_NODE)
893 ctype = ctype->ctype.base_type;
894 if (ctype->type == SYM_ENUM) {
895 if (ctype == parent)
896 ctype = base_type;
897 else
898 ctype = ctype->ctype.base_type;
901 * base_type rules:
902 * - if all enums are of the same type, then
903 * the base_type is that type (two first
904 * cases)
905 * - if enums are of different types, they
906 * all have to be integer types, and the
907 * base type is at least "int_ctype".
908 * - otherwise the base_type is "bad_ctype".
910 if (!base_type) {
911 base_type = ctype;
912 } else if (ctype == base_type) {
913 /* nothing */
914 } else if (is_int_type(base_type) && is_int_type(ctype)) {
915 base_type = bigger_enum_type(base_type, ctype);
916 } else
917 base_type = &bad_ctype;
918 parent->ctype.base_type = base_type;
920 if (is_int_type(base_type)) {
921 Num v = {.y = lastval};
922 if (ctype->ctype.modifiers & MOD_UNSIGNED)
923 v.x = 0;
924 else if ((long long)lastval >= 0)
925 v.x = 0;
926 else
927 v.x = -1;
928 upper_boundary(&upper, &v);
929 lower_boundary(&lower, &v);
931 token = next;
933 sym->endpos = token->pos;
935 if (!match_op(token, ','))
936 break;
937 token = token->next;
939 if (!base_type) {
940 sparse_error(token->pos, "bad enum definition");
941 base_type = &bad_ctype;
943 else if (!is_int_type(base_type))
944 base_type = base_type;
945 else if (type_is_ok(base_type, &upper, &lower))
946 base_type = base_type;
947 else if (type_is_ok(&int_ctype, &upper, &lower))
948 base_type = &int_ctype;
949 else if (type_is_ok(&uint_ctype, &upper, &lower))
950 base_type = &uint_ctype;
951 else if (type_is_ok(&long_ctype, &upper, &lower))
952 base_type = &long_ctype;
953 else if (type_is_ok(&ulong_ctype, &upper, &lower))
954 base_type = &ulong_ctype;
955 else if (type_is_ok(&llong_ctype, &upper, &lower))
956 base_type = &llong_ctype;
957 else if (type_is_ok(&ullong_ctype, &upper, &lower))
958 base_type = &ullong_ctype;
959 else
960 base_type = &bad_ctype;
961 parent->ctype.base_type = base_type;
962 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
963 parent->examined = 0;
965 cast_enum_list(parent->symbol_list, base_type);
967 return token;
970 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
972 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
973 struct ctype *ctype = &ctx->ctype.base_type->ctype;
975 if (!ctype->base_type)
976 ctype->base_type = &incomplete_ctype;
978 return ret;
981 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
983 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
985 struct symbol *sym;
987 if (!match_op(token, '(')) {
988 sparse_error(token->pos, "expected '(' after typeof");
989 return token;
991 if (lookup_type(token->next)) {
992 token = typename(token->next, &sym, NULL);
993 ctx->ctype.base_type = sym->ctype.base_type;
994 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
995 } else {
996 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
997 token = parse_expression(token->next, &typeof_sym->initializer);
999 typeof_sym->endpos = token->pos;
1000 if (!typeof_sym->initializer) {
1001 sparse_error(token->pos, "expected expression after the '(' token");
1002 typeof_sym = &bad_ctype;
1004 ctx->ctype.base_type = typeof_sym;
1006 return expect(token, ')', "after typeof");
1009 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1011 struct expression *expr = NULL;
1012 if (match_op(token, '('))
1013 token = parens_expression(token, &expr, "in attribute");
1014 return token;
1017 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1019 if (!ctx->ctype.alignment)
1020 ctx->ctype.alignment = 1;
1021 return token;
1024 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1026 int alignment = max_alignment;
1027 struct expression *expr = NULL;
1029 if (match_op(token, '(')) {
1030 token = parens_expression(token, &expr, "in attribute");
1031 if (expr)
1032 alignment = const_expression_value(expr);
1034 if (alignment & (alignment-1)) {
1035 warning(token->pos, "I don't like non-power-of-2 alignments");
1036 return token;
1037 } else if (alignment > ctx->ctype.alignment)
1038 ctx->ctype.alignment = alignment;
1039 return token;
1042 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1044 if (ctx->modifiers & qual)
1045 warning(*pos, "duplicate %s", modifier_string(qual));
1046 ctx->modifiers |= qual;
1049 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1051 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1052 return token;
1055 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1057 struct expression *expr = NULL;
1058 int as;
1059 token = expect(token, '(', "after address_space attribute");
1060 token = conditional_expression(token, &expr);
1061 if (expr) {
1062 as = const_expression_value(expr);
1063 if (Waddress_space && as)
1064 ctx->ctype.as = as;
1066 token = expect(token, ')', "after address_space attribute");
1067 return token;
1070 static struct symbol *to_QI_mode(struct symbol *ctype)
1072 if (ctype->ctype.base_type != &int_type)
1073 return NULL;
1074 if (ctype == &char_ctype)
1075 return ctype;
1076 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1077 : &schar_ctype;
1080 static struct symbol *to_HI_mode(struct symbol *ctype)
1082 if (ctype->ctype.base_type != &int_type)
1083 return NULL;
1084 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1085 : &sshort_ctype;
1088 static struct symbol *to_SI_mode(struct symbol *ctype)
1090 if (ctype->ctype.base_type != &int_type)
1091 return NULL;
1092 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1093 : &sint_ctype;
1096 static struct symbol *to_DI_mode(struct symbol *ctype)
1098 if (ctype->ctype.base_type != &int_type)
1099 return NULL;
1100 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1101 : &sllong_ctype;
1104 static struct symbol *to_TI_mode(struct symbol *ctype)
1106 if (ctype->ctype.base_type != &int_type)
1107 return NULL;
1108 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1109 : &slllong_ctype;
1112 static struct symbol *to_word_mode(struct symbol *ctype)
1114 if (ctype->ctype.base_type != &int_type)
1115 return NULL;
1116 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1117 : &slong_ctype;
1120 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1122 token = expect(token, '(', "after mode attribute");
1123 if (token_type(token) == TOKEN_IDENT) {
1124 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1125 if (mode && mode->op->type == KW_MODE)
1126 ctx->mode = mode->op;
1127 else
1128 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1129 token = token->next;
1130 } else
1131 sparse_error(token->pos, "expect attribute mode symbol\n");
1132 token = expect(token, ')', "after mode attribute");
1133 return token;
1136 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1138 struct context *context = alloc_context();
1139 struct expression *args[3];
1140 int argc = 0;
1142 token = expect(token, '(', "after context attribute");
1143 while (!match_op(token, ')')) {
1144 struct expression *expr = NULL;
1145 token = conditional_expression(token, &expr);
1146 if (!expr)
1147 break;
1148 if (argc < 3)
1149 args[argc++] = expr;
1150 if (!match_op(token, ','))
1151 break;
1152 token = token->next;
1155 switch(argc) {
1156 case 0:
1157 sparse_error(token->pos, "expected context input/output values");
1158 break;
1159 case 1:
1160 context->in = get_expression_value(args[0]);
1161 break;
1162 case 2:
1163 context->in = get_expression_value(args[0]);
1164 context->out = get_expression_value(args[1]);
1165 break;
1166 case 3:
1167 context->context = args[0];
1168 context->in = get_expression_value(args[1]);
1169 context->out = get_expression_value(args[2]);
1170 break;
1173 if (argc)
1174 add_ptr_list(&ctx->ctype.contexts, context);
1176 token = expect(token, ')', "after context attribute");
1177 return token;
1180 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1182 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1183 ctx->ctype.base_type->designated_init = 1;
1184 else
1185 warning(token->pos, "attribute designated_init applied to non-structure type");
1186 return token;
1189 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1191 if (Wtransparent_union)
1192 warning(token->pos, "ignoring attribute __transparent_union__");
1193 return token;
1196 static struct token *recover_unknown_attribute(struct token *token)
1198 struct expression *expr = NULL;
1200 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1201 token = token->next;
1202 if (match_op(token, '('))
1203 token = parens_expression(token, &expr, "in attribute");
1204 return token;
1207 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1209 token = expect(token, '(', "after attribute");
1210 token = expect(token, '(', "after attribute");
1212 for (;;) {
1213 struct ident *attribute_name;
1214 struct symbol *attr;
1216 if (eof_token(token))
1217 break;
1218 if (match_op(token, ';'))
1219 break;
1220 if (token_type(token) != TOKEN_IDENT)
1221 break;
1222 attribute_name = token->ident;
1223 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1224 if (attr && attr->op->attribute)
1225 token = attr->op->attribute(token->next, attr, ctx);
1226 else
1227 token = recover_unknown_attribute(token);
1229 if (!match_op(token, ','))
1230 break;
1231 token = token->next;
1234 token = expect(token, ')', "after attribute");
1235 token = expect(token, ')', "after attribute");
1236 return token;
1239 static const char *storage_class[] =
1241 [STypedef] = "typedef",
1242 [SAuto] = "auto",
1243 [SExtern] = "extern",
1244 [SStatic] = "static",
1245 [SRegister] = "register",
1246 [SForced] = "[force]"
1249 static unsigned long storage_modifiers(struct decl_state *ctx)
1251 static unsigned long mod[] =
1253 [SAuto] = MOD_AUTO,
1254 [SExtern] = MOD_EXTERN,
1255 [SStatic] = MOD_STATIC,
1256 [SRegister] = MOD_REGISTER
1258 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1259 | (ctx->is_tls ? MOD_TLS : 0);
1262 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1264 /* __thread can be used alone, or with extern or static */
1265 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1266 sparse_error(*pos, "__thread can only be used alone, or with "
1267 "extern or static");
1268 return;
1271 if (!ctx->storage_class) {
1272 ctx->storage_class = class;
1273 return;
1275 if (ctx->storage_class == class)
1276 sparse_error(*pos, "duplicate %s", storage_class[class]);
1277 else
1278 sparse_error(*pos, "multiple storage classes");
1281 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1283 set_storage_class(&next->pos, ctx, STypedef);
1284 return next;
1287 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1289 set_storage_class(&next->pos, ctx, SAuto);
1290 return next;
1293 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1295 set_storage_class(&next->pos, ctx, SRegister);
1296 return next;
1299 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1301 set_storage_class(&next->pos, ctx, SStatic);
1302 return next;
1305 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1307 set_storage_class(&next->pos, ctx, SExtern);
1308 return next;
1311 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1313 /* This GCC extension can be used alone, or with extern or static */
1314 if (!ctx->storage_class || ctx->storage_class == SStatic
1315 || ctx->storage_class == SExtern) {
1316 ctx->is_tls = 1;
1317 } else {
1318 sparse_error(next->pos, "__thread can only be used alone, or "
1319 "with extern or static");
1322 return next;
1325 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1327 set_storage_class(&token->pos, ctx, SForced);
1328 return token;
1331 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1333 ctx->is_inline = 1;
1334 return next;
1337 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1339 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1340 return next;
1343 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1345 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1346 return next;
1349 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1351 unsigned long mod = thistype->modifiers;
1353 if (mod)
1354 apply_qualifier(&pos, ctype, mod);
1356 /* Context */
1357 concat_ptr_list((struct ptr_list *)thistype->contexts,
1358 (struct ptr_list **)&ctype->contexts);
1360 /* Alignment */
1361 if (thistype->alignment > ctype->alignment)
1362 ctype->alignment = thistype->alignment;
1364 /* Address space */
1365 if (thistype->as)
1366 ctype->as = thistype->as;
1369 static void specifier_conflict(struct position pos, int what, struct ident *new)
1371 const char *old;
1372 if (what & (Set_S | Set_T))
1373 goto Catch_all;
1374 if (what & Set_Char)
1375 old = "char";
1376 else if (what & Set_Double)
1377 old = "double";
1378 else if (what & Set_Float)
1379 old = "float";
1380 else if (what & Set_Signed)
1381 old = "signed";
1382 else if (what & Set_Unsigned)
1383 old = "unsigned";
1384 else if (what & Set_Short)
1385 old = "short";
1386 else if (what & Set_Long)
1387 old = "long";
1388 else
1389 old = "long long";
1390 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1391 old, show_ident(new));
1392 return;
1394 Catch_all:
1395 sparse_error(pos, "two or more data types in declaration specifiers");
1398 static struct symbol * const int_types[] =
1399 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1400 static struct symbol * const signed_types[] =
1401 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1402 &slllong_ctype};
1403 static struct symbol * const unsigned_types[] =
1404 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1405 &ulllong_ctype};
1406 static struct symbol * const real_types[] =
1407 {&float_ctype, &double_ctype, &ldouble_ctype};
1408 static struct symbol * const char_types[] =
1409 {&char_ctype, &schar_ctype, &uchar_ctype};
1410 static struct symbol * const * const types[] = {
1411 int_types + 1, signed_types + 1, unsigned_types + 1,
1412 real_types + 1, char_types, char_types + 1, char_types + 2
1415 struct symbol *ctype_integer(int size, int want_unsigned)
1417 return types[want_unsigned ? CUInt : CInt][size];
1420 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1422 while (token_type(t) == TOKEN_IDENT) {
1423 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1424 if (!s)
1425 break;
1426 if (s->type != SYM_KEYWORD)
1427 break;
1428 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1429 break;
1430 t = t->next;
1431 if (s->op->declarator)
1432 t = s->op->declarator(t, ctx);
1434 return t;
1437 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1439 int seen = 0;
1440 int class = CInt;
1441 int size = 0;
1443 while (token_type(token) == TOKEN_IDENT) {
1444 struct symbol *s = lookup_symbol(token->ident,
1445 NS_TYPEDEF | NS_SYMBOL);
1446 if (!s || !(s->namespace & NS_TYPEDEF))
1447 break;
1448 if (s->type != SYM_KEYWORD) {
1449 if (seen & Set_Any)
1450 break;
1451 seen |= Set_S | Set_T;
1452 ctx->ctype.base_type = s->ctype.base_type;
1453 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1454 token = token->next;
1455 continue;
1457 if (s->op->type & KW_SPECIFIER) {
1458 if (seen & s->op->test) {
1459 specifier_conflict(token->pos,
1460 seen & s->op->test,
1461 token->ident);
1462 break;
1464 seen |= s->op->set;
1465 class += s->op->class;
1466 if (s->op->type & KW_SHORT) {
1467 size = -1;
1468 } else if (s->op->type & KW_LONG && size++) {
1469 if (class == CReal) {
1470 specifier_conflict(token->pos,
1471 Set_Vlong,
1472 &double_ident);
1473 break;
1475 seen |= Set_Vlong;
1478 token = token->next;
1479 if (s->op->declarator)
1480 token = s->op->declarator(token, ctx);
1481 if (s->op->type & KW_EXACT) {
1482 ctx->ctype.base_type = s->ctype.base_type;
1483 ctx->ctype.modifiers |= s->ctype.modifiers;
1487 if (!(seen & Set_S)) { /* not set explicitly? */
1488 struct symbol *base = &incomplete_ctype;
1489 if (seen & Set_Any)
1490 base = types[class][size];
1491 ctx->ctype.base_type = base;
1494 if (ctx->ctype.modifiers & MOD_BITWISE) {
1495 struct symbol *type;
1496 ctx->ctype.modifiers &= ~MOD_BITWISE;
1497 if (!is_int_type(ctx->ctype.base_type)) {
1498 sparse_error(token->pos, "invalid modifier");
1499 return token;
1501 type = alloc_symbol(token->pos, SYM_BASETYPE);
1502 *type = *ctx->ctype.base_type;
1503 type->ctype.modifiers &= ~MOD_SPECIFIER;
1504 type->ctype.base_type = ctx->ctype.base_type;
1505 type->type = SYM_RESTRICT;
1506 ctx->ctype.base_type = type;
1507 create_fouled(type);
1509 return token;
1512 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1514 struct expression *expr = NULL;
1516 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1517 token = token->next;
1518 token = parse_expression(token, &expr);
1519 sym->array_size = expr;
1520 return token;
1523 static struct token *parameter_type_list(struct token *, struct symbol *);
1524 static struct token *identifier_list(struct token *, struct symbol *);
1525 static struct token *declarator(struct token *token, struct decl_state *ctx);
1527 static struct token *skip_attribute(struct token *token)
1529 token = token->next;
1530 if (match_op(token, '(')) {
1531 int depth = 1;
1532 token = token->next;
1533 while (depth && !eof_token(token)) {
1534 if (token_type(token) == TOKEN_SPECIAL) {
1535 if (token->special == '(')
1536 depth++;
1537 else if (token->special == ')')
1538 depth--;
1540 token = token->next;
1543 return token;
1546 static struct token *skip_attributes(struct token *token)
1548 struct symbol *keyword;
1549 for (;;) {
1550 if (token_type(token) != TOKEN_IDENT)
1551 break;
1552 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1553 if (!keyword || keyword->type != SYM_KEYWORD)
1554 break;
1555 if (!(keyword->op->type & KW_ATTRIBUTE))
1556 break;
1557 token = expect(token->next, '(', "after attribute");
1558 token = expect(token, '(', "after attribute");
1559 for (;;) {
1560 if (eof_token(token))
1561 break;
1562 if (match_op(token, ';'))
1563 break;
1564 if (token_type(token) != TOKEN_IDENT)
1565 break;
1566 token = skip_attribute(token);
1567 if (!match_op(token, ','))
1568 break;
1569 token = token->next;
1571 token = expect(token, ')', "after attribute");
1572 token = expect(token, ')', "after attribute");
1574 return token;
1577 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1579 struct symbol *keyword;
1580 for (;;) {
1581 if (token_type(token) != TOKEN_IDENT)
1582 break;
1583 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1584 if (!keyword || keyword->type != SYM_KEYWORD)
1585 break;
1586 if (!(keyword->op->type & keywords))
1587 break;
1588 token = keyword->op->declarator(token->next, ctx);
1589 keywords &= KW_ATTRIBUTE;
1591 return token;
1594 static int is_nested(struct token *token, struct token **p,
1595 int prefer_abstract)
1598 * This can be either a parameter list or a grouping.
1599 * For the direct (non-abstract) case, we know if must be
1600 * a parameter list if we already saw the identifier.
1601 * For the abstract case, we know if must be a parameter
1602 * list if it is empty or starts with a type.
1604 struct token *next = token->next;
1606 *p = next = skip_attributes(next);
1608 if (token_type(next) == TOKEN_IDENT) {
1609 if (lookup_type(next))
1610 return !prefer_abstract;
1611 return 1;
1614 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1615 return 0;
1617 return 1;
1620 enum kind {
1621 Empty, K_R, Proto, Bad_Func,
1624 static enum kind which_func(struct token *token,
1625 struct ident **n,
1626 int prefer_abstract)
1628 struct token *next = token->next;
1630 if (token_type(next) == TOKEN_IDENT) {
1631 if (lookup_type(next))
1632 return Proto;
1633 /* identifier list not in definition; complain */
1634 if (prefer_abstract)
1635 warning(token->pos,
1636 "identifier list not in definition");
1637 return K_R;
1640 if (token_type(next) != TOKEN_SPECIAL)
1641 return Bad_Func;
1643 if (next->special == ')') {
1644 /* don't complain about those */
1645 if (!n || match_op(next->next, ';'))
1646 return Empty;
1647 warning(next->pos,
1648 "non-ANSI function declaration of function '%s'",
1649 show_ident(*n));
1650 return Empty;
1653 if (next->special == SPECIAL_ELLIPSIS) {
1654 warning(next->pos,
1655 "variadic functions must have one named argument");
1656 return Proto;
1659 return Bad_Func;
1662 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1664 struct ctype *ctype = &ctx->ctype;
1665 struct token *next;
1666 struct ident **p = ctx->ident;
1668 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1669 *ctx->ident = token->ident;
1670 token = token->next;
1671 } else if (match_op(token, '(') &&
1672 is_nested(token, &next, ctx->prefer_abstract)) {
1673 struct symbol *base_type = ctype->base_type;
1674 if (token->next != next)
1675 next = handle_attributes(token->next, ctx,
1676 KW_ATTRIBUTE);
1677 token = declarator(next, ctx);
1678 token = expect(token, ')', "in nested declarator");
1679 while (ctype->base_type != base_type)
1680 ctype = &ctype->base_type->ctype;
1681 p = NULL;
1684 if (match_op(token, '(')) {
1685 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1686 struct symbol *fn;
1687 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1688 token = token->next;
1689 if (kind == K_R)
1690 token = identifier_list(token, fn);
1691 else if (kind == Proto)
1692 token = parameter_type_list(token, fn);
1693 token = expect(token, ')', "in function declarator");
1694 fn->endpos = token->pos;
1695 return token;
1698 while (match_op(token, '[')) {
1699 struct symbol *array;
1700 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1701 token = abstract_array_declarator(token->next, array);
1702 token = expect(token, ']', "in abstract_array_declarator");
1703 array->endpos = token->pos;
1704 ctype = &array->ctype;
1706 return token;
1709 static struct token *pointer(struct token *token, struct decl_state *ctx)
1711 while (match_op(token,'*')) {
1712 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1713 ptr->ctype.modifiers = ctx->ctype.modifiers;
1714 ptr->ctype.base_type = ctx->ctype.base_type;
1715 ptr->ctype.as = ctx->ctype.as;
1716 ptr->ctype.contexts = ctx->ctype.contexts;
1717 ctx->ctype.modifiers = 0;
1718 ctx->ctype.base_type = ptr;
1719 ctx->ctype.as = 0;
1720 ctx->ctype.contexts = NULL;
1721 ctx->ctype.alignment = 0;
1723 token = handle_qualifiers(token->next, ctx);
1724 ctx->ctype.base_type->endpos = token->pos;
1726 return token;
1729 static struct token *declarator(struct token *token, struct decl_state *ctx)
1731 token = pointer(token, ctx);
1732 return direct_declarator(token, ctx);
1735 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1737 struct ctype *ctype = &ctx->ctype;
1738 struct expression *expr;
1739 struct symbol *bitfield;
1740 long long width;
1742 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1743 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1744 show_typename(ctype->base_type));
1745 // Parse this to recover gracefully.
1746 return conditional_expression(token->next, &expr);
1749 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1750 token = conditional_expression(token->next, &expr);
1751 width = const_expression_value(expr);
1752 bitfield->bit_size = width;
1754 if (width < 0 || width > INT_MAX) {
1755 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1756 width = -1;
1757 } else if (*ctx->ident && width == 0) {
1758 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1759 show_ident(*ctx->ident));
1760 width = -1;
1761 } else if (*ctx->ident) {
1762 struct symbol *base_type = bitfield->ctype.base_type;
1763 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1764 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1765 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1766 // Valid values are either {-1;0} or {0}, depending on integer
1767 // representation. The latter makes for very efficient code...
1768 sparse_error(token->pos, "dubious one-bit signed bitfield");
1770 if (Wdefault_bitfield_sign &&
1771 bitfield_type->type != SYM_ENUM &&
1772 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1773 is_signed) {
1774 // The sign of bitfields is unspecified by default.
1775 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1778 bitfield->bit_size = width;
1779 bitfield->endpos = token->pos;
1780 return token;
1783 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1785 struct decl_state ctx = {.prefer_abstract = 0};
1786 struct ctype saved;
1787 unsigned long mod;
1789 token = declaration_specifiers(token, &ctx);
1790 mod = storage_modifiers(&ctx);
1791 saved = ctx.ctype;
1792 for (;;) {
1793 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1794 ctx.ident = &decl->ident;
1796 token = declarator(token, &ctx);
1797 if (match_op(token, ':'))
1798 token = handle_bitfield(token, &ctx);
1800 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1801 apply_modifiers(token->pos, &ctx);
1803 decl->ctype = ctx.ctype;
1804 decl->ctype.modifiers |= mod;
1805 decl->endpos = token->pos;
1806 add_symbol(list, decl);
1807 if (!match_op(token, ','))
1808 break;
1809 token = token->next;
1810 ctx.ctype = saved;
1812 return token;
1815 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1817 while (!match_op(token, '}')) {
1818 if (!match_op(token, ';'))
1819 token = declaration_list(token, list);
1820 if (!match_op(token, ';')) {
1821 sparse_error(token->pos, "expected ; at end of declaration");
1822 break;
1824 token = token->next;
1826 return token;
1829 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1831 struct decl_state ctx = {.prefer_abstract = 1};
1833 token = declaration_specifiers(token, &ctx);
1834 ctx.ident = &sym->ident;
1835 token = declarator(token, &ctx);
1836 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1837 apply_modifiers(token->pos, &ctx);
1838 sym->ctype = ctx.ctype;
1839 sym->ctype.modifiers |= storage_modifiers(&ctx);
1840 sym->endpos = token->pos;
1841 return token;
1844 struct token *typename(struct token *token, struct symbol **p, int *forced)
1846 struct decl_state ctx = {.prefer_abstract = 1};
1847 int class;
1848 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1849 *p = sym;
1850 token = declaration_specifiers(token, &ctx);
1851 token = declarator(token, &ctx);
1852 apply_modifiers(token->pos, &ctx);
1853 sym->ctype = ctx.ctype;
1854 sym->endpos = token->pos;
1855 class = ctx.storage_class;
1856 if (forced) {
1857 *forced = 0;
1858 if (class == SForced) {
1859 *forced = 1;
1860 class = 0;
1863 if (class)
1864 warning(sym->pos, "storage class in typename (%s %s)",
1865 storage_class[class], show_typename(sym));
1866 return token;
1869 static struct token *expression_statement(struct token *token, struct expression **tree)
1871 token = parse_expression(token, tree);
1872 return expect(token, ';', "at end of statement");
1875 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1876 struct expression_list **inout)
1878 struct expression *expr;
1880 /* Allow empty operands */
1881 if (match_op(token->next, ':') || match_op(token->next, ')'))
1882 return token->next;
1883 do {
1884 struct ident *ident = NULL;
1885 if (match_op(token->next, '[') &&
1886 token_type(token->next->next) == TOKEN_IDENT &&
1887 match_op(token->next->next->next, ']')) {
1888 ident = token->next->next->ident;
1889 token = token->next->next->next;
1891 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1892 token = primary_expression(token->next, &expr);
1893 add_expression(inout, expr);
1894 token = parens_expression(token, &expr, "in asm parameter");
1895 add_expression(inout, expr);
1896 } while (match_op(token, ','));
1897 return token;
1900 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1901 struct expression_list **clobbers)
1903 struct expression *expr;
1905 do {
1906 token = primary_expression(token->next, &expr);
1907 if (expr)
1908 add_expression(clobbers, expr);
1909 } while (match_op(token, ','));
1910 return token;
1913 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1914 struct symbol_list **labels)
1916 struct symbol *label;
1918 do {
1919 token = token->next; /* skip ':' and ',' */
1920 if (token_type(token) != TOKEN_IDENT)
1921 return token;
1922 label = label_symbol(token);
1923 add_symbol(labels, label);
1924 token = token->next;
1925 } while (match_op(token, ','));
1926 return token;
1929 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1931 int is_goto = 0;
1933 token = token->next;
1934 stmt->type = STMT_ASM;
1935 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1936 token = token->next;
1938 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1939 is_goto = 1;
1940 token = token->next;
1942 token = expect(token, '(', "after asm");
1943 token = parse_expression(token, &stmt->asm_string);
1944 if (match_op(token, ':'))
1945 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1946 if (match_op(token, ':'))
1947 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1948 if (match_op(token, ':'))
1949 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1950 if (is_goto && match_op(token, ':'))
1951 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1952 token = expect(token, ')', "after asm");
1953 return expect(token, ';', "at end of asm-statement");
1956 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1958 struct expression *expr;
1959 token = expect(token, '(', "after asm");
1960 token = parse_expression(token->next, &expr);
1961 token = expect(token, ')', "after asm");
1962 return token;
1965 /* Make a statement out of an expression */
1966 static struct statement *make_statement(struct expression *expr)
1968 struct statement *stmt;
1970 if (!expr)
1971 return NULL;
1972 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1973 stmt->expression = expr;
1974 return stmt;
1978 * All iterators have two symbols associated with them:
1979 * the "continue" and "break" symbols, which are targets
1980 * for continue and break statements respectively.
1982 * They are in a special name-space, but they follow
1983 * all the normal visibility rules, so nested iterators
1984 * automatically work right.
1986 static void start_iterator(struct statement *stmt)
1988 struct symbol *cont, *brk;
1990 start_symbol_scope(stmt->pos);
1991 cont = alloc_symbol(stmt->pos, SYM_NODE);
1992 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1993 brk = alloc_symbol(stmt->pos, SYM_NODE);
1994 bind_symbol(brk, &break_ident, NS_ITERATOR);
1996 stmt->type = STMT_ITERATOR;
1997 stmt->iterator_break = brk;
1998 stmt->iterator_continue = cont;
1999 fn_local_symbol(brk);
2000 fn_local_symbol(cont);
2003 static void end_iterator(struct statement *stmt)
2005 end_symbol_scope();
2008 static struct statement *start_function(struct symbol *sym)
2010 struct symbol *ret;
2011 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2013 start_function_scope(stmt->pos);
2014 ret = alloc_symbol(sym->pos, SYM_NODE);
2015 ret->ctype = sym->ctype.base_type->ctype;
2016 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2017 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2018 bind_symbol(ret, &return_ident, NS_ITERATOR);
2019 stmt->ret = ret;
2020 fn_local_symbol(ret);
2022 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2023 current_fn = sym;
2025 return stmt;
2028 static void end_function(struct symbol *sym)
2030 current_fn = NULL;
2031 end_function_scope();
2035 * A "switch()" statement, like an iterator, has a
2036 * the "break" symbol associated with it. It works
2037 * exactly like the iterator break - it's the target
2038 * for any break-statements in scope, and means that
2039 * "break" handling doesn't even need to know whether
2040 * it's breaking out of an iterator or a switch.
2042 * In addition, the "case" symbol is a marker for the
2043 * case/default statements to find the switch statement
2044 * that they are associated with.
2046 static void start_switch(struct statement *stmt)
2048 struct symbol *brk, *switch_case;
2050 start_symbol_scope(stmt->pos);
2051 brk = alloc_symbol(stmt->pos, SYM_NODE);
2052 bind_symbol(brk, &break_ident, NS_ITERATOR);
2054 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2055 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2056 switch_case->stmt = stmt;
2058 stmt->type = STMT_SWITCH;
2059 stmt->switch_break = brk;
2060 stmt->switch_case = switch_case;
2062 fn_local_symbol(brk);
2063 fn_local_symbol(switch_case);
2066 static void end_switch(struct statement *stmt)
2068 if (!stmt->switch_case->symbol_list)
2069 warning(stmt->pos, "switch with no cases");
2070 end_symbol_scope();
2073 static void add_case_statement(struct statement *stmt)
2075 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2076 struct symbol *sym;
2078 if (!target) {
2079 sparse_error(stmt->pos, "not in switch scope");
2080 stmt->type = STMT_NONE;
2081 return;
2083 sym = alloc_symbol(stmt->pos, SYM_NODE);
2084 add_symbol(&target->symbol_list, sym);
2085 sym->stmt = stmt;
2086 stmt->case_label = sym;
2087 fn_local_symbol(sym);
2090 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2092 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2094 if (!target)
2095 error_die(token->pos, "internal error: return without a function target");
2096 stmt->type = STMT_RETURN;
2097 stmt->ret_target = target;
2098 return expression_statement(token->next, &stmt->ret_value);
2101 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2103 struct symbol_list *syms;
2104 struct expression *e1, *e2, *e3;
2105 struct statement *iterator;
2107 start_iterator(stmt);
2108 token = expect(token->next, '(', "after 'for'");
2110 syms = NULL;
2111 e1 = NULL;
2112 /* C99 variable declaration? */
2113 if (lookup_type(token)) {
2114 token = external_declaration(token, &syms);
2115 } else {
2116 token = parse_expression(token, &e1);
2117 token = expect(token, ';', "in 'for'");
2119 token = parse_expression(token, &e2);
2120 token = expect(token, ';', "in 'for'");
2121 token = parse_expression(token, &e3);
2122 token = expect(token, ')', "in 'for'");
2123 token = statement(token, &iterator);
2125 stmt->iterator_syms = syms;
2126 stmt->iterator_pre_statement = make_statement(e1);
2127 stmt->iterator_pre_condition = e2;
2128 stmt->iterator_post_statement = make_statement(e3);
2129 stmt->iterator_post_condition = NULL;
2130 stmt->iterator_statement = iterator;
2131 end_iterator(stmt);
2133 return token;
2136 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2138 struct expression *expr;
2139 struct statement *iterator;
2141 start_iterator(stmt);
2142 token = parens_expression(token->next, &expr, "after 'while'");
2143 token = statement(token, &iterator);
2145 stmt->iterator_pre_condition = expr;
2146 stmt->iterator_post_condition = NULL;
2147 stmt->iterator_statement = iterator;
2148 end_iterator(stmt);
2150 return token;
2153 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2155 struct expression *expr;
2156 struct statement *iterator;
2158 start_iterator(stmt);
2159 token = statement(token->next, &iterator);
2160 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2161 token = token->next;
2162 else
2163 sparse_error(token->pos, "expected 'while' after 'do'");
2164 token = parens_expression(token, &expr, "after 'do-while'");
2166 stmt->iterator_post_condition = expr;
2167 stmt->iterator_statement = iterator;
2168 end_iterator(stmt);
2170 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2171 warning(iterator->pos, "do-while statement is not a compound statement");
2173 return expect(token, ';', "after statement");
2176 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2178 stmt->type = STMT_IF;
2179 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2180 token = statement(token, &stmt->if_true);
2181 if (token_type(token) != TOKEN_IDENT)
2182 return token;
2183 if (token->ident != &else_ident)
2184 return token;
2185 return statement(token->next, &stmt->if_false);
2188 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2190 stmt->type = STMT_CASE;
2191 token = expect(token, ':', "after default/case");
2192 add_case_statement(stmt);
2193 return statement(token, &stmt->case_statement);
2196 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2198 token = parse_expression(token->next, &stmt->case_expression);
2199 if (match_op(token, SPECIAL_ELLIPSIS))
2200 token = parse_expression(token->next, &stmt->case_to);
2201 return case_statement(token, stmt);
2204 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2206 return case_statement(token->next, stmt);
2209 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2211 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2212 stmt->type = STMT_GOTO;
2213 stmt->goto_label = target;
2214 if (!target)
2215 sparse_error(stmt->pos, "break/continue not in iterator scope");
2216 return expect(token->next, ';', "at end of statement");
2219 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2221 stmt->type = STMT_SWITCH;
2222 start_switch(stmt);
2223 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2224 token = statement(token, &stmt->switch_statement);
2225 end_switch(stmt);
2226 return token;
2229 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2231 stmt->type = STMT_GOTO;
2232 token = token->next;
2233 if (match_op(token, '*')) {
2234 token = parse_expression(token->next, &stmt->goto_expression);
2235 add_statement(&function_computed_goto_list, stmt);
2236 } else if (token_type(token) == TOKEN_IDENT) {
2237 stmt->goto_label = label_symbol(token);
2238 token = token->next;
2239 } else {
2240 sparse_error(token->pos, "Expected identifier or goto expression");
2242 return expect(token, ';', "at end of statement");
2245 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2247 stmt->type = STMT_CONTEXT;
2248 token = parse_expression(token->next, &stmt->expression);
2249 if (stmt->expression->type == EXPR_PREOP
2250 && stmt->expression->op == '('
2251 && stmt->expression->unop->type == EXPR_COMMA) {
2252 struct expression *expr;
2253 expr = stmt->expression->unop;
2254 stmt->context = expr->left;
2255 stmt->expression = expr->right;
2257 return expect(token, ';', "at end of statement");
2260 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2262 stmt->type = STMT_RANGE;
2263 token = assignment_expression(token->next, &stmt->range_expression);
2264 token = expect(token, ',', "after range expression");
2265 token = assignment_expression(token, &stmt->range_low);
2266 token = expect(token, ',', "after low range");
2267 token = assignment_expression(token, &stmt->range_high);
2268 return expect(token, ';', "after range statement");
2271 static struct token *statement(struct token *token, struct statement **tree)
2273 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2275 *tree = stmt;
2276 if (token_type(token) == TOKEN_IDENT) {
2277 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2278 if (s && s->op->statement)
2279 return s->op->statement(token, stmt);
2281 if (match_op(token->next, ':')) {
2282 stmt->type = STMT_LABEL;
2283 stmt->label_identifier = label_symbol(token);
2284 token = skip_attributes(token->next->next);
2285 return statement(token, &stmt->label_statement);
2289 if (match_op(token, '{')) {
2290 stmt->type = STMT_COMPOUND;
2291 start_symbol_scope(stmt->pos);
2292 token = compound_statement(token->next, stmt);
2293 end_symbol_scope();
2295 return expect(token, '}', "at end of compound statement");
2298 stmt->type = STMT_EXPRESSION;
2299 return expression_statement(token, &stmt->expression);
2302 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2303 static struct token *label_statement(struct token *token)
2305 while (token_type(token) == TOKEN_IDENT) {
2306 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2307 /* it's block-scope, but we want label namespace */
2308 bind_symbol(sym, token->ident, NS_SYMBOL);
2309 sym->namespace = NS_LABEL;
2310 fn_local_symbol(sym);
2311 token = token->next;
2312 if (!match_op(token, ','))
2313 break;
2314 token = token->next;
2316 return expect(token, ';', "at end of label declaration");
2319 static struct token * statement_list(struct token *token, struct statement_list **list)
2321 int seen_statement = 0;
2322 while (token_type(token) == TOKEN_IDENT &&
2323 token->ident == &__label___ident)
2324 token = label_statement(token->next);
2325 for (;;) {
2326 struct statement * stmt;
2327 if (eof_token(token))
2328 break;
2329 if (match_op(token, '}'))
2330 break;
2331 if (lookup_type(token)) {
2332 if (seen_statement) {
2333 warning(token->pos, "mixing declarations and code");
2334 seen_statement = 0;
2336 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2337 token = external_declaration(token, &stmt->declaration);
2338 } else {
2339 seen_statement = Wdeclarationafterstatement;
2340 token = statement(token, &stmt);
2342 add_statement(list, stmt);
2344 return token;
2347 static struct token *identifier_list(struct token *token, struct symbol *fn)
2349 struct symbol_list **list = &fn->arguments;
2350 for (;;) {
2351 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2352 sym->ident = token->ident;
2353 token = token->next;
2354 sym->endpos = token->pos;
2355 sym->ctype.base_type = &incomplete_ctype;
2356 add_symbol(list, sym);
2357 if (!match_op(token, ',') ||
2358 token_type(token->next) != TOKEN_IDENT ||
2359 lookup_type(token->next))
2360 break;
2361 token = token->next;
2363 return token;
2366 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2368 struct symbol_list **list = &fn->arguments;
2370 for (;;) {
2371 struct symbol *sym;
2373 if (match_op(token, SPECIAL_ELLIPSIS)) {
2374 fn->variadic = 1;
2375 token = token->next;
2376 break;
2379 sym = alloc_symbol(token->pos, SYM_NODE);
2380 token = parameter_declaration(token, sym);
2381 if (sym->ctype.base_type == &void_ctype) {
2382 /* Special case: (void) */
2383 if (!*list && !sym->ident)
2384 break;
2385 warning(token->pos, "void parameter");
2387 add_symbol(list, sym);
2388 if (!match_op(token, ','))
2389 break;
2390 token = token->next;
2392 return token;
2395 struct token *compound_statement(struct token *token, struct statement *stmt)
2397 token = statement_list(token, &stmt->stmts);
2398 return token;
2401 static struct expression *identifier_expression(struct token *token)
2403 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2404 expr->expr_ident = token->ident;
2405 return expr;
2408 static struct expression *index_expression(struct expression *from, struct expression *to)
2410 int idx_from, idx_to;
2411 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2413 idx_from = const_expression_value(from);
2414 idx_to = idx_from;
2415 if (to) {
2416 idx_to = const_expression_value(to);
2417 if (idx_to < idx_from || idx_from < 0)
2418 warning(from->pos, "nonsense array initializer index range");
2420 expr->idx_from = idx_from;
2421 expr->idx_to = idx_to;
2422 return expr;
2425 static struct token *single_initializer(struct expression **ep, struct token *token)
2427 int expect_equal = 0;
2428 struct token *next = token->next;
2429 struct expression **tail = ep;
2430 int nested;
2432 *ep = NULL;
2434 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2435 struct expression *expr = identifier_expression(token);
2436 if (Wold_initializer)
2437 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2438 token = initializer(&expr->ident_expression, next->next);
2439 if (expr->ident_expression)
2440 *ep = expr;
2441 return token;
2444 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2445 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2446 struct expression *expr = identifier_expression(next);
2447 *tail = expr;
2448 tail = &expr->ident_expression;
2449 expect_equal = 1;
2450 token = next->next;
2451 } else if (match_op(token, '[')) {
2452 struct expression *from = NULL, *to = NULL, *expr;
2453 token = constant_expression(token->next, &from);
2454 if (!from) {
2455 sparse_error(token->pos, "Expected constant expression");
2456 break;
2458 if (match_op(token, SPECIAL_ELLIPSIS))
2459 token = constant_expression(token->next, &to);
2460 expr = index_expression(from, to);
2461 *tail = expr;
2462 tail = &expr->idx_expression;
2463 token = expect(token, ']', "at end of initializer index");
2464 if (nested)
2465 expect_equal = 1;
2466 } else {
2467 break;
2470 if (nested && !expect_equal) {
2471 if (!match_op(token, '='))
2472 warning(token->pos, "obsolete array initializer, use C99 syntax");
2473 else
2474 expect_equal = 1;
2476 if (expect_equal)
2477 token = expect(token, '=', "at end of initializer index");
2479 token = initializer(tail, token);
2480 if (!*tail)
2481 *ep = NULL;
2482 return token;
2485 static struct token *initializer_list(struct expression_list **list, struct token *token)
2487 struct expression *expr;
2489 for (;;) {
2490 token = single_initializer(&expr, token);
2491 if (!expr)
2492 break;
2493 add_expression(list, expr);
2494 if (!match_op(token, ','))
2495 break;
2496 token = token->next;
2498 return token;
2501 struct token *initializer(struct expression **tree, struct token *token)
2503 if (match_op(token, '{')) {
2504 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2505 *tree = expr;
2506 token = initializer_list(&expr->expr_list, token->next);
2507 return expect(token, '}', "at end of initializer");
2509 return assignment_expression(token, tree);
2512 static void declare_argument(struct symbol *sym, struct symbol *fn)
2514 if (!sym->ident) {
2515 sparse_error(sym->pos, "no identifier for function argument");
2516 return;
2518 bind_symbol(sym, sym->ident, NS_SYMBOL);
2521 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2522 struct symbol_list **list)
2524 struct symbol_list **old_symbol_list;
2525 struct symbol *base_type = decl->ctype.base_type;
2526 struct statement *stmt, **p;
2527 struct symbol *prev;
2528 struct symbol *arg;
2530 old_symbol_list = function_symbol_list;
2531 if (decl->ctype.modifiers & MOD_INLINE) {
2532 function_symbol_list = &decl->inline_symbol_list;
2533 p = &base_type->inline_stmt;
2534 } else {
2535 function_symbol_list = &decl->symbol_list;
2536 p = &base_type->stmt;
2538 function_computed_target_list = NULL;
2539 function_computed_goto_list = NULL;
2541 if (decl->ctype.modifiers & MOD_EXTERN) {
2542 if (!(decl->ctype.modifiers & MOD_INLINE))
2543 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2545 if (!(decl->ctype.modifiers & MOD_STATIC))
2546 decl->ctype.modifiers |= MOD_EXTERN;
2548 stmt = start_function(decl);
2550 *p = stmt;
2551 FOR_EACH_PTR (base_type->arguments, arg) {
2552 declare_argument(arg, base_type);
2553 } END_FOR_EACH_PTR(arg);
2555 token = compound_statement(token->next, stmt);
2557 end_function(decl);
2558 if (!(decl->ctype.modifiers & MOD_INLINE))
2559 add_symbol(list, decl);
2560 check_declaration(decl);
2561 decl->definition = decl;
2562 prev = decl->same_symbol;
2563 if (prev && prev->definition) {
2564 warning(decl->pos, "multiple definitions for function '%s'",
2565 show_ident(decl->ident));
2566 info(prev->definition->pos, " the previous one is here");
2567 } else {
2568 while (prev) {
2569 prev->definition = decl;
2570 prev = prev->same_symbol;
2573 function_symbol_list = old_symbol_list;
2574 if (function_computed_goto_list) {
2575 if (!function_computed_target_list)
2576 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2577 else {
2578 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2579 stmt->target_list = function_computed_target_list;
2580 } END_FOR_EACH_PTR(stmt);
2583 return expect(token, '}', "at end of function");
2586 static void promote_k_r_types(struct symbol *arg)
2588 struct symbol *base = arg->ctype.base_type;
2589 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2590 arg->ctype.base_type = &int_ctype;
2594 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2596 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2597 struct symbol *arg;
2599 FOR_EACH_PTR(real_args, arg) {
2600 struct symbol *type;
2602 /* This is quadratic in the number of arguments. We _really_ don't care */
2603 FOR_EACH_PTR(argtypes, type) {
2604 if (type->ident == arg->ident)
2605 goto match;
2606 } END_FOR_EACH_PTR(type);
2607 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2608 continue;
2609 match:
2610 type->used = 1;
2611 /* "char" and "short" promote to "int" */
2612 promote_k_r_types(type);
2614 arg->ctype = type->ctype;
2615 } END_FOR_EACH_PTR(arg);
2617 FOR_EACH_PTR(argtypes, arg) {
2618 if (!arg->used)
2619 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2620 } END_FOR_EACH_PTR(arg);
2624 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2625 struct symbol_list **list)
2627 struct symbol_list *args = NULL;
2629 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2630 do {
2631 token = declaration_list(token, &args);
2632 if (!match_op(token, ';')) {
2633 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2634 break;
2636 token = token->next;
2637 } while (lookup_type(token));
2639 apply_k_r_types(args, decl);
2641 if (!match_op(token, '{')) {
2642 sparse_error(token->pos, "expected function body");
2643 return token;
2645 return parse_function_body(token, decl, list);
2648 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2650 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2651 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2652 struct statement *stmt;
2654 anon->ctype.base_type = fn;
2655 stmt = alloc_statement(token->pos, STMT_NONE);
2656 fn->stmt = stmt;
2658 token = parse_asm_statement(token, stmt);
2660 add_symbol(list, anon);
2661 return token;
2664 struct token *external_declaration(struct token *token, struct symbol_list **list)
2666 struct ident *ident = NULL;
2667 struct symbol *decl;
2668 struct decl_state ctx = { .ident = &ident };
2669 struct ctype saved;
2670 struct symbol *base_type;
2671 unsigned long mod;
2672 int is_typedef;
2674 /* Top-level inline asm? */
2675 if (token_type(token) == TOKEN_IDENT) {
2676 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2677 if (s && s->op->toplevel)
2678 return s->op->toplevel(token, list);
2681 /* Parse declaration-specifiers, if any */
2682 token = declaration_specifiers(token, &ctx);
2683 mod = storage_modifiers(&ctx);
2684 decl = alloc_symbol(token->pos, SYM_NODE);
2685 /* Just a type declaration? */
2686 if (match_op(token, ';')) {
2687 apply_modifiers(token->pos, &ctx);
2688 return token->next;
2691 saved = ctx.ctype;
2692 token = declarator(token, &ctx);
2693 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2694 apply_modifiers(token->pos, &ctx);
2696 decl->ctype = ctx.ctype;
2697 decl->ctype.modifiers |= mod;
2698 decl->endpos = token->pos;
2700 /* Just a type declaration? */
2701 if (!ident) {
2702 warning(token->pos, "missing identifier in declaration");
2703 return expect(token, ';', "at the end of type declaration");
2706 /* type define declaration? */
2707 is_typedef = ctx.storage_class == STypedef;
2709 /* Typedefs don't have meaningful storage */
2710 if (is_typedef)
2711 decl->ctype.modifiers |= MOD_USERTYPE;
2713 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2715 base_type = decl->ctype.base_type;
2717 if (is_typedef) {
2718 if (base_type && !base_type->ident) {
2719 switch (base_type->type) {
2720 case SYM_STRUCT:
2721 case SYM_UNION:
2722 case SYM_ENUM:
2723 case SYM_RESTRICT:
2724 base_type->ident = ident;
2727 } else if (base_type && base_type->type == SYM_FN) {
2728 /* K&R argument declaration? */
2729 if (lookup_type(token))
2730 return parse_k_r_arguments(token, decl, list);
2731 if (match_op(token, '{'))
2732 return parse_function_body(token, decl, list);
2734 if (!(decl->ctype.modifiers & MOD_STATIC))
2735 decl->ctype.modifiers |= MOD_EXTERN;
2736 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2737 sparse_error(token->pos, "void declaration");
2740 for (;;) {
2741 if (!is_typedef && match_op(token, '=')) {
2742 if (decl->ctype.modifiers & MOD_EXTERN) {
2743 warning(decl->pos, "symbol with external linkage has initializer");
2744 decl->ctype.modifiers &= ~MOD_EXTERN;
2746 token = initializer(&decl->initializer, token->next);
2748 if (!is_typedef) {
2749 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2750 add_symbol(list, decl);
2751 fn_local_symbol(decl);
2754 check_declaration(decl);
2755 if (decl->same_symbol)
2756 decl->definition = decl->same_symbol->definition;
2758 if (!match_op(token, ','))
2759 break;
2761 token = token->next;
2762 ident = NULL;
2763 decl = alloc_symbol(token->pos, SYM_NODE);
2764 ctx.ctype = saved;
2765 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2766 token = declarator(token, &ctx);
2767 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2768 apply_modifiers(token->pos, &ctx);
2769 decl->ctype = ctx.ctype;
2770 decl->ctype.modifiers |= mod;
2771 decl->endpos = token->pos;
2772 if (!ident) {
2773 sparse_error(token->pos, "expected identifier name in type definition");
2774 return token;
2777 if (is_typedef)
2778 decl->ctype.modifiers |= MOD_USERTYPE;
2780 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2782 /* Function declarations are automatically extern unless specifically static */
2783 base_type = decl->ctype.base_type;
2784 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2785 if (!(decl->ctype.modifiers & MOD_STATIC))
2786 decl->ctype.modifiers |= MOD_EXTERN;
2789 return expect(token, ';', "at end of declaration");