Avoid "attribute 'warning': unknown attribute" warning
[smatch.git] / parse.c
blobe5ad8677b711d2185e23e0a1f2841870093099e0
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_transparent_union, ignore_attribute,
68 attribute_mode, attribute_force;
70 typedef struct symbol *to_mode_t(struct symbol *);
72 static to_mode_t
73 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_word_mode;
75 enum {
76 Set_T = 1,
77 Set_S = 2,
78 Set_Char = 4,
79 Set_Int = 8,
80 Set_Double = 16,
81 Set_Float = 32,
82 Set_Signed = 64,
83 Set_Unsigned = 128,
84 Set_Short = 256,
85 Set_Long = 512,
86 Set_Vlong = 1024,
87 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
90 enum {
91 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar
94 enum {
95 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced
98 static struct symbol_op typedef_op = {
99 .type = KW_MODIFIER,
100 .declarator = typedef_specifier,
103 static struct symbol_op inline_op = {
104 .type = KW_MODIFIER,
105 .declarator = inline_specifier,
108 static struct symbol_op auto_op = {
109 .type = KW_MODIFIER,
110 .declarator = auto_specifier,
113 static struct symbol_op register_op = {
114 .type = KW_MODIFIER,
115 .declarator = register_specifier,
118 static struct symbol_op static_op = {
119 .type = KW_MODIFIER,
120 .declarator = static_specifier,
123 static struct symbol_op extern_op = {
124 .type = KW_MODIFIER,
125 .declarator = extern_specifier,
128 static struct symbol_op thread_op = {
129 .type = KW_MODIFIER,
130 .declarator = thread_specifier,
133 static struct symbol_op const_op = {
134 .type = KW_QUALIFIER,
135 .declarator = const_qualifier,
138 static struct symbol_op volatile_op = {
139 .type = KW_QUALIFIER,
140 .declarator = volatile_qualifier,
143 static struct symbol_op restrict_op = {
144 .type = KW_QUALIFIER,
147 static struct symbol_op typeof_op = {
148 .type = KW_SPECIFIER,
149 .declarator = typeof_specifier,
150 .test = Set_Any,
151 .set = Set_S|Set_T,
154 static struct symbol_op attribute_op = {
155 .type = KW_ATTRIBUTE,
156 .declarator = attribute_specifier,
159 static struct symbol_op struct_op = {
160 .type = KW_SPECIFIER,
161 .declarator = struct_specifier,
162 .test = Set_Any,
163 .set = Set_S|Set_T,
166 static struct symbol_op union_op = {
167 .type = KW_SPECIFIER,
168 .declarator = union_specifier,
169 .test = Set_Any,
170 .set = Set_S|Set_T,
173 static struct symbol_op enum_op = {
174 .type = KW_SPECIFIER,
175 .declarator = enum_specifier,
176 .test = Set_Any,
177 .set = Set_S|Set_T,
180 static struct symbol_op spec_op = {
181 .type = KW_SPECIFIER | KW_EXACT,
182 .test = Set_Any,
183 .set = Set_S|Set_T,
186 static struct symbol_op char_op = {
187 .type = KW_SPECIFIER,
188 .test = Set_T|Set_Long|Set_Short,
189 .set = Set_T|Set_Char,
190 .class = CChar,
193 static struct symbol_op int_op = {
194 .type = KW_SPECIFIER,
195 .test = Set_T,
196 .set = Set_T|Set_Int,
199 static struct symbol_op double_op = {
200 .type = KW_SPECIFIER,
201 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
202 .set = Set_T|Set_Double,
203 .class = CReal,
206 static struct symbol_op float_op = {
207 .type = KW_SPECIFIER | KW_SHORT,
208 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
209 .set = Set_T|Set_Float,
210 .class = CReal,
213 static struct symbol_op short_op = {
214 .type = KW_SPECIFIER | KW_SHORT,
215 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
216 .set = Set_Short,
219 static struct symbol_op signed_op = {
220 .type = KW_SPECIFIER,
221 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
222 .set = Set_Signed,
223 .class = CSInt,
226 static struct symbol_op unsigned_op = {
227 .type = KW_SPECIFIER,
228 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
229 .set = Set_Unsigned,
230 .class = CUInt,
233 static struct symbol_op long_op = {
234 .type = KW_SPECIFIER | KW_LONG,
235 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
236 .set = Set_Long,
239 static struct symbol_op if_op = {
240 .statement = parse_if_statement,
243 static struct symbol_op return_op = {
244 .statement = parse_return_statement,
247 static struct symbol_op loop_iter_op = {
248 .statement = parse_loop_iterator,
251 static struct symbol_op default_op = {
252 .statement = parse_default_statement,
255 static struct symbol_op case_op = {
256 .statement = parse_case_statement,
259 static struct symbol_op switch_op = {
260 .statement = parse_switch_statement,
263 static struct symbol_op for_op = {
264 .statement = parse_for_statement,
267 static struct symbol_op while_op = {
268 .statement = parse_while_statement,
271 static struct symbol_op do_op = {
272 .statement = parse_do_statement,
275 static struct symbol_op goto_op = {
276 .statement = parse_goto_statement,
279 static struct symbol_op __context___op = {
280 .statement = parse_context_statement,
283 static struct symbol_op range_op = {
284 .statement = parse_range_statement,
287 static struct symbol_op asm_op = {
288 .type = KW_ASM,
289 .declarator = parse_asm_declarator,
290 .statement = parse_asm_statement,
291 .toplevel = toplevel_asm_declaration,
294 static struct symbol_op packed_op = {
295 .attribute = attribute_packed,
298 static struct symbol_op aligned_op = {
299 .attribute = attribute_aligned,
302 static struct symbol_op attr_mod_op = {
303 .attribute = attribute_modifier,
306 static struct symbol_op attr_force_op = {
307 .attribute = attribute_force,
310 static struct symbol_op address_space_op = {
311 .attribute = attribute_address_space,
314 static struct symbol_op mode_op = {
315 .attribute = attribute_mode,
318 static struct symbol_op context_op = {
319 .attribute = attribute_context,
322 static struct symbol_op transparent_union_op = {
323 .attribute = attribute_transparent_union,
326 static struct symbol_op ignore_attr_op = {
327 .attribute = ignore_attribute,
330 static struct symbol_op mode_QI_op = {
331 .type = KW_MODE,
332 .to_mode = to_QI_mode
335 static struct symbol_op mode_HI_op = {
336 .type = KW_MODE,
337 .to_mode = to_HI_mode
340 static struct symbol_op mode_SI_op = {
341 .type = KW_MODE,
342 .to_mode = to_SI_mode
345 static struct symbol_op mode_DI_op = {
346 .type = KW_MODE,
347 .to_mode = to_DI_mode
350 static struct symbol_op mode_word_op = {
351 .type = KW_MODE,
352 .to_mode = to_word_mode
355 static struct init_keyword {
356 const char *name;
357 enum namespace ns;
358 unsigned long modifiers;
359 struct symbol_op *op;
360 struct symbol *type;
361 } keyword_table[] = {
362 /* Type qualifiers */
363 { "const", NS_TYPEDEF, .op = &const_op },
364 { "__const", NS_TYPEDEF, .op = &const_op },
365 { "__const__", NS_TYPEDEF, .op = &const_op },
366 { "volatile", NS_TYPEDEF, .op = &volatile_op },
367 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
368 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
370 /* Typedef.. */
371 { "typedef", NS_TYPEDEF, .op = &typedef_op },
373 /* Type specifiers */
374 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
375 { "char", NS_TYPEDEF, .op = &char_op },
376 { "short", NS_TYPEDEF, .op = &short_op },
377 { "int", NS_TYPEDEF, .op = &int_op },
378 { "long", NS_TYPEDEF, .op = &long_op },
379 { "float", NS_TYPEDEF, .op = &float_op },
380 { "double", NS_TYPEDEF, .op = &double_op },
381 { "signed", NS_TYPEDEF, .op = &signed_op },
382 { "__signed", NS_TYPEDEF, .op = &signed_op },
383 { "__signed__", NS_TYPEDEF, .op = &signed_op },
384 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
385 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
387 /* Predeclared types */
388 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
390 /* Extended types */
391 { "typeof", NS_TYPEDEF, .op = &typeof_op },
392 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
393 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
395 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
396 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
398 { "struct", NS_TYPEDEF, .op = &struct_op },
399 { "union", NS_TYPEDEF, .op = &union_op },
400 { "enum", NS_TYPEDEF, .op = &enum_op },
402 { "inline", NS_TYPEDEF, .op = &inline_op },
403 { "__inline", NS_TYPEDEF, .op = &inline_op },
404 { "__inline__", NS_TYPEDEF, .op = &inline_op },
406 /* Ignored for now.. */
407 { "restrict", NS_TYPEDEF, .op = &restrict_op},
408 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
410 /* Storage class */
411 { "auto", NS_TYPEDEF, .op = &auto_op },
412 { "register", NS_TYPEDEF, .op = &register_op },
413 { "static", NS_TYPEDEF, .op = &static_op },
414 { "extern", NS_TYPEDEF, .op = &extern_op },
415 { "__thread", NS_TYPEDEF, .op = &thread_op },
417 /* Statement */
418 { "if", NS_KEYWORD, .op = &if_op },
419 { "return", NS_KEYWORD, .op = &return_op },
420 { "break", NS_KEYWORD, .op = &loop_iter_op },
421 { "continue", NS_KEYWORD, .op = &loop_iter_op },
422 { "default", NS_KEYWORD, .op = &default_op },
423 { "case", NS_KEYWORD, .op = &case_op },
424 { "switch", NS_KEYWORD, .op = &switch_op },
425 { "for", NS_KEYWORD, .op = &for_op },
426 { "while", NS_KEYWORD, .op = &while_op },
427 { "do", NS_KEYWORD, .op = &do_op },
428 { "goto", NS_KEYWORD, .op = &goto_op },
429 { "__context__",NS_KEYWORD, .op = &__context___op },
430 { "__range__", NS_KEYWORD, .op = &range_op },
431 { "asm", NS_KEYWORD, .op = &asm_op },
432 { "__asm", NS_KEYWORD, .op = &asm_op },
433 { "__asm__", NS_KEYWORD, .op = &asm_op },
435 /* Attribute */
436 { "packed", NS_KEYWORD, .op = &packed_op },
437 { "__packed__", NS_KEYWORD, .op = &packed_op },
438 { "aligned", NS_KEYWORD, .op = &aligned_op },
439 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
440 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
441 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
442 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
443 { "force", NS_KEYWORD, .op = &attr_force_op },
444 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
445 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
446 { "address_space",NS_KEYWORD, .op = &address_space_op },
447 { "mode", NS_KEYWORD, .op = &mode_op },
448 { "context", NS_KEYWORD, .op = &context_op },
449 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
451 { "__mode__", NS_KEYWORD, .op = &mode_op },
452 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
453 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
454 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
455 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
456 { "SI", NS_KEYWORD, .op = &mode_SI_op },
457 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
458 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
459 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
460 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
461 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
463 /* Ignored attributes */
464 { "nothrow", NS_KEYWORD, .op = &ignore_attr_op },
465 { "__nothrow", NS_KEYWORD, .op = &ignore_attr_op },
466 { "__nothrow__", NS_KEYWORD, .op = &ignore_attr_op },
467 { "malloc", NS_KEYWORD, .op = &ignore_attr_op },
468 { "__malloc__", NS_KEYWORD, .op = &ignore_attr_op },
469 { "nonnull", NS_KEYWORD, .op = &ignore_attr_op },
470 { "__nonnull", NS_KEYWORD, .op = &ignore_attr_op },
471 { "__nonnull__", NS_KEYWORD, .op = &ignore_attr_op },
472 { "format", NS_KEYWORD, .op = &ignore_attr_op },
473 { "__format__", NS_KEYWORD, .op = &ignore_attr_op },
474 { "format_arg", NS_KEYWORD, .op = &ignore_attr_op },
475 { "__format_arg__", NS_KEYWORD, .op = &ignore_attr_op },
476 { "section", NS_KEYWORD, .op = &ignore_attr_op },
477 { "__section__",NS_KEYWORD, .op = &ignore_attr_op },
478 { "unused", NS_KEYWORD, .op = &ignore_attr_op },
479 { "__unused__", NS_KEYWORD, .op = &ignore_attr_op },
480 { "const", NS_KEYWORD, .op = &ignore_attr_op },
481 { "__const", NS_KEYWORD, .op = &ignore_attr_op },
482 { "__const__", NS_KEYWORD, .op = &ignore_attr_op },
483 { "noreturn", NS_KEYWORD, .op = &ignore_attr_op },
484 { "__noreturn__", NS_KEYWORD, .op = &ignore_attr_op },
485 { "no_instrument_function", NS_KEYWORD, .op = &ignore_attr_op },
486 { "__no_instrument_function__", NS_KEYWORD, .op = &ignore_attr_op },
487 { "sentinel", NS_KEYWORD, .op = &ignore_attr_op },
488 { "__sentinel__", NS_KEYWORD, .op = &ignore_attr_op },
489 { "regparm", NS_KEYWORD, .op = &ignore_attr_op },
490 { "__regparm__", NS_KEYWORD, .op = &ignore_attr_op },
491 { "weak", NS_KEYWORD, .op = &ignore_attr_op },
492 { "__weak__", NS_KEYWORD, .op = &ignore_attr_op },
493 { "alias", NS_KEYWORD, .op = &ignore_attr_op },
494 { "__alias__", NS_KEYWORD, .op = &ignore_attr_op },
495 { "pure", NS_KEYWORD, .op = &ignore_attr_op },
496 { "__pure__", NS_KEYWORD, .op = &ignore_attr_op },
497 { "always_inline", NS_KEYWORD, .op = &ignore_attr_op },
498 { "__always_inline__", NS_KEYWORD, .op = &ignore_attr_op },
499 { "syscall_linkage", NS_KEYWORD, .op = &ignore_attr_op },
500 { "__syscall_linkage__", NS_KEYWORD, .op = &ignore_attr_op },
501 { "visibility", NS_KEYWORD, .op = &ignore_attr_op },
502 { "__visibility__", NS_KEYWORD, .op = &ignore_attr_op },
503 { "deprecated", NS_KEYWORD, .op = &ignore_attr_op },
504 { "__deprecated__", NS_KEYWORD, .op = &ignore_attr_op },
505 { "noinline", NS_KEYWORD, .op = &ignore_attr_op },
506 { "__noinline__", NS_KEYWORD, .op = &ignore_attr_op },
507 { "used", NS_KEYWORD, .op = &ignore_attr_op },
508 { "__used__", NS_KEYWORD, .op = &ignore_attr_op },
509 { "warn_unused_result", NS_KEYWORD, .op = &ignore_attr_op },
510 { "__warn_unused_result__", NS_KEYWORD, .op = &ignore_attr_op },
511 { "model", NS_KEYWORD, .op = &ignore_attr_op },
512 { "__model__", NS_KEYWORD, .op = &ignore_attr_op },
513 { "cdecl", NS_KEYWORD, .op = &ignore_attr_op },
514 { "__cdecl__", NS_KEYWORD, .op = &ignore_attr_op },
515 { "stdcall", NS_KEYWORD, .op = &ignore_attr_op },
516 { "__stdcall__", NS_KEYWORD, .op = &ignore_attr_op },
517 { "fastcall", NS_KEYWORD, .op = &ignore_attr_op },
518 { "__fastcall__", NS_KEYWORD, .op = &ignore_attr_op },
519 { "dllimport", NS_KEYWORD, .op = &ignore_attr_op },
520 { "__dllimport__", NS_KEYWORD, .op = &ignore_attr_op },
521 { "dllexport", NS_KEYWORD, .op = &ignore_attr_op },
522 { "__dllexport__", NS_KEYWORD, .op = &ignore_attr_op },
523 { "constructor", NS_KEYWORD, .op = &ignore_attr_op },
524 { "__constructor__", NS_KEYWORD, .op = &ignore_attr_op },
525 { "destructor", NS_KEYWORD, .op = &ignore_attr_op },
526 { "__destructor__", NS_KEYWORD, .op = &ignore_attr_op },
527 { "cold", NS_KEYWORD, .op = &ignore_attr_op },
528 { "__cold__", NS_KEYWORD, .op = &ignore_attr_op },
529 { "hot", NS_KEYWORD, .op = &ignore_attr_op },
530 { "__hot__", NS_KEYWORD, .op = &ignore_attr_op },
531 { "warning", NS_KEYWORD, .op = &ignore_attr_op },
532 { "__warning__", NS_KEYWORD, .op = &ignore_attr_op },
535 void init_parser(int stream)
537 int i;
538 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
539 struct init_keyword *ptr = keyword_table + i;
540 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
541 sym->ident->keyword = 1;
542 if (ptr->ns == NS_TYPEDEF)
543 sym->ident->reserved = 1;
544 sym->ctype.modifiers = ptr->modifiers;
545 sym->ctype.base_type = ptr->type;
546 sym->op = ptr->op;
550 // Add a symbol to the list of function-local symbols
551 static void fn_local_symbol(struct symbol *sym)
553 if (function_symbol_list)
554 add_symbol(function_symbol_list, sym);
557 static int SENTINEL_ATTR match_idents(struct token *token, ...)
559 va_list args;
560 struct ident * next;
562 if (token_type(token) != TOKEN_IDENT)
563 return 0;
565 va_start(args, token);
566 do {
567 next = va_arg(args, struct ident *);
568 } while (next && token->ident != next);
569 va_end(args);
571 return next && token->ident == next;
575 struct statement *alloc_statement(struct position pos, int type)
577 struct statement *stmt = __alloc_statement(0);
578 stmt->type = type;
579 stmt->pos = pos;
580 return stmt;
583 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
585 static void apply_modifiers(struct position pos, struct decl_state *ctx)
587 struct symbol *ctype;
588 if (!ctx->mode)
589 return;
590 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
591 if (!ctype)
592 sparse_error(pos, "don't know how to apply mode to %s",
593 show_typename(ctx->ctype.base_type));
594 else
595 ctx->ctype.base_type = ctype;
599 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
601 struct symbol *sym = alloc_symbol(pos, type);
603 sym->ctype.base_type = ctype->base_type;
604 sym->ctype.modifiers = ctype->modifiers;
606 ctype->base_type = sym;
607 ctype->modifiers = 0;
608 return sym;
612 * NOTE! NS_LABEL is not just a different namespace,
613 * it also ends up using function scope instead of the
614 * regular symbol scope.
616 struct symbol *label_symbol(struct token *token)
618 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
619 if (!sym) {
620 sym = alloc_symbol(token->pos, SYM_LABEL);
621 bind_symbol(sym, token->ident, NS_LABEL);
622 fn_local_symbol(sym);
624 return sym;
627 static struct token *struct_union_enum_specifier(enum type type,
628 struct token *token, struct decl_state *ctx,
629 struct token *(*parse)(struct token *, struct symbol *))
631 struct symbol *sym;
632 struct position *repos;
634 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
635 if (token_type(token) == TOKEN_IDENT) {
636 sym = lookup_symbol(token->ident, NS_STRUCT);
637 if (!sym ||
638 (is_outer_scope(sym->scope) &&
639 (match_op(token->next,';') || match_op(token->next,'{')))) {
640 // Either a new symbol, or else an out-of-scope
641 // symbol being redefined.
642 sym = alloc_symbol(token->pos, type);
643 bind_symbol(sym, token->ident, NS_STRUCT);
645 if (sym->type != type)
646 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
647 ctx->ctype.base_type = sym;
648 repos = &token->pos;
649 token = token->next;
650 if (match_op(token, '{')) {
651 // The following test is actually wrong for empty
652 // structs, but (1) they are not C99, (2) gcc does
653 // the same thing, and (3) it's easier.
654 if (sym->symbol_list)
655 error_die(token->pos, "redefinition of %s", show_typename (sym));
656 sym->pos = *repos;
657 token = parse(token->next, sym);
658 token = expect(token, '}', "at end of struct-union-enum-specifier");
660 // Mark the structure as needing re-examination
661 sym->examined = 0;
662 sym->endpos = token->pos;
664 return token;
667 // private struct/union/enum type
668 if (!match_op(token, '{')) {
669 sparse_error(token->pos, "expected declaration");
670 ctx->ctype.base_type = &bad_ctype;
671 return token;
674 sym = alloc_symbol(token->pos, type);
675 token = parse(token->next, sym);
676 ctx->ctype.base_type = sym;
677 token = expect(token, '}', "at end of specifier");
678 sym->endpos = token->pos;
680 return token;
683 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
685 struct symbol *field, *last = NULL;
686 struct token *res;
687 res = struct_declaration_list(token, &sym->symbol_list);
688 FOR_EACH_PTR(sym->symbol_list, field) {
689 if (!field->ident) {
690 struct symbol *base = field->ctype.base_type;
691 if (base && base->type == SYM_BITFIELD)
692 continue;
694 if (last)
695 last->next_subobject = field;
696 last = field;
697 } END_FOR_EACH_PTR(field);
698 return res;
701 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
703 return struct_declaration_list(token, &sym->symbol_list);
706 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
708 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
711 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
713 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
717 typedef struct {
718 int x;
719 unsigned long long y;
720 } Num;
722 static void upper_boundary(Num *n, Num *v)
724 if (n->x > v->x)
725 return;
726 if (n->x < v->x) {
727 *n = *v;
728 return;
730 if (n->y < v->y)
731 n->y = v->y;
734 static void lower_boundary(Num *n, Num *v)
736 if (n->x < v->x)
737 return;
738 if (n->x > v->x) {
739 *n = *v;
740 return;
742 if (n->y > v->y)
743 n->y = v->y;
746 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
748 int shift = type->bit_size;
749 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
751 if (!is_unsigned)
752 shift--;
753 if (upper->x == 0 && upper->y >> shift)
754 return 0;
755 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
756 return 1;
757 return 0;
760 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
762 if (s1->bit_size < s2->bit_size) {
763 s1 = s2;
764 } else if (s1->bit_size == s2->bit_size) {
765 if (s2->ctype.modifiers & MOD_UNSIGNED)
766 s1 = s2;
768 if (s1->bit_size < bits_in_int)
769 return &int_ctype;
770 return s1;
773 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
775 struct symbol *sym;
777 FOR_EACH_PTR(list, sym) {
778 struct expression *expr = sym->initializer;
779 struct symbol *ctype;
780 if (expr->type != EXPR_VALUE)
781 continue;
782 ctype = expr->ctype;
783 if (ctype->bit_size == base_type->bit_size)
784 continue;
785 cast_value(expr, base_type, expr, ctype);
786 } END_FOR_EACH_PTR(sym);
789 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
791 unsigned long long lastval = 0;
792 struct symbol *ctype = NULL, *base_type = NULL;
793 Num upper = {-1, 0}, lower = {1, 0};
795 parent->examined = 1;
796 parent->ctype.base_type = &int_ctype;
797 while (token_type(token) == TOKEN_IDENT) {
798 struct expression *expr = NULL;
799 struct token *next = token->next;
800 struct symbol *sym;
802 if (match_op(next, '=')) {
803 next = constant_expression(next->next, &expr);
804 lastval = get_expression_value(expr);
805 ctype = &void_ctype;
806 if (expr && expr->ctype)
807 ctype = expr->ctype;
808 } else if (!ctype) {
809 ctype = &int_ctype;
810 } else if (is_int_type(ctype)) {
811 lastval++;
812 } else {
813 error_die(token->pos, "can't increment the last enum member");
816 if (!expr) {
817 expr = alloc_expression(token->pos, EXPR_VALUE);
818 expr->value = lastval;
819 expr->ctype = ctype;
822 sym = alloc_symbol(token->pos, SYM_NODE);
823 bind_symbol(sym, token->ident, NS_SYMBOL);
824 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
825 sym->initializer = expr;
826 sym->enum_member = 1;
827 sym->ctype.base_type = parent;
828 add_ptr_list(&parent->symbol_list, sym);
830 if (base_type != &bad_ctype) {
831 if (ctype->type == SYM_NODE)
832 ctype = ctype->ctype.base_type;
833 if (ctype->type == SYM_ENUM) {
834 if (ctype == parent)
835 ctype = base_type;
836 else
837 ctype = ctype->ctype.base_type;
840 * base_type rules:
841 * - if all enums are of the same type, then
842 * the base_type is that type (two first
843 * cases)
844 * - if enums are of different types, they
845 * all have to be integer types, and the
846 * base type is at least "int_ctype".
847 * - otherwise the base_type is "bad_ctype".
849 if (!base_type) {
850 base_type = ctype;
851 } else if (ctype == base_type) {
852 /* nothing */
853 } else if (is_int_type(base_type) && is_int_type(ctype)) {
854 base_type = bigger_enum_type(base_type, ctype);
855 } else
856 base_type = &bad_ctype;
857 parent->ctype.base_type = base_type;
859 if (is_int_type(base_type)) {
860 Num v = {.y = lastval};
861 if (ctype->ctype.modifiers & MOD_UNSIGNED)
862 v.x = 0;
863 else if ((long long)lastval >= 0)
864 v.x = 0;
865 else
866 v.x = -1;
867 upper_boundary(&upper, &v);
868 lower_boundary(&lower, &v);
870 token = next;
872 sym->endpos = token->pos;
874 if (!match_op(token, ','))
875 break;
876 token = token->next;
878 if (!base_type) {
879 sparse_error(token->pos, "bad enum definition");
880 base_type = &bad_ctype;
882 else if (!is_int_type(base_type))
883 base_type = base_type;
884 else if (type_is_ok(base_type, &upper, &lower))
885 base_type = base_type;
886 else if (type_is_ok(&int_ctype, &upper, &lower))
887 base_type = &int_ctype;
888 else if (type_is_ok(&uint_ctype, &upper, &lower))
889 base_type = &uint_ctype;
890 else if (type_is_ok(&long_ctype, &upper, &lower))
891 base_type = &long_ctype;
892 else if (type_is_ok(&ulong_ctype, &upper, &lower))
893 base_type = &ulong_ctype;
894 else if (type_is_ok(&llong_ctype, &upper, &lower))
895 base_type = &llong_ctype;
896 else if (type_is_ok(&ullong_ctype, &upper, &lower))
897 base_type = &ullong_ctype;
898 else
899 base_type = &bad_ctype;
900 parent->ctype.base_type = base_type;
901 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
902 parent->examined = 0;
904 cast_enum_list(parent->symbol_list, base_type);
906 return token;
909 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
911 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
912 struct ctype *ctype = &ctx->ctype.base_type->ctype;
914 if (!ctype->base_type)
915 ctype->base_type = &incomplete_ctype;
917 return ret;
920 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
922 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
924 struct symbol *sym;
926 if (!match_op(token, '(')) {
927 sparse_error(token->pos, "expected '(' after typeof");
928 return token;
930 if (lookup_type(token->next)) {
931 token = typename(token->next, &sym, NULL);
932 ctx->ctype.base_type = sym->ctype.base_type;
933 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
934 } else {
935 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
936 token = parse_expression(token->next, &typeof_sym->initializer);
938 typeof_sym->endpos = token->pos;
939 if (!typeof_sym->initializer) {
940 sparse_error(token->pos, "expected expression after the '(' token");
941 typeof_sym = &bad_ctype;
943 ctx->ctype.base_type = typeof_sym;
945 return expect(token, ')', "after typeof");
948 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
950 struct expression *expr = NULL;
951 if (match_op(token, '('))
952 token = parens_expression(token, &expr, "in attribute");
953 return token;
956 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
958 if (!ctx->ctype.alignment)
959 ctx->ctype.alignment = 1;
960 return token;
963 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
965 int alignment = max_alignment;
966 struct expression *expr = NULL;
968 if (match_op(token, '(')) {
969 token = parens_expression(token, &expr, "in attribute");
970 if (expr)
971 alignment = const_expression_value(expr);
973 if (alignment & (alignment-1)) {
974 warning(token->pos, "I don't like non-power-of-2 alignments");
975 return token;
976 } else if (alignment > ctx->ctype.alignment)
977 ctx->ctype.alignment = alignment;
978 return token;
981 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
983 if (ctx->modifiers & qual)
984 warning(*pos, "duplicate %s", modifier_string(qual));
985 ctx->modifiers |= qual;
988 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
990 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
991 return token;
994 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
996 struct expression *expr = NULL;
997 int as;
998 token = expect(token, '(', "after address_space attribute");
999 token = conditional_expression(token, &expr);
1000 if (expr) {
1001 as = const_expression_value(expr);
1002 if (Waddress_space && as)
1003 ctx->ctype.as = as;
1005 token = expect(token, ')', "after address_space attribute");
1006 return token;
1009 static struct symbol *to_QI_mode(struct symbol *ctype)
1011 if (ctype->ctype.base_type != &int_type)
1012 return NULL;
1013 if (ctype == &char_ctype)
1014 return ctype;
1015 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1016 : &schar_ctype;
1019 static struct symbol *to_HI_mode(struct symbol *ctype)
1021 if (ctype->ctype.base_type != &int_type)
1022 return NULL;
1023 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1024 : &sshort_ctype;
1027 static struct symbol *to_SI_mode(struct symbol *ctype)
1029 if (ctype->ctype.base_type != &int_type)
1030 return NULL;
1031 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1032 : &sint_ctype;
1035 static struct symbol *to_DI_mode(struct symbol *ctype)
1037 if (ctype->ctype.base_type != &int_type)
1038 return NULL;
1039 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1040 : &sllong_ctype;
1043 static struct symbol *to_word_mode(struct symbol *ctype)
1045 if (ctype->ctype.base_type != &int_type)
1046 return NULL;
1047 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1048 : &slong_ctype;
1051 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1053 token = expect(token, '(', "after mode attribute");
1054 if (token_type(token) == TOKEN_IDENT) {
1055 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1056 if (mode && mode->op->type == KW_MODE)
1057 ctx->mode = mode->op;
1058 else
1059 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1060 token = token->next;
1061 } else
1062 sparse_error(token->pos, "expect attribute mode symbol\n");
1063 token = expect(token, ')', "after mode attribute");
1064 return token;
1067 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1069 struct context *context = alloc_context();
1070 struct expression *args[3];
1071 int argc = 0;
1073 token = expect(token, '(', "after context attribute");
1074 while (!match_op(token, ')')) {
1075 struct expression *expr = NULL;
1076 token = conditional_expression(token, &expr);
1077 if (!expr)
1078 break;
1079 if (argc < 3)
1080 args[argc++] = expr;
1081 if (!match_op(token, ','))
1082 break;
1083 token = token->next;
1086 switch(argc) {
1087 case 0:
1088 sparse_error(token->pos, "expected context input/output values");
1089 break;
1090 case 1:
1091 context->in = get_expression_value(args[0]);
1092 break;
1093 case 2:
1094 context->in = get_expression_value(args[0]);
1095 context->out = get_expression_value(args[1]);
1096 break;
1097 case 3:
1098 context->context = args[0];
1099 context->in = get_expression_value(args[1]);
1100 context->out = get_expression_value(args[2]);
1101 break;
1104 if (argc)
1105 add_ptr_list(&ctx->ctype.contexts, context);
1107 token = expect(token, ')', "after context attribute");
1108 return token;
1111 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1113 if (Wtransparent_union)
1114 warning(token->pos, "ignoring attribute __transparent_union__");
1115 return token;
1118 static struct token *recover_unknown_attribute(struct token *token)
1120 struct expression *expr = NULL;
1122 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1123 token = token->next;
1124 if (match_op(token, '('))
1125 token = parens_expression(token, &expr, "in attribute");
1126 return token;
1129 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1131 token = expect(token, '(', "after attribute");
1132 token = expect(token, '(', "after attribute");
1134 for (;;) {
1135 struct ident *attribute_name;
1136 struct symbol *attr;
1138 if (eof_token(token))
1139 break;
1140 if (match_op(token, ';'))
1141 break;
1142 if (token_type(token) != TOKEN_IDENT)
1143 break;
1144 attribute_name = token->ident;
1145 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1146 if (attr && attr->op->attribute)
1147 token = attr->op->attribute(token->next, attr, ctx);
1148 else
1149 token = recover_unknown_attribute(token);
1151 if (!match_op(token, ','))
1152 break;
1153 token = token->next;
1156 token = expect(token, ')', "after attribute");
1157 token = expect(token, ')', "after attribute");
1158 return token;
1161 static const char *storage_class[] =
1163 [STypedef] = "typedef",
1164 [SAuto] = "auto",
1165 [SExtern] = "extern",
1166 [SStatic] = "static",
1167 [SRegister] = "register",
1168 [SForced] = "[force]"
1171 static unsigned long storage_modifiers(struct decl_state *ctx)
1173 static unsigned long mod[] =
1175 [SAuto] = MOD_AUTO,
1176 [SExtern] = MOD_EXTERN,
1177 [SStatic] = MOD_STATIC,
1178 [SRegister] = MOD_REGISTER
1180 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1181 | (ctx->is_tls ? MOD_TLS : 0);
1184 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1186 /* __thread can be used alone, or with extern or static */
1187 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1188 sparse_error(*pos, "__thread can only be used alone, or with "
1189 "extern or static");
1190 return;
1193 if (!ctx->storage_class) {
1194 ctx->storage_class = class;
1195 return;
1197 if (ctx->storage_class == class)
1198 sparse_error(*pos, "duplicate %s", storage_class[class]);
1199 else
1200 sparse_error(*pos, "multiple storage classes");
1203 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1205 set_storage_class(&next->pos, ctx, STypedef);
1206 return next;
1209 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1211 set_storage_class(&next->pos, ctx, SAuto);
1212 return next;
1215 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1217 set_storage_class(&next->pos, ctx, SRegister);
1218 return next;
1221 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1223 set_storage_class(&next->pos, ctx, SStatic);
1224 return next;
1227 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1229 set_storage_class(&next->pos, ctx, SExtern);
1230 return next;
1233 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1235 /* This GCC extension can be used alone, or with extern or static */
1236 if (!ctx->storage_class || ctx->storage_class == SStatic
1237 || ctx->storage_class == SExtern) {
1238 ctx->is_tls = 1;
1239 } else {
1240 sparse_error(next->pos, "__thread can only be used alone, or "
1241 "with extern or static");
1244 return next;
1247 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1249 set_storage_class(&token->pos, ctx, SForced);
1250 return token;
1253 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1255 ctx->is_inline = 1;
1256 return next;
1259 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1261 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1262 return next;
1265 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1267 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1268 return next;
1271 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1273 unsigned long mod = thistype->modifiers;
1275 if (mod)
1276 apply_qualifier(&pos, ctype, mod);
1278 /* Context */
1279 concat_ptr_list((struct ptr_list *)thistype->contexts,
1280 (struct ptr_list **)&ctype->contexts);
1282 /* Alignment */
1283 if (thistype->alignment > ctype->alignment)
1284 ctype->alignment = thistype->alignment;
1286 /* Address space */
1287 if (thistype->as)
1288 ctype->as = thistype->as;
1291 static void specifier_conflict(struct position pos, int what, struct ident *new)
1293 const char *old;
1294 if (what & (Set_S | Set_T))
1295 goto Catch_all;
1296 if (what & Set_Char)
1297 old = "char";
1298 else if (what & Set_Double)
1299 old = "double";
1300 else if (what & Set_Float)
1301 old = "float";
1302 else if (what & Set_Signed)
1303 old = "signed";
1304 else if (what & Set_Unsigned)
1305 old = "unsigned";
1306 else if (what & Set_Short)
1307 old = "short";
1308 else if (what & Set_Long)
1309 old = "long";
1310 else
1311 old = "long long";
1312 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1313 old, show_ident(new));
1314 return;
1316 Catch_all:
1317 sparse_error(pos, "two or more data types in declaration specifiers");
1320 static struct symbol * const int_types[] =
1321 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1322 static struct symbol * const signed_types[] =
1323 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype};
1324 static struct symbol * const unsigned_types[] =
1325 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype};
1326 static struct symbol * const real_types[] =
1327 {&float_ctype, &double_ctype, &ldouble_ctype};
1328 static struct symbol * const char_types[] =
1329 {&char_ctype, &schar_ctype, &uchar_ctype};
1330 static struct symbol * const * const types[] = {
1331 int_types + 1, signed_types + 1, unsigned_types + 1,
1332 real_types + 1, char_types, char_types + 1, char_types + 2
1335 struct symbol *ctype_integer(int size, int want_unsigned)
1337 return types[want_unsigned ? CUInt : CInt][size];
1340 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1342 while (token_type(t) == TOKEN_IDENT) {
1343 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1344 if (!s)
1345 break;
1346 if (s->type != SYM_KEYWORD)
1347 break;
1348 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1349 break;
1350 t = t->next;
1351 if (s->op->declarator)
1352 t = s->op->declarator(t, ctx);
1354 return t;
1357 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1359 int seen = 0;
1360 int class = CInt;
1361 int size = 0;
1363 while (token_type(token) == TOKEN_IDENT) {
1364 struct symbol *s = lookup_symbol(token->ident,
1365 NS_TYPEDEF | NS_SYMBOL);
1366 if (!s || !(s->namespace & NS_TYPEDEF))
1367 break;
1368 if (s->type != SYM_KEYWORD) {
1369 if (seen & Set_Any)
1370 break;
1371 seen |= Set_S | Set_T;
1372 ctx->ctype.base_type = s->ctype.base_type;
1373 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1374 token = token->next;
1375 continue;
1377 if (s->op->type & KW_SPECIFIER) {
1378 if (seen & s->op->test) {
1379 specifier_conflict(token->pos,
1380 seen & s->op->test,
1381 token->ident);
1382 break;
1384 seen |= s->op->set;
1385 class += s->op->class;
1386 if (s->op->type & KW_SHORT) {
1387 size = -1;
1388 } else if (s->op->type & KW_LONG && size++) {
1389 if (class == CReal) {
1390 specifier_conflict(token->pos,
1391 Set_Vlong,
1392 &double_ident);
1393 break;
1395 seen |= Set_Vlong;
1398 token = token->next;
1399 if (s->op->declarator)
1400 token = s->op->declarator(token, ctx);
1401 if (s->op->type & KW_EXACT) {
1402 ctx->ctype.base_type = s->ctype.base_type;
1403 ctx->ctype.modifiers |= s->ctype.modifiers;
1407 if (!(seen & Set_S)) { /* not set explicitly? */
1408 struct symbol *base = &incomplete_ctype;
1409 if (seen & Set_Any)
1410 base = types[class][size];
1411 ctx->ctype.base_type = base;
1414 if (ctx->ctype.modifiers & MOD_BITWISE) {
1415 struct symbol *type;
1416 ctx->ctype.modifiers &= ~MOD_BITWISE;
1417 if (!is_int_type(ctx->ctype.base_type)) {
1418 sparse_error(token->pos, "invalid modifier");
1419 return token;
1421 type = alloc_symbol(token->pos, SYM_BASETYPE);
1422 *type = *ctx->ctype.base_type;
1423 type->ctype.modifiers &= ~MOD_SPECIFIER;
1424 type->ctype.base_type = ctx->ctype.base_type;
1425 type->type = SYM_RESTRICT;
1426 ctx->ctype.base_type = type;
1427 create_fouled(type);
1429 return token;
1432 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1434 struct expression *expr = NULL;
1436 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1437 token = token->next;
1438 token = parse_expression(token, &expr);
1439 sym->array_size = expr;
1440 return token;
1443 static struct token *parameter_type_list(struct token *, struct symbol *);
1444 static struct token *identifier_list(struct token *, struct symbol *);
1445 static struct token *declarator(struct token *token, struct decl_state *ctx);
1447 static struct token *skip_attribute(struct token *token)
1449 token = token->next;
1450 if (match_op(token, '(')) {
1451 int depth = 1;
1452 token = token->next;
1453 while (depth && !eof_token(token)) {
1454 if (token_type(token) == TOKEN_SPECIAL) {
1455 if (token->special == '(')
1456 depth++;
1457 else if (token->special == ')')
1458 depth--;
1460 token = token->next;
1463 return token;
1466 static struct token *skip_attributes(struct token *token)
1468 struct symbol *keyword;
1469 for (;;) {
1470 if (token_type(token) != TOKEN_IDENT)
1471 break;
1472 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1473 if (!keyword || keyword->type != SYM_KEYWORD)
1474 break;
1475 if (!(keyword->op->type & KW_ATTRIBUTE))
1476 break;
1477 token = expect(token->next, '(', "after attribute");
1478 token = expect(token, '(', "after attribute");
1479 for (;;) {
1480 if (eof_token(token))
1481 break;
1482 if (match_op(token, ';'))
1483 break;
1484 if (token_type(token) != TOKEN_IDENT)
1485 break;
1486 token = skip_attribute(token);
1487 if (!match_op(token, ','))
1488 break;
1489 token = token->next;
1491 token = expect(token, ')', "after attribute");
1492 token = expect(token, ')', "after attribute");
1494 return token;
1497 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1499 struct symbol *keyword;
1500 for (;;) {
1501 if (token_type(token) != TOKEN_IDENT)
1502 break;
1503 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1504 if (!keyword || keyword->type != SYM_KEYWORD)
1505 break;
1506 if (!(keyword->op->type & keywords))
1507 break;
1508 token = keyword->op->declarator(token->next, ctx);
1509 keywords &= KW_ATTRIBUTE;
1511 return token;
1514 static int is_nested(struct token *token, struct token **p,
1515 int prefer_abstract)
1518 * This can be either a parameter list or a grouping.
1519 * For the direct (non-abstract) case, we know if must be
1520 * a parameter list if we already saw the identifier.
1521 * For the abstract case, we know if must be a parameter
1522 * list if it is empty or starts with a type.
1524 struct token *next = token->next;
1526 *p = next = skip_attributes(next);
1528 if (token_type(next) == TOKEN_IDENT) {
1529 if (lookup_type(next))
1530 return !prefer_abstract;
1531 return 1;
1534 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1535 return 0;
1537 return 1;
1540 enum kind {
1541 Empty, K_R, Proto, Bad_Func,
1544 static enum kind which_func(struct token *token,
1545 struct ident **n,
1546 int prefer_abstract)
1548 struct token *next = token->next;
1550 if (token_type(next) == TOKEN_IDENT) {
1551 if (lookup_type(next))
1552 return Proto;
1553 /* identifier list not in definition; complain */
1554 if (prefer_abstract)
1555 warning(token->pos,
1556 "identifier list not in definition");
1557 return K_R;
1560 if (token_type(next) != TOKEN_SPECIAL)
1561 return Bad_Func;
1563 if (next->special == ')') {
1564 /* don't complain about those */
1565 if (!n || match_op(next->next, ';'))
1566 return Empty;
1567 warning(next->pos,
1568 "non-ANSI function declaration of function '%s'",
1569 show_ident(*n));
1570 return Empty;
1573 if (next->special == SPECIAL_ELLIPSIS) {
1574 warning(next->pos,
1575 "variadic functions must have one named argument");
1576 return Proto;
1579 return Bad_Func;
1582 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1584 struct ctype *ctype = &ctx->ctype;
1585 struct token *next;
1586 struct ident **p = ctx->ident;
1588 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1589 *ctx->ident = token->ident;
1590 token = token->next;
1591 } else if (match_op(token, '(') &&
1592 is_nested(token, &next, ctx->prefer_abstract)) {
1593 struct symbol *base_type = ctype->base_type;
1594 if (token->next != next)
1595 next = handle_attributes(token->next, ctx,
1596 KW_ATTRIBUTE);
1597 token = declarator(next, ctx);
1598 token = expect(token, ')', "in nested declarator");
1599 while (ctype->base_type != base_type)
1600 ctype = &ctype->base_type->ctype;
1601 p = NULL;
1604 if (match_op(token, '(')) {
1605 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1606 struct symbol *fn;
1607 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1608 token = token->next;
1609 if (kind == K_R)
1610 token = identifier_list(token, fn);
1611 else if (kind == Proto)
1612 token = parameter_type_list(token, fn);
1613 token = expect(token, ')', "in function declarator");
1614 fn->endpos = token->pos;
1615 return token;
1618 while (match_op(token, '[')) {
1619 struct symbol *array;
1620 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1621 token = abstract_array_declarator(token->next, array);
1622 token = expect(token, ']', "in abstract_array_declarator");
1623 array->endpos = token->pos;
1624 ctype = &array->ctype;
1626 return token;
1629 static struct token *pointer(struct token *token, struct decl_state *ctx)
1631 while (match_op(token,'*')) {
1632 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1633 ptr->ctype.modifiers = ctx->ctype.modifiers;
1634 ptr->ctype.base_type = ctx->ctype.base_type;
1635 ptr->ctype.as = ctx->ctype.as;
1636 ptr->ctype.contexts = ctx->ctype.contexts;
1637 ctx->ctype.modifiers = 0;
1638 ctx->ctype.base_type = ptr;
1639 ctx->ctype.as = 0;
1640 ctx->ctype.contexts = NULL;
1642 token = handle_qualifiers(token->next, ctx);
1643 ctx->ctype.base_type->endpos = token->pos;
1645 return token;
1648 static struct token *declarator(struct token *token, struct decl_state *ctx)
1650 token = pointer(token, ctx);
1651 return direct_declarator(token, ctx);
1654 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1656 struct ctype *ctype = &ctx->ctype;
1657 struct expression *expr;
1658 struct symbol *bitfield;
1659 long long width;
1661 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1662 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1663 show_typename(ctype->base_type));
1664 // Parse this to recover gracefully.
1665 return conditional_expression(token->next, &expr);
1668 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1669 token = conditional_expression(token->next, &expr);
1670 width = const_expression_value(expr);
1671 bitfield->bit_size = width;
1673 if (width < 0 || width > INT_MAX) {
1674 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1675 width = -1;
1676 } else if (*ctx->ident && width == 0) {
1677 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1678 show_ident(*ctx->ident));
1679 width = -1;
1680 } else if (*ctx->ident) {
1681 struct symbol *base_type = bitfield->ctype.base_type;
1682 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1683 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1684 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1685 // Valid values are either {-1;0} or {0}, depending on integer
1686 // representation. The latter makes for very efficient code...
1687 sparse_error(token->pos, "dubious one-bit signed bitfield");
1689 if (Wdefault_bitfield_sign &&
1690 bitfield_type->type != SYM_ENUM &&
1691 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1692 is_signed) {
1693 // The sign of bitfields is unspecified by default.
1694 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1697 bitfield->bit_size = width;
1698 bitfield->endpos = token->pos;
1699 return token;
1702 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1704 struct decl_state ctx = {.prefer_abstract = 0};
1705 struct ctype saved;
1706 unsigned long mod;
1708 token = declaration_specifiers(token, &ctx);
1709 mod = storage_modifiers(&ctx);
1710 saved = ctx.ctype;
1711 for (;;) {
1712 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1713 ctx.ident = &decl->ident;
1715 token = declarator(token, &ctx);
1716 if (match_op(token, ':'))
1717 token = handle_bitfield(token, &ctx);
1719 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1720 apply_modifiers(token->pos, &ctx);
1722 decl->ctype = ctx.ctype;
1723 decl->ctype.modifiers |= mod;
1724 decl->endpos = token->pos;
1725 add_symbol(list, decl);
1726 if (!match_op(token, ','))
1727 break;
1728 token = token->next;
1729 ctx.ctype = saved;
1731 return token;
1734 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1736 while (!match_op(token, '}')) {
1737 if (!match_op(token, ';'))
1738 token = declaration_list(token, list);
1739 if (!match_op(token, ';')) {
1740 sparse_error(token->pos, "expected ; at end of declaration");
1741 break;
1743 token = token->next;
1745 return token;
1748 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1750 struct decl_state ctx = {.prefer_abstract = 1};
1752 token = declaration_specifiers(token, &ctx);
1753 ctx.ident = &sym->ident;
1754 token = declarator(token, &ctx);
1755 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1756 apply_modifiers(token->pos, &ctx);
1757 sym->ctype = ctx.ctype;
1758 sym->ctype.modifiers |= storage_modifiers(&ctx);
1759 sym->endpos = token->pos;
1760 return token;
1763 struct token *typename(struct token *token, struct symbol **p, int *forced)
1765 struct decl_state ctx = {.prefer_abstract = 1};
1766 int class;
1767 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1768 *p = sym;
1769 token = declaration_specifiers(token, &ctx);
1770 token = declarator(token, &ctx);
1771 apply_modifiers(token->pos, &ctx);
1772 sym->ctype = ctx.ctype;
1773 sym->endpos = token->pos;
1774 class = ctx.storage_class;
1775 if (forced) {
1776 *forced = 0;
1777 if (class == SForced) {
1778 *forced = 1;
1779 class = 0;
1782 if (class)
1783 warning(sym->pos, "storage class in typename (%s %s)",
1784 storage_class[class], show_typename(sym));
1785 return token;
1788 static struct token *expression_statement(struct token *token, struct expression **tree)
1790 token = parse_expression(token, tree);
1791 return expect(token, ';', "at end of statement");
1794 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1795 struct expression_list **inout)
1797 struct expression *expr;
1799 /* Allow empty operands */
1800 if (match_op(token->next, ':') || match_op(token->next, ')'))
1801 return token->next;
1802 do {
1803 struct ident *ident = NULL;
1804 if (match_op(token->next, '[') &&
1805 token_type(token->next->next) == TOKEN_IDENT &&
1806 match_op(token->next->next->next, ']')) {
1807 ident = token->next->next->ident;
1808 token = token->next->next->next;
1810 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1811 token = primary_expression(token->next, &expr);
1812 add_expression(inout, expr);
1813 token = parens_expression(token, &expr, "in asm parameter");
1814 add_expression(inout, expr);
1815 } while (match_op(token, ','));
1816 return token;
1819 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1820 struct expression_list **clobbers)
1822 struct expression *expr;
1824 do {
1825 token = primary_expression(token->next, &expr);
1826 add_expression(clobbers, expr);
1827 } while (match_op(token, ','));
1828 return token;
1831 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1833 token = token->next;
1834 stmt->type = STMT_ASM;
1835 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1836 token = token->next;
1838 token = expect(token, '(', "after asm");
1839 token = parse_expression(token, &stmt->asm_string);
1840 if (match_op(token, ':'))
1841 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1842 if (match_op(token, ':'))
1843 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1844 if (match_op(token, ':'))
1845 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1846 token = expect(token, ')', "after asm");
1847 return expect(token, ';', "at end of asm-statement");
1850 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1852 struct expression *expr;
1853 token = expect(token, '(', "after asm");
1854 token = parse_expression(token->next, &expr);
1855 token = expect(token, ')', "after asm");
1856 return token;
1859 /* Make a statement out of an expression */
1860 static struct statement *make_statement(struct expression *expr)
1862 struct statement *stmt;
1864 if (!expr)
1865 return NULL;
1866 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1867 stmt->expression = expr;
1868 return stmt;
1872 * All iterators have two symbols associated with them:
1873 * the "continue" and "break" symbols, which are targets
1874 * for continue and break statements respectively.
1876 * They are in a special name-space, but they follow
1877 * all the normal visibility rules, so nested iterators
1878 * automatically work right.
1880 static void start_iterator(struct statement *stmt)
1882 struct symbol *cont, *brk;
1884 start_symbol_scope();
1885 cont = alloc_symbol(stmt->pos, SYM_NODE);
1886 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1887 brk = alloc_symbol(stmt->pos, SYM_NODE);
1888 bind_symbol(brk, &break_ident, NS_ITERATOR);
1890 stmt->type = STMT_ITERATOR;
1891 stmt->iterator_break = brk;
1892 stmt->iterator_continue = cont;
1893 fn_local_symbol(brk);
1894 fn_local_symbol(cont);
1897 static void end_iterator(struct statement *stmt)
1899 end_symbol_scope();
1902 static struct statement *start_function(struct symbol *sym)
1904 struct symbol *ret;
1905 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1907 start_function_scope();
1908 ret = alloc_symbol(sym->pos, SYM_NODE);
1909 ret->ctype = sym->ctype.base_type->ctype;
1910 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1911 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1912 bind_symbol(ret, &return_ident, NS_ITERATOR);
1913 stmt->ret = ret;
1914 fn_local_symbol(ret);
1916 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1917 current_fn = sym;
1919 return stmt;
1922 static void end_function(struct symbol *sym)
1924 current_fn = NULL;
1925 end_function_scope();
1929 * A "switch()" statement, like an iterator, has a
1930 * the "break" symbol associated with it. It works
1931 * exactly like the iterator break - it's the target
1932 * for any break-statements in scope, and means that
1933 * "break" handling doesn't even need to know whether
1934 * it's breaking out of an iterator or a switch.
1936 * In addition, the "case" symbol is a marker for the
1937 * case/default statements to find the switch statement
1938 * that they are associated with.
1940 static void start_switch(struct statement *stmt)
1942 struct symbol *brk, *switch_case;
1944 start_symbol_scope();
1945 brk = alloc_symbol(stmt->pos, SYM_NODE);
1946 bind_symbol(brk, &break_ident, NS_ITERATOR);
1948 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1949 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1950 switch_case->stmt = stmt;
1952 stmt->type = STMT_SWITCH;
1953 stmt->switch_break = brk;
1954 stmt->switch_case = switch_case;
1956 fn_local_symbol(brk);
1957 fn_local_symbol(switch_case);
1960 static void end_switch(struct statement *stmt)
1962 if (!stmt->switch_case->symbol_list)
1963 warning(stmt->pos, "switch with no cases");
1964 end_symbol_scope();
1967 static void add_case_statement(struct statement *stmt)
1969 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1970 struct symbol *sym;
1972 if (!target) {
1973 sparse_error(stmt->pos, "not in switch scope");
1974 stmt->type = STMT_NONE;
1975 return;
1977 sym = alloc_symbol(stmt->pos, SYM_NODE);
1978 add_symbol(&target->symbol_list, sym);
1979 sym->stmt = stmt;
1980 stmt->case_label = sym;
1981 fn_local_symbol(sym);
1984 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1986 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1988 if (!target)
1989 error_die(token->pos, "internal error: return without a function target");
1990 stmt->type = STMT_RETURN;
1991 stmt->ret_target = target;
1992 return expression_statement(token->next, &stmt->ret_value);
1995 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1997 struct symbol_list *syms;
1998 struct expression *e1, *e2, *e3;
1999 struct statement *iterator;
2001 start_iterator(stmt);
2002 token = expect(token->next, '(', "after 'for'");
2004 syms = NULL;
2005 e1 = NULL;
2006 /* C99 variable declaration? */
2007 if (lookup_type(token)) {
2008 token = external_declaration(token, &syms);
2009 } else {
2010 token = parse_expression(token, &e1);
2011 token = expect(token, ';', "in 'for'");
2013 token = parse_expression(token, &e2);
2014 token = expect(token, ';', "in 'for'");
2015 token = parse_expression(token, &e3);
2016 token = expect(token, ')', "in 'for'");
2017 token = statement(token, &iterator);
2019 stmt->iterator_syms = syms;
2020 stmt->iterator_pre_statement = make_statement(e1);
2021 stmt->iterator_pre_condition = e2;
2022 stmt->iterator_post_statement = make_statement(e3);
2023 stmt->iterator_post_condition = NULL;
2024 stmt->iterator_statement = iterator;
2025 end_iterator(stmt);
2027 return token;
2030 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2032 struct expression *expr;
2033 struct statement *iterator;
2035 start_iterator(stmt);
2036 token = parens_expression(token->next, &expr, "after 'while'");
2037 token = statement(token, &iterator);
2039 stmt->iterator_pre_condition = expr;
2040 stmt->iterator_post_condition = NULL;
2041 stmt->iterator_statement = iterator;
2042 end_iterator(stmt);
2044 return token;
2047 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2049 struct expression *expr;
2050 struct statement *iterator;
2052 start_iterator(stmt);
2053 token = statement(token->next, &iterator);
2054 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2055 token = token->next;
2056 else
2057 sparse_error(token->pos, "expected 'while' after 'do'");
2058 token = parens_expression(token, &expr, "after 'do-while'");
2060 stmt->iterator_post_condition = expr;
2061 stmt->iterator_statement = iterator;
2062 end_iterator(stmt);
2064 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2065 warning(iterator->pos, "do-while statement is not a compound statement");
2067 return expect(token, ';', "after statement");
2070 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2072 stmt->type = STMT_IF;
2073 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2074 token = statement(token, &stmt->if_true);
2075 if (token_type(token) != TOKEN_IDENT)
2076 return token;
2077 if (token->ident != &else_ident)
2078 return token;
2079 return statement(token->next, &stmt->if_false);
2082 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2084 stmt->type = STMT_CASE;
2085 token = expect(token, ':', "after default/case");
2086 add_case_statement(stmt);
2087 return statement(token, &stmt->case_statement);
2090 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2092 token = parse_expression(token->next, &stmt->case_expression);
2093 if (match_op(token, SPECIAL_ELLIPSIS))
2094 token = parse_expression(token->next, &stmt->case_to);
2095 return case_statement(token, stmt);
2098 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2100 return case_statement(token->next, stmt);
2103 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2105 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2106 stmt->type = STMT_GOTO;
2107 stmt->goto_label = target;
2108 if (!target)
2109 sparse_error(stmt->pos, "break/continue not in iterator scope");
2110 return expect(token->next, ';', "at end of statement");
2113 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2115 stmt->type = STMT_SWITCH;
2116 start_switch(stmt);
2117 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2118 token = statement(token, &stmt->switch_statement);
2119 end_switch(stmt);
2120 return token;
2123 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2125 stmt->type = STMT_GOTO;
2126 token = token->next;
2127 if (match_op(token, '*')) {
2128 token = parse_expression(token->next, &stmt->goto_expression);
2129 add_statement(&function_computed_goto_list, stmt);
2130 } else if (token_type(token) == TOKEN_IDENT) {
2131 stmt->goto_label = label_symbol(token);
2132 token = token->next;
2133 } else {
2134 sparse_error(token->pos, "Expected identifier or goto expression");
2136 return expect(token, ';', "at end of statement");
2139 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2141 stmt->type = STMT_CONTEXT;
2142 token = parse_expression(token->next, &stmt->expression);
2143 if(stmt->expression->type == EXPR_PREOP
2144 && stmt->expression->op == '('
2145 && stmt->expression->unop->type == EXPR_COMMA) {
2146 struct expression *expr;
2147 expr = stmt->expression->unop;
2148 stmt->context = expr->left;
2149 stmt->expression = expr->right;
2151 return expect(token, ';', "at end of statement");
2154 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2156 stmt->type = STMT_RANGE;
2157 token = assignment_expression(token->next, &stmt->range_expression);
2158 token = expect(token, ',', "after range expression");
2159 token = assignment_expression(token, &stmt->range_low);
2160 token = expect(token, ',', "after low range");
2161 token = assignment_expression(token, &stmt->range_high);
2162 return expect(token, ';', "after range statement");
2165 static struct token *statement(struct token *token, struct statement **tree)
2167 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2169 *tree = stmt;
2170 if (token_type(token) == TOKEN_IDENT) {
2171 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2172 if (s && s->op->statement)
2173 return s->op->statement(token, stmt);
2175 if (match_op(token->next, ':')) {
2176 stmt->type = STMT_LABEL;
2177 stmt->label_identifier = label_symbol(token);
2178 token = skip_attributes(token->next->next);
2179 return statement(token, &stmt->label_statement);
2183 if (match_op(token, '{')) {
2184 stmt->type = STMT_COMPOUND;
2185 start_symbol_scope();
2186 token = compound_statement(token->next, stmt);
2187 end_symbol_scope();
2189 return expect(token, '}', "at end of compound statement");
2192 stmt->type = STMT_EXPRESSION;
2193 return expression_statement(token, &stmt->expression);
2196 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2197 static struct token *label_statement(struct token *token)
2199 while (token_type(token) == TOKEN_IDENT) {
2200 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2201 /* it's block-scope, but we want label namespace */
2202 bind_symbol(sym, token->ident, NS_SYMBOL);
2203 sym->namespace = NS_LABEL;
2204 fn_local_symbol(sym);
2205 token = token->next;
2206 if (!match_op(token, ','))
2207 break;
2208 token = token->next;
2210 return expect(token, ';', "at end of label declaration");
2213 static struct token * statement_list(struct token *token, struct statement_list **list)
2215 int seen_statement = 0;
2216 while (token_type(token) == TOKEN_IDENT &&
2217 token->ident == &__label___ident)
2218 token = label_statement(token->next);
2219 for (;;) {
2220 struct statement * stmt;
2221 if (eof_token(token))
2222 break;
2223 if (match_op(token, '}'))
2224 break;
2225 if (lookup_type(token)) {
2226 if (seen_statement) {
2227 warning(token->pos, "mixing declarations and code");
2228 seen_statement = 0;
2230 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2231 token = external_declaration(token, &stmt->declaration);
2232 } else {
2233 seen_statement = Wdeclarationafterstatement;
2234 token = statement(token, &stmt);
2236 add_statement(list, stmt);
2238 return token;
2241 static struct token *identifier_list(struct token *token, struct symbol *fn)
2243 struct symbol_list **list = &fn->arguments;
2244 for (;;) {
2245 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2246 sym->ident = token->ident;
2247 token = token->next;
2248 sym->endpos = token->pos;
2249 sym->ctype.base_type = &incomplete_ctype;
2250 add_symbol(list, sym);
2251 if (!match_op(token, ',') ||
2252 token_type(token->next) != TOKEN_IDENT ||
2253 lookup_type(token->next))
2254 break;
2255 token = token->next;
2257 return token;
2260 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2262 struct symbol_list **list = &fn->arguments;
2264 for (;;) {
2265 struct symbol *sym;
2267 if (match_op(token, SPECIAL_ELLIPSIS)) {
2268 fn->variadic = 1;
2269 token = token->next;
2270 break;
2273 sym = alloc_symbol(token->pos, SYM_NODE);
2274 token = parameter_declaration(token, sym);
2275 if (sym->ctype.base_type == &void_ctype) {
2276 /* Special case: (void) */
2277 if (!*list && !sym->ident)
2278 break;
2279 warning(token->pos, "void parameter");
2281 add_symbol(list, sym);
2282 if (!match_op(token, ','))
2283 break;
2284 token = token->next;
2286 return token;
2289 struct token *compound_statement(struct token *token, struct statement *stmt)
2291 token = statement_list(token, &stmt->stmts);
2292 return token;
2295 static struct expression *identifier_expression(struct token *token)
2297 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2298 expr->expr_ident = token->ident;
2299 return expr;
2302 static struct expression *index_expression(struct expression *from, struct expression *to)
2304 int idx_from, idx_to;
2305 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2307 idx_from = const_expression_value(from);
2308 idx_to = idx_from;
2309 if (to) {
2310 idx_to = const_expression_value(to);
2311 if (idx_to < idx_from || idx_from < 0)
2312 warning(from->pos, "nonsense array initializer index range");
2314 expr->idx_from = idx_from;
2315 expr->idx_to = idx_to;
2316 return expr;
2319 static struct token *single_initializer(struct expression **ep, struct token *token)
2321 int expect_equal = 0;
2322 struct token *next = token->next;
2323 struct expression **tail = ep;
2324 int nested;
2326 *ep = NULL;
2328 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2329 struct expression *expr = identifier_expression(token);
2330 if (Wold_initializer)
2331 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2332 token = initializer(&expr->ident_expression, next->next);
2333 if (expr->ident_expression)
2334 *ep = expr;
2335 return token;
2338 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2339 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2340 struct expression *expr = identifier_expression(next);
2341 *tail = expr;
2342 tail = &expr->ident_expression;
2343 expect_equal = 1;
2344 token = next->next;
2345 } else if (match_op(token, '[')) {
2346 struct expression *from = NULL, *to = NULL, *expr;
2347 token = constant_expression(token->next, &from);
2348 if (!from) {
2349 sparse_error(token->pos, "Expected constant expression");
2350 break;
2352 if (match_op(token, SPECIAL_ELLIPSIS))
2353 token = constant_expression(token->next, &to);
2354 expr = index_expression(from, to);
2355 *tail = expr;
2356 tail = &expr->idx_expression;
2357 token = expect(token, ']', "at end of initializer index");
2358 if (nested)
2359 expect_equal = 1;
2360 } else {
2361 break;
2364 if (nested && !expect_equal) {
2365 if (!match_op(token, '='))
2366 warning(token->pos, "obsolete array initializer, use C99 syntax");
2367 else
2368 expect_equal = 1;
2370 if (expect_equal)
2371 token = expect(token, '=', "at end of initializer index");
2373 token = initializer(tail, token);
2374 if (!*tail)
2375 *ep = NULL;
2376 return token;
2379 static struct token *initializer_list(struct expression_list **list, struct token *token)
2381 struct expression *expr;
2383 for (;;) {
2384 token = single_initializer(&expr, token);
2385 if (!expr)
2386 break;
2387 add_expression(list, expr);
2388 if (!match_op(token, ','))
2389 break;
2390 token = token->next;
2392 return token;
2395 struct token *initializer(struct expression **tree, struct token *token)
2397 if (match_op(token, '{')) {
2398 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2399 *tree = expr;
2400 token = initializer_list(&expr->expr_list, token->next);
2401 return expect(token, '}', "at end of initializer");
2403 return assignment_expression(token, tree);
2406 static void declare_argument(struct symbol *sym, struct symbol *fn)
2408 if (!sym->ident) {
2409 sparse_error(sym->pos, "no identifier for function argument");
2410 return;
2412 bind_symbol(sym, sym->ident, NS_SYMBOL);
2415 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2416 struct symbol_list **list)
2418 struct symbol_list **old_symbol_list;
2419 struct symbol *base_type = decl->ctype.base_type;
2420 struct statement *stmt, **p;
2421 struct symbol *prev;
2422 struct symbol *arg;
2424 old_symbol_list = function_symbol_list;
2425 if (decl->ctype.modifiers & MOD_INLINE) {
2426 function_symbol_list = &decl->inline_symbol_list;
2427 p = &base_type->inline_stmt;
2428 } else {
2429 function_symbol_list = &decl->symbol_list;
2430 p = &base_type->stmt;
2432 function_computed_target_list = NULL;
2433 function_computed_goto_list = NULL;
2435 if (decl->ctype.modifiers & MOD_EXTERN) {
2436 if (!(decl->ctype.modifiers & MOD_INLINE))
2437 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2439 if (!(decl->ctype.modifiers & MOD_STATIC))
2440 decl->ctype.modifiers |= MOD_EXTERN;
2442 stmt = start_function(decl);
2444 *p = stmt;
2445 FOR_EACH_PTR (base_type->arguments, arg) {
2446 declare_argument(arg, base_type);
2447 } END_FOR_EACH_PTR(arg);
2449 token = compound_statement(token->next, stmt);
2451 end_function(decl);
2452 if (!(decl->ctype.modifiers & MOD_INLINE))
2453 add_symbol(list, decl);
2454 check_declaration(decl);
2455 decl->definition = decl;
2456 prev = decl->same_symbol;
2457 if (prev && prev->definition) {
2458 warning(decl->pos, "multiple definitions for function '%s'",
2459 show_ident(decl->ident));
2460 info(prev->definition->pos, " the previous one is here");
2461 } else {
2462 while (prev) {
2463 prev->definition = decl;
2464 prev = prev->same_symbol;
2467 function_symbol_list = old_symbol_list;
2468 if (function_computed_goto_list) {
2469 if (!function_computed_target_list)
2470 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2471 else {
2472 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2473 stmt->target_list = function_computed_target_list;
2474 } END_FOR_EACH_PTR(stmt);
2477 return expect(token, '}', "at end of function");
2480 static void promote_k_r_types(struct symbol *arg)
2482 struct symbol *base = arg->ctype.base_type;
2483 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2484 arg->ctype.base_type = &int_ctype;
2488 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2490 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2491 struct symbol *arg;
2493 FOR_EACH_PTR(real_args, arg) {
2494 struct symbol *type;
2496 /* This is quadratic in the number of arguments. We _really_ don't care */
2497 FOR_EACH_PTR(argtypes, type) {
2498 if (type->ident == arg->ident)
2499 goto match;
2500 } END_FOR_EACH_PTR(type);
2501 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2502 continue;
2503 match:
2504 type->used = 1;
2505 /* "char" and "short" promote to "int" */
2506 promote_k_r_types(type);
2508 arg->ctype = type->ctype;
2509 } END_FOR_EACH_PTR(arg);
2511 FOR_EACH_PTR(argtypes, arg) {
2512 if (!arg->used)
2513 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2514 } END_FOR_EACH_PTR(arg);
2518 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2519 struct symbol_list **list)
2521 struct symbol_list *args = NULL;
2523 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2524 do {
2525 token = declaration_list(token, &args);
2526 if (!match_op(token, ';')) {
2527 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2528 break;
2530 token = token->next;
2531 } while (lookup_type(token));
2533 apply_k_r_types(args, decl);
2535 if (!match_op(token, '{')) {
2536 sparse_error(token->pos, "expected function body");
2537 return token;
2539 return parse_function_body(token, decl, list);
2542 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2544 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2545 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2546 struct statement *stmt;
2548 anon->ctype.base_type = fn;
2549 stmt = alloc_statement(token->pos, STMT_NONE);
2550 fn->stmt = stmt;
2552 token = parse_asm_statement(token, stmt);
2554 add_symbol(list, anon);
2555 return token;
2558 struct token *external_declaration(struct token *token, struct symbol_list **list)
2560 struct ident *ident = NULL;
2561 struct symbol *decl;
2562 struct decl_state ctx = { .ident = &ident };
2563 struct ctype saved;
2564 struct symbol *base_type;
2565 unsigned long mod;
2566 int is_typedef;
2568 /* Top-level inline asm? */
2569 if (token_type(token) == TOKEN_IDENT) {
2570 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2571 if (s && s->op->toplevel)
2572 return s->op->toplevel(token, list);
2575 /* Parse declaration-specifiers, if any */
2576 token = declaration_specifiers(token, &ctx);
2577 mod = storage_modifiers(&ctx);
2578 decl = alloc_symbol(token->pos, SYM_NODE);
2579 /* Just a type declaration? */
2580 if (match_op(token, ';')) {
2581 apply_modifiers(token->pos, &ctx);
2582 return token->next;
2585 saved = ctx.ctype;
2586 token = declarator(token, &ctx);
2587 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2588 apply_modifiers(token->pos, &ctx);
2590 decl->ctype = ctx.ctype;
2591 decl->ctype.modifiers |= mod;
2592 decl->endpos = token->pos;
2594 /* Just a type declaration? */
2595 if (!ident) {
2596 warning(token->pos, "missing identifier in declaration");
2597 return expect(token, ';', "at the end of type declaration");
2600 /* type define declaration? */
2601 is_typedef = ctx.storage_class == STypedef;
2603 /* Typedefs don't have meaningful storage */
2604 if (is_typedef)
2605 decl->ctype.modifiers |= MOD_USERTYPE;
2607 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2609 base_type = decl->ctype.base_type;
2611 if (is_typedef) {
2612 if (base_type && !base_type->ident) {
2613 switch (base_type->type) {
2614 case SYM_STRUCT:
2615 case SYM_UNION:
2616 case SYM_ENUM:
2617 case SYM_RESTRICT:
2618 base_type->ident = ident;
2621 } else if (base_type && base_type->type == SYM_FN) {
2622 /* K&R argument declaration? */
2623 if (lookup_type(token))
2624 return parse_k_r_arguments(token, decl, list);
2625 if (match_op(token, '{'))
2626 return parse_function_body(token, decl, list);
2628 if (!(decl->ctype.modifiers & MOD_STATIC))
2629 decl->ctype.modifiers |= MOD_EXTERN;
2630 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2631 sparse_error(token->pos, "void declaration");
2634 for (;;) {
2635 if (!is_typedef && match_op(token, '=')) {
2636 if (decl->ctype.modifiers & MOD_EXTERN) {
2637 warning(decl->pos, "symbol with external linkage has initializer");
2638 decl->ctype.modifiers &= ~MOD_EXTERN;
2640 token = initializer(&decl->initializer, token->next);
2642 if (!is_typedef) {
2643 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2644 add_symbol(list, decl);
2645 fn_local_symbol(decl);
2648 check_declaration(decl);
2649 if (decl->same_symbol)
2650 decl->definition = decl->same_symbol->definition;
2652 if (!match_op(token, ','))
2653 break;
2655 token = token->next;
2656 ident = NULL;
2657 decl = alloc_symbol(token->pos, SYM_NODE);
2658 ctx.ctype = saved;
2659 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2660 token = declarator(token, &ctx);
2661 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2662 apply_modifiers(token->pos, &ctx);
2663 decl->ctype = ctx.ctype;
2664 decl->ctype.modifiers |= mod;
2665 decl->endpos = token->pos;
2666 if (!ident) {
2667 sparse_error(token->pos, "expected identifier name in type definition");
2668 return token;
2671 if (is_typedef)
2672 decl->ctype.modifiers |= MOD_USERTYPE;
2674 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2676 /* Function declarations are automatically extern unless specifically static */
2677 base_type = decl->ctype.base_type;
2678 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2679 if (!(decl->ctype.modifiers & MOD_STATIC))
2680 decl->ctype.modifiers |= MOD_EXTERN;
2683 return expect(token, ';', "at end of declaration");