sparse, llvm: simplify function generation
[smatch.git] / parse.c
blob890e56b43f3c958f6b34f1e2fb43173156f31007
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 "noclone",
549 "__noclone",
550 "__noclone__",
551 "noinline",
552 "__noinline__",
553 "nonnull",
554 "__nonnull",
555 "__nonnull__",
556 "nothrow",
557 "__nothrow",
558 "__nothrow__",
559 "regparm",
560 "__regparm__",
561 "section",
562 "__section__",
563 "sentinel",
564 "__sentinel__",
565 "signal",
566 "__signal__",
567 "stdcall",
568 "__stdcall__",
569 "syscall_linkage",
570 "__syscall_linkage__",
571 "sysv_abi",
572 "__sysv_abi__",
573 "unused",
574 "__unused__",
575 "used",
576 "__used__",
577 "vector_size",
578 "__vector_size__",
579 "visibility",
580 "__visibility__",
581 "warn_unused_result",
582 "__warn_unused_result__",
583 "warning",
584 "__warning__",
585 "weak",
586 "__weak__",
590 void init_parser(int stream)
592 int i;
593 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
594 struct init_keyword *ptr = keyword_table + i;
595 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
596 sym->ident->keyword = 1;
597 if (ptr->ns == NS_TYPEDEF)
598 sym->ident->reserved = 1;
599 sym->ctype.modifiers = ptr->modifiers;
600 sym->ctype.base_type = ptr->type;
601 sym->op = ptr->op;
604 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
605 const char * name = ignored_attributes[i];
606 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
607 NS_KEYWORD);
608 sym->ident->keyword = 1;
609 sym->op = &ignore_attr_op;
614 // Add a symbol to the list of function-local symbols
615 static void fn_local_symbol(struct symbol *sym)
617 if (function_symbol_list)
618 add_symbol(function_symbol_list, sym);
621 static int SENTINEL_ATTR match_idents(struct token *token, ...)
623 va_list args;
624 struct ident * next;
626 if (token_type(token) != TOKEN_IDENT)
627 return 0;
629 va_start(args, token);
630 do {
631 next = va_arg(args, struct ident *);
632 } while (next && token->ident != next);
633 va_end(args);
635 return next && token->ident == next;
639 struct statement *alloc_statement(struct position pos, int type)
641 struct statement *stmt = __alloc_statement(0);
642 stmt->type = type;
643 stmt->pos = pos;
644 return stmt;
647 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
649 static void apply_modifiers(struct position pos, struct decl_state *ctx)
651 struct symbol *ctype;
652 if (!ctx->mode)
653 return;
654 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
655 if (!ctype)
656 sparse_error(pos, "don't know how to apply mode to %s",
657 show_typename(ctx->ctype.base_type));
658 else
659 ctx->ctype.base_type = ctype;
663 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
665 struct symbol *sym = alloc_symbol(pos, type);
667 sym->ctype.base_type = ctype->base_type;
668 sym->ctype.modifiers = ctype->modifiers;
670 ctype->base_type = sym;
671 ctype->modifiers = 0;
672 return sym;
676 * NOTE! NS_LABEL is not just a different namespace,
677 * it also ends up using function scope instead of the
678 * regular symbol scope.
680 struct symbol *label_symbol(struct token *token)
682 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
683 if (!sym) {
684 sym = alloc_symbol(token->pos, SYM_LABEL);
685 bind_symbol(sym, token->ident, NS_LABEL);
686 fn_local_symbol(sym);
688 return sym;
691 static struct token *struct_union_enum_specifier(enum type type,
692 struct token *token, struct decl_state *ctx,
693 struct token *(*parse)(struct token *, struct symbol *))
695 struct symbol *sym;
696 struct position *repos;
698 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
699 if (token_type(token) == TOKEN_IDENT) {
700 sym = lookup_symbol(token->ident, NS_STRUCT);
701 if (!sym ||
702 (is_outer_scope(sym->scope) &&
703 (match_op(token->next,';') || match_op(token->next,'{')))) {
704 // Either a new symbol, or else an out-of-scope
705 // symbol being redefined.
706 sym = alloc_symbol(token->pos, type);
707 bind_symbol(sym, token->ident, NS_STRUCT);
709 if (sym->type != type)
710 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
711 ctx->ctype.base_type = sym;
712 repos = &token->pos;
713 token = token->next;
714 if (match_op(token, '{')) {
715 // The following test is actually wrong for empty
716 // structs, but (1) they are not C99, (2) gcc does
717 // the same thing, and (3) it's easier.
718 if (sym->symbol_list)
719 error_die(token->pos, "redefinition of %s", show_typename (sym));
720 sym->pos = *repos;
721 token = parse(token->next, sym);
722 token = expect(token, '}', "at end of struct-union-enum-specifier");
724 // Mark the structure as needing re-examination
725 sym->examined = 0;
726 sym->endpos = token->pos;
728 return token;
731 // private struct/union/enum type
732 if (!match_op(token, '{')) {
733 sparse_error(token->pos, "expected declaration");
734 ctx->ctype.base_type = &bad_ctype;
735 return token;
738 sym = alloc_symbol(token->pos, type);
739 token = parse(token->next, sym);
740 ctx->ctype.base_type = sym;
741 token = expect(token, '}', "at end of specifier");
742 sym->endpos = token->pos;
744 return token;
747 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
749 struct symbol *field, *last = NULL;
750 struct token *res;
751 res = struct_declaration_list(token, &sym->symbol_list);
752 FOR_EACH_PTR(sym->symbol_list, field) {
753 if (!field->ident) {
754 struct symbol *base = field->ctype.base_type;
755 if (base && base->type == SYM_BITFIELD)
756 continue;
758 if (last)
759 last->next_subobject = field;
760 last = field;
761 } END_FOR_EACH_PTR(field);
762 return res;
765 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
767 return struct_declaration_list(token, &sym->symbol_list);
770 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
772 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
775 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
777 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
781 typedef struct {
782 int x;
783 unsigned long long y;
784 } Num;
786 static void upper_boundary(Num *n, Num *v)
788 if (n->x > v->x)
789 return;
790 if (n->x < v->x) {
791 *n = *v;
792 return;
794 if (n->y < v->y)
795 n->y = v->y;
798 static void lower_boundary(Num *n, Num *v)
800 if (n->x < v->x)
801 return;
802 if (n->x > v->x) {
803 *n = *v;
804 return;
806 if (n->y > v->y)
807 n->y = v->y;
810 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
812 int shift = type->bit_size;
813 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
815 if (!is_unsigned)
816 shift--;
817 if (upper->x == 0 && upper->y >> shift)
818 return 0;
819 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
820 return 1;
821 return 0;
824 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
826 if (s1->bit_size < s2->bit_size) {
827 s1 = s2;
828 } else if (s1->bit_size == s2->bit_size) {
829 if (s2->ctype.modifiers & MOD_UNSIGNED)
830 s1 = s2;
832 if (s1->bit_size < bits_in_int)
833 return &int_ctype;
834 return s1;
837 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
839 struct symbol *sym;
841 FOR_EACH_PTR(list, sym) {
842 struct expression *expr = sym->initializer;
843 struct symbol *ctype;
844 if (expr->type != EXPR_VALUE)
845 continue;
846 ctype = expr->ctype;
847 if (ctype->bit_size == base_type->bit_size)
848 continue;
849 cast_value(expr, base_type, expr, ctype);
850 } END_FOR_EACH_PTR(sym);
853 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
855 unsigned long long lastval = 0;
856 struct symbol *ctype = NULL, *base_type = NULL;
857 Num upper = {-1, 0}, lower = {1, 0};
859 parent->examined = 1;
860 parent->ctype.base_type = &int_ctype;
861 while (token_type(token) == TOKEN_IDENT) {
862 struct expression *expr = NULL;
863 struct token *next = token->next;
864 struct symbol *sym;
866 if (match_op(next, '=')) {
867 next = constant_expression(next->next, &expr);
868 lastval = get_expression_value(expr);
869 ctype = &void_ctype;
870 if (expr && expr->ctype)
871 ctype = expr->ctype;
872 } else if (!ctype) {
873 ctype = &int_ctype;
874 } else if (is_int_type(ctype)) {
875 lastval++;
876 } else {
877 error_die(token->pos, "can't increment the last enum member");
880 if (!expr) {
881 expr = alloc_expression(token->pos, EXPR_VALUE);
882 expr->value = lastval;
883 expr->ctype = ctype;
886 sym = alloc_symbol(token->pos, SYM_NODE);
887 bind_symbol(sym, token->ident, NS_SYMBOL);
888 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
889 sym->initializer = expr;
890 sym->enum_member = 1;
891 sym->ctype.base_type = parent;
892 add_ptr_list(&parent->symbol_list, sym);
894 if (base_type != &bad_ctype) {
895 if (ctype->type == SYM_NODE)
896 ctype = ctype->ctype.base_type;
897 if (ctype->type == SYM_ENUM) {
898 if (ctype == parent)
899 ctype = base_type;
900 else
901 ctype = ctype->ctype.base_type;
904 * base_type rules:
905 * - if all enums are of the same type, then
906 * the base_type is that type (two first
907 * cases)
908 * - if enums are of different types, they
909 * all have to be integer types, and the
910 * base type is at least "int_ctype".
911 * - otherwise the base_type is "bad_ctype".
913 if (!base_type) {
914 base_type = ctype;
915 } else if (ctype == base_type) {
916 /* nothing */
917 } else if (is_int_type(base_type) && is_int_type(ctype)) {
918 base_type = bigger_enum_type(base_type, ctype);
919 } else
920 base_type = &bad_ctype;
921 parent->ctype.base_type = base_type;
923 if (is_int_type(base_type)) {
924 Num v = {.y = lastval};
925 if (ctype->ctype.modifiers & MOD_UNSIGNED)
926 v.x = 0;
927 else if ((long long)lastval >= 0)
928 v.x = 0;
929 else
930 v.x = -1;
931 upper_boundary(&upper, &v);
932 lower_boundary(&lower, &v);
934 token = next;
936 sym->endpos = token->pos;
938 if (!match_op(token, ','))
939 break;
940 token = token->next;
942 if (!base_type) {
943 sparse_error(token->pos, "bad enum definition");
944 base_type = &bad_ctype;
946 else if (!is_int_type(base_type))
947 base_type = base_type;
948 else if (type_is_ok(base_type, &upper, &lower))
949 base_type = base_type;
950 else if (type_is_ok(&int_ctype, &upper, &lower))
951 base_type = &int_ctype;
952 else if (type_is_ok(&uint_ctype, &upper, &lower))
953 base_type = &uint_ctype;
954 else if (type_is_ok(&long_ctype, &upper, &lower))
955 base_type = &long_ctype;
956 else if (type_is_ok(&ulong_ctype, &upper, &lower))
957 base_type = &ulong_ctype;
958 else if (type_is_ok(&llong_ctype, &upper, &lower))
959 base_type = &llong_ctype;
960 else if (type_is_ok(&ullong_ctype, &upper, &lower))
961 base_type = &ullong_ctype;
962 else
963 base_type = &bad_ctype;
964 parent->ctype.base_type = base_type;
965 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
966 parent->examined = 0;
968 cast_enum_list(parent->symbol_list, base_type);
970 return token;
973 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
975 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
976 struct ctype *ctype = &ctx->ctype.base_type->ctype;
978 if (!ctype->base_type)
979 ctype->base_type = &incomplete_ctype;
981 return ret;
984 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
986 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
988 struct symbol *sym;
990 if (!match_op(token, '(')) {
991 sparse_error(token->pos, "expected '(' after typeof");
992 return token;
994 if (lookup_type(token->next)) {
995 token = typename(token->next, &sym, NULL);
996 ctx->ctype.base_type = sym->ctype.base_type;
997 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
998 } else {
999 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1000 token = parse_expression(token->next, &typeof_sym->initializer);
1002 typeof_sym->endpos = token->pos;
1003 if (!typeof_sym->initializer) {
1004 sparse_error(token->pos, "expected expression after the '(' token");
1005 typeof_sym = &bad_ctype;
1007 ctx->ctype.base_type = typeof_sym;
1009 return expect(token, ')', "after typeof");
1012 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1014 struct expression *expr = NULL;
1015 if (match_op(token, '('))
1016 token = parens_expression(token, &expr, "in attribute");
1017 return token;
1020 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1022 if (!ctx->ctype.alignment)
1023 ctx->ctype.alignment = 1;
1024 return token;
1027 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1029 int alignment = max_alignment;
1030 struct expression *expr = NULL;
1032 if (match_op(token, '(')) {
1033 token = parens_expression(token, &expr, "in attribute");
1034 if (expr)
1035 alignment = const_expression_value(expr);
1037 if (alignment & (alignment-1)) {
1038 warning(token->pos, "I don't like non-power-of-2 alignments");
1039 return token;
1040 } else if (alignment > ctx->ctype.alignment)
1041 ctx->ctype.alignment = alignment;
1042 return token;
1045 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1047 if (ctx->modifiers & qual)
1048 warning(*pos, "duplicate %s", modifier_string(qual));
1049 ctx->modifiers |= qual;
1052 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1054 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1055 return token;
1058 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1060 struct expression *expr = NULL;
1061 int as;
1062 token = expect(token, '(', "after address_space attribute");
1063 token = conditional_expression(token, &expr);
1064 if (expr) {
1065 as = const_expression_value(expr);
1066 if (Waddress_space && as)
1067 ctx->ctype.as = as;
1069 token = expect(token, ')', "after address_space attribute");
1070 return token;
1073 static struct symbol *to_QI_mode(struct symbol *ctype)
1075 if (ctype->ctype.base_type != &int_type)
1076 return NULL;
1077 if (ctype == &char_ctype)
1078 return ctype;
1079 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1080 : &schar_ctype;
1083 static struct symbol *to_HI_mode(struct symbol *ctype)
1085 if (ctype->ctype.base_type != &int_type)
1086 return NULL;
1087 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1088 : &sshort_ctype;
1091 static struct symbol *to_SI_mode(struct symbol *ctype)
1093 if (ctype->ctype.base_type != &int_type)
1094 return NULL;
1095 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1096 : &sint_ctype;
1099 static struct symbol *to_DI_mode(struct symbol *ctype)
1101 if (ctype->ctype.base_type != &int_type)
1102 return NULL;
1103 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1104 : &sllong_ctype;
1107 static struct symbol *to_TI_mode(struct symbol *ctype)
1109 if (ctype->ctype.base_type != &int_type)
1110 return NULL;
1111 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1112 : &slllong_ctype;
1115 static struct symbol *to_word_mode(struct symbol *ctype)
1117 if (ctype->ctype.base_type != &int_type)
1118 return NULL;
1119 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1120 : &slong_ctype;
1123 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1125 token = expect(token, '(', "after mode attribute");
1126 if (token_type(token) == TOKEN_IDENT) {
1127 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1128 if (mode && mode->op->type == KW_MODE)
1129 ctx->mode = mode->op;
1130 else
1131 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1132 token = token->next;
1133 } else
1134 sparse_error(token->pos, "expect attribute mode symbol\n");
1135 token = expect(token, ')', "after mode attribute");
1136 return token;
1139 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1141 struct context *context = alloc_context();
1142 struct expression *args[3];
1143 int argc = 0;
1145 token = expect(token, '(', "after context attribute");
1146 while (!match_op(token, ')')) {
1147 struct expression *expr = NULL;
1148 token = conditional_expression(token, &expr);
1149 if (!expr)
1150 break;
1151 if (argc < 3)
1152 args[argc++] = expr;
1153 if (!match_op(token, ','))
1154 break;
1155 token = token->next;
1158 switch(argc) {
1159 case 0:
1160 sparse_error(token->pos, "expected context input/output values");
1161 break;
1162 case 1:
1163 context->in = get_expression_value(args[0]);
1164 break;
1165 case 2:
1166 context->in = get_expression_value(args[0]);
1167 context->out = get_expression_value(args[1]);
1168 break;
1169 case 3:
1170 context->context = args[0];
1171 context->in = get_expression_value(args[1]);
1172 context->out = get_expression_value(args[2]);
1173 break;
1176 if (argc)
1177 add_ptr_list(&ctx->ctype.contexts, context);
1179 token = expect(token, ')', "after context attribute");
1180 return token;
1183 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1185 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1186 ctx->ctype.base_type->designated_init = 1;
1187 else
1188 warning(token->pos, "attribute designated_init applied to non-structure type");
1189 return token;
1192 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1194 if (Wtransparent_union)
1195 warning(token->pos, "ignoring attribute __transparent_union__");
1196 return token;
1199 static struct token *recover_unknown_attribute(struct token *token)
1201 struct expression *expr = NULL;
1203 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1204 token = token->next;
1205 if (match_op(token, '('))
1206 token = parens_expression(token, &expr, "in attribute");
1207 return token;
1210 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1212 token = expect(token, '(', "after attribute");
1213 token = expect(token, '(', "after attribute");
1215 for (;;) {
1216 struct ident *attribute_name;
1217 struct symbol *attr;
1219 if (eof_token(token))
1220 break;
1221 if (match_op(token, ';'))
1222 break;
1223 if (token_type(token) != TOKEN_IDENT)
1224 break;
1225 attribute_name = token->ident;
1226 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1227 if (attr && attr->op->attribute)
1228 token = attr->op->attribute(token->next, attr, ctx);
1229 else
1230 token = recover_unknown_attribute(token);
1232 if (!match_op(token, ','))
1233 break;
1234 token = token->next;
1237 token = expect(token, ')', "after attribute");
1238 token = expect(token, ')', "after attribute");
1239 return token;
1242 static const char *storage_class[] =
1244 [STypedef] = "typedef",
1245 [SAuto] = "auto",
1246 [SExtern] = "extern",
1247 [SStatic] = "static",
1248 [SRegister] = "register",
1249 [SForced] = "[force]"
1252 static unsigned long storage_modifiers(struct decl_state *ctx)
1254 static unsigned long mod[] =
1256 [SAuto] = MOD_AUTO,
1257 [SExtern] = MOD_EXTERN,
1258 [SStatic] = MOD_STATIC,
1259 [SRegister] = MOD_REGISTER
1261 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1262 | (ctx->is_tls ? MOD_TLS : 0);
1265 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1267 /* __thread can be used alone, or with extern or static */
1268 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1269 sparse_error(*pos, "__thread can only be used alone, or with "
1270 "extern or static");
1271 return;
1274 if (!ctx->storage_class) {
1275 ctx->storage_class = class;
1276 return;
1278 if (ctx->storage_class == class)
1279 sparse_error(*pos, "duplicate %s", storage_class[class]);
1280 else
1281 sparse_error(*pos, "multiple storage classes");
1284 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1286 set_storage_class(&next->pos, ctx, STypedef);
1287 return next;
1290 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1292 set_storage_class(&next->pos, ctx, SAuto);
1293 return next;
1296 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1298 set_storage_class(&next->pos, ctx, SRegister);
1299 return next;
1302 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1304 set_storage_class(&next->pos, ctx, SStatic);
1305 return next;
1308 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1310 set_storage_class(&next->pos, ctx, SExtern);
1311 return next;
1314 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1316 /* This GCC extension can be used alone, or with extern or static */
1317 if (!ctx->storage_class || ctx->storage_class == SStatic
1318 || ctx->storage_class == SExtern) {
1319 ctx->is_tls = 1;
1320 } else {
1321 sparse_error(next->pos, "__thread can only be used alone, or "
1322 "with extern or static");
1325 return next;
1328 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1330 set_storage_class(&token->pos, ctx, SForced);
1331 return token;
1334 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1336 ctx->is_inline = 1;
1337 return next;
1340 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1342 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1343 return next;
1346 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1348 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1349 return next;
1352 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1354 unsigned long mod = thistype->modifiers;
1356 if (mod)
1357 apply_qualifier(&pos, ctype, mod);
1359 /* Context */
1360 concat_ptr_list((struct ptr_list *)thistype->contexts,
1361 (struct ptr_list **)&ctype->contexts);
1363 /* Alignment */
1364 if (thistype->alignment > ctype->alignment)
1365 ctype->alignment = thistype->alignment;
1367 /* Address space */
1368 if (thistype->as)
1369 ctype->as = thistype->as;
1372 static void specifier_conflict(struct position pos, int what, struct ident *new)
1374 const char *old;
1375 if (what & (Set_S | Set_T))
1376 goto Catch_all;
1377 if (what & Set_Char)
1378 old = "char";
1379 else if (what & Set_Double)
1380 old = "double";
1381 else if (what & Set_Float)
1382 old = "float";
1383 else if (what & Set_Signed)
1384 old = "signed";
1385 else if (what & Set_Unsigned)
1386 old = "unsigned";
1387 else if (what & Set_Short)
1388 old = "short";
1389 else if (what & Set_Long)
1390 old = "long";
1391 else
1392 old = "long long";
1393 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1394 old, show_ident(new));
1395 return;
1397 Catch_all:
1398 sparse_error(pos, "two or more data types in declaration specifiers");
1401 static struct symbol * const int_types[] =
1402 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1403 static struct symbol * const signed_types[] =
1404 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1405 &slllong_ctype};
1406 static struct symbol * const unsigned_types[] =
1407 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1408 &ulllong_ctype};
1409 static struct symbol * const real_types[] =
1410 {&float_ctype, &double_ctype, &ldouble_ctype};
1411 static struct symbol * const char_types[] =
1412 {&char_ctype, &schar_ctype, &uchar_ctype};
1413 static struct symbol * const * const types[] = {
1414 int_types + 1, signed_types + 1, unsigned_types + 1,
1415 real_types + 1, char_types, char_types + 1, char_types + 2
1418 struct symbol *ctype_integer(int size, int want_unsigned)
1420 return types[want_unsigned ? CUInt : CInt][size];
1423 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1425 while (token_type(t) == TOKEN_IDENT) {
1426 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1427 if (!s)
1428 break;
1429 if (s->type != SYM_KEYWORD)
1430 break;
1431 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1432 break;
1433 t = t->next;
1434 if (s->op->declarator)
1435 t = s->op->declarator(t, ctx);
1437 return t;
1440 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1442 int seen = 0;
1443 int class = CInt;
1444 int size = 0;
1446 while (token_type(token) == TOKEN_IDENT) {
1447 struct symbol *s = lookup_symbol(token->ident,
1448 NS_TYPEDEF | NS_SYMBOL);
1449 if (!s || !(s->namespace & NS_TYPEDEF))
1450 break;
1451 if (s->type != SYM_KEYWORD) {
1452 if (seen & Set_Any)
1453 break;
1454 seen |= Set_S | Set_T;
1455 ctx->ctype.base_type = s->ctype.base_type;
1456 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1457 token = token->next;
1458 continue;
1460 if (s->op->type & KW_SPECIFIER) {
1461 if (seen & s->op->test) {
1462 specifier_conflict(token->pos,
1463 seen & s->op->test,
1464 token->ident);
1465 break;
1467 seen |= s->op->set;
1468 class += s->op->class;
1469 if (s->op->type & KW_SHORT) {
1470 size = -1;
1471 } else if (s->op->type & KW_LONG && size++) {
1472 if (class == CReal) {
1473 specifier_conflict(token->pos,
1474 Set_Vlong,
1475 &double_ident);
1476 break;
1478 seen |= Set_Vlong;
1481 token = token->next;
1482 if (s->op->declarator)
1483 token = s->op->declarator(token, ctx);
1484 if (s->op->type & KW_EXACT) {
1485 ctx->ctype.base_type = s->ctype.base_type;
1486 ctx->ctype.modifiers |= s->ctype.modifiers;
1490 if (!(seen & Set_S)) { /* not set explicitly? */
1491 struct symbol *base = &incomplete_ctype;
1492 if (seen & Set_Any)
1493 base = types[class][size];
1494 ctx->ctype.base_type = base;
1497 if (ctx->ctype.modifiers & MOD_BITWISE) {
1498 struct symbol *type;
1499 ctx->ctype.modifiers &= ~MOD_BITWISE;
1500 if (!is_int_type(ctx->ctype.base_type)) {
1501 sparse_error(token->pos, "invalid modifier");
1502 return token;
1504 type = alloc_symbol(token->pos, SYM_BASETYPE);
1505 *type = *ctx->ctype.base_type;
1506 type->ctype.modifiers &= ~MOD_SPECIFIER;
1507 type->ctype.base_type = ctx->ctype.base_type;
1508 type->type = SYM_RESTRICT;
1509 ctx->ctype.base_type = type;
1510 create_fouled(type);
1512 return token;
1515 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1517 struct expression *expr = NULL;
1519 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1520 token = token->next;
1521 token = parse_expression(token, &expr);
1522 sym->array_size = expr;
1523 return token;
1526 static struct token *parameter_type_list(struct token *, struct symbol *);
1527 static struct token *identifier_list(struct token *, struct symbol *);
1528 static struct token *declarator(struct token *token, struct decl_state *ctx);
1530 static struct token *skip_attribute(struct token *token)
1532 token = token->next;
1533 if (match_op(token, '(')) {
1534 int depth = 1;
1535 token = token->next;
1536 while (depth && !eof_token(token)) {
1537 if (token_type(token) == TOKEN_SPECIAL) {
1538 if (token->special == '(')
1539 depth++;
1540 else if (token->special == ')')
1541 depth--;
1543 token = token->next;
1546 return token;
1549 static struct token *skip_attributes(struct token *token)
1551 struct symbol *keyword;
1552 for (;;) {
1553 if (token_type(token) != TOKEN_IDENT)
1554 break;
1555 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1556 if (!keyword || keyword->type != SYM_KEYWORD)
1557 break;
1558 if (!(keyword->op->type & KW_ATTRIBUTE))
1559 break;
1560 token = expect(token->next, '(', "after attribute");
1561 token = expect(token, '(', "after attribute");
1562 for (;;) {
1563 if (eof_token(token))
1564 break;
1565 if (match_op(token, ';'))
1566 break;
1567 if (token_type(token) != TOKEN_IDENT)
1568 break;
1569 token = skip_attribute(token);
1570 if (!match_op(token, ','))
1571 break;
1572 token = token->next;
1574 token = expect(token, ')', "after attribute");
1575 token = expect(token, ')', "after attribute");
1577 return token;
1580 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1582 struct symbol *keyword;
1583 for (;;) {
1584 if (token_type(token) != TOKEN_IDENT)
1585 break;
1586 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1587 if (!keyword || keyword->type != SYM_KEYWORD)
1588 break;
1589 if (!(keyword->op->type & keywords))
1590 break;
1591 token = keyword->op->declarator(token->next, ctx);
1592 keywords &= KW_ATTRIBUTE;
1594 return token;
1597 static int is_nested(struct token *token, struct token **p,
1598 int prefer_abstract)
1601 * This can be either a parameter list or a grouping.
1602 * For the direct (non-abstract) case, we know if must be
1603 * a parameter list if we already saw the identifier.
1604 * For the abstract case, we know if must be a parameter
1605 * list if it is empty or starts with a type.
1607 struct token *next = token->next;
1609 *p = next = skip_attributes(next);
1611 if (token_type(next) == TOKEN_IDENT) {
1612 if (lookup_type(next))
1613 return !prefer_abstract;
1614 return 1;
1617 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1618 return 0;
1620 return 1;
1623 enum kind {
1624 Empty, K_R, Proto, Bad_Func,
1627 static enum kind which_func(struct token *token,
1628 struct ident **n,
1629 int prefer_abstract)
1631 struct token *next = token->next;
1633 if (token_type(next) == TOKEN_IDENT) {
1634 if (lookup_type(next))
1635 return Proto;
1636 /* identifier list not in definition; complain */
1637 if (prefer_abstract)
1638 warning(token->pos,
1639 "identifier list not in definition");
1640 return K_R;
1643 if (token_type(next) != TOKEN_SPECIAL)
1644 return Bad_Func;
1646 if (next->special == ')') {
1647 /* don't complain about those */
1648 if (!n || match_op(next->next, ';'))
1649 return Empty;
1650 warning(next->pos,
1651 "non-ANSI function declaration of function '%s'",
1652 show_ident(*n));
1653 return Empty;
1656 if (next->special == SPECIAL_ELLIPSIS) {
1657 warning(next->pos,
1658 "variadic functions must have one named argument");
1659 return Proto;
1662 return Bad_Func;
1665 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1667 struct ctype *ctype = &ctx->ctype;
1668 struct token *next;
1669 struct ident **p = ctx->ident;
1671 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1672 *ctx->ident = token->ident;
1673 token = token->next;
1674 } else if (match_op(token, '(') &&
1675 is_nested(token, &next, ctx->prefer_abstract)) {
1676 struct symbol *base_type = ctype->base_type;
1677 if (token->next != next)
1678 next = handle_attributes(token->next, ctx,
1679 KW_ATTRIBUTE);
1680 token = declarator(next, ctx);
1681 token = expect(token, ')', "in nested declarator");
1682 while (ctype->base_type != base_type)
1683 ctype = &ctype->base_type->ctype;
1684 p = NULL;
1687 if (match_op(token, '(')) {
1688 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1689 struct symbol *fn;
1690 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1691 token = token->next;
1692 if (kind == K_R)
1693 token = identifier_list(token, fn);
1694 else if (kind == Proto)
1695 token = parameter_type_list(token, fn);
1696 token = expect(token, ')', "in function declarator");
1697 fn->endpos = token->pos;
1698 return token;
1701 while (match_op(token, '[')) {
1702 struct symbol *array;
1703 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1704 token = abstract_array_declarator(token->next, array);
1705 token = expect(token, ']', "in abstract_array_declarator");
1706 array->endpos = token->pos;
1707 ctype = &array->ctype;
1709 return token;
1712 static struct token *pointer(struct token *token, struct decl_state *ctx)
1714 while (match_op(token,'*')) {
1715 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1716 ptr->ctype.modifiers = ctx->ctype.modifiers;
1717 ptr->ctype.base_type = ctx->ctype.base_type;
1718 ptr->ctype.as = ctx->ctype.as;
1719 ptr->ctype.contexts = ctx->ctype.contexts;
1720 ctx->ctype.modifiers = 0;
1721 ctx->ctype.base_type = ptr;
1722 ctx->ctype.as = 0;
1723 ctx->ctype.contexts = NULL;
1724 ctx->ctype.alignment = 0;
1726 token = handle_qualifiers(token->next, ctx);
1727 ctx->ctype.base_type->endpos = token->pos;
1729 return token;
1732 static struct token *declarator(struct token *token, struct decl_state *ctx)
1734 token = pointer(token, ctx);
1735 return direct_declarator(token, ctx);
1738 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1740 struct ctype *ctype = &ctx->ctype;
1741 struct expression *expr;
1742 struct symbol *bitfield;
1743 long long width;
1745 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1746 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1747 show_typename(ctype->base_type));
1748 // Parse this to recover gracefully.
1749 return conditional_expression(token->next, &expr);
1752 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1753 token = conditional_expression(token->next, &expr);
1754 width = const_expression_value(expr);
1755 bitfield->bit_size = width;
1757 if (width < 0 || width > INT_MAX) {
1758 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1759 width = -1;
1760 } else if (*ctx->ident && width == 0) {
1761 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1762 show_ident(*ctx->ident));
1763 width = -1;
1764 } else if (*ctx->ident) {
1765 struct symbol *base_type = bitfield->ctype.base_type;
1766 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1767 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1768 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1769 // Valid values are either {-1;0} or {0}, depending on integer
1770 // representation. The latter makes for very efficient code...
1771 sparse_error(token->pos, "dubious one-bit signed bitfield");
1773 if (Wdefault_bitfield_sign &&
1774 bitfield_type->type != SYM_ENUM &&
1775 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1776 is_signed) {
1777 // The sign of bitfields is unspecified by default.
1778 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1781 bitfield->bit_size = width;
1782 bitfield->endpos = token->pos;
1783 return token;
1786 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1788 struct decl_state ctx = {.prefer_abstract = 0};
1789 struct ctype saved;
1790 unsigned long mod;
1792 token = declaration_specifiers(token, &ctx);
1793 mod = storage_modifiers(&ctx);
1794 saved = ctx.ctype;
1795 for (;;) {
1796 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1797 ctx.ident = &decl->ident;
1799 token = declarator(token, &ctx);
1800 if (match_op(token, ':'))
1801 token = handle_bitfield(token, &ctx);
1803 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1804 apply_modifiers(token->pos, &ctx);
1806 decl->ctype = ctx.ctype;
1807 decl->ctype.modifiers |= mod;
1808 decl->endpos = token->pos;
1809 add_symbol(list, decl);
1810 if (!match_op(token, ','))
1811 break;
1812 token = token->next;
1813 ctx.ctype = saved;
1815 return token;
1818 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1820 while (!match_op(token, '}')) {
1821 if (!match_op(token, ';'))
1822 token = declaration_list(token, list);
1823 if (!match_op(token, ';')) {
1824 sparse_error(token->pos, "expected ; at end of declaration");
1825 break;
1827 token = token->next;
1829 return token;
1832 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1834 struct decl_state ctx = {.prefer_abstract = 1};
1836 token = declaration_specifiers(token, &ctx);
1837 ctx.ident = &sym->ident;
1838 token = declarator(token, &ctx);
1839 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1840 apply_modifiers(token->pos, &ctx);
1841 sym->ctype = ctx.ctype;
1842 sym->ctype.modifiers |= storage_modifiers(&ctx);
1843 sym->endpos = token->pos;
1844 sym->forced_arg = ctx.storage_class == SForced;
1845 return token;
1848 struct token *typename(struct token *token, struct symbol **p, int *forced)
1850 struct decl_state ctx = {.prefer_abstract = 1};
1851 int class;
1852 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1853 *p = sym;
1854 token = declaration_specifiers(token, &ctx);
1855 token = declarator(token, &ctx);
1856 apply_modifiers(token->pos, &ctx);
1857 sym->ctype = ctx.ctype;
1858 sym->endpos = token->pos;
1859 class = ctx.storage_class;
1860 if (forced) {
1861 *forced = 0;
1862 if (class == SForced) {
1863 *forced = 1;
1864 class = 0;
1867 if (class)
1868 warning(sym->pos, "storage class in typename (%s %s)",
1869 storage_class[class], show_typename(sym));
1870 return token;
1873 static struct token *expression_statement(struct token *token, struct expression **tree)
1875 token = parse_expression(token, tree);
1876 return expect(token, ';', "at end of statement");
1879 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1880 struct expression_list **inout)
1882 struct expression *expr;
1884 /* Allow empty operands */
1885 if (match_op(token->next, ':') || match_op(token->next, ')'))
1886 return token->next;
1887 do {
1888 struct ident *ident = NULL;
1889 if (match_op(token->next, '[') &&
1890 token_type(token->next->next) == TOKEN_IDENT &&
1891 match_op(token->next->next->next, ']')) {
1892 ident = token->next->next->ident;
1893 token = token->next->next->next;
1895 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1896 token = primary_expression(token->next, &expr);
1897 add_expression(inout, expr);
1898 token = parens_expression(token, &expr, "in asm parameter");
1899 add_expression(inout, expr);
1900 } while (match_op(token, ','));
1901 return token;
1904 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1905 struct expression_list **clobbers)
1907 struct expression *expr;
1909 do {
1910 token = primary_expression(token->next, &expr);
1911 if (expr)
1912 add_expression(clobbers, expr);
1913 } while (match_op(token, ','));
1914 return token;
1917 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1918 struct symbol_list **labels)
1920 struct symbol *label;
1922 do {
1923 token = token->next; /* skip ':' and ',' */
1924 if (token_type(token) != TOKEN_IDENT)
1925 return token;
1926 label = label_symbol(token);
1927 add_symbol(labels, label);
1928 token = token->next;
1929 } while (match_op(token, ','));
1930 return token;
1933 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1935 int is_goto = 0;
1937 token = token->next;
1938 stmt->type = STMT_ASM;
1939 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1940 token = token->next;
1942 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1943 is_goto = 1;
1944 token = token->next;
1946 token = expect(token, '(', "after asm");
1947 token = parse_expression(token, &stmt->asm_string);
1948 if (match_op(token, ':'))
1949 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1950 if (match_op(token, ':'))
1951 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1952 if (match_op(token, ':'))
1953 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1954 if (is_goto && match_op(token, ':'))
1955 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1956 token = expect(token, ')', "after asm");
1957 return expect(token, ';', "at end of asm-statement");
1960 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1962 struct expression *expr;
1963 token = expect(token, '(', "after asm");
1964 token = parse_expression(token->next, &expr);
1965 token = expect(token, ')', "after asm");
1966 return token;
1969 /* Make a statement out of an expression */
1970 static struct statement *make_statement(struct expression *expr)
1972 struct statement *stmt;
1974 if (!expr)
1975 return NULL;
1976 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1977 stmt->expression = expr;
1978 return stmt;
1982 * All iterators have two symbols associated with them:
1983 * the "continue" and "break" symbols, which are targets
1984 * for continue and break statements respectively.
1986 * They are in a special name-space, but they follow
1987 * all the normal visibility rules, so nested iterators
1988 * automatically work right.
1990 static void start_iterator(struct statement *stmt)
1992 struct symbol *cont, *brk;
1994 start_symbol_scope();
1995 cont = alloc_symbol(stmt->pos, SYM_NODE);
1996 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1997 brk = alloc_symbol(stmt->pos, SYM_NODE);
1998 bind_symbol(brk, &break_ident, NS_ITERATOR);
2000 stmt->type = STMT_ITERATOR;
2001 stmt->iterator_break = brk;
2002 stmt->iterator_continue = cont;
2003 fn_local_symbol(brk);
2004 fn_local_symbol(cont);
2007 static void end_iterator(struct statement *stmt)
2009 end_symbol_scope();
2012 static struct statement *start_function(struct symbol *sym)
2014 struct symbol *ret;
2015 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2017 start_function_scope();
2018 ret = alloc_symbol(sym->pos, SYM_NODE);
2019 ret->ctype = sym->ctype.base_type->ctype;
2020 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2021 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2022 bind_symbol(ret, &return_ident, NS_ITERATOR);
2023 stmt->ret = ret;
2024 fn_local_symbol(ret);
2026 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2027 current_fn = sym;
2029 return stmt;
2032 static void end_function(struct symbol *sym)
2034 current_fn = NULL;
2035 end_function_scope();
2039 * A "switch()" statement, like an iterator, has a
2040 * the "break" symbol associated with it. It works
2041 * exactly like the iterator break - it's the target
2042 * for any break-statements in scope, and means that
2043 * "break" handling doesn't even need to know whether
2044 * it's breaking out of an iterator or a switch.
2046 * In addition, the "case" symbol is a marker for the
2047 * case/default statements to find the switch statement
2048 * that they are associated with.
2050 static void start_switch(struct statement *stmt)
2052 struct symbol *brk, *switch_case;
2054 start_symbol_scope();
2055 brk = alloc_symbol(stmt->pos, SYM_NODE);
2056 bind_symbol(brk, &break_ident, NS_ITERATOR);
2058 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2059 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2060 switch_case->stmt = stmt;
2062 stmt->type = STMT_SWITCH;
2063 stmt->switch_break = brk;
2064 stmt->switch_case = switch_case;
2066 fn_local_symbol(brk);
2067 fn_local_symbol(switch_case);
2070 static void end_switch(struct statement *stmt)
2072 if (!stmt->switch_case->symbol_list)
2073 warning(stmt->pos, "switch with no cases");
2074 end_symbol_scope();
2077 static void add_case_statement(struct statement *stmt)
2079 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2080 struct symbol *sym;
2082 if (!target) {
2083 sparse_error(stmt->pos, "not in switch scope");
2084 stmt->type = STMT_NONE;
2085 return;
2087 sym = alloc_symbol(stmt->pos, SYM_NODE);
2088 add_symbol(&target->symbol_list, sym);
2089 sym->stmt = stmt;
2090 stmt->case_label = sym;
2091 fn_local_symbol(sym);
2094 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2096 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2098 if (!target)
2099 error_die(token->pos, "internal error: return without a function target");
2100 stmt->type = STMT_RETURN;
2101 stmt->ret_target = target;
2102 return expression_statement(token->next, &stmt->ret_value);
2105 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2107 struct symbol_list *syms;
2108 struct expression *e1, *e2, *e3;
2109 struct statement *iterator;
2111 start_iterator(stmt);
2112 token = expect(token->next, '(', "after 'for'");
2114 syms = NULL;
2115 e1 = NULL;
2116 /* C99 variable declaration? */
2117 if (lookup_type(token)) {
2118 token = external_declaration(token, &syms);
2119 } else {
2120 token = parse_expression(token, &e1);
2121 token = expect(token, ';', "in 'for'");
2123 token = parse_expression(token, &e2);
2124 token = expect(token, ';', "in 'for'");
2125 token = parse_expression(token, &e3);
2126 token = expect(token, ')', "in 'for'");
2127 token = statement(token, &iterator);
2129 stmt->iterator_syms = syms;
2130 stmt->iterator_pre_statement = make_statement(e1);
2131 stmt->iterator_pre_condition = e2;
2132 stmt->iterator_post_statement = make_statement(e3);
2133 stmt->iterator_post_condition = NULL;
2134 stmt->iterator_statement = iterator;
2135 end_iterator(stmt);
2137 return token;
2140 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2142 struct expression *expr;
2143 struct statement *iterator;
2145 start_iterator(stmt);
2146 token = parens_expression(token->next, &expr, "after 'while'");
2147 token = statement(token, &iterator);
2149 stmt->iterator_pre_condition = expr;
2150 stmt->iterator_post_condition = NULL;
2151 stmt->iterator_statement = iterator;
2152 end_iterator(stmt);
2154 return token;
2157 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2159 struct expression *expr;
2160 struct statement *iterator;
2162 start_iterator(stmt);
2163 token = statement(token->next, &iterator);
2164 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2165 token = token->next;
2166 else
2167 sparse_error(token->pos, "expected 'while' after 'do'");
2168 token = parens_expression(token, &expr, "after 'do-while'");
2170 stmt->iterator_post_condition = expr;
2171 stmt->iterator_statement = iterator;
2172 end_iterator(stmt);
2174 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2175 warning(iterator->pos, "do-while statement is not a compound statement");
2177 return expect(token, ';', "after statement");
2180 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2182 stmt->type = STMT_IF;
2183 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2184 token = statement(token, &stmt->if_true);
2185 if (token_type(token) != TOKEN_IDENT)
2186 return token;
2187 if (token->ident != &else_ident)
2188 return token;
2189 return statement(token->next, &stmt->if_false);
2192 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2194 stmt->type = STMT_CASE;
2195 token = expect(token, ':', "after default/case");
2196 add_case_statement(stmt);
2197 return statement(token, &stmt->case_statement);
2200 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2202 token = parse_expression(token->next, &stmt->case_expression);
2203 if (match_op(token, SPECIAL_ELLIPSIS))
2204 token = parse_expression(token->next, &stmt->case_to);
2205 return case_statement(token, stmt);
2208 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2210 return case_statement(token->next, stmt);
2213 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2215 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2216 stmt->type = STMT_GOTO;
2217 stmt->goto_label = target;
2218 if (!target)
2219 sparse_error(stmt->pos, "break/continue not in iterator scope");
2220 return expect(token->next, ';', "at end of statement");
2223 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2225 stmt->type = STMT_SWITCH;
2226 start_switch(stmt);
2227 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2228 token = statement(token, &stmt->switch_statement);
2229 end_switch(stmt);
2230 return token;
2233 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2235 stmt->type = STMT_GOTO;
2236 token = token->next;
2237 if (match_op(token, '*')) {
2238 token = parse_expression(token->next, &stmt->goto_expression);
2239 add_statement(&function_computed_goto_list, stmt);
2240 } else if (token_type(token) == TOKEN_IDENT) {
2241 stmt->goto_label = label_symbol(token);
2242 token = token->next;
2243 } else {
2244 sparse_error(token->pos, "Expected identifier or goto expression");
2246 return expect(token, ';', "at end of statement");
2249 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2251 stmt->type = STMT_CONTEXT;
2252 token = parse_expression(token->next, &stmt->expression);
2253 if (stmt->expression->type == EXPR_PREOP
2254 && stmt->expression->op == '('
2255 && stmt->expression->unop->type == EXPR_COMMA) {
2256 struct expression *expr;
2257 expr = stmt->expression->unop;
2258 stmt->context = expr->left;
2259 stmt->expression = expr->right;
2261 return expect(token, ';', "at end of statement");
2264 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2266 stmt->type = STMT_RANGE;
2267 token = assignment_expression(token->next, &stmt->range_expression);
2268 token = expect(token, ',', "after range expression");
2269 token = assignment_expression(token, &stmt->range_low);
2270 token = expect(token, ',', "after low range");
2271 token = assignment_expression(token, &stmt->range_high);
2272 return expect(token, ';', "after range statement");
2275 static struct token *statement(struct token *token, struct statement **tree)
2277 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2279 *tree = stmt;
2280 if (token_type(token) == TOKEN_IDENT) {
2281 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2282 if (s && s->op->statement)
2283 return s->op->statement(token, stmt);
2285 if (match_op(token->next, ':')) {
2286 struct symbol *s = label_symbol(token);
2287 stmt->type = STMT_LABEL;
2288 stmt->label_identifier = s;
2289 if (s->stmt)
2290 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2291 s->stmt = stmt;
2292 token = skip_attributes(token->next->next);
2293 return statement(token, &stmt->label_statement);
2297 if (match_op(token, '{')) {
2298 stmt->type = STMT_COMPOUND;
2299 start_symbol_scope();
2300 token = compound_statement(token->next, stmt);
2301 end_symbol_scope();
2303 return expect(token, '}', "at end of compound statement");
2306 stmt->type = STMT_EXPRESSION;
2307 return expression_statement(token, &stmt->expression);
2310 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2311 static struct token *label_statement(struct token *token)
2313 while (token_type(token) == TOKEN_IDENT) {
2314 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2315 /* it's block-scope, but we want label namespace */
2316 bind_symbol(sym, token->ident, NS_SYMBOL);
2317 sym->namespace = NS_LABEL;
2318 fn_local_symbol(sym);
2319 token = token->next;
2320 if (!match_op(token, ','))
2321 break;
2322 token = token->next;
2324 return expect(token, ';', "at end of label declaration");
2327 static struct token * statement_list(struct token *token, struct statement_list **list)
2329 int seen_statement = 0;
2330 while (token_type(token) == TOKEN_IDENT &&
2331 token->ident == &__label___ident)
2332 token = label_statement(token->next);
2333 for (;;) {
2334 struct statement * stmt;
2335 if (eof_token(token))
2336 break;
2337 if (match_op(token, '}'))
2338 break;
2339 if (lookup_type(token)) {
2340 if (seen_statement) {
2341 warning(token->pos, "mixing declarations and code");
2342 seen_statement = 0;
2344 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2345 token = external_declaration(token, &stmt->declaration);
2346 } else {
2347 seen_statement = Wdeclarationafterstatement;
2348 token = statement(token, &stmt);
2350 add_statement(list, stmt);
2352 return token;
2355 static struct token *identifier_list(struct token *token, struct symbol *fn)
2357 struct symbol_list **list = &fn->arguments;
2358 for (;;) {
2359 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2360 sym->ident = token->ident;
2361 token = token->next;
2362 sym->endpos = token->pos;
2363 sym->ctype.base_type = &incomplete_ctype;
2364 add_symbol(list, sym);
2365 if (!match_op(token, ',') ||
2366 token_type(token->next) != TOKEN_IDENT ||
2367 lookup_type(token->next))
2368 break;
2369 token = token->next;
2371 return token;
2374 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2376 struct symbol_list **list = &fn->arguments;
2378 for (;;) {
2379 struct symbol *sym;
2381 if (match_op(token, SPECIAL_ELLIPSIS)) {
2382 fn->variadic = 1;
2383 token = token->next;
2384 break;
2387 sym = alloc_symbol(token->pos, SYM_NODE);
2388 token = parameter_declaration(token, sym);
2389 if (sym->ctype.base_type == &void_ctype) {
2390 /* Special case: (void) */
2391 if (!*list && !sym->ident)
2392 break;
2393 warning(token->pos, "void parameter");
2395 add_symbol(list, sym);
2396 if (!match_op(token, ','))
2397 break;
2398 token = token->next;
2400 return token;
2403 struct token *compound_statement(struct token *token, struct statement *stmt)
2405 token = statement_list(token, &stmt->stmts);
2406 return token;
2409 static struct expression *identifier_expression(struct token *token)
2411 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2412 expr->expr_ident = token->ident;
2413 return expr;
2416 static struct expression *index_expression(struct expression *from, struct expression *to)
2418 int idx_from, idx_to;
2419 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2421 idx_from = const_expression_value(from);
2422 idx_to = idx_from;
2423 if (to) {
2424 idx_to = const_expression_value(to);
2425 if (idx_to < idx_from || idx_from < 0)
2426 warning(from->pos, "nonsense array initializer index range");
2428 expr->idx_from = idx_from;
2429 expr->idx_to = idx_to;
2430 return expr;
2433 static struct token *single_initializer(struct expression **ep, struct token *token)
2435 int expect_equal = 0;
2436 struct token *next = token->next;
2437 struct expression **tail = ep;
2438 int nested;
2440 *ep = NULL;
2442 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2443 struct expression *expr = identifier_expression(token);
2444 if (Wold_initializer)
2445 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2446 token = initializer(&expr->ident_expression, next->next);
2447 if (expr->ident_expression)
2448 *ep = expr;
2449 return token;
2452 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2453 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2454 struct expression *expr = identifier_expression(next);
2455 *tail = expr;
2456 tail = &expr->ident_expression;
2457 expect_equal = 1;
2458 token = next->next;
2459 } else if (match_op(token, '[')) {
2460 struct expression *from = NULL, *to = NULL, *expr;
2461 token = constant_expression(token->next, &from);
2462 if (!from) {
2463 sparse_error(token->pos, "Expected constant expression");
2464 break;
2466 if (match_op(token, SPECIAL_ELLIPSIS))
2467 token = constant_expression(token->next, &to);
2468 expr = index_expression(from, to);
2469 *tail = expr;
2470 tail = &expr->idx_expression;
2471 token = expect(token, ']', "at end of initializer index");
2472 if (nested)
2473 expect_equal = 1;
2474 } else {
2475 break;
2478 if (nested && !expect_equal) {
2479 if (!match_op(token, '='))
2480 warning(token->pos, "obsolete array initializer, use C99 syntax");
2481 else
2482 expect_equal = 1;
2484 if (expect_equal)
2485 token = expect(token, '=', "at end of initializer index");
2487 token = initializer(tail, token);
2488 if (!*tail)
2489 *ep = NULL;
2490 return token;
2493 static struct token *initializer_list(struct expression_list **list, struct token *token)
2495 struct expression *expr;
2497 for (;;) {
2498 token = single_initializer(&expr, token);
2499 if (!expr)
2500 break;
2501 add_expression(list, expr);
2502 if (!match_op(token, ','))
2503 break;
2504 token = token->next;
2506 return token;
2509 struct token *initializer(struct expression **tree, struct token *token)
2511 if (match_op(token, '{')) {
2512 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2513 *tree = expr;
2514 token = initializer_list(&expr->expr_list, token->next);
2515 return expect(token, '}', "at end of initializer");
2517 return assignment_expression(token, tree);
2520 static void declare_argument(struct symbol *sym, struct symbol *fn)
2522 if (!sym->ident) {
2523 sparse_error(sym->pos, "no identifier for function argument");
2524 return;
2526 bind_symbol(sym, sym->ident, NS_SYMBOL);
2529 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2530 struct symbol_list **list)
2532 struct symbol_list **old_symbol_list;
2533 struct symbol *base_type = decl->ctype.base_type;
2534 struct statement *stmt, **p;
2535 struct symbol *prev;
2536 struct symbol *arg;
2538 old_symbol_list = function_symbol_list;
2539 if (decl->ctype.modifiers & MOD_INLINE) {
2540 function_symbol_list = &decl->inline_symbol_list;
2541 p = &base_type->inline_stmt;
2542 } else {
2543 function_symbol_list = &decl->symbol_list;
2544 p = &base_type->stmt;
2546 function_computed_target_list = NULL;
2547 function_computed_goto_list = NULL;
2549 if (decl->ctype.modifiers & MOD_EXTERN) {
2550 if (!(decl->ctype.modifiers & MOD_INLINE))
2551 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2553 if (!(decl->ctype.modifiers & MOD_STATIC))
2554 decl->ctype.modifiers |= MOD_EXTERN;
2556 stmt = start_function(decl);
2558 *p = stmt;
2559 FOR_EACH_PTR (base_type->arguments, arg) {
2560 declare_argument(arg, base_type);
2561 } END_FOR_EACH_PTR(arg);
2563 token = compound_statement(token->next, stmt);
2565 end_function(decl);
2566 if (!(decl->ctype.modifiers & MOD_INLINE))
2567 add_symbol(list, decl);
2568 check_declaration(decl);
2569 decl->definition = decl;
2570 prev = decl->same_symbol;
2571 if (prev && prev->definition) {
2572 warning(decl->pos, "multiple definitions for function '%s'",
2573 show_ident(decl->ident));
2574 info(prev->definition->pos, " the previous one is here");
2575 } else {
2576 while (prev) {
2577 prev->definition = decl;
2578 prev = prev->same_symbol;
2581 function_symbol_list = old_symbol_list;
2582 if (function_computed_goto_list) {
2583 if (!function_computed_target_list)
2584 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2585 else {
2586 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2587 stmt->target_list = function_computed_target_list;
2588 } END_FOR_EACH_PTR(stmt);
2591 return expect(token, '}', "at end of function");
2594 static void promote_k_r_types(struct symbol *arg)
2596 struct symbol *base = arg->ctype.base_type;
2597 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2598 arg->ctype.base_type = &int_ctype;
2602 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2604 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2605 struct symbol *arg;
2607 FOR_EACH_PTR(real_args, arg) {
2608 struct symbol *type;
2610 /* This is quadratic in the number of arguments. We _really_ don't care */
2611 FOR_EACH_PTR(argtypes, type) {
2612 if (type->ident == arg->ident)
2613 goto match;
2614 } END_FOR_EACH_PTR(type);
2615 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2616 continue;
2617 match:
2618 type->used = 1;
2619 /* "char" and "short" promote to "int" */
2620 promote_k_r_types(type);
2622 arg->ctype = type->ctype;
2623 } END_FOR_EACH_PTR(arg);
2625 FOR_EACH_PTR(argtypes, arg) {
2626 if (!arg->used)
2627 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2628 } END_FOR_EACH_PTR(arg);
2632 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2633 struct symbol_list **list)
2635 struct symbol_list *args = NULL;
2637 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2638 do {
2639 token = declaration_list(token, &args);
2640 if (!match_op(token, ';')) {
2641 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2642 break;
2644 token = token->next;
2645 } while (lookup_type(token));
2647 apply_k_r_types(args, decl);
2649 if (!match_op(token, '{')) {
2650 sparse_error(token->pos, "expected function body");
2651 return token;
2653 return parse_function_body(token, decl, list);
2656 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2658 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2659 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2660 struct statement *stmt;
2662 anon->ctype.base_type = fn;
2663 stmt = alloc_statement(token->pos, STMT_NONE);
2664 fn->stmt = stmt;
2666 token = parse_asm_statement(token, stmt);
2668 add_symbol(list, anon);
2669 return token;
2672 struct token *external_declaration(struct token *token, struct symbol_list **list)
2674 struct ident *ident = NULL;
2675 struct symbol *decl;
2676 struct decl_state ctx = { .ident = &ident };
2677 struct ctype saved;
2678 struct symbol *base_type;
2679 unsigned long mod;
2680 int is_typedef;
2682 /* Top-level inline asm? */
2683 if (token_type(token) == TOKEN_IDENT) {
2684 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2685 if (s && s->op->toplevel)
2686 return s->op->toplevel(token, list);
2689 /* Parse declaration-specifiers, if any */
2690 token = declaration_specifiers(token, &ctx);
2691 mod = storage_modifiers(&ctx);
2692 decl = alloc_symbol(token->pos, SYM_NODE);
2693 /* Just a type declaration? */
2694 if (match_op(token, ';')) {
2695 apply_modifiers(token->pos, &ctx);
2696 return token->next;
2699 saved = ctx.ctype;
2700 token = declarator(token, &ctx);
2701 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2702 apply_modifiers(token->pos, &ctx);
2704 decl->ctype = ctx.ctype;
2705 decl->ctype.modifiers |= mod;
2706 decl->endpos = token->pos;
2708 /* Just a type declaration? */
2709 if (!ident) {
2710 warning(token->pos, "missing identifier in declaration");
2711 return expect(token, ';', "at the end of type declaration");
2714 /* type define declaration? */
2715 is_typedef = ctx.storage_class == STypedef;
2717 /* Typedefs don't have meaningful storage */
2718 if (is_typedef)
2719 decl->ctype.modifiers |= MOD_USERTYPE;
2721 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2723 base_type = decl->ctype.base_type;
2725 if (is_typedef) {
2726 if (base_type && !base_type->ident) {
2727 switch (base_type->type) {
2728 case SYM_STRUCT:
2729 case SYM_UNION:
2730 case SYM_ENUM:
2731 case SYM_RESTRICT:
2732 base_type->ident = ident;
2735 } else if (base_type && base_type->type == SYM_FN) {
2736 /* K&R argument declaration? */
2737 if (lookup_type(token))
2738 return parse_k_r_arguments(token, decl, list);
2739 if (match_op(token, '{'))
2740 return parse_function_body(token, decl, list);
2742 if (!(decl->ctype.modifiers & MOD_STATIC))
2743 decl->ctype.modifiers |= MOD_EXTERN;
2744 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2745 sparse_error(token->pos, "void declaration");
2748 for (;;) {
2749 if (!is_typedef && match_op(token, '=')) {
2750 if (decl->ctype.modifiers & MOD_EXTERN) {
2751 warning(decl->pos, "symbol with external linkage has initializer");
2752 decl->ctype.modifiers &= ~MOD_EXTERN;
2754 token = initializer(&decl->initializer, token->next);
2756 if (!is_typedef) {
2757 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2758 add_symbol(list, decl);
2759 fn_local_symbol(decl);
2762 check_declaration(decl);
2763 if (decl->same_symbol)
2764 decl->definition = decl->same_symbol->definition;
2766 if (!match_op(token, ','))
2767 break;
2769 token = token->next;
2770 ident = NULL;
2771 decl = alloc_symbol(token->pos, SYM_NODE);
2772 ctx.ctype = saved;
2773 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2774 token = declarator(token, &ctx);
2775 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2776 apply_modifiers(token->pos, &ctx);
2777 decl->ctype = ctx.ctype;
2778 decl->ctype.modifiers |= mod;
2779 decl->endpos = token->pos;
2780 if (!ident) {
2781 sparse_error(token->pos, "expected identifier name in type definition");
2782 return token;
2785 if (is_typedef)
2786 decl->ctype.modifiers |= MOD_USERTYPE;
2788 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2790 /* Function declarations are automatically extern unless specifically static */
2791 base_type = decl->ctype.base_type;
2792 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2793 if (!(decl->ctype.modifiers & MOD_STATIC))
2794 decl->ctype.modifiers |= MOD_EXTERN;
2797 return expect(token, ';', "at end of declaration");