bit_shift: add test_bit() function
[smatch.git] / parse.c
blobca497d6b8188dbc4d6ad0fe354c46b548ef5f2cc
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 },
467 { "__mode__", NS_KEYWORD, .op = &mode_op },
468 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
469 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
470 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
471 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
472 { "SI", NS_KEYWORD, .op = &mode_SI_op },
473 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
474 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
475 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
476 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
477 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
478 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
479 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
482 const char *ignored_attributes[] = {
483 "alias",
484 "__alias__",
485 "alloc_size",
486 "__alloc_size__",
487 "always_inline",
488 "__always_inline__",
489 "bounded",
490 "__bounded__",
491 "cdecl",
492 "__cdecl__",
493 "cold",
494 "__cold__",
495 "const",
496 "__const",
497 "__const__",
498 "constructor",
499 "__constructor__",
500 "deprecated",
501 "__deprecated__",
502 "destructor",
503 "__destructor__",
504 "dllexport",
505 "__dllexport__",
506 "dllimport",
507 "__dllimport__",
508 "externally_visible",
509 "__externally_visible__",
510 "fastcall",
511 "__fastcall__",
512 "format",
513 "__format__",
514 "format_arg",
515 "__format_arg__",
516 "hot",
517 "__hot__",
518 "l1_text",
519 "__l1_text__",
520 "l1_data",
521 "__l1_data__",
522 "l2",
523 "__l2__",
524 "may_alias",
525 "__may_alias__",
526 "malloc",
527 "__malloc__",
528 "may_alias",
529 "__may_alias__",
530 "model",
531 "__model__",
532 "ms_abi",
533 "__ms_abi__",
534 "naked",
535 "__naked__",
536 "no_instrument_function",
537 "__no_instrument_function__",
538 "noinline",
539 "__noinline__",
540 "nonnull",
541 "__nonnull",
542 "__nonnull__",
543 "nothrow",
544 "__nothrow",
545 "__nothrow__",
546 "pure",
547 "__pure__",
548 "regparm",
549 "__regparm__",
550 "section",
551 "__section__",
552 "sentinel",
553 "__sentinel__",
554 "signal",
555 "__signal__",
556 "stdcall",
557 "__stdcall__",
558 "syscall_linkage",
559 "__syscall_linkage__",
560 "sysv_abi",
561 "__sysv_abi__",
562 "unused",
563 "__unused__",
564 "used",
565 "__used__",
566 "visibility",
567 "__visibility__",
568 "warn_unused_result",
569 "__warn_unused_result__",
570 "warning",
571 "__warning__",
572 "weak",
573 "__weak__",
577 void init_parser(int stream)
579 int i;
580 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
581 struct init_keyword *ptr = keyword_table + i;
582 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
583 sym->ident->keyword = 1;
584 if (ptr->ns == NS_TYPEDEF)
585 sym->ident->reserved = 1;
586 sym->ctype.modifiers = ptr->modifiers;
587 sym->ctype.base_type = ptr->type;
588 sym->op = ptr->op;
591 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
592 const char * name = ignored_attributes[i];
593 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
594 NS_KEYWORD);
595 sym->ident->keyword = 1;
596 sym->op = &ignore_attr_op;
601 // Add a symbol to the list of function-local symbols
602 static void fn_local_symbol(struct symbol *sym)
604 if (function_symbol_list)
605 add_symbol(function_symbol_list, sym);
608 static int SENTINEL_ATTR match_idents(struct token *token, ...)
610 va_list args;
611 struct ident * next;
613 if (token_type(token) != TOKEN_IDENT)
614 return 0;
616 va_start(args, token);
617 do {
618 next = va_arg(args, struct ident *);
619 } while (next && token->ident != next);
620 va_end(args);
622 return next && token->ident == next;
626 struct statement *alloc_statement(struct position pos, int type)
628 struct statement *stmt = __alloc_statement(0);
629 stmt->type = type;
630 stmt->pos = pos;
631 return stmt;
634 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
636 static void apply_modifiers(struct position pos, struct decl_state *ctx)
638 struct symbol *ctype;
639 if (!ctx->mode)
640 return;
641 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
642 if (!ctype)
643 sparse_error(pos, "don't know how to apply mode to %s",
644 show_typename(ctx->ctype.base_type));
645 else
646 ctx->ctype.base_type = ctype;
650 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
652 struct symbol *sym = alloc_symbol(pos, type);
654 sym->ctype.base_type = ctype->base_type;
655 sym->ctype.modifiers = ctype->modifiers;
657 ctype->base_type = sym;
658 ctype->modifiers = 0;
659 return sym;
663 * NOTE! NS_LABEL is not just a different namespace,
664 * it also ends up using function scope instead of the
665 * regular symbol scope.
667 struct symbol *label_symbol(struct token *token)
669 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
670 if (!sym) {
671 sym = alloc_symbol(token->pos, SYM_LABEL);
672 bind_symbol(sym, token->ident, NS_LABEL);
673 fn_local_symbol(sym);
675 return sym;
678 static struct token *struct_union_enum_specifier(enum type type,
679 struct token *token, struct decl_state *ctx,
680 struct token *(*parse)(struct token *, struct symbol *))
682 struct symbol *sym;
683 struct position *repos;
685 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
686 if (token_type(token) == TOKEN_IDENT) {
687 sym = lookup_symbol(token->ident, NS_STRUCT);
688 if (!sym ||
689 (is_outer_scope(sym->scope) &&
690 (match_op(token->next,';') || match_op(token->next,'{')))) {
691 // Either a new symbol, or else an out-of-scope
692 // symbol being redefined.
693 sym = alloc_symbol(token->pos, type);
694 bind_symbol(sym, token->ident, NS_STRUCT);
696 if (sym->type != type)
697 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
698 ctx->ctype.base_type = sym;
699 repos = &token->pos;
700 token = token->next;
701 if (match_op(token, '{')) {
702 // The following test is actually wrong for empty
703 // structs, but (1) they are not C99, (2) gcc does
704 // the same thing, and (3) it's easier.
705 if (sym->symbol_list)
706 error_die(token->pos, "redefinition of %s", show_typename (sym));
707 sym->pos = *repos;
708 token = parse(token->next, sym);
709 token = expect(token, '}', "at end of struct-union-enum-specifier");
711 // Mark the structure as needing re-examination
712 sym->examined = 0;
713 sym->endpos = token->pos;
715 return token;
718 // private struct/union/enum type
719 if (!match_op(token, '{')) {
720 sparse_error(token->pos, "expected declaration");
721 ctx->ctype.base_type = &bad_ctype;
722 return token;
725 sym = alloc_symbol(token->pos, type);
726 token = parse(token->next, sym);
727 ctx->ctype.base_type = sym;
728 token = expect(token, '}', "at end of specifier");
729 sym->endpos = token->pos;
731 return token;
734 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
736 struct symbol *field, *last = NULL;
737 struct token *res;
738 res = struct_declaration_list(token, &sym->symbol_list);
739 FOR_EACH_PTR(sym->symbol_list, field) {
740 if (!field->ident) {
741 struct symbol *base = field->ctype.base_type;
742 if (base && base->type == SYM_BITFIELD)
743 continue;
745 if (last)
746 last->next_subobject = field;
747 last = field;
748 } END_FOR_EACH_PTR(field);
749 return res;
752 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
754 return struct_declaration_list(token, &sym->symbol_list);
757 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
759 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
762 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
764 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
768 typedef struct {
769 int x;
770 unsigned long long y;
771 } Num;
773 static void upper_boundary(Num *n, Num *v)
775 if (n->x > v->x)
776 return;
777 if (n->x < v->x) {
778 *n = *v;
779 return;
781 if (n->y < v->y)
782 n->y = v->y;
785 static void lower_boundary(Num *n, Num *v)
787 if (n->x < v->x)
788 return;
789 if (n->x > v->x) {
790 *n = *v;
791 return;
793 if (n->y > v->y)
794 n->y = v->y;
797 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
799 int shift = type->bit_size;
800 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
802 if (!is_unsigned)
803 shift--;
804 if (upper->x == 0 && upper->y >> shift)
805 return 0;
806 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
807 return 1;
808 return 0;
811 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
813 if (s1->bit_size < s2->bit_size) {
814 s1 = s2;
815 } else if (s1->bit_size == s2->bit_size) {
816 if (s2->ctype.modifiers & MOD_UNSIGNED)
817 s1 = s2;
819 if (s1->bit_size < bits_in_int)
820 return &int_ctype;
821 return s1;
824 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
826 struct symbol *sym;
828 FOR_EACH_PTR(list, sym) {
829 struct expression *expr = sym->initializer;
830 struct symbol *ctype;
831 if (expr->type != EXPR_VALUE)
832 continue;
833 ctype = expr->ctype;
834 if (ctype->bit_size == base_type->bit_size)
835 continue;
836 cast_value(expr, base_type, expr, ctype);
837 } END_FOR_EACH_PTR(sym);
840 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
842 unsigned long long lastval = 0;
843 struct symbol *ctype = NULL, *base_type = NULL;
844 Num upper = {-1, 0}, lower = {1, 0};
846 parent->examined = 1;
847 parent->ctype.base_type = &int_ctype;
848 while (token_type(token) == TOKEN_IDENT) {
849 struct expression *expr = NULL;
850 struct token *next = token->next;
851 struct symbol *sym;
853 if (match_op(next, '=')) {
854 next = constant_expression(next->next, &expr);
855 lastval = get_expression_value(expr);
856 ctype = &void_ctype;
857 if (expr && expr->ctype)
858 ctype = expr->ctype;
859 } else if (!ctype) {
860 ctype = &int_ctype;
861 } else if (is_int_type(ctype)) {
862 lastval++;
863 } else {
864 error_die(token->pos, "can't increment the last enum member");
867 if (!expr) {
868 expr = alloc_expression(token->pos, EXPR_VALUE);
869 expr->value = lastval;
870 expr->ctype = ctype;
873 sym = alloc_symbol(token->pos, SYM_NODE);
874 bind_symbol(sym, token->ident, NS_SYMBOL);
875 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
876 sym->initializer = expr;
877 sym->enum_member = 1;
878 sym->ctype.base_type = parent;
879 add_ptr_list(&parent->symbol_list, sym);
881 if (base_type != &bad_ctype) {
882 if (ctype->type == SYM_NODE)
883 ctype = ctype->ctype.base_type;
884 if (ctype->type == SYM_ENUM) {
885 if (ctype == parent)
886 ctype = base_type;
887 else
888 ctype = ctype->ctype.base_type;
891 * base_type rules:
892 * - if all enums are of the same type, then
893 * the base_type is that type (two first
894 * cases)
895 * - if enums are of different types, they
896 * all have to be integer types, and the
897 * base type is at least "int_ctype".
898 * - otherwise the base_type is "bad_ctype".
900 if (!base_type) {
901 base_type = ctype;
902 } else if (ctype == base_type) {
903 /* nothing */
904 } else if (is_int_type(base_type) && is_int_type(ctype)) {
905 base_type = bigger_enum_type(base_type, ctype);
906 } else
907 base_type = &bad_ctype;
908 parent->ctype.base_type = base_type;
910 if (is_int_type(base_type)) {
911 Num v = {.y = lastval};
912 if (ctype->ctype.modifiers & MOD_UNSIGNED)
913 v.x = 0;
914 else if ((long long)lastval >= 0)
915 v.x = 0;
916 else
917 v.x = -1;
918 upper_boundary(&upper, &v);
919 lower_boundary(&lower, &v);
921 token = next;
923 sym->endpos = token->pos;
925 if (!match_op(token, ','))
926 break;
927 token = token->next;
929 if (!base_type) {
930 sparse_error(token->pos, "bad enum definition");
931 base_type = &bad_ctype;
933 else if (!is_int_type(base_type))
934 base_type = base_type;
935 else if (type_is_ok(base_type, &upper, &lower))
936 base_type = base_type;
937 else if (type_is_ok(&int_ctype, &upper, &lower))
938 base_type = &int_ctype;
939 else if (type_is_ok(&uint_ctype, &upper, &lower))
940 base_type = &uint_ctype;
941 else if (type_is_ok(&long_ctype, &upper, &lower))
942 base_type = &long_ctype;
943 else if (type_is_ok(&ulong_ctype, &upper, &lower))
944 base_type = &ulong_ctype;
945 else if (type_is_ok(&llong_ctype, &upper, &lower))
946 base_type = &llong_ctype;
947 else if (type_is_ok(&ullong_ctype, &upper, &lower))
948 base_type = &ullong_ctype;
949 else
950 base_type = &bad_ctype;
951 parent->ctype.base_type = base_type;
952 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
953 parent->examined = 0;
955 cast_enum_list(parent->symbol_list, base_type);
957 return token;
960 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
962 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
963 struct ctype *ctype = &ctx->ctype.base_type->ctype;
965 if (!ctype->base_type)
966 ctype->base_type = &incomplete_ctype;
968 return ret;
971 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
973 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
975 struct symbol *sym;
977 if (!match_op(token, '(')) {
978 sparse_error(token->pos, "expected '(' after typeof");
979 return token;
981 if (lookup_type(token->next)) {
982 token = typename(token->next, &sym, NULL);
983 ctx->ctype.base_type = sym->ctype.base_type;
984 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
985 } else {
986 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
987 token = parse_expression(token->next, &typeof_sym->initializer);
989 typeof_sym->endpos = token->pos;
990 if (!typeof_sym->initializer) {
991 sparse_error(token->pos, "expected expression after the '(' token");
992 typeof_sym = &bad_ctype;
994 ctx->ctype.base_type = typeof_sym;
996 return expect(token, ')', "after typeof");
999 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1001 struct expression *expr = NULL;
1002 if (match_op(token, '('))
1003 token = parens_expression(token, &expr, "in attribute");
1004 return token;
1007 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1009 if (!ctx->ctype.alignment)
1010 ctx->ctype.alignment = 1;
1011 return token;
1014 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1016 int alignment = max_alignment;
1017 struct expression *expr = NULL;
1019 if (match_op(token, '(')) {
1020 token = parens_expression(token, &expr, "in attribute");
1021 if (expr)
1022 alignment = const_expression_value(expr);
1024 if (alignment & (alignment-1)) {
1025 warning(token->pos, "I don't like non-power-of-2 alignments");
1026 return token;
1027 } else if (alignment > ctx->ctype.alignment)
1028 ctx->ctype.alignment = alignment;
1029 return token;
1032 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1034 if (ctx->modifiers & qual)
1035 warning(*pos, "duplicate %s", modifier_string(qual));
1036 ctx->modifiers |= qual;
1039 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1041 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1042 return token;
1045 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1047 struct expression *expr = NULL;
1048 int as;
1049 token = expect(token, '(', "after address_space attribute");
1050 token = conditional_expression(token, &expr);
1051 if (expr) {
1052 as = const_expression_value(expr);
1053 if (Waddress_space && as)
1054 ctx->ctype.as = as;
1056 token = expect(token, ')', "after address_space attribute");
1057 return token;
1060 static struct symbol *to_QI_mode(struct symbol *ctype)
1062 if (ctype->ctype.base_type != &int_type)
1063 return NULL;
1064 if (ctype == &char_ctype)
1065 return ctype;
1066 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1067 : &schar_ctype;
1070 static struct symbol *to_HI_mode(struct symbol *ctype)
1072 if (ctype->ctype.base_type != &int_type)
1073 return NULL;
1074 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1075 : &sshort_ctype;
1078 static struct symbol *to_SI_mode(struct symbol *ctype)
1080 if (ctype->ctype.base_type != &int_type)
1081 return NULL;
1082 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1083 : &sint_ctype;
1086 static struct symbol *to_DI_mode(struct symbol *ctype)
1088 if (ctype->ctype.base_type != &int_type)
1089 return NULL;
1090 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1091 : &sllong_ctype;
1094 static struct symbol *to_TI_mode(struct symbol *ctype)
1096 if (ctype->ctype.base_type != &int_type)
1097 return NULL;
1098 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1099 : &slllong_ctype;
1102 static struct symbol *to_word_mode(struct symbol *ctype)
1104 if (ctype->ctype.base_type != &int_type)
1105 return NULL;
1106 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1107 : &slong_ctype;
1110 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1112 token = expect(token, '(', "after mode attribute");
1113 if (token_type(token) == TOKEN_IDENT) {
1114 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1115 if (mode && mode->op->type == KW_MODE)
1116 ctx->mode = mode->op;
1117 else
1118 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1119 token = token->next;
1120 } else
1121 sparse_error(token->pos, "expect attribute mode symbol\n");
1122 token = expect(token, ')', "after mode attribute");
1123 return token;
1126 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1128 struct context *context = alloc_context();
1129 struct expression *args[3];
1130 int argc = 0;
1132 token = expect(token, '(', "after context attribute");
1133 while (!match_op(token, ')')) {
1134 struct expression *expr = NULL;
1135 token = conditional_expression(token, &expr);
1136 if (!expr)
1137 break;
1138 if (argc < 3)
1139 args[argc++] = expr;
1140 if (!match_op(token, ','))
1141 break;
1142 token = token->next;
1145 switch(argc) {
1146 case 0:
1147 sparse_error(token->pos, "expected context input/output values");
1148 break;
1149 case 1:
1150 context->in = get_expression_value(args[0]);
1151 break;
1152 case 2:
1153 context->in = get_expression_value(args[0]);
1154 context->out = get_expression_value(args[1]);
1155 break;
1156 case 3:
1157 context->context = args[0];
1158 context->in = get_expression_value(args[1]);
1159 context->out = get_expression_value(args[2]);
1160 break;
1163 if (argc)
1164 add_ptr_list(&ctx->ctype.contexts, context);
1166 token = expect(token, ')', "after context attribute");
1167 return token;
1170 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1172 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1173 ctx->ctype.base_type->designated_init = 1;
1174 else
1175 warning(token->pos, "attribute designated_init applied to non-structure type");
1176 return token;
1179 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1181 if (Wtransparent_union)
1182 warning(token->pos, "ignoring attribute __transparent_union__");
1183 return token;
1186 static struct token *recover_unknown_attribute(struct token *token)
1188 struct expression *expr = NULL;
1190 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1191 token = token->next;
1192 if (match_op(token, '('))
1193 token = parens_expression(token, &expr, "in attribute");
1194 return token;
1197 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1199 token = expect(token, '(', "after attribute");
1200 token = expect(token, '(', "after attribute");
1202 for (;;) {
1203 struct ident *attribute_name;
1204 struct symbol *attr;
1206 if (eof_token(token))
1207 break;
1208 if (match_op(token, ';'))
1209 break;
1210 if (token_type(token) != TOKEN_IDENT)
1211 break;
1212 attribute_name = token->ident;
1213 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1214 if (attr && attr->op->attribute)
1215 token = attr->op->attribute(token->next, attr, ctx);
1216 else
1217 token = recover_unknown_attribute(token);
1219 if (!match_op(token, ','))
1220 break;
1221 token = token->next;
1224 token = expect(token, ')', "after attribute");
1225 token = expect(token, ')', "after attribute");
1226 return token;
1229 static const char *storage_class[] =
1231 [STypedef] = "typedef",
1232 [SAuto] = "auto",
1233 [SExtern] = "extern",
1234 [SStatic] = "static",
1235 [SRegister] = "register",
1236 [SForced] = "[force]"
1239 static unsigned long storage_modifiers(struct decl_state *ctx)
1241 static unsigned long mod[] =
1243 [SAuto] = MOD_AUTO,
1244 [SExtern] = MOD_EXTERN,
1245 [SStatic] = MOD_STATIC,
1246 [SRegister] = MOD_REGISTER
1248 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1249 | (ctx->is_tls ? MOD_TLS : 0);
1252 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1254 /* __thread can be used alone, or with extern or static */
1255 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1256 sparse_error(*pos, "__thread can only be used alone, or with "
1257 "extern or static");
1258 return;
1261 if (!ctx->storage_class) {
1262 ctx->storage_class = class;
1263 return;
1265 if (ctx->storage_class == class)
1266 sparse_error(*pos, "duplicate %s", storage_class[class]);
1267 else
1268 sparse_error(*pos, "multiple storage classes");
1271 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1273 set_storage_class(&next->pos, ctx, STypedef);
1274 return next;
1277 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1279 set_storage_class(&next->pos, ctx, SAuto);
1280 return next;
1283 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1285 set_storage_class(&next->pos, ctx, SRegister);
1286 return next;
1289 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1291 set_storage_class(&next->pos, ctx, SStatic);
1292 return next;
1295 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1297 set_storage_class(&next->pos, ctx, SExtern);
1298 return next;
1301 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1303 /* This GCC extension can be used alone, or with extern or static */
1304 if (!ctx->storage_class || ctx->storage_class == SStatic
1305 || ctx->storage_class == SExtern) {
1306 ctx->is_tls = 1;
1307 } else {
1308 sparse_error(next->pos, "__thread can only be used alone, or "
1309 "with extern or static");
1312 return next;
1315 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1317 set_storage_class(&token->pos, ctx, SForced);
1318 return token;
1321 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1323 ctx->is_inline = 1;
1324 return next;
1327 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1329 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1330 return next;
1333 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1335 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1336 return next;
1339 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1341 unsigned long mod = thistype->modifiers;
1343 if (mod)
1344 apply_qualifier(&pos, ctype, mod);
1346 /* Context */
1347 concat_ptr_list((struct ptr_list *)thistype->contexts,
1348 (struct ptr_list **)&ctype->contexts);
1350 /* Alignment */
1351 if (thistype->alignment > ctype->alignment)
1352 ctype->alignment = thistype->alignment;
1354 /* Address space */
1355 if (thistype->as)
1356 ctype->as = thistype->as;
1359 static void specifier_conflict(struct position pos, int what, struct ident *new)
1361 const char *old;
1362 if (what & (Set_S | Set_T))
1363 goto Catch_all;
1364 if (what & Set_Char)
1365 old = "char";
1366 else if (what & Set_Double)
1367 old = "double";
1368 else if (what & Set_Float)
1369 old = "float";
1370 else if (what & Set_Signed)
1371 old = "signed";
1372 else if (what & Set_Unsigned)
1373 old = "unsigned";
1374 else if (what & Set_Short)
1375 old = "short";
1376 else if (what & Set_Long)
1377 old = "long";
1378 else
1379 old = "long long";
1380 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1381 old, show_ident(new));
1382 return;
1384 Catch_all:
1385 sparse_error(pos, "two or more data types in declaration specifiers");
1388 static struct symbol * const int_types[] =
1389 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1390 static struct symbol * const signed_types[] =
1391 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1392 &slllong_ctype};
1393 static struct symbol * const unsigned_types[] =
1394 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1395 &ulllong_ctype};
1396 static struct symbol * const real_types[] =
1397 {&float_ctype, &double_ctype, &ldouble_ctype};
1398 static struct symbol * const char_types[] =
1399 {&char_ctype, &schar_ctype, &uchar_ctype};
1400 static struct symbol * const * const types[] = {
1401 int_types + 1, signed_types + 1, unsigned_types + 1,
1402 real_types + 1, char_types, char_types + 1, char_types + 2
1405 struct symbol *ctype_integer(int size, int want_unsigned)
1407 return types[want_unsigned ? CUInt : CInt][size];
1410 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1412 while (token_type(t) == TOKEN_IDENT) {
1413 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1414 if (!s)
1415 break;
1416 if (s->type != SYM_KEYWORD)
1417 break;
1418 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1419 break;
1420 t = t->next;
1421 if (s->op->declarator)
1422 t = s->op->declarator(t, ctx);
1424 return t;
1427 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1429 int seen = 0;
1430 int class = CInt;
1431 int size = 0;
1433 while (token_type(token) == TOKEN_IDENT) {
1434 struct symbol *s = lookup_symbol(token->ident,
1435 NS_TYPEDEF | NS_SYMBOL);
1436 if (!s || !(s->namespace & NS_TYPEDEF))
1437 break;
1438 if (s->type != SYM_KEYWORD) {
1439 if (seen & Set_Any)
1440 break;
1441 seen |= Set_S | Set_T;
1442 ctx->ctype.base_type = s->ctype.base_type;
1443 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1444 token = token->next;
1445 continue;
1447 if (s->op->type & KW_SPECIFIER) {
1448 if (seen & s->op->test) {
1449 specifier_conflict(token->pos,
1450 seen & s->op->test,
1451 token->ident);
1452 break;
1454 seen |= s->op->set;
1455 class += s->op->class;
1456 if (s->op->type & KW_SHORT) {
1457 size = -1;
1458 } else if (s->op->type & KW_LONG && size++) {
1459 if (class == CReal) {
1460 specifier_conflict(token->pos,
1461 Set_Vlong,
1462 &double_ident);
1463 break;
1465 seen |= Set_Vlong;
1468 token = token->next;
1469 if (s->op->declarator)
1470 token = s->op->declarator(token, ctx);
1471 if (s->op->type & KW_EXACT) {
1472 ctx->ctype.base_type = s->ctype.base_type;
1473 ctx->ctype.modifiers |= s->ctype.modifiers;
1477 if (!(seen & Set_S)) { /* not set explicitly? */
1478 struct symbol *base = &incomplete_ctype;
1479 if (seen & Set_Any)
1480 base = types[class][size];
1481 ctx->ctype.base_type = base;
1484 if (ctx->ctype.modifiers & MOD_BITWISE) {
1485 struct symbol *type;
1486 ctx->ctype.modifiers &= ~MOD_BITWISE;
1487 if (!is_int_type(ctx->ctype.base_type)) {
1488 sparse_error(token->pos, "invalid modifier");
1489 return token;
1491 type = alloc_symbol(token->pos, SYM_BASETYPE);
1492 *type = *ctx->ctype.base_type;
1493 type->ctype.modifiers &= ~MOD_SPECIFIER;
1494 type->ctype.base_type = ctx->ctype.base_type;
1495 type->type = SYM_RESTRICT;
1496 ctx->ctype.base_type = type;
1497 create_fouled(type);
1499 return token;
1502 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1504 struct expression *expr = NULL;
1506 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1507 token = token->next;
1508 token = parse_expression(token, &expr);
1509 sym->array_size = expr;
1510 return token;
1513 static struct token *parameter_type_list(struct token *, struct symbol *);
1514 static struct token *identifier_list(struct token *, struct symbol *);
1515 static struct token *declarator(struct token *token, struct decl_state *ctx);
1517 static struct token *skip_attribute(struct token *token)
1519 token = token->next;
1520 if (match_op(token, '(')) {
1521 int depth = 1;
1522 token = token->next;
1523 while (depth && !eof_token(token)) {
1524 if (token_type(token) == TOKEN_SPECIAL) {
1525 if (token->special == '(')
1526 depth++;
1527 else if (token->special == ')')
1528 depth--;
1530 token = token->next;
1533 return token;
1536 static struct token *skip_attributes(struct token *token)
1538 struct symbol *keyword;
1539 for (;;) {
1540 if (token_type(token) != TOKEN_IDENT)
1541 break;
1542 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1543 if (!keyword || keyword->type != SYM_KEYWORD)
1544 break;
1545 if (!(keyword->op->type & KW_ATTRIBUTE))
1546 break;
1547 token = expect(token->next, '(', "after attribute");
1548 token = expect(token, '(', "after attribute");
1549 for (;;) {
1550 if (eof_token(token))
1551 break;
1552 if (match_op(token, ';'))
1553 break;
1554 if (token_type(token) != TOKEN_IDENT)
1555 break;
1556 token = skip_attribute(token);
1557 if (!match_op(token, ','))
1558 break;
1559 token = token->next;
1561 token = expect(token, ')', "after attribute");
1562 token = expect(token, ')', "after attribute");
1564 return token;
1567 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1569 struct symbol *keyword;
1570 for (;;) {
1571 if (token_type(token) != TOKEN_IDENT)
1572 break;
1573 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1574 if (!keyword || keyword->type != SYM_KEYWORD)
1575 break;
1576 if (!(keyword->op->type & keywords))
1577 break;
1578 token = keyword->op->declarator(token->next, ctx);
1579 keywords &= KW_ATTRIBUTE;
1581 return token;
1584 static int is_nested(struct token *token, struct token **p,
1585 int prefer_abstract)
1588 * This can be either a parameter list or a grouping.
1589 * For the direct (non-abstract) case, we know if must be
1590 * a parameter list if we already saw the identifier.
1591 * For the abstract case, we know if must be a parameter
1592 * list if it is empty or starts with a type.
1594 struct token *next = token->next;
1596 *p = next = skip_attributes(next);
1598 if (token_type(next) == TOKEN_IDENT) {
1599 if (lookup_type(next))
1600 return !prefer_abstract;
1601 return 1;
1604 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1605 return 0;
1607 return 1;
1610 enum kind {
1611 Empty, K_R, Proto, Bad_Func,
1614 static enum kind which_func(struct token *token,
1615 struct ident **n,
1616 int prefer_abstract)
1618 struct token *next = token->next;
1620 if (token_type(next) == TOKEN_IDENT) {
1621 if (lookup_type(next))
1622 return Proto;
1623 /* identifier list not in definition; complain */
1624 if (prefer_abstract)
1625 warning(token->pos,
1626 "identifier list not in definition");
1627 return K_R;
1630 if (token_type(next) != TOKEN_SPECIAL)
1631 return Bad_Func;
1633 if (next->special == ')') {
1634 /* don't complain about those */
1635 if (!n || match_op(next->next, ';'))
1636 return Empty;
1637 warning(next->pos,
1638 "non-ANSI function declaration of function '%s'",
1639 show_ident(*n));
1640 return Empty;
1643 if (next->special == SPECIAL_ELLIPSIS) {
1644 warning(next->pos,
1645 "variadic functions must have one named argument");
1646 return Proto;
1649 return Bad_Func;
1652 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1654 struct ctype *ctype = &ctx->ctype;
1655 struct token *next;
1656 struct ident **p = ctx->ident;
1658 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1659 *ctx->ident = token->ident;
1660 token = token->next;
1661 } else if (match_op(token, '(') &&
1662 is_nested(token, &next, ctx->prefer_abstract)) {
1663 struct symbol *base_type = ctype->base_type;
1664 if (token->next != next)
1665 next = handle_attributes(token->next, ctx,
1666 KW_ATTRIBUTE);
1667 token = declarator(next, ctx);
1668 token = expect(token, ')', "in nested declarator");
1669 while (ctype->base_type != base_type)
1670 ctype = &ctype->base_type->ctype;
1671 p = NULL;
1674 if (match_op(token, '(')) {
1675 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1676 struct symbol *fn;
1677 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1678 token = token->next;
1679 if (kind == K_R)
1680 token = identifier_list(token, fn);
1681 else if (kind == Proto)
1682 token = parameter_type_list(token, fn);
1683 token = expect(token, ')', "in function declarator");
1684 fn->endpos = token->pos;
1685 return token;
1688 while (match_op(token, '[')) {
1689 struct symbol *array;
1690 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1691 token = abstract_array_declarator(token->next, array);
1692 token = expect(token, ']', "in abstract_array_declarator");
1693 array->endpos = token->pos;
1694 ctype = &array->ctype;
1696 return token;
1699 static struct token *pointer(struct token *token, struct decl_state *ctx)
1701 while (match_op(token,'*')) {
1702 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1703 ptr->ctype.modifiers = ctx->ctype.modifiers;
1704 ptr->ctype.base_type = ctx->ctype.base_type;
1705 ptr->ctype.as = ctx->ctype.as;
1706 ptr->ctype.contexts = ctx->ctype.contexts;
1707 ctx->ctype.modifiers = 0;
1708 ctx->ctype.base_type = ptr;
1709 ctx->ctype.as = 0;
1710 ctx->ctype.contexts = NULL;
1711 ctx->ctype.alignment = 0;
1713 token = handle_qualifiers(token->next, ctx);
1714 ctx->ctype.base_type->endpos = token->pos;
1716 return token;
1719 static struct token *declarator(struct token *token, struct decl_state *ctx)
1721 token = pointer(token, ctx);
1722 return direct_declarator(token, ctx);
1725 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1727 struct ctype *ctype = &ctx->ctype;
1728 struct expression *expr;
1729 struct symbol *bitfield;
1730 long long width;
1732 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1733 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1734 show_typename(ctype->base_type));
1735 // Parse this to recover gracefully.
1736 return conditional_expression(token->next, &expr);
1739 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1740 token = conditional_expression(token->next, &expr);
1741 width = const_expression_value(expr);
1742 bitfield->bit_size = width;
1744 if (width < 0 || width > INT_MAX) {
1745 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1746 width = -1;
1747 } else if (*ctx->ident && width == 0) {
1748 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1749 show_ident(*ctx->ident));
1750 width = -1;
1751 } else if (*ctx->ident) {
1752 struct symbol *base_type = bitfield->ctype.base_type;
1753 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1754 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1755 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1756 // Valid values are either {-1;0} or {0}, depending on integer
1757 // representation. The latter makes for very efficient code...
1758 sparse_error(token->pos, "dubious one-bit signed bitfield");
1760 if (Wdefault_bitfield_sign &&
1761 bitfield_type->type != SYM_ENUM &&
1762 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1763 is_signed) {
1764 // The sign of bitfields is unspecified by default.
1765 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1768 bitfield->bit_size = width;
1769 bitfield->endpos = token->pos;
1770 return token;
1773 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1775 struct decl_state ctx = {.prefer_abstract = 0};
1776 struct ctype saved;
1777 unsigned long mod;
1779 token = declaration_specifiers(token, &ctx);
1780 mod = storage_modifiers(&ctx);
1781 saved = ctx.ctype;
1782 for (;;) {
1783 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1784 ctx.ident = &decl->ident;
1786 token = declarator(token, &ctx);
1787 if (match_op(token, ':'))
1788 token = handle_bitfield(token, &ctx);
1790 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1791 apply_modifiers(token->pos, &ctx);
1793 decl->ctype = ctx.ctype;
1794 decl->ctype.modifiers |= mod;
1795 decl->endpos = token->pos;
1796 add_symbol(list, decl);
1797 if (!match_op(token, ','))
1798 break;
1799 token = token->next;
1800 ctx.ctype = saved;
1802 return token;
1805 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1807 while (!match_op(token, '}')) {
1808 if (!match_op(token, ';'))
1809 token = declaration_list(token, list);
1810 if (!match_op(token, ';')) {
1811 sparse_error(token->pos, "expected ; at end of declaration");
1812 break;
1814 token = token->next;
1816 return token;
1819 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1821 struct decl_state ctx = {.prefer_abstract = 1};
1823 token = declaration_specifiers(token, &ctx);
1824 ctx.ident = &sym->ident;
1825 token = declarator(token, &ctx);
1826 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1827 apply_modifiers(token->pos, &ctx);
1828 sym->ctype = ctx.ctype;
1829 sym->ctype.modifiers |= storage_modifiers(&ctx);
1830 sym->endpos = token->pos;
1831 return token;
1834 struct token *typename(struct token *token, struct symbol **p, int *forced)
1836 struct decl_state ctx = {.prefer_abstract = 1};
1837 int class;
1838 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1839 *p = sym;
1840 token = declaration_specifiers(token, &ctx);
1841 token = declarator(token, &ctx);
1842 apply_modifiers(token->pos, &ctx);
1843 sym->ctype = ctx.ctype;
1844 sym->endpos = token->pos;
1845 class = ctx.storage_class;
1846 if (forced) {
1847 *forced = 0;
1848 if (class == SForced) {
1849 *forced = 1;
1850 class = 0;
1853 if (class)
1854 warning(sym->pos, "storage class in typename (%s %s)",
1855 storage_class[class], show_typename(sym));
1856 return token;
1859 static struct token *expression_statement(struct token *token, struct expression **tree)
1861 token = parse_expression(token, tree);
1862 return expect(token, ';', "at end of statement");
1865 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1866 struct expression_list **inout)
1868 struct expression *expr;
1870 /* Allow empty operands */
1871 if (match_op(token->next, ':') || match_op(token->next, ')'))
1872 return token->next;
1873 do {
1874 struct ident *ident = NULL;
1875 if (match_op(token->next, '[') &&
1876 token_type(token->next->next) == TOKEN_IDENT &&
1877 match_op(token->next->next->next, ']')) {
1878 ident = token->next->next->ident;
1879 token = token->next->next->next;
1881 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1882 token = primary_expression(token->next, &expr);
1883 add_expression(inout, expr);
1884 token = parens_expression(token, &expr, "in asm parameter");
1885 add_expression(inout, expr);
1886 } while (match_op(token, ','));
1887 return token;
1890 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1891 struct expression_list **clobbers)
1893 struct expression *expr;
1895 do {
1896 token = primary_expression(token->next, &expr);
1897 add_expression(clobbers, expr);
1898 } while (match_op(token, ','));
1899 return token;
1902 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1903 struct symbol_list **labels)
1905 struct symbol *label;
1907 do {
1908 token = token->next; /* skip ':' and ',' */
1909 if (token_type(token) != TOKEN_IDENT)
1910 return token;
1911 label = label_symbol(token);
1912 add_symbol(labels, label);
1913 token = token->next;
1914 } while (match_op(token, ','));
1915 return token;
1918 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1920 int is_goto = 0;
1922 token = token->next;
1923 stmt->type = STMT_ASM;
1924 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1925 token = token->next;
1927 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1928 is_goto = 1;
1929 token = token->next;
1931 token = expect(token, '(', "after asm");
1932 token = parse_expression(token, &stmt->asm_string);
1933 if (match_op(token, ':'))
1934 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1935 if (match_op(token, ':'))
1936 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1937 if (match_op(token, ':'))
1938 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1939 if (is_goto && match_op(token, ':'))
1940 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1941 token = expect(token, ')', "after asm");
1942 return expect(token, ';', "at end of asm-statement");
1945 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1947 struct expression *expr;
1948 token = expect(token, '(', "after asm");
1949 token = parse_expression(token->next, &expr);
1950 token = expect(token, ')', "after asm");
1951 return token;
1954 /* Make a statement out of an expression */
1955 static struct statement *make_statement(struct expression *expr)
1957 struct statement *stmt;
1959 if (!expr)
1960 return NULL;
1961 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1962 stmt->expression = expr;
1963 return stmt;
1967 * All iterators have two symbols associated with them:
1968 * the "continue" and "break" symbols, which are targets
1969 * for continue and break statements respectively.
1971 * They are in a special name-space, but they follow
1972 * all the normal visibility rules, so nested iterators
1973 * automatically work right.
1975 static void start_iterator(struct statement *stmt)
1977 struct symbol *cont, *brk;
1979 start_symbol_scope(stmt->pos);
1980 cont = alloc_symbol(stmt->pos, SYM_NODE);
1981 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1982 brk = alloc_symbol(stmt->pos, SYM_NODE);
1983 bind_symbol(brk, &break_ident, NS_ITERATOR);
1985 stmt->type = STMT_ITERATOR;
1986 stmt->iterator_break = brk;
1987 stmt->iterator_continue = cont;
1988 fn_local_symbol(brk);
1989 fn_local_symbol(cont);
1992 static void end_iterator(struct statement *stmt)
1994 end_symbol_scope();
1997 static struct statement *start_function(struct symbol *sym)
1999 struct symbol *ret;
2000 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2002 start_function_scope(stmt->pos);
2003 ret = alloc_symbol(sym->pos, SYM_NODE);
2004 ret->ctype = sym->ctype.base_type->ctype;
2005 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2006 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2007 bind_symbol(ret, &return_ident, NS_ITERATOR);
2008 stmt->ret = ret;
2009 fn_local_symbol(ret);
2011 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2012 current_fn = sym;
2014 return stmt;
2017 static void end_function(struct symbol *sym)
2019 current_fn = NULL;
2020 end_function_scope();
2024 * A "switch()" statement, like an iterator, has a
2025 * the "break" symbol associated with it. It works
2026 * exactly like the iterator break - it's the target
2027 * for any break-statements in scope, and means that
2028 * "break" handling doesn't even need to know whether
2029 * it's breaking out of an iterator or a switch.
2031 * In addition, the "case" symbol is a marker for the
2032 * case/default statements to find the switch statement
2033 * that they are associated with.
2035 static void start_switch(struct statement *stmt)
2037 struct symbol *brk, *switch_case;
2039 start_symbol_scope(stmt->pos);
2040 brk = alloc_symbol(stmt->pos, SYM_NODE);
2041 bind_symbol(brk, &break_ident, NS_ITERATOR);
2043 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2044 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2045 switch_case->stmt = stmt;
2047 stmt->type = STMT_SWITCH;
2048 stmt->switch_break = brk;
2049 stmt->switch_case = switch_case;
2051 fn_local_symbol(brk);
2052 fn_local_symbol(switch_case);
2055 static void end_switch(struct statement *stmt)
2057 if (!stmt->switch_case->symbol_list)
2058 warning(stmt->pos, "switch with no cases");
2059 end_symbol_scope();
2062 static void add_case_statement(struct statement *stmt)
2064 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2065 struct symbol *sym;
2067 if (!target) {
2068 sparse_error(stmt->pos, "not in switch scope");
2069 stmt->type = STMT_NONE;
2070 return;
2072 sym = alloc_symbol(stmt->pos, SYM_NODE);
2073 add_symbol(&target->symbol_list, sym);
2074 sym->stmt = stmt;
2075 stmt->case_label = sym;
2076 fn_local_symbol(sym);
2079 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2081 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2083 if (!target)
2084 error_die(token->pos, "internal error: return without a function target");
2085 stmt->type = STMT_RETURN;
2086 stmt->ret_target = target;
2087 return expression_statement(token->next, &stmt->ret_value);
2090 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2092 struct symbol_list *syms;
2093 struct expression *e1, *e2, *e3;
2094 struct statement *iterator;
2096 start_iterator(stmt);
2097 token = expect(token->next, '(', "after 'for'");
2099 syms = NULL;
2100 e1 = NULL;
2101 /* C99 variable declaration? */
2102 if (lookup_type(token)) {
2103 token = external_declaration(token, &syms);
2104 } else {
2105 token = parse_expression(token, &e1);
2106 token = expect(token, ';', "in 'for'");
2108 token = parse_expression(token, &e2);
2109 token = expect(token, ';', "in 'for'");
2110 token = parse_expression(token, &e3);
2111 token = expect(token, ')', "in 'for'");
2112 token = statement(token, &iterator);
2114 stmt->iterator_syms = syms;
2115 stmt->iterator_pre_statement = make_statement(e1);
2116 stmt->iterator_pre_condition = e2;
2117 stmt->iterator_post_statement = make_statement(e3);
2118 stmt->iterator_post_condition = NULL;
2119 stmt->iterator_statement = iterator;
2120 end_iterator(stmt);
2122 return token;
2125 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2127 struct expression *expr;
2128 struct statement *iterator;
2130 start_iterator(stmt);
2131 token = parens_expression(token->next, &expr, "after 'while'");
2132 token = statement(token, &iterator);
2134 stmt->iterator_pre_condition = expr;
2135 stmt->iterator_post_condition = NULL;
2136 stmt->iterator_statement = iterator;
2137 end_iterator(stmt);
2139 return token;
2142 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2144 struct expression *expr;
2145 struct statement *iterator;
2147 start_iterator(stmt);
2148 token = statement(token->next, &iterator);
2149 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2150 token = token->next;
2151 else
2152 sparse_error(token->pos, "expected 'while' after 'do'");
2153 token = parens_expression(token, &expr, "after 'do-while'");
2155 stmt->iterator_post_condition = expr;
2156 stmt->iterator_statement = iterator;
2157 end_iterator(stmt);
2159 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2160 warning(iterator->pos, "do-while statement is not a compound statement");
2162 return expect(token, ';', "after statement");
2165 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2167 stmt->type = STMT_IF;
2168 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2169 token = statement(token, &stmt->if_true);
2170 if (token_type(token) != TOKEN_IDENT)
2171 return token;
2172 if (token->ident != &else_ident)
2173 return token;
2174 return statement(token->next, &stmt->if_false);
2177 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2179 stmt->type = STMT_CASE;
2180 token = expect(token, ':', "after default/case");
2181 add_case_statement(stmt);
2182 return statement(token, &stmt->case_statement);
2185 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2187 token = parse_expression(token->next, &stmt->case_expression);
2188 if (match_op(token, SPECIAL_ELLIPSIS))
2189 token = parse_expression(token->next, &stmt->case_to);
2190 return case_statement(token, stmt);
2193 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2195 return case_statement(token->next, stmt);
2198 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2200 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2201 stmt->type = STMT_GOTO;
2202 stmt->goto_label = target;
2203 if (!target)
2204 sparse_error(stmt->pos, "break/continue not in iterator scope");
2205 return expect(token->next, ';', "at end of statement");
2208 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2210 stmt->type = STMT_SWITCH;
2211 start_switch(stmt);
2212 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2213 token = statement(token, &stmt->switch_statement);
2214 end_switch(stmt);
2215 return token;
2218 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2220 stmt->type = STMT_GOTO;
2221 token = token->next;
2222 if (match_op(token, '*')) {
2223 token = parse_expression(token->next, &stmt->goto_expression);
2224 add_statement(&function_computed_goto_list, stmt);
2225 } else if (token_type(token) == TOKEN_IDENT) {
2226 stmt->goto_label = label_symbol(token);
2227 token = token->next;
2228 } else {
2229 sparse_error(token->pos, "Expected identifier or goto expression");
2231 return expect(token, ';', "at end of statement");
2234 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2236 stmt->type = STMT_CONTEXT;
2237 token = parse_expression(token->next, &stmt->expression);
2238 if(stmt->expression->type == EXPR_PREOP
2239 && stmt->expression->op == '('
2240 && stmt->expression->unop->type == EXPR_COMMA) {
2241 struct expression *expr;
2242 expr = stmt->expression->unop;
2243 stmt->context = expr->left;
2244 stmt->expression = expr->right;
2246 return expect(token, ';', "at end of statement");
2249 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2251 stmt->type = STMT_RANGE;
2252 token = assignment_expression(token->next, &stmt->range_expression);
2253 token = expect(token, ',', "after range expression");
2254 token = assignment_expression(token, &stmt->range_low);
2255 token = expect(token, ',', "after low range");
2256 token = assignment_expression(token, &stmt->range_high);
2257 return expect(token, ';', "after range statement");
2260 static struct token *statement(struct token *token, struct statement **tree)
2262 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2264 *tree = stmt;
2265 if (token_type(token) == TOKEN_IDENT) {
2266 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2267 if (s && s->op->statement)
2268 return s->op->statement(token, stmt);
2270 if (match_op(token->next, ':')) {
2271 stmt->type = STMT_LABEL;
2272 stmt->label_identifier = label_symbol(token);
2273 token = skip_attributes(token->next->next);
2274 return statement(token, &stmt->label_statement);
2278 if (match_op(token, '{')) {
2279 stmt->type = STMT_COMPOUND;
2280 start_symbol_scope(stmt->pos);
2281 token = compound_statement(token->next, stmt);
2282 end_symbol_scope();
2284 return expect(token, '}', "at end of compound statement");
2287 stmt->type = STMT_EXPRESSION;
2288 return expression_statement(token, &stmt->expression);
2291 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2292 static struct token *label_statement(struct token *token)
2294 while (token_type(token) == TOKEN_IDENT) {
2295 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2296 /* it's block-scope, but we want label namespace */
2297 bind_symbol(sym, token->ident, NS_SYMBOL);
2298 sym->namespace = NS_LABEL;
2299 fn_local_symbol(sym);
2300 token = token->next;
2301 if (!match_op(token, ','))
2302 break;
2303 token = token->next;
2305 return expect(token, ';', "at end of label declaration");
2308 static struct token * statement_list(struct token *token, struct statement_list **list)
2310 int seen_statement = 0;
2311 while (token_type(token) == TOKEN_IDENT &&
2312 token->ident == &__label___ident)
2313 token = label_statement(token->next);
2314 for (;;) {
2315 struct statement * stmt;
2316 if (eof_token(token))
2317 break;
2318 if (match_op(token, '}'))
2319 break;
2320 if (lookup_type(token)) {
2321 if (seen_statement) {
2322 warning(token->pos, "mixing declarations and code");
2323 seen_statement = 0;
2325 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2326 token = external_declaration(token, &stmt->declaration);
2327 } else {
2328 seen_statement = Wdeclarationafterstatement;
2329 token = statement(token, &stmt);
2331 add_statement(list, stmt);
2333 return token;
2336 static struct token *identifier_list(struct token *token, struct symbol *fn)
2338 struct symbol_list **list = &fn->arguments;
2339 for (;;) {
2340 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2341 sym->ident = token->ident;
2342 token = token->next;
2343 sym->endpos = token->pos;
2344 sym->ctype.base_type = &incomplete_ctype;
2345 add_symbol(list, sym);
2346 if (!match_op(token, ',') ||
2347 token_type(token->next) != TOKEN_IDENT ||
2348 lookup_type(token->next))
2349 break;
2350 token = token->next;
2352 return token;
2355 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2357 struct symbol_list **list = &fn->arguments;
2359 for (;;) {
2360 struct symbol *sym;
2362 if (match_op(token, SPECIAL_ELLIPSIS)) {
2363 fn->variadic = 1;
2364 token = token->next;
2365 break;
2368 sym = alloc_symbol(token->pos, SYM_NODE);
2369 token = parameter_declaration(token, sym);
2370 if (sym->ctype.base_type == &void_ctype) {
2371 /* Special case: (void) */
2372 if (!*list && !sym->ident)
2373 break;
2374 warning(token->pos, "void parameter");
2376 add_symbol(list, sym);
2377 if (!match_op(token, ','))
2378 break;
2379 token = token->next;
2381 return token;
2384 struct token *compound_statement(struct token *token, struct statement *stmt)
2386 token = statement_list(token, &stmt->stmts);
2387 return token;
2390 static struct expression *identifier_expression(struct token *token)
2392 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2393 expr->expr_ident = token->ident;
2394 return expr;
2397 static struct expression *index_expression(struct expression *from, struct expression *to)
2399 int idx_from, idx_to;
2400 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2402 idx_from = const_expression_value(from);
2403 idx_to = idx_from;
2404 if (to) {
2405 idx_to = const_expression_value(to);
2406 if (idx_to < idx_from || idx_from < 0)
2407 warning(from->pos, "nonsense array initializer index range");
2409 expr->idx_from = idx_from;
2410 expr->idx_to = idx_to;
2411 return expr;
2414 static struct token *single_initializer(struct expression **ep, struct token *token)
2416 int expect_equal = 0;
2417 struct token *next = token->next;
2418 struct expression **tail = ep;
2419 int nested;
2421 *ep = NULL;
2423 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2424 struct expression *expr = identifier_expression(token);
2425 if (Wold_initializer)
2426 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2427 token = initializer(&expr->ident_expression, next->next);
2428 if (expr->ident_expression)
2429 *ep = expr;
2430 return token;
2433 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2434 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2435 struct expression *expr = identifier_expression(next);
2436 *tail = expr;
2437 tail = &expr->ident_expression;
2438 expect_equal = 1;
2439 token = next->next;
2440 } else if (match_op(token, '[')) {
2441 struct expression *from = NULL, *to = NULL, *expr;
2442 token = constant_expression(token->next, &from);
2443 if (!from) {
2444 sparse_error(token->pos, "Expected constant expression");
2445 break;
2447 if (match_op(token, SPECIAL_ELLIPSIS))
2448 token = constant_expression(token->next, &to);
2449 expr = index_expression(from, to);
2450 *tail = expr;
2451 tail = &expr->idx_expression;
2452 token = expect(token, ']', "at end of initializer index");
2453 if (nested)
2454 expect_equal = 1;
2455 } else {
2456 break;
2459 if (nested && !expect_equal) {
2460 if (!match_op(token, '='))
2461 warning(token->pos, "obsolete array initializer, use C99 syntax");
2462 else
2463 expect_equal = 1;
2465 if (expect_equal)
2466 token = expect(token, '=', "at end of initializer index");
2468 token = initializer(tail, token);
2469 if (!*tail)
2470 *ep = NULL;
2471 return token;
2474 static struct token *initializer_list(struct expression_list **list, struct token *token)
2476 struct expression *expr;
2478 for (;;) {
2479 token = single_initializer(&expr, token);
2480 if (!expr)
2481 break;
2482 add_expression(list, expr);
2483 if (!match_op(token, ','))
2484 break;
2485 token = token->next;
2487 return token;
2490 struct token *initializer(struct expression **tree, struct token *token)
2492 if (match_op(token, '{')) {
2493 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2494 *tree = expr;
2495 token = initializer_list(&expr->expr_list, token->next);
2496 return expect(token, '}', "at end of initializer");
2498 return assignment_expression(token, tree);
2501 static void declare_argument(struct symbol *sym, struct symbol *fn)
2503 if (!sym->ident) {
2504 sparse_error(sym->pos, "no identifier for function argument");
2505 return;
2507 bind_symbol(sym, sym->ident, NS_SYMBOL);
2510 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2511 struct symbol_list **list)
2513 struct symbol_list **old_symbol_list;
2514 struct symbol *base_type = decl->ctype.base_type;
2515 struct statement *stmt, **p;
2516 struct symbol *prev;
2517 struct symbol *arg;
2519 old_symbol_list = function_symbol_list;
2520 if (decl->ctype.modifiers & MOD_INLINE) {
2521 function_symbol_list = &decl->inline_symbol_list;
2522 p = &base_type->inline_stmt;
2523 } else {
2524 function_symbol_list = &decl->symbol_list;
2525 p = &base_type->stmt;
2527 function_computed_target_list = NULL;
2528 function_computed_goto_list = NULL;
2530 if (decl->ctype.modifiers & MOD_EXTERN) {
2531 if (!(decl->ctype.modifiers & MOD_INLINE))
2532 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2534 if (!(decl->ctype.modifiers & MOD_STATIC))
2535 decl->ctype.modifiers |= MOD_EXTERN;
2537 stmt = start_function(decl);
2539 *p = stmt;
2540 FOR_EACH_PTR (base_type->arguments, arg) {
2541 declare_argument(arg, base_type);
2542 } END_FOR_EACH_PTR(arg);
2544 token = compound_statement(token->next, stmt);
2546 end_function(decl);
2547 if (!(decl->ctype.modifiers & MOD_INLINE))
2548 add_symbol(list, decl);
2549 check_declaration(decl);
2550 decl->definition = decl;
2551 prev = decl->same_symbol;
2552 if (prev && prev->definition) {
2553 warning(decl->pos, "multiple definitions for function '%s'",
2554 show_ident(decl->ident));
2555 info(prev->definition->pos, " the previous one is here");
2556 } else {
2557 while (prev) {
2558 prev->definition = decl;
2559 prev = prev->same_symbol;
2562 function_symbol_list = old_symbol_list;
2563 if (function_computed_goto_list) {
2564 if (!function_computed_target_list)
2565 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2566 else {
2567 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2568 stmt->target_list = function_computed_target_list;
2569 } END_FOR_EACH_PTR(stmt);
2572 return expect(token, '}', "at end of function");
2575 static void promote_k_r_types(struct symbol *arg)
2577 struct symbol *base = arg->ctype.base_type;
2578 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2579 arg->ctype.base_type = &int_ctype;
2583 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2585 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2586 struct symbol *arg;
2588 FOR_EACH_PTR(real_args, arg) {
2589 struct symbol *type;
2591 /* This is quadratic in the number of arguments. We _really_ don't care */
2592 FOR_EACH_PTR(argtypes, type) {
2593 if (type->ident == arg->ident)
2594 goto match;
2595 } END_FOR_EACH_PTR(type);
2596 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2597 continue;
2598 match:
2599 type->used = 1;
2600 /* "char" and "short" promote to "int" */
2601 promote_k_r_types(type);
2603 arg->ctype = type->ctype;
2604 } END_FOR_EACH_PTR(arg);
2606 FOR_EACH_PTR(argtypes, arg) {
2607 if (!arg->used)
2608 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2609 } END_FOR_EACH_PTR(arg);
2613 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2614 struct symbol_list **list)
2616 struct symbol_list *args = NULL;
2618 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2619 do {
2620 token = declaration_list(token, &args);
2621 if (!match_op(token, ';')) {
2622 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2623 break;
2625 token = token->next;
2626 } while (lookup_type(token));
2628 apply_k_r_types(args, decl);
2630 if (!match_op(token, '{')) {
2631 sparse_error(token->pos, "expected function body");
2632 return token;
2634 return parse_function_body(token, decl, list);
2637 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2639 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2640 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2641 struct statement *stmt;
2643 anon->ctype.base_type = fn;
2644 stmt = alloc_statement(token->pos, STMT_NONE);
2645 fn->stmt = stmt;
2647 token = parse_asm_statement(token, stmt);
2649 add_symbol(list, anon);
2650 return token;
2653 struct token *external_declaration(struct token *token, struct symbol_list **list)
2655 struct ident *ident = NULL;
2656 struct symbol *decl;
2657 struct decl_state ctx = { .ident = &ident };
2658 struct ctype saved;
2659 struct symbol *base_type;
2660 unsigned long mod;
2661 int is_typedef;
2663 /* Top-level inline asm? */
2664 if (token_type(token) == TOKEN_IDENT) {
2665 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2666 if (s && s->op->toplevel)
2667 return s->op->toplevel(token, list);
2670 /* Parse declaration-specifiers, if any */
2671 token = declaration_specifiers(token, &ctx);
2672 mod = storage_modifiers(&ctx);
2673 decl = alloc_symbol(token->pos, SYM_NODE);
2674 /* Just a type declaration? */
2675 if (match_op(token, ';')) {
2676 apply_modifiers(token->pos, &ctx);
2677 return token->next;
2680 saved = ctx.ctype;
2681 token = declarator(token, &ctx);
2682 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2683 apply_modifiers(token->pos, &ctx);
2685 decl->ctype = ctx.ctype;
2686 decl->ctype.modifiers |= mod;
2687 decl->endpos = token->pos;
2689 /* Just a type declaration? */
2690 if (!ident) {
2691 warning(token->pos, "missing identifier in declaration");
2692 return expect(token, ';', "at the end of type declaration");
2695 /* type define declaration? */
2696 is_typedef = ctx.storage_class == STypedef;
2698 /* Typedefs don't have meaningful storage */
2699 if (is_typedef)
2700 decl->ctype.modifiers |= MOD_USERTYPE;
2702 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2704 base_type = decl->ctype.base_type;
2706 if (is_typedef) {
2707 if (base_type && !base_type->ident) {
2708 switch (base_type->type) {
2709 case SYM_STRUCT:
2710 case SYM_UNION:
2711 case SYM_ENUM:
2712 case SYM_RESTRICT:
2713 base_type->ident = ident;
2716 } else if (base_type && base_type->type == SYM_FN) {
2717 /* K&R argument declaration? */
2718 if (lookup_type(token))
2719 return parse_k_r_arguments(token, decl, list);
2720 if (match_op(token, '{'))
2721 return parse_function_body(token, decl, list);
2723 if (!(decl->ctype.modifiers & MOD_STATIC))
2724 decl->ctype.modifiers |= MOD_EXTERN;
2725 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2726 sparse_error(token->pos, "void declaration");
2729 for (;;) {
2730 if (!is_typedef && match_op(token, '=')) {
2731 if (decl->ctype.modifiers & MOD_EXTERN) {
2732 warning(decl->pos, "symbol with external linkage has initializer");
2733 decl->ctype.modifiers &= ~MOD_EXTERN;
2735 token = initializer(&decl->initializer, token->next);
2737 if (!is_typedef) {
2738 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2739 add_symbol(list, decl);
2740 fn_local_symbol(decl);
2743 check_declaration(decl);
2744 if (decl->same_symbol)
2745 decl->definition = decl->same_symbol->definition;
2747 if (!match_op(token, ','))
2748 break;
2750 token = token->next;
2751 ident = NULL;
2752 decl = alloc_symbol(token->pos, SYM_NODE);
2753 ctx.ctype = saved;
2754 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2755 token = declarator(token, &ctx);
2756 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2757 apply_modifiers(token->pos, &ctx);
2758 decl->ctype = ctx.ctype;
2759 decl->ctype.modifiers |= mod;
2760 decl->endpos = token->pos;
2761 if (!ident) {
2762 sparse_error(token->pos, "expected identifier name in type definition");
2763 return token;
2766 if (is_typedef)
2767 decl->ctype.modifiers |= MOD_USERTYPE;
2769 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2771 /* Function declarations are automatically extern unless specifically static */
2772 base_type = decl->ctype.base_type;
2773 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2774 if (!(decl->ctype.modifiers & MOD_STATIC))
2775 decl->ctype.modifiers |= MOD_EXTERN;
2778 return expect(token, ';', "at end of declaration");