sparse, llvm: Introduce 'struct function' to clean up code
[smatch.git] / parse.c
blob082c2c401bf881fdae9ce6479a43d1484b030a0a
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 "artificial",
490 "__artificial__",
491 "bounded",
492 "__bounded__",
493 "cdecl",
494 "__cdecl__",
495 "cold",
496 "__cold__",
497 "const",
498 "__const",
499 "__const__",
500 "constructor",
501 "__constructor__",
502 "deprecated",
503 "__deprecated__",
504 "destructor",
505 "__destructor__",
506 "dllexport",
507 "__dllexport__",
508 "dllimport",
509 "__dllimport__",
510 "externally_visible",
511 "__externally_visible__",
512 "fastcall",
513 "__fastcall__",
514 "format",
515 "__format__",
516 "format_arg",
517 "__format_arg__",
518 "hot",
519 "__hot__",
520 "l1_text",
521 "__l1_text__",
522 "l1_data",
523 "__l1_data__",
524 "l2",
525 "__l2__",
526 "may_alias",
527 "__may_alias__",
528 "malloc",
529 "__malloc__",
530 "may_alias",
531 "__may_alias__",
532 "model",
533 "__model__",
534 "ms_abi",
535 "__ms_abi__",
536 "naked",
537 "__naked__",
538 "no_instrument_function",
539 "__no_instrument_function__",
540 "noinline",
541 "__noinline__",
542 "nonnull",
543 "__nonnull",
544 "__nonnull__",
545 "nothrow",
546 "__nothrow",
547 "__nothrow__",
548 "pure",
549 "__pure__",
550 "regparm",
551 "__regparm__",
552 "section",
553 "__section__",
554 "sentinel",
555 "__sentinel__",
556 "signal",
557 "__signal__",
558 "stdcall",
559 "__stdcall__",
560 "syscall_linkage",
561 "__syscall_linkage__",
562 "sysv_abi",
563 "__sysv_abi__",
564 "unused",
565 "__unused__",
566 "used",
567 "__used__",
568 "vector_size",
569 "visibility",
570 "__visibility__",
571 "warn_unused_result",
572 "__warn_unused_result__",
573 "warning",
574 "__warning__",
575 "weak",
576 "__weak__",
580 void init_parser(int stream)
582 int i;
583 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
584 struct init_keyword *ptr = keyword_table + i;
585 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
586 sym->ident->keyword = 1;
587 if (ptr->ns == NS_TYPEDEF)
588 sym->ident->reserved = 1;
589 sym->ctype.modifiers = ptr->modifiers;
590 sym->ctype.base_type = ptr->type;
591 sym->op = ptr->op;
594 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
595 const char * name = ignored_attributes[i];
596 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
597 NS_KEYWORD);
598 sym->ident->keyword = 1;
599 sym->op = &ignore_attr_op;
604 // Add a symbol to the list of function-local symbols
605 static void fn_local_symbol(struct symbol *sym)
607 if (function_symbol_list)
608 add_symbol(function_symbol_list, sym);
611 static int SENTINEL_ATTR match_idents(struct token *token, ...)
613 va_list args;
614 struct ident * next;
616 if (token_type(token) != TOKEN_IDENT)
617 return 0;
619 va_start(args, token);
620 do {
621 next = va_arg(args, struct ident *);
622 } while (next && token->ident != next);
623 va_end(args);
625 return next && token->ident == next;
629 struct statement *alloc_statement(struct position pos, int type)
631 struct statement *stmt = __alloc_statement(0);
632 stmt->type = type;
633 stmt->pos = pos;
634 return stmt;
637 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
639 static void apply_modifiers(struct position pos, struct decl_state *ctx)
641 struct symbol *ctype;
642 if (!ctx->mode)
643 return;
644 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
645 if (!ctype)
646 sparse_error(pos, "don't know how to apply mode to %s",
647 show_typename(ctx->ctype.base_type));
648 else
649 ctx->ctype.base_type = ctype;
653 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
655 struct symbol *sym = alloc_symbol(pos, type);
657 sym->ctype.base_type = ctype->base_type;
658 sym->ctype.modifiers = ctype->modifiers;
660 ctype->base_type = sym;
661 ctype->modifiers = 0;
662 return sym;
666 * NOTE! NS_LABEL is not just a different namespace,
667 * it also ends up using function scope instead of the
668 * regular symbol scope.
670 struct symbol *label_symbol(struct token *token)
672 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
673 if (!sym) {
674 sym = alloc_symbol(token->pos, SYM_LABEL);
675 bind_symbol(sym, token->ident, NS_LABEL);
676 fn_local_symbol(sym);
678 return sym;
681 static struct token *struct_union_enum_specifier(enum type type,
682 struct token *token, struct decl_state *ctx,
683 struct token *(*parse)(struct token *, struct symbol *))
685 struct symbol *sym;
686 struct position *repos;
688 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
689 if (token_type(token) == TOKEN_IDENT) {
690 sym = lookup_symbol(token->ident, NS_STRUCT);
691 if (!sym ||
692 (is_outer_scope(sym->scope) &&
693 (match_op(token->next,';') || match_op(token->next,'{')))) {
694 // Either a new symbol, or else an out-of-scope
695 // symbol being redefined.
696 sym = alloc_symbol(token->pos, type);
697 bind_symbol(sym, token->ident, NS_STRUCT);
699 if (sym->type != type)
700 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
701 ctx->ctype.base_type = sym;
702 repos = &token->pos;
703 token = token->next;
704 if (match_op(token, '{')) {
705 // The following test is actually wrong for empty
706 // structs, but (1) they are not C99, (2) gcc does
707 // the same thing, and (3) it's easier.
708 if (sym->symbol_list)
709 error_die(token->pos, "redefinition of %s", show_typename (sym));
710 sym->pos = *repos;
711 token = parse(token->next, sym);
712 token = expect(token, '}', "at end of struct-union-enum-specifier");
714 // Mark the structure as needing re-examination
715 sym->examined = 0;
716 sym->endpos = token->pos;
718 return token;
721 // private struct/union/enum type
722 if (!match_op(token, '{')) {
723 sparse_error(token->pos, "expected declaration");
724 ctx->ctype.base_type = &bad_ctype;
725 return token;
728 sym = alloc_symbol(token->pos, type);
729 token = parse(token->next, sym);
730 ctx->ctype.base_type = sym;
731 token = expect(token, '}', "at end of specifier");
732 sym->endpos = token->pos;
734 return token;
737 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
739 struct symbol *field, *last = NULL;
740 struct token *res;
741 res = struct_declaration_list(token, &sym->symbol_list);
742 FOR_EACH_PTR(sym->symbol_list, field) {
743 if (!field->ident) {
744 struct symbol *base = field->ctype.base_type;
745 if (base && base->type == SYM_BITFIELD)
746 continue;
748 if (last)
749 last->next_subobject = field;
750 last = field;
751 } END_FOR_EACH_PTR(field);
752 return res;
755 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
757 return struct_declaration_list(token, &sym->symbol_list);
760 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
762 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
765 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
767 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
771 typedef struct {
772 int x;
773 unsigned long long y;
774 } Num;
776 static void upper_boundary(Num *n, Num *v)
778 if (n->x > v->x)
779 return;
780 if (n->x < v->x) {
781 *n = *v;
782 return;
784 if (n->y < v->y)
785 n->y = v->y;
788 static void lower_boundary(Num *n, Num *v)
790 if (n->x < v->x)
791 return;
792 if (n->x > v->x) {
793 *n = *v;
794 return;
796 if (n->y > v->y)
797 n->y = v->y;
800 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
802 int shift = type->bit_size;
803 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
805 if (!is_unsigned)
806 shift--;
807 if (upper->x == 0 && upper->y >> shift)
808 return 0;
809 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
810 return 1;
811 return 0;
814 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
816 if (s1->bit_size < s2->bit_size) {
817 s1 = s2;
818 } else if (s1->bit_size == s2->bit_size) {
819 if (s2->ctype.modifiers & MOD_UNSIGNED)
820 s1 = s2;
822 if (s1->bit_size < bits_in_int)
823 return &int_ctype;
824 return s1;
827 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
829 struct symbol *sym;
831 FOR_EACH_PTR(list, sym) {
832 struct expression *expr = sym->initializer;
833 struct symbol *ctype;
834 if (expr->type != EXPR_VALUE)
835 continue;
836 ctype = expr->ctype;
837 if (ctype->bit_size == base_type->bit_size)
838 continue;
839 cast_value(expr, base_type, expr, ctype);
840 } END_FOR_EACH_PTR(sym);
843 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
845 unsigned long long lastval = 0;
846 struct symbol *ctype = NULL, *base_type = NULL;
847 Num upper = {-1, 0}, lower = {1, 0};
849 parent->examined = 1;
850 parent->ctype.base_type = &int_ctype;
851 while (token_type(token) == TOKEN_IDENT) {
852 struct expression *expr = NULL;
853 struct token *next = token->next;
854 struct symbol *sym;
856 if (match_op(next, '=')) {
857 next = constant_expression(next->next, &expr);
858 lastval = get_expression_value(expr);
859 ctype = &void_ctype;
860 if (expr && expr->ctype)
861 ctype = expr->ctype;
862 } else if (!ctype) {
863 ctype = &int_ctype;
864 } else if (is_int_type(ctype)) {
865 lastval++;
866 } else {
867 error_die(token->pos, "can't increment the last enum member");
870 if (!expr) {
871 expr = alloc_expression(token->pos, EXPR_VALUE);
872 expr->value = lastval;
873 expr->ctype = ctype;
876 sym = alloc_symbol(token->pos, SYM_NODE);
877 bind_symbol(sym, token->ident, NS_SYMBOL);
878 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
879 sym->initializer = expr;
880 sym->enum_member = 1;
881 sym->ctype.base_type = parent;
882 add_ptr_list(&parent->symbol_list, sym);
884 if (base_type != &bad_ctype) {
885 if (ctype->type == SYM_NODE)
886 ctype = ctype->ctype.base_type;
887 if (ctype->type == SYM_ENUM) {
888 if (ctype == parent)
889 ctype = base_type;
890 else
891 ctype = ctype->ctype.base_type;
894 * base_type rules:
895 * - if all enums are of the same type, then
896 * the base_type is that type (two first
897 * cases)
898 * - if enums are of different types, they
899 * all have to be integer types, and the
900 * base type is at least "int_ctype".
901 * - otherwise the base_type is "bad_ctype".
903 if (!base_type) {
904 base_type = ctype;
905 } else if (ctype == base_type) {
906 /* nothing */
907 } else if (is_int_type(base_type) && is_int_type(ctype)) {
908 base_type = bigger_enum_type(base_type, ctype);
909 } else
910 base_type = &bad_ctype;
911 parent->ctype.base_type = base_type;
913 if (is_int_type(base_type)) {
914 Num v = {.y = lastval};
915 if (ctype->ctype.modifiers & MOD_UNSIGNED)
916 v.x = 0;
917 else if ((long long)lastval >= 0)
918 v.x = 0;
919 else
920 v.x = -1;
921 upper_boundary(&upper, &v);
922 lower_boundary(&lower, &v);
924 token = next;
926 sym->endpos = token->pos;
928 if (!match_op(token, ','))
929 break;
930 token = token->next;
932 if (!base_type) {
933 sparse_error(token->pos, "bad enum definition");
934 base_type = &bad_ctype;
936 else if (!is_int_type(base_type))
937 base_type = base_type;
938 else if (type_is_ok(base_type, &upper, &lower))
939 base_type = base_type;
940 else if (type_is_ok(&int_ctype, &upper, &lower))
941 base_type = &int_ctype;
942 else if (type_is_ok(&uint_ctype, &upper, &lower))
943 base_type = &uint_ctype;
944 else if (type_is_ok(&long_ctype, &upper, &lower))
945 base_type = &long_ctype;
946 else if (type_is_ok(&ulong_ctype, &upper, &lower))
947 base_type = &ulong_ctype;
948 else if (type_is_ok(&llong_ctype, &upper, &lower))
949 base_type = &llong_ctype;
950 else if (type_is_ok(&ullong_ctype, &upper, &lower))
951 base_type = &ullong_ctype;
952 else
953 base_type = &bad_ctype;
954 parent->ctype.base_type = base_type;
955 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
956 parent->examined = 0;
958 cast_enum_list(parent->symbol_list, base_type);
960 return token;
963 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
965 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
966 struct ctype *ctype = &ctx->ctype.base_type->ctype;
968 if (!ctype->base_type)
969 ctype->base_type = &incomplete_ctype;
971 return ret;
974 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
976 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
978 struct symbol *sym;
980 if (!match_op(token, '(')) {
981 sparse_error(token->pos, "expected '(' after typeof");
982 return token;
984 if (lookup_type(token->next)) {
985 token = typename(token->next, &sym, NULL);
986 ctx->ctype.base_type = sym->ctype.base_type;
987 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
988 } else {
989 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
990 token = parse_expression(token->next, &typeof_sym->initializer);
992 typeof_sym->endpos = token->pos;
993 if (!typeof_sym->initializer) {
994 sparse_error(token->pos, "expected expression after the '(' token");
995 typeof_sym = &bad_ctype;
997 ctx->ctype.base_type = typeof_sym;
999 return expect(token, ')', "after typeof");
1002 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1004 struct expression *expr = NULL;
1005 if (match_op(token, '('))
1006 token = parens_expression(token, &expr, "in attribute");
1007 return token;
1010 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1012 if (!ctx->ctype.alignment)
1013 ctx->ctype.alignment = 1;
1014 return token;
1017 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1019 int alignment = max_alignment;
1020 struct expression *expr = NULL;
1022 if (match_op(token, '(')) {
1023 token = parens_expression(token, &expr, "in attribute");
1024 if (expr)
1025 alignment = const_expression_value(expr);
1027 if (alignment & (alignment-1)) {
1028 warning(token->pos, "I don't like non-power-of-2 alignments");
1029 return token;
1030 } else if (alignment > ctx->ctype.alignment)
1031 ctx->ctype.alignment = alignment;
1032 return token;
1035 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1037 if (ctx->modifiers & qual)
1038 warning(*pos, "duplicate %s", modifier_string(qual));
1039 ctx->modifiers |= qual;
1042 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1044 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1045 return token;
1048 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1050 struct expression *expr = NULL;
1051 int as;
1052 token = expect(token, '(', "after address_space attribute");
1053 token = conditional_expression(token, &expr);
1054 if (expr) {
1055 as = const_expression_value(expr);
1056 if (Waddress_space && as)
1057 ctx->ctype.as = as;
1059 token = expect(token, ')', "after address_space attribute");
1060 return token;
1063 static struct symbol *to_QI_mode(struct symbol *ctype)
1065 if (ctype->ctype.base_type != &int_type)
1066 return NULL;
1067 if (ctype == &char_ctype)
1068 return ctype;
1069 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1070 : &schar_ctype;
1073 static struct symbol *to_HI_mode(struct symbol *ctype)
1075 if (ctype->ctype.base_type != &int_type)
1076 return NULL;
1077 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1078 : &sshort_ctype;
1081 static struct symbol *to_SI_mode(struct symbol *ctype)
1083 if (ctype->ctype.base_type != &int_type)
1084 return NULL;
1085 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1086 : &sint_ctype;
1089 static struct symbol *to_DI_mode(struct symbol *ctype)
1091 if (ctype->ctype.base_type != &int_type)
1092 return NULL;
1093 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1094 : &sllong_ctype;
1097 static struct symbol *to_TI_mode(struct symbol *ctype)
1099 if (ctype->ctype.base_type != &int_type)
1100 return NULL;
1101 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1102 : &slllong_ctype;
1105 static struct symbol *to_word_mode(struct symbol *ctype)
1107 if (ctype->ctype.base_type != &int_type)
1108 return NULL;
1109 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1110 : &slong_ctype;
1113 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1115 token = expect(token, '(', "after mode attribute");
1116 if (token_type(token) == TOKEN_IDENT) {
1117 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1118 if (mode && mode->op->type == KW_MODE)
1119 ctx->mode = mode->op;
1120 else
1121 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1122 token = token->next;
1123 } else
1124 sparse_error(token->pos, "expect attribute mode symbol\n");
1125 token = expect(token, ')', "after mode attribute");
1126 return token;
1129 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1131 struct context *context = alloc_context();
1132 struct expression *args[3];
1133 int argc = 0;
1135 token = expect(token, '(', "after context attribute");
1136 while (!match_op(token, ')')) {
1137 struct expression *expr = NULL;
1138 token = conditional_expression(token, &expr);
1139 if (!expr)
1140 break;
1141 if (argc < 3)
1142 args[argc++] = expr;
1143 if (!match_op(token, ','))
1144 break;
1145 token = token->next;
1148 switch(argc) {
1149 case 0:
1150 sparse_error(token->pos, "expected context input/output values");
1151 break;
1152 case 1:
1153 context->in = get_expression_value(args[0]);
1154 break;
1155 case 2:
1156 context->in = get_expression_value(args[0]);
1157 context->out = get_expression_value(args[1]);
1158 break;
1159 case 3:
1160 context->context = args[0];
1161 context->in = get_expression_value(args[1]);
1162 context->out = get_expression_value(args[2]);
1163 break;
1166 if (argc)
1167 add_ptr_list(&ctx->ctype.contexts, context);
1169 token = expect(token, ')', "after context attribute");
1170 return token;
1173 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1175 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1176 ctx->ctype.base_type->designated_init = 1;
1177 else
1178 warning(token->pos, "attribute designated_init applied to non-structure type");
1179 return token;
1182 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1184 if (Wtransparent_union)
1185 warning(token->pos, "ignoring attribute __transparent_union__");
1186 return token;
1189 static struct token *recover_unknown_attribute(struct token *token)
1191 struct expression *expr = NULL;
1193 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1194 token = token->next;
1195 if (match_op(token, '('))
1196 token = parens_expression(token, &expr, "in attribute");
1197 return token;
1200 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1202 token = expect(token, '(', "after attribute");
1203 token = expect(token, '(', "after attribute");
1205 for (;;) {
1206 struct ident *attribute_name;
1207 struct symbol *attr;
1209 if (eof_token(token))
1210 break;
1211 if (match_op(token, ';'))
1212 break;
1213 if (token_type(token) != TOKEN_IDENT)
1214 break;
1215 attribute_name = token->ident;
1216 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1217 if (attr && attr->op->attribute)
1218 token = attr->op->attribute(token->next, attr, ctx);
1219 else
1220 token = recover_unknown_attribute(token);
1222 if (!match_op(token, ','))
1223 break;
1224 token = token->next;
1227 token = expect(token, ')', "after attribute");
1228 token = expect(token, ')', "after attribute");
1229 return token;
1232 static const char *storage_class[] =
1234 [STypedef] = "typedef",
1235 [SAuto] = "auto",
1236 [SExtern] = "extern",
1237 [SStatic] = "static",
1238 [SRegister] = "register",
1239 [SForced] = "[force]"
1242 static unsigned long storage_modifiers(struct decl_state *ctx)
1244 static unsigned long mod[] =
1246 [SAuto] = MOD_AUTO,
1247 [SExtern] = MOD_EXTERN,
1248 [SStatic] = MOD_STATIC,
1249 [SRegister] = MOD_REGISTER
1251 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1252 | (ctx->is_tls ? MOD_TLS : 0);
1255 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1257 /* __thread can be used alone, or with extern or static */
1258 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1259 sparse_error(*pos, "__thread can only be used alone, or with "
1260 "extern or static");
1261 return;
1264 if (!ctx->storage_class) {
1265 ctx->storage_class = class;
1266 return;
1268 if (ctx->storage_class == class)
1269 sparse_error(*pos, "duplicate %s", storage_class[class]);
1270 else
1271 sparse_error(*pos, "multiple storage classes");
1274 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1276 set_storage_class(&next->pos, ctx, STypedef);
1277 return next;
1280 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1282 set_storage_class(&next->pos, ctx, SAuto);
1283 return next;
1286 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1288 set_storage_class(&next->pos, ctx, SRegister);
1289 return next;
1292 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1294 set_storage_class(&next->pos, ctx, SStatic);
1295 return next;
1298 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1300 set_storage_class(&next->pos, ctx, SExtern);
1301 return next;
1304 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1306 /* This GCC extension can be used alone, or with extern or static */
1307 if (!ctx->storage_class || ctx->storage_class == SStatic
1308 || ctx->storage_class == SExtern) {
1309 ctx->is_tls = 1;
1310 } else {
1311 sparse_error(next->pos, "__thread can only be used alone, or "
1312 "with extern or static");
1315 return next;
1318 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1320 set_storage_class(&token->pos, ctx, SForced);
1321 return token;
1324 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1326 ctx->is_inline = 1;
1327 return next;
1330 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1332 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1333 return next;
1336 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1338 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1339 return next;
1342 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1344 unsigned long mod = thistype->modifiers;
1346 if (mod)
1347 apply_qualifier(&pos, ctype, mod);
1349 /* Context */
1350 concat_ptr_list((struct ptr_list *)thistype->contexts,
1351 (struct ptr_list **)&ctype->contexts);
1353 /* Alignment */
1354 if (thistype->alignment > ctype->alignment)
1355 ctype->alignment = thistype->alignment;
1357 /* Address space */
1358 if (thistype->as)
1359 ctype->as = thistype->as;
1362 static void specifier_conflict(struct position pos, int what, struct ident *new)
1364 const char *old;
1365 if (what & (Set_S | Set_T))
1366 goto Catch_all;
1367 if (what & Set_Char)
1368 old = "char";
1369 else if (what & Set_Double)
1370 old = "double";
1371 else if (what & Set_Float)
1372 old = "float";
1373 else if (what & Set_Signed)
1374 old = "signed";
1375 else if (what & Set_Unsigned)
1376 old = "unsigned";
1377 else if (what & Set_Short)
1378 old = "short";
1379 else if (what & Set_Long)
1380 old = "long";
1381 else
1382 old = "long long";
1383 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1384 old, show_ident(new));
1385 return;
1387 Catch_all:
1388 sparse_error(pos, "two or more data types in declaration specifiers");
1391 static struct symbol * const int_types[] =
1392 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1393 static struct symbol * const signed_types[] =
1394 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1395 &slllong_ctype};
1396 static struct symbol * const unsigned_types[] =
1397 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1398 &ulllong_ctype};
1399 static struct symbol * const real_types[] =
1400 {&float_ctype, &double_ctype, &ldouble_ctype};
1401 static struct symbol * const char_types[] =
1402 {&char_ctype, &schar_ctype, &uchar_ctype};
1403 static struct symbol * const * const types[] = {
1404 int_types + 1, signed_types + 1, unsigned_types + 1,
1405 real_types + 1, char_types, char_types + 1, char_types + 2
1408 struct symbol *ctype_integer(int size, int want_unsigned)
1410 return types[want_unsigned ? CUInt : CInt][size];
1413 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1415 while (token_type(t) == TOKEN_IDENT) {
1416 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1417 if (!s)
1418 break;
1419 if (s->type != SYM_KEYWORD)
1420 break;
1421 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1422 break;
1423 t = t->next;
1424 if (s->op->declarator)
1425 t = s->op->declarator(t, ctx);
1427 return t;
1430 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1432 int seen = 0;
1433 int class = CInt;
1434 int size = 0;
1436 while (token_type(token) == TOKEN_IDENT) {
1437 struct symbol *s = lookup_symbol(token->ident,
1438 NS_TYPEDEF | NS_SYMBOL);
1439 if (!s || !(s->namespace & NS_TYPEDEF))
1440 break;
1441 if (s->type != SYM_KEYWORD) {
1442 if (seen & Set_Any)
1443 break;
1444 seen |= Set_S | Set_T;
1445 ctx->ctype.base_type = s->ctype.base_type;
1446 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1447 token = token->next;
1448 continue;
1450 if (s->op->type & KW_SPECIFIER) {
1451 if (seen & s->op->test) {
1452 specifier_conflict(token->pos,
1453 seen & s->op->test,
1454 token->ident);
1455 break;
1457 seen |= s->op->set;
1458 class += s->op->class;
1459 if (s->op->type & KW_SHORT) {
1460 size = -1;
1461 } else if (s->op->type & KW_LONG && size++) {
1462 if (class == CReal) {
1463 specifier_conflict(token->pos,
1464 Set_Vlong,
1465 &double_ident);
1466 break;
1468 seen |= Set_Vlong;
1471 token = token->next;
1472 if (s->op->declarator)
1473 token = s->op->declarator(token, ctx);
1474 if (s->op->type & KW_EXACT) {
1475 ctx->ctype.base_type = s->ctype.base_type;
1476 ctx->ctype.modifiers |= s->ctype.modifiers;
1480 if (!(seen & Set_S)) { /* not set explicitly? */
1481 struct symbol *base = &incomplete_ctype;
1482 if (seen & Set_Any)
1483 base = types[class][size];
1484 ctx->ctype.base_type = base;
1487 if (ctx->ctype.modifiers & MOD_BITWISE) {
1488 struct symbol *type;
1489 ctx->ctype.modifiers &= ~MOD_BITWISE;
1490 if (!is_int_type(ctx->ctype.base_type)) {
1491 sparse_error(token->pos, "invalid modifier");
1492 return token;
1494 type = alloc_symbol(token->pos, SYM_BASETYPE);
1495 *type = *ctx->ctype.base_type;
1496 type->ctype.modifiers &= ~MOD_SPECIFIER;
1497 type->ctype.base_type = ctx->ctype.base_type;
1498 type->type = SYM_RESTRICT;
1499 ctx->ctype.base_type = type;
1500 create_fouled(type);
1502 return token;
1505 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1507 struct expression *expr = NULL;
1509 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1510 token = token->next;
1511 token = parse_expression(token, &expr);
1512 sym->array_size = expr;
1513 return token;
1516 static struct token *parameter_type_list(struct token *, struct symbol *);
1517 static struct token *identifier_list(struct token *, struct symbol *);
1518 static struct token *declarator(struct token *token, struct decl_state *ctx);
1520 static struct token *skip_attribute(struct token *token)
1522 token = token->next;
1523 if (match_op(token, '(')) {
1524 int depth = 1;
1525 token = token->next;
1526 while (depth && !eof_token(token)) {
1527 if (token_type(token) == TOKEN_SPECIAL) {
1528 if (token->special == '(')
1529 depth++;
1530 else if (token->special == ')')
1531 depth--;
1533 token = token->next;
1536 return token;
1539 static struct token *skip_attributes(struct token *token)
1541 struct symbol *keyword;
1542 for (;;) {
1543 if (token_type(token) != TOKEN_IDENT)
1544 break;
1545 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1546 if (!keyword || keyword->type != SYM_KEYWORD)
1547 break;
1548 if (!(keyword->op->type & KW_ATTRIBUTE))
1549 break;
1550 token = expect(token->next, '(', "after attribute");
1551 token = expect(token, '(', "after attribute");
1552 for (;;) {
1553 if (eof_token(token))
1554 break;
1555 if (match_op(token, ';'))
1556 break;
1557 if (token_type(token) != TOKEN_IDENT)
1558 break;
1559 token = skip_attribute(token);
1560 if (!match_op(token, ','))
1561 break;
1562 token = token->next;
1564 token = expect(token, ')', "after attribute");
1565 token = expect(token, ')', "after attribute");
1567 return token;
1570 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1572 struct symbol *keyword;
1573 for (;;) {
1574 if (token_type(token) != TOKEN_IDENT)
1575 break;
1576 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1577 if (!keyword || keyword->type != SYM_KEYWORD)
1578 break;
1579 if (!(keyword->op->type & keywords))
1580 break;
1581 token = keyword->op->declarator(token->next, ctx);
1582 keywords &= KW_ATTRIBUTE;
1584 return token;
1587 static int is_nested(struct token *token, struct token **p,
1588 int prefer_abstract)
1591 * This can be either a parameter list or a grouping.
1592 * For the direct (non-abstract) case, we know if must be
1593 * a parameter list if we already saw the identifier.
1594 * For the abstract case, we know if must be a parameter
1595 * list if it is empty or starts with a type.
1597 struct token *next = token->next;
1599 *p = next = skip_attributes(next);
1601 if (token_type(next) == TOKEN_IDENT) {
1602 if (lookup_type(next))
1603 return !prefer_abstract;
1604 return 1;
1607 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1608 return 0;
1610 return 1;
1613 enum kind {
1614 Empty, K_R, Proto, Bad_Func,
1617 static enum kind which_func(struct token *token,
1618 struct ident **n,
1619 int prefer_abstract)
1621 struct token *next = token->next;
1623 if (token_type(next) == TOKEN_IDENT) {
1624 if (lookup_type(next))
1625 return Proto;
1626 /* identifier list not in definition; complain */
1627 if (prefer_abstract)
1628 warning(token->pos,
1629 "identifier list not in definition");
1630 return K_R;
1633 if (token_type(next) != TOKEN_SPECIAL)
1634 return Bad_Func;
1636 if (next->special == ')') {
1637 /* don't complain about those */
1638 if (!n || match_op(next->next, ';'))
1639 return Empty;
1640 warning(next->pos,
1641 "non-ANSI function declaration of function '%s'",
1642 show_ident(*n));
1643 return Empty;
1646 if (next->special == SPECIAL_ELLIPSIS) {
1647 warning(next->pos,
1648 "variadic functions must have one named argument");
1649 return Proto;
1652 return Bad_Func;
1655 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1657 struct ctype *ctype = &ctx->ctype;
1658 struct token *next;
1659 struct ident **p = ctx->ident;
1661 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1662 *ctx->ident = token->ident;
1663 token = token->next;
1664 } else if (match_op(token, '(') &&
1665 is_nested(token, &next, ctx->prefer_abstract)) {
1666 struct symbol *base_type = ctype->base_type;
1667 if (token->next != next)
1668 next = handle_attributes(token->next, ctx,
1669 KW_ATTRIBUTE);
1670 token = declarator(next, ctx);
1671 token = expect(token, ')', "in nested declarator");
1672 while (ctype->base_type != base_type)
1673 ctype = &ctype->base_type->ctype;
1674 p = NULL;
1677 if (match_op(token, '(')) {
1678 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1679 struct symbol *fn;
1680 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1681 token = token->next;
1682 if (kind == K_R)
1683 token = identifier_list(token, fn);
1684 else if (kind == Proto)
1685 token = parameter_type_list(token, fn);
1686 token = expect(token, ')', "in function declarator");
1687 fn->endpos = token->pos;
1688 return token;
1691 while (match_op(token, '[')) {
1692 struct symbol *array;
1693 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1694 token = abstract_array_declarator(token->next, array);
1695 token = expect(token, ']', "in abstract_array_declarator");
1696 array->endpos = token->pos;
1697 ctype = &array->ctype;
1699 return token;
1702 static struct token *pointer(struct token *token, struct decl_state *ctx)
1704 while (match_op(token,'*')) {
1705 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1706 ptr->ctype.modifiers = ctx->ctype.modifiers;
1707 ptr->ctype.base_type = ctx->ctype.base_type;
1708 ptr->ctype.as = ctx->ctype.as;
1709 ptr->ctype.contexts = ctx->ctype.contexts;
1710 ctx->ctype.modifiers = 0;
1711 ctx->ctype.base_type = ptr;
1712 ctx->ctype.as = 0;
1713 ctx->ctype.contexts = NULL;
1714 ctx->ctype.alignment = 0;
1716 token = handle_qualifiers(token->next, ctx);
1717 ctx->ctype.base_type->endpos = token->pos;
1719 return token;
1722 static struct token *declarator(struct token *token, struct decl_state *ctx)
1724 token = pointer(token, ctx);
1725 return direct_declarator(token, ctx);
1728 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1730 struct ctype *ctype = &ctx->ctype;
1731 struct expression *expr;
1732 struct symbol *bitfield;
1733 long long width;
1735 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1736 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1737 show_typename(ctype->base_type));
1738 // Parse this to recover gracefully.
1739 return conditional_expression(token->next, &expr);
1742 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1743 token = conditional_expression(token->next, &expr);
1744 width = const_expression_value(expr);
1745 bitfield->bit_size = width;
1747 if (width < 0 || width > INT_MAX) {
1748 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1749 width = -1;
1750 } else if (*ctx->ident && width == 0) {
1751 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1752 show_ident(*ctx->ident));
1753 width = -1;
1754 } else if (*ctx->ident) {
1755 struct symbol *base_type = bitfield->ctype.base_type;
1756 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1757 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1758 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1759 // Valid values are either {-1;0} or {0}, depending on integer
1760 // representation. The latter makes for very efficient code...
1761 sparse_error(token->pos, "dubious one-bit signed bitfield");
1763 if (Wdefault_bitfield_sign &&
1764 bitfield_type->type != SYM_ENUM &&
1765 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1766 is_signed) {
1767 // The sign of bitfields is unspecified by default.
1768 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1771 bitfield->bit_size = width;
1772 bitfield->endpos = token->pos;
1773 return token;
1776 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1778 struct decl_state ctx = {.prefer_abstract = 0};
1779 struct ctype saved;
1780 unsigned long mod;
1782 token = declaration_specifiers(token, &ctx);
1783 mod = storage_modifiers(&ctx);
1784 saved = ctx.ctype;
1785 for (;;) {
1786 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1787 ctx.ident = &decl->ident;
1789 token = declarator(token, &ctx);
1790 if (match_op(token, ':'))
1791 token = handle_bitfield(token, &ctx);
1793 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1794 apply_modifiers(token->pos, &ctx);
1796 decl->ctype = ctx.ctype;
1797 decl->ctype.modifiers |= mod;
1798 decl->endpos = token->pos;
1799 add_symbol(list, decl);
1800 if (!match_op(token, ','))
1801 break;
1802 token = token->next;
1803 ctx.ctype = saved;
1805 return token;
1808 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1810 while (!match_op(token, '}')) {
1811 if (!match_op(token, ';'))
1812 token = declaration_list(token, list);
1813 if (!match_op(token, ';')) {
1814 sparse_error(token->pos, "expected ; at end of declaration");
1815 break;
1817 token = token->next;
1819 return token;
1822 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1824 struct decl_state ctx = {.prefer_abstract = 1};
1826 token = declaration_specifiers(token, &ctx);
1827 ctx.ident = &sym->ident;
1828 token = declarator(token, &ctx);
1829 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1830 apply_modifiers(token->pos, &ctx);
1831 sym->ctype = ctx.ctype;
1832 sym->ctype.modifiers |= storage_modifiers(&ctx);
1833 sym->endpos = token->pos;
1834 return token;
1837 struct token *typename(struct token *token, struct symbol **p, int *forced)
1839 struct decl_state ctx = {.prefer_abstract = 1};
1840 int class;
1841 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1842 *p = sym;
1843 token = declaration_specifiers(token, &ctx);
1844 token = declarator(token, &ctx);
1845 apply_modifiers(token->pos, &ctx);
1846 sym->ctype = ctx.ctype;
1847 sym->endpos = token->pos;
1848 class = ctx.storage_class;
1849 if (forced) {
1850 *forced = 0;
1851 if (class == SForced) {
1852 *forced = 1;
1853 class = 0;
1856 if (class)
1857 warning(sym->pos, "storage class in typename (%s %s)",
1858 storage_class[class], show_typename(sym));
1859 return token;
1862 static struct token *expression_statement(struct token *token, struct expression **tree)
1864 token = parse_expression(token, tree);
1865 return expect(token, ';', "at end of statement");
1868 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1869 struct expression_list **inout)
1871 struct expression *expr;
1873 /* Allow empty operands */
1874 if (match_op(token->next, ':') || match_op(token->next, ')'))
1875 return token->next;
1876 do {
1877 struct ident *ident = NULL;
1878 if (match_op(token->next, '[') &&
1879 token_type(token->next->next) == TOKEN_IDENT &&
1880 match_op(token->next->next->next, ']')) {
1881 ident = token->next->next->ident;
1882 token = token->next->next->next;
1884 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1885 token = primary_expression(token->next, &expr);
1886 add_expression(inout, expr);
1887 token = parens_expression(token, &expr, "in asm parameter");
1888 add_expression(inout, expr);
1889 } while (match_op(token, ','));
1890 return token;
1893 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1894 struct expression_list **clobbers)
1896 struct expression *expr;
1898 do {
1899 token = primary_expression(token->next, &expr);
1900 add_expression(clobbers, expr);
1901 } while (match_op(token, ','));
1902 return token;
1905 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1906 struct symbol_list **labels)
1908 struct symbol *label;
1910 do {
1911 token = token->next; /* skip ':' and ',' */
1912 if (token_type(token) != TOKEN_IDENT)
1913 return token;
1914 label = label_symbol(token);
1915 add_symbol(labels, label);
1916 token = token->next;
1917 } while (match_op(token, ','));
1918 return token;
1921 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1923 int is_goto = 0;
1925 token = token->next;
1926 stmt->type = STMT_ASM;
1927 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1928 token = token->next;
1930 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1931 is_goto = 1;
1932 token = token->next;
1934 token = expect(token, '(', "after asm");
1935 token = parse_expression(token, &stmt->asm_string);
1936 if (match_op(token, ':'))
1937 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1938 if (match_op(token, ':'))
1939 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1940 if (match_op(token, ':'))
1941 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1942 if (is_goto && match_op(token, ':'))
1943 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1944 token = expect(token, ')', "after asm");
1945 return expect(token, ';', "at end of asm-statement");
1948 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1950 struct expression *expr;
1951 token = expect(token, '(', "after asm");
1952 token = parse_expression(token->next, &expr);
1953 token = expect(token, ')', "after asm");
1954 return token;
1957 /* Make a statement out of an expression */
1958 static struct statement *make_statement(struct expression *expr)
1960 struct statement *stmt;
1962 if (!expr)
1963 return NULL;
1964 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1965 stmt->expression = expr;
1966 return stmt;
1970 * All iterators have two symbols associated with them:
1971 * the "continue" and "break" symbols, which are targets
1972 * for continue and break statements respectively.
1974 * They are in a special name-space, but they follow
1975 * all the normal visibility rules, so nested iterators
1976 * automatically work right.
1978 static void start_iterator(struct statement *stmt)
1980 struct symbol *cont, *brk;
1982 start_symbol_scope();
1983 cont = alloc_symbol(stmt->pos, SYM_NODE);
1984 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1985 brk = alloc_symbol(stmt->pos, SYM_NODE);
1986 bind_symbol(brk, &break_ident, NS_ITERATOR);
1988 stmt->type = STMT_ITERATOR;
1989 stmt->iterator_break = brk;
1990 stmt->iterator_continue = cont;
1991 fn_local_symbol(brk);
1992 fn_local_symbol(cont);
1995 static void end_iterator(struct statement *stmt)
1997 end_symbol_scope();
2000 static struct statement *start_function(struct symbol *sym)
2002 struct symbol *ret;
2003 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2005 start_function_scope();
2006 ret = alloc_symbol(sym->pos, SYM_NODE);
2007 ret->ctype = sym->ctype.base_type->ctype;
2008 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2009 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2010 bind_symbol(ret, &return_ident, NS_ITERATOR);
2011 stmt->ret = ret;
2012 fn_local_symbol(ret);
2014 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2015 current_fn = sym;
2017 return stmt;
2020 static void end_function(struct symbol *sym)
2022 current_fn = NULL;
2023 end_function_scope();
2027 * A "switch()" statement, like an iterator, has a
2028 * the "break" symbol associated with it. It works
2029 * exactly like the iterator break - it's the target
2030 * for any break-statements in scope, and means that
2031 * "break" handling doesn't even need to know whether
2032 * it's breaking out of an iterator or a switch.
2034 * In addition, the "case" symbol is a marker for the
2035 * case/default statements to find the switch statement
2036 * that they are associated with.
2038 static void start_switch(struct statement *stmt)
2040 struct symbol *brk, *switch_case;
2042 start_symbol_scope();
2043 brk = alloc_symbol(stmt->pos, SYM_NODE);
2044 bind_symbol(brk, &break_ident, NS_ITERATOR);
2046 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2047 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2048 switch_case->stmt = stmt;
2050 stmt->type = STMT_SWITCH;
2051 stmt->switch_break = brk;
2052 stmt->switch_case = switch_case;
2054 fn_local_symbol(brk);
2055 fn_local_symbol(switch_case);
2058 static void end_switch(struct statement *stmt)
2060 if (!stmt->switch_case->symbol_list)
2061 warning(stmt->pos, "switch with no cases");
2062 end_symbol_scope();
2065 static void add_case_statement(struct statement *stmt)
2067 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2068 struct symbol *sym;
2070 if (!target) {
2071 sparse_error(stmt->pos, "not in switch scope");
2072 stmt->type = STMT_NONE;
2073 return;
2075 sym = alloc_symbol(stmt->pos, SYM_NODE);
2076 add_symbol(&target->symbol_list, sym);
2077 sym->stmt = stmt;
2078 stmt->case_label = sym;
2079 fn_local_symbol(sym);
2082 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2084 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2086 if (!target)
2087 error_die(token->pos, "internal error: return without a function target");
2088 stmt->type = STMT_RETURN;
2089 stmt->ret_target = target;
2090 return expression_statement(token->next, &stmt->ret_value);
2093 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2095 struct symbol_list *syms;
2096 struct expression *e1, *e2, *e3;
2097 struct statement *iterator;
2099 start_iterator(stmt);
2100 token = expect(token->next, '(', "after 'for'");
2102 syms = NULL;
2103 e1 = NULL;
2104 /* C99 variable declaration? */
2105 if (lookup_type(token)) {
2106 token = external_declaration(token, &syms);
2107 } else {
2108 token = parse_expression(token, &e1);
2109 token = expect(token, ';', "in 'for'");
2111 token = parse_expression(token, &e2);
2112 token = expect(token, ';', "in 'for'");
2113 token = parse_expression(token, &e3);
2114 token = expect(token, ')', "in 'for'");
2115 token = statement(token, &iterator);
2117 stmt->iterator_syms = syms;
2118 stmt->iterator_pre_statement = make_statement(e1);
2119 stmt->iterator_pre_condition = e2;
2120 stmt->iterator_post_statement = make_statement(e3);
2121 stmt->iterator_post_condition = NULL;
2122 stmt->iterator_statement = iterator;
2123 end_iterator(stmt);
2125 return token;
2128 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2130 struct expression *expr;
2131 struct statement *iterator;
2133 start_iterator(stmt);
2134 token = parens_expression(token->next, &expr, "after 'while'");
2135 token = statement(token, &iterator);
2137 stmt->iterator_pre_condition = expr;
2138 stmt->iterator_post_condition = NULL;
2139 stmt->iterator_statement = iterator;
2140 end_iterator(stmt);
2142 return token;
2145 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2147 struct expression *expr;
2148 struct statement *iterator;
2150 start_iterator(stmt);
2151 token = statement(token->next, &iterator);
2152 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2153 token = token->next;
2154 else
2155 sparse_error(token->pos, "expected 'while' after 'do'");
2156 token = parens_expression(token, &expr, "after 'do-while'");
2158 stmt->iterator_post_condition = expr;
2159 stmt->iterator_statement = iterator;
2160 end_iterator(stmt);
2162 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2163 warning(iterator->pos, "do-while statement is not a compound statement");
2165 return expect(token, ';', "after statement");
2168 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2170 stmt->type = STMT_IF;
2171 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2172 token = statement(token, &stmt->if_true);
2173 if (token_type(token) != TOKEN_IDENT)
2174 return token;
2175 if (token->ident != &else_ident)
2176 return token;
2177 return statement(token->next, &stmt->if_false);
2180 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2182 stmt->type = STMT_CASE;
2183 token = expect(token, ':', "after default/case");
2184 add_case_statement(stmt);
2185 return statement(token, &stmt->case_statement);
2188 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2190 token = parse_expression(token->next, &stmt->case_expression);
2191 if (match_op(token, SPECIAL_ELLIPSIS))
2192 token = parse_expression(token->next, &stmt->case_to);
2193 return case_statement(token, stmt);
2196 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2198 return case_statement(token->next, stmt);
2201 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2203 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2204 stmt->type = STMT_GOTO;
2205 stmt->goto_label = target;
2206 if (!target)
2207 sparse_error(stmt->pos, "break/continue not in iterator scope");
2208 return expect(token->next, ';', "at end of statement");
2211 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2213 stmt->type = STMT_SWITCH;
2214 start_switch(stmt);
2215 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2216 token = statement(token, &stmt->switch_statement);
2217 end_switch(stmt);
2218 return token;
2221 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2223 stmt->type = STMT_GOTO;
2224 token = token->next;
2225 if (match_op(token, '*')) {
2226 token = parse_expression(token->next, &stmt->goto_expression);
2227 add_statement(&function_computed_goto_list, stmt);
2228 } else if (token_type(token) == TOKEN_IDENT) {
2229 stmt->goto_label = label_symbol(token);
2230 token = token->next;
2231 } else {
2232 sparse_error(token->pos, "Expected identifier or goto expression");
2234 return expect(token, ';', "at end of statement");
2237 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2239 stmt->type = STMT_CONTEXT;
2240 token = parse_expression(token->next, &stmt->expression);
2241 if (stmt->expression->type == EXPR_PREOP
2242 && stmt->expression->op == '('
2243 && stmt->expression->unop->type == EXPR_COMMA) {
2244 struct expression *expr;
2245 expr = stmt->expression->unop;
2246 stmt->context = expr->left;
2247 stmt->expression = expr->right;
2249 return expect(token, ';', "at end of statement");
2252 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2254 stmt->type = STMT_RANGE;
2255 token = assignment_expression(token->next, &stmt->range_expression);
2256 token = expect(token, ',', "after range expression");
2257 token = assignment_expression(token, &stmt->range_low);
2258 token = expect(token, ',', "after low range");
2259 token = assignment_expression(token, &stmt->range_high);
2260 return expect(token, ';', "after range statement");
2263 static struct token *statement(struct token *token, struct statement **tree)
2265 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2267 *tree = stmt;
2268 if (token_type(token) == TOKEN_IDENT) {
2269 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2270 if (s && s->op->statement)
2271 return s->op->statement(token, stmt);
2273 if (match_op(token->next, ':')) {
2274 stmt->type = STMT_LABEL;
2275 stmt->label_identifier = label_symbol(token);
2276 token = skip_attributes(token->next->next);
2277 return statement(token, &stmt->label_statement);
2281 if (match_op(token, '{')) {
2282 stmt->type = STMT_COMPOUND;
2283 start_symbol_scope();
2284 token = compound_statement(token->next, stmt);
2285 end_symbol_scope();
2287 return expect(token, '}', "at end of compound statement");
2290 stmt->type = STMT_EXPRESSION;
2291 return expression_statement(token, &stmt->expression);
2294 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2295 static struct token *label_statement(struct token *token)
2297 while (token_type(token) == TOKEN_IDENT) {
2298 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2299 /* it's block-scope, but we want label namespace */
2300 bind_symbol(sym, token->ident, NS_SYMBOL);
2301 sym->namespace = NS_LABEL;
2302 fn_local_symbol(sym);
2303 token = token->next;
2304 if (!match_op(token, ','))
2305 break;
2306 token = token->next;
2308 return expect(token, ';', "at end of label declaration");
2311 static struct token * statement_list(struct token *token, struct statement_list **list)
2313 int seen_statement = 0;
2314 while (token_type(token) == TOKEN_IDENT &&
2315 token->ident == &__label___ident)
2316 token = label_statement(token->next);
2317 for (;;) {
2318 struct statement * stmt;
2319 if (eof_token(token))
2320 break;
2321 if (match_op(token, '}'))
2322 break;
2323 if (lookup_type(token)) {
2324 if (seen_statement) {
2325 warning(token->pos, "mixing declarations and code");
2326 seen_statement = 0;
2328 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2329 token = external_declaration(token, &stmt->declaration);
2330 } else {
2331 seen_statement = Wdeclarationafterstatement;
2332 token = statement(token, &stmt);
2334 add_statement(list, stmt);
2336 return token;
2339 static struct token *identifier_list(struct token *token, struct symbol *fn)
2341 struct symbol_list **list = &fn->arguments;
2342 for (;;) {
2343 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2344 sym->ident = token->ident;
2345 token = token->next;
2346 sym->endpos = token->pos;
2347 sym->ctype.base_type = &incomplete_ctype;
2348 add_symbol(list, sym);
2349 if (!match_op(token, ',') ||
2350 token_type(token->next) != TOKEN_IDENT ||
2351 lookup_type(token->next))
2352 break;
2353 token = token->next;
2355 return token;
2358 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2360 struct symbol_list **list = &fn->arguments;
2362 for (;;) {
2363 struct symbol *sym;
2365 if (match_op(token, SPECIAL_ELLIPSIS)) {
2366 fn->variadic = 1;
2367 token = token->next;
2368 break;
2371 sym = alloc_symbol(token->pos, SYM_NODE);
2372 token = parameter_declaration(token, sym);
2373 if (sym->ctype.base_type == &void_ctype) {
2374 /* Special case: (void) */
2375 if (!*list && !sym->ident)
2376 break;
2377 warning(token->pos, "void parameter");
2379 add_symbol(list, sym);
2380 if (!match_op(token, ','))
2381 break;
2382 token = token->next;
2384 return token;
2387 struct token *compound_statement(struct token *token, struct statement *stmt)
2389 token = statement_list(token, &stmt->stmts);
2390 return token;
2393 static struct expression *identifier_expression(struct token *token)
2395 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2396 expr->expr_ident = token->ident;
2397 return expr;
2400 static struct expression *index_expression(struct expression *from, struct expression *to)
2402 int idx_from, idx_to;
2403 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2405 idx_from = const_expression_value(from);
2406 idx_to = idx_from;
2407 if (to) {
2408 idx_to = const_expression_value(to);
2409 if (idx_to < idx_from || idx_from < 0)
2410 warning(from->pos, "nonsense array initializer index range");
2412 expr->idx_from = idx_from;
2413 expr->idx_to = idx_to;
2414 return expr;
2417 static struct token *single_initializer(struct expression **ep, struct token *token)
2419 int expect_equal = 0;
2420 struct token *next = token->next;
2421 struct expression **tail = ep;
2422 int nested;
2424 *ep = NULL;
2426 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2427 struct expression *expr = identifier_expression(token);
2428 if (Wold_initializer)
2429 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2430 token = initializer(&expr->ident_expression, next->next);
2431 if (expr->ident_expression)
2432 *ep = expr;
2433 return token;
2436 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2437 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2438 struct expression *expr = identifier_expression(next);
2439 *tail = expr;
2440 tail = &expr->ident_expression;
2441 expect_equal = 1;
2442 token = next->next;
2443 } else if (match_op(token, '[')) {
2444 struct expression *from = NULL, *to = NULL, *expr;
2445 token = constant_expression(token->next, &from);
2446 if (!from) {
2447 sparse_error(token->pos, "Expected constant expression");
2448 break;
2450 if (match_op(token, SPECIAL_ELLIPSIS))
2451 token = constant_expression(token->next, &to);
2452 expr = index_expression(from, to);
2453 *tail = expr;
2454 tail = &expr->idx_expression;
2455 token = expect(token, ']', "at end of initializer index");
2456 if (nested)
2457 expect_equal = 1;
2458 } else {
2459 break;
2462 if (nested && !expect_equal) {
2463 if (!match_op(token, '='))
2464 warning(token->pos, "obsolete array initializer, use C99 syntax");
2465 else
2466 expect_equal = 1;
2468 if (expect_equal)
2469 token = expect(token, '=', "at end of initializer index");
2471 token = initializer(tail, token);
2472 if (!*tail)
2473 *ep = NULL;
2474 return token;
2477 static struct token *initializer_list(struct expression_list **list, struct token *token)
2479 struct expression *expr;
2481 for (;;) {
2482 token = single_initializer(&expr, token);
2483 if (!expr)
2484 break;
2485 add_expression(list, expr);
2486 if (!match_op(token, ','))
2487 break;
2488 token = token->next;
2490 return token;
2493 struct token *initializer(struct expression **tree, struct token *token)
2495 if (match_op(token, '{')) {
2496 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2497 *tree = expr;
2498 token = initializer_list(&expr->expr_list, token->next);
2499 return expect(token, '}', "at end of initializer");
2501 return assignment_expression(token, tree);
2504 static void declare_argument(struct symbol *sym, struct symbol *fn)
2506 if (!sym->ident) {
2507 sparse_error(sym->pos, "no identifier for function argument");
2508 return;
2510 bind_symbol(sym, sym->ident, NS_SYMBOL);
2513 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2514 struct symbol_list **list)
2516 struct symbol_list **old_symbol_list;
2517 struct symbol *base_type = decl->ctype.base_type;
2518 struct statement *stmt, **p;
2519 struct symbol *prev;
2520 struct symbol *arg;
2522 old_symbol_list = function_symbol_list;
2523 if (decl->ctype.modifiers & MOD_INLINE) {
2524 function_symbol_list = &decl->inline_symbol_list;
2525 p = &base_type->inline_stmt;
2526 } else {
2527 function_symbol_list = &decl->symbol_list;
2528 p = &base_type->stmt;
2530 function_computed_target_list = NULL;
2531 function_computed_goto_list = NULL;
2533 if (decl->ctype.modifiers & MOD_EXTERN) {
2534 if (!(decl->ctype.modifiers & MOD_INLINE))
2535 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2537 if (!(decl->ctype.modifiers & MOD_STATIC))
2538 decl->ctype.modifiers |= MOD_EXTERN;
2540 stmt = start_function(decl);
2542 *p = stmt;
2543 FOR_EACH_PTR (base_type->arguments, arg) {
2544 declare_argument(arg, base_type);
2545 } END_FOR_EACH_PTR(arg);
2547 token = compound_statement(token->next, stmt);
2549 end_function(decl);
2550 if (!(decl->ctype.modifiers & MOD_INLINE))
2551 add_symbol(list, decl);
2552 check_declaration(decl);
2553 decl->definition = decl;
2554 prev = decl->same_symbol;
2555 if (prev && prev->definition) {
2556 warning(decl->pos, "multiple definitions for function '%s'",
2557 show_ident(decl->ident));
2558 info(prev->definition->pos, " the previous one is here");
2559 } else {
2560 while (prev) {
2561 prev->definition = decl;
2562 prev = prev->same_symbol;
2565 function_symbol_list = old_symbol_list;
2566 if (function_computed_goto_list) {
2567 if (!function_computed_target_list)
2568 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2569 else {
2570 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2571 stmt->target_list = function_computed_target_list;
2572 } END_FOR_EACH_PTR(stmt);
2575 return expect(token, '}', "at end of function");
2578 static void promote_k_r_types(struct symbol *arg)
2580 struct symbol *base = arg->ctype.base_type;
2581 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2582 arg->ctype.base_type = &int_ctype;
2586 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2588 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2589 struct symbol *arg;
2591 FOR_EACH_PTR(real_args, arg) {
2592 struct symbol *type;
2594 /* This is quadratic in the number of arguments. We _really_ don't care */
2595 FOR_EACH_PTR(argtypes, type) {
2596 if (type->ident == arg->ident)
2597 goto match;
2598 } END_FOR_EACH_PTR(type);
2599 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2600 continue;
2601 match:
2602 type->used = 1;
2603 /* "char" and "short" promote to "int" */
2604 promote_k_r_types(type);
2606 arg->ctype = type->ctype;
2607 } END_FOR_EACH_PTR(arg);
2609 FOR_EACH_PTR(argtypes, arg) {
2610 if (!arg->used)
2611 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2612 } END_FOR_EACH_PTR(arg);
2616 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2617 struct symbol_list **list)
2619 struct symbol_list *args = NULL;
2621 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2622 do {
2623 token = declaration_list(token, &args);
2624 if (!match_op(token, ';')) {
2625 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2626 break;
2628 token = token->next;
2629 } while (lookup_type(token));
2631 apply_k_r_types(args, decl);
2633 if (!match_op(token, '{')) {
2634 sparse_error(token->pos, "expected function body");
2635 return token;
2637 return parse_function_body(token, decl, list);
2640 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2642 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2643 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2644 struct statement *stmt;
2646 anon->ctype.base_type = fn;
2647 stmt = alloc_statement(token->pos, STMT_NONE);
2648 fn->stmt = stmt;
2650 token = parse_asm_statement(token, stmt);
2652 add_symbol(list, anon);
2653 return token;
2656 struct token *external_declaration(struct token *token, struct symbol_list **list)
2658 struct ident *ident = NULL;
2659 struct symbol *decl;
2660 struct decl_state ctx = { .ident = &ident };
2661 struct ctype saved;
2662 struct symbol *base_type;
2663 unsigned long mod;
2664 int is_typedef;
2666 /* Top-level inline asm? */
2667 if (token_type(token) == TOKEN_IDENT) {
2668 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2669 if (s && s->op->toplevel)
2670 return s->op->toplevel(token, list);
2673 /* Parse declaration-specifiers, if any */
2674 token = declaration_specifiers(token, &ctx);
2675 mod = storage_modifiers(&ctx);
2676 decl = alloc_symbol(token->pos, SYM_NODE);
2677 /* Just a type declaration? */
2678 if (match_op(token, ';')) {
2679 apply_modifiers(token->pos, &ctx);
2680 return token->next;
2683 saved = ctx.ctype;
2684 token = declarator(token, &ctx);
2685 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2686 apply_modifiers(token->pos, &ctx);
2688 decl->ctype = ctx.ctype;
2689 decl->ctype.modifiers |= mod;
2690 decl->endpos = token->pos;
2692 /* Just a type declaration? */
2693 if (!ident) {
2694 warning(token->pos, "missing identifier in declaration");
2695 return expect(token, ';', "at the end of type declaration");
2698 /* type define declaration? */
2699 is_typedef = ctx.storage_class == STypedef;
2701 /* Typedefs don't have meaningful storage */
2702 if (is_typedef)
2703 decl->ctype.modifiers |= MOD_USERTYPE;
2705 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2707 base_type = decl->ctype.base_type;
2709 if (is_typedef) {
2710 if (base_type && !base_type->ident) {
2711 switch (base_type->type) {
2712 case SYM_STRUCT:
2713 case SYM_UNION:
2714 case SYM_ENUM:
2715 case SYM_RESTRICT:
2716 base_type->ident = ident;
2719 } else if (base_type && base_type->type == SYM_FN) {
2720 /* K&R argument declaration? */
2721 if (lookup_type(token))
2722 return parse_k_r_arguments(token, decl, list);
2723 if (match_op(token, '{'))
2724 return parse_function_body(token, decl, list);
2726 if (!(decl->ctype.modifiers & MOD_STATIC))
2727 decl->ctype.modifiers |= MOD_EXTERN;
2728 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2729 sparse_error(token->pos, "void declaration");
2732 for (;;) {
2733 if (!is_typedef && match_op(token, '=')) {
2734 if (decl->ctype.modifiers & MOD_EXTERN) {
2735 warning(decl->pos, "symbol with external linkage has initializer");
2736 decl->ctype.modifiers &= ~MOD_EXTERN;
2738 token = initializer(&decl->initializer, token->next);
2740 if (!is_typedef) {
2741 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2742 add_symbol(list, decl);
2743 fn_local_symbol(decl);
2746 check_declaration(decl);
2747 if (decl->same_symbol)
2748 decl->definition = decl->same_symbol->definition;
2750 if (!match_op(token, ','))
2751 break;
2753 token = token->next;
2754 ident = NULL;
2755 decl = alloc_symbol(token->pos, SYM_NODE);
2756 ctx.ctype = saved;
2757 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2758 token = declarator(token, &ctx);
2759 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2760 apply_modifiers(token->pos, &ctx);
2761 decl->ctype = ctx.ctype;
2762 decl->ctype.modifiers |= mod;
2763 decl->endpos = token->pos;
2764 if (!ident) {
2765 sparse_error(token->pos, "expected identifier name in type definition");
2766 return token;
2769 if (is_typedef)
2770 decl->ctype.modifiers |= MOD_USERTYPE;
2772 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2774 /* Function declarations are automatically extern unless specifically static */
2775 base_type = decl->ctype.base_type;
2776 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2777 if (!(decl->ctype.modifiers & MOD_STATIC))
2778 decl->ctype.modifiers |= MOD_EXTERN;
2781 return expect(token, ';', "at end of declaration");