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
28 #include "expression.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
*);
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
*,
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
*);
74 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
, to_word_mode
;
88 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
92 CInt
= 0, CSInt
, CUInt
, CReal
, CChar
, CSChar
, CUChar
96 SNone
= 0, STypedef
, SAuto
, SRegister
, SExtern
, SStatic
, SForced
99 static struct symbol_op typedef_op
= {
101 .declarator
= typedef_specifier
,
104 static struct symbol_op inline_op
= {
106 .declarator
= inline_specifier
,
109 static struct symbol_op auto_op
= {
111 .declarator
= auto_specifier
,
114 static struct symbol_op register_op
= {
116 .declarator
= register_specifier
,
119 static struct symbol_op static_op
= {
121 .declarator
= static_specifier
,
124 static struct symbol_op extern_op
= {
126 .declarator
= extern_specifier
,
129 static struct symbol_op thread_op
= {
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
,
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
,
167 static struct symbol_op union_op
= {
168 .type
= KW_SPECIFIER
,
169 .declarator
= union_specifier
,
174 static struct symbol_op enum_op
= {
175 .type
= KW_SPECIFIER
,
176 .declarator
= enum_specifier
,
181 static struct symbol_op spec_op
= {
182 .type
= KW_SPECIFIER
| KW_EXACT
,
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
,
194 static struct symbol_op int_op
= {
195 .type
= KW_SPECIFIER
,
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
,
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
,
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
,
220 static struct symbol_op signed_op
= {
221 .type
= KW_SPECIFIER
,
222 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
227 static struct symbol_op unsigned_op
= {
228 .type
= KW_SPECIFIER
,
229 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
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
,
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
= {
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
= {
337 .to_mode
= to_QI_mode
340 static struct symbol_op mode_HI_op
= {
342 .to_mode
= to_HI_mode
345 static struct symbol_op mode_SI_op
= {
347 .to_mode
= to_SI_mode
350 static struct symbol_op mode_DI_op
= {
352 .to_mode
= to_DI_mode
355 static struct symbol_op mode_TI_op
= {
357 .to_mode
= to_TI_mode
360 static struct symbol_op mode_word_op
= {
362 .to_mode
= to_word_mode
365 static struct init_keyword
{
368 unsigned long modifiers
;
369 struct symbol_op
*op
;
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
},
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
},
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
},
422 { "__restrict__", NS_TYPEDEF
, .op
= &restrict_op
},
425 { "auto", NS_TYPEDEF
, .op
= &auto_op
},
426 { "register", NS_TYPEDEF
, .op
= ®ister_op
},
427 { "static", NS_TYPEDEF
, .op
= &static_op
},
428 { "extern", NS_TYPEDEF
, .op
= &extern_op
},
429 { "__thread", NS_TYPEDEF
, .op
= &thread_op
},
432 { "if", NS_KEYWORD
, .op
= &if_op
},
433 { "return", NS_KEYWORD
, .op
= &return_op
},
434 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
435 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
436 { "default", NS_KEYWORD
, .op
= &default_op
},
437 { "case", NS_KEYWORD
, .op
= &case_op
},
438 { "switch", NS_KEYWORD
, .op
= &switch_op
},
439 { "for", NS_KEYWORD
, .op
= &for_op
},
440 { "while", NS_KEYWORD
, .op
= &while_op
},
441 { "do", NS_KEYWORD
, .op
= &do_op
},
442 { "goto", NS_KEYWORD
, .op
= &goto_op
},
443 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
444 { "__range__", NS_KEYWORD
, .op
= &range_op
},
445 { "asm", NS_KEYWORD
, .op
= &asm_op
},
446 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
447 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
450 { "packed", NS_KEYWORD
, .op
= &packed_op
},
451 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
452 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
453 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
454 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
455 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
456 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
457 { "force", NS_KEYWORD
, .op
= &attr_force_op
},
458 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
459 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
460 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
461 { "mode", NS_KEYWORD
, .op
= &mode_op
},
462 { "context", NS_KEYWORD
, .op
= &context_op
},
463 { "designated_init", NS_KEYWORD
, .op
= &designated_init_op
},
464 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
465 { "noreturn", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
466 { "__noreturn__", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
467 { "pure", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
468 {"__pure__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
469 {"const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
470 {"__const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
471 {"__const__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
473 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
474 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
475 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
476 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
477 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
478 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
479 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
480 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
481 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
482 { "TI", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
483 { "__TI__", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
484 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
485 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
488 const char *ignored_attributes
[] = {
515 "externally_visible",
516 "__externally_visible__",
544 "__ms_hook_prologue__",
547 "no_instrument_function",
548 "__no_instrument_function__",
571 "__syscall_linkage__",
582 "warn_unused_result",
583 "__warn_unused_result__",
591 void init_parser(int stream
)
594 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
595 struct init_keyword
*ptr
= keyword_table
+ i
;
596 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
597 sym
->ident
->keyword
= 1;
598 if (ptr
->ns
== NS_TYPEDEF
)
599 sym
->ident
->reserved
= 1;
600 sym
->ctype
.modifiers
= ptr
->modifiers
;
601 sym
->ctype
.base_type
= ptr
->type
;
605 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
606 const char * name
= ignored_attributes
[i
];
607 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
609 sym
->ident
->keyword
= 1;
610 sym
->op
= &ignore_attr_op
;
615 // Add a symbol to the list of function-local symbols
616 static void fn_local_symbol(struct symbol
*sym
)
618 if (function_symbol_list
)
619 add_symbol(function_symbol_list
, sym
);
622 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
627 if (token_type(token
) != TOKEN_IDENT
)
630 va_start(args
, token
);
632 next
= va_arg(args
, struct ident
*);
633 } while (next
&& token
->ident
!= next
);
636 return next
&& token
->ident
== next
;
640 struct statement
*alloc_statement(struct position pos
, int type
)
642 struct statement
*stmt
= __alloc_statement(0);
648 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
650 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
652 struct symbol
*ctype
;
655 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
657 sparse_error(pos
, "don't know how to apply mode to %s",
658 show_typename(ctx
->ctype
.base_type
));
660 ctx
->ctype
.base_type
= ctype
;
664 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
666 struct symbol
*sym
= alloc_symbol(pos
, type
);
668 sym
->ctype
.base_type
= ctype
->base_type
;
669 sym
->ctype
.modifiers
= ctype
->modifiers
;
671 ctype
->base_type
= sym
;
672 ctype
->modifiers
= 0;
677 * NOTE! NS_LABEL is not just a different namespace,
678 * it also ends up using function scope instead of the
679 * regular symbol scope.
681 struct symbol
*label_symbol(struct token
*token
)
683 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
685 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
686 bind_symbol(sym
, token
->ident
, NS_LABEL
);
687 fn_local_symbol(sym
);
692 static struct token
*struct_union_enum_specifier(enum type type
,
693 struct token
*token
, struct decl_state
*ctx
,
694 struct token
*(*parse
)(struct token
*, struct symbol
*))
697 struct position
*repos
;
699 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
700 if (token_type(token
) == TOKEN_IDENT
) {
701 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
703 (is_outer_scope(sym
->scope
) &&
704 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
705 // Either a new symbol, or else an out-of-scope
706 // symbol being redefined.
707 sym
= alloc_symbol(token
->pos
, type
);
708 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
710 if (sym
->type
!= type
)
711 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
712 ctx
->ctype
.base_type
= sym
;
715 if (match_op(token
, '{')) {
716 // The following test is actually wrong for empty
717 // structs, but (1) they are not C99, (2) gcc does
718 // the same thing, and (3) it's easier.
719 if (sym
->symbol_list
)
720 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
722 token
= parse(token
->next
, sym
);
723 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
725 // Mark the structure as needing re-examination
727 sym
->endpos
= token
->pos
;
732 // private struct/union/enum type
733 if (!match_op(token
, '{')) {
734 sparse_error(token
->pos
, "expected declaration");
735 ctx
->ctype
.base_type
= &bad_ctype
;
739 sym
= alloc_symbol(token
->pos
, type
);
740 token
= parse(token
->next
, sym
);
741 ctx
->ctype
.base_type
= sym
;
742 token
= expect(token
, '}', "at end of specifier");
743 sym
->endpos
= token
->pos
;
748 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
750 struct symbol
*field
, *last
= NULL
;
752 res
= struct_declaration_list(token
, &sym
->symbol_list
);
753 FOR_EACH_PTR(sym
->symbol_list
, field
) {
755 struct symbol
*base
= field
->ctype
.base_type
;
756 if (base
&& base
->type
== SYM_BITFIELD
)
760 last
->next_subobject
= field
;
762 } END_FOR_EACH_PTR(field
);
766 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
768 return struct_declaration_list(token
, &sym
->symbol_list
);
771 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
773 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
776 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
778 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
784 unsigned long long y
;
787 static void upper_boundary(Num
*n
, Num
*v
)
799 static void lower_boundary(Num
*n
, Num
*v
)
811 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
813 int shift
= type
->bit_size
;
814 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
818 if (upper
->x
== 0 && upper
->y
>> shift
)
820 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
825 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
827 if (s1
->bit_size
< s2
->bit_size
) {
829 } else if (s1
->bit_size
== s2
->bit_size
) {
830 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
833 if (s1
->bit_size
< bits_in_int
)
838 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
842 FOR_EACH_PTR(list
, sym
) {
843 struct expression
*expr
= sym
->initializer
;
844 struct symbol
*ctype
;
845 if (expr
->type
!= EXPR_VALUE
)
848 if (ctype
->bit_size
== base_type
->bit_size
)
850 cast_value(expr
, base_type
, expr
, ctype
);
851 } END_FOR_EACH_PTR(sym
);
854 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
856 unsigned long long lastval
= 0;
857 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
858 Num upper
= {-1, 0}, lower
= {1, 0};
860 parent
->examined
= 1;
861 parent
->ctype
.base_type
= &int_ctype
;
862 while (token_type(token
) == TOKEN_IDENT
) {
863 struct expression
*expr
= NULL
;
864 struct token
*next
= token
->next
;
867 if (match_op(next
, '=')) {
868 next
= constant_expression(next
->next
, &expr
);
869 lastval
= get_expression_value(expr
);
871 if (expr
&& expr
->ctype
)
875 } else if (is_int_type(ctype
)) {
878 error_die(token
->pos
, "can't increment the last enum member");
882 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
883 expr
->value
= lastval
;
887 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
888 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
889 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
890 sym
->initializer
= expr
;
891 sym
->enum_member
= 1;
892 sym
->ctype
.base_type
= parent
;
893 add_ptr_list(&parent
->symbol_list
, sym
);
895 if (base_type
!= &bad_ctype
) {
896 if (ctype
->type
== SYM_NODE
)
897 ctype
= ctype
->ctype
.base_type
;
898 if (ctype
->type
== SYM_ENUM
) {
902 ctype
= ctype
->ctype
.base_type
;
906 * - if all enums are of the same type, then
907 * the base_type is that type (two first
909 * - if enums are of different types, they
910 * all have to be integer types, and the
911 * base type is at least "int_ctype".
912 * - otherwise the base_type is "bad_ctype".
916 } else if (ctype
== base_type
) {
918 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
919 base_type
= bigger_enum_type(base_type
, ctype
);
921 base_type
= &bad_ctype
;
922 parent
->ctype
.base_type
= base_type
;
924 if (is_int_type(base_type
)) {
925 Num v
= {.y
= lastval
};
926 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
928 else if ((long long)lastval
>= 0)
932 upper_boundary(&upper
, &v
);
933 lower_boundary(&lower
, &v
);
937 sym
->endpos
= token
->pos
;
939 if (!match_op(token
, ','))
944 sparse_error(token
->pos
, "bad enum definition");
945 base_type
= &bad_ctype
;
947 else if (!is_int_type(base_type
))
948 base_type
= base_type
;
949 else if (type_is_ok(base_type
, &upper
, &lower
))
950 base_type
= base_type
;
951 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
952 base_type
= &int_ctype
;
953 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
954 base_type
= &uint_ctype
;
955 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
956 base_type
= &long_ctype
;
957 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
958 base_type
= &ulong_ctype
;
959 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
960 base_type
= &llong_ctype
;
961 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
962 base_type
= &ullong_ctype
;
964 base_type
= &bad_ctype
;
965 parent
->ctype
.base_type
= base_type
;
966 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
967 parent
->examined
= 0;
969 cast_enum_list(parent
->symbol_list
, base_type
);
974 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
976 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
977 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
979 if (!ctype
->base_type
)
980 ctype
->base_type
= &incomplete_ctype
;
985 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
987 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
991 if (!match_op(token
, '(')) {
992 sparse_error(token
->pos
, "expected '(' after typeof");
995 if (lookup_type(token
->next
)) {
996 token
= typename(token
->next
, &sym
, NULL
);
997 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
998 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
1000 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1001 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1003 typeof_sym
->endpos
= token
->pos
;
1004 if (!typeof_sym
->initializer
) {
1005 sparse_error(token
->pos
, "expected expression after the '(' token");
1006 typeof_sym
= &bad_ctype
;
1008 ctx
->ctype
.base_type
= typeof_sym
;
1010 return expect(token
, ')', "after typeof");
1013 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1015 struct expression
*expr
= NULL
;
1016 if (match_op(token
, '('))
1017 token
= parens_expression(token
, &expr
, "in attribute");
1021 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1023 set_attr_is_packed(&ctx
->ctype
);
1024 if (!ctx
->ctype
.alignment
)
1025 ctx
->ctype
.alignment
= 1;
1029 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1031 int alignment
= max_alignment
;
1032 struct expression
*expr
= NULL
;
1034 if (match_op(token
, '(')) {
1035 token
= parens_expression(token
, &expr
, "in attribute");
1037 alignment
= const_expression_value(expr
);
1039 if (alignment
& (alignment
-1)) {
1040 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1042 } else if (alignment
> ctx
->ctype
.alignment
)
1043 ctx
->ctype
.alignment
= alignment
;
1047 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1049 if (ctx
->modifiers
& qual
)
1050 warning(*pos
, "duplicate %s", modifier_string(qual
));
1051 ctx
->modifiers
|= qual
;
1054 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1056 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1060 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1062 struct expression
*expr
= NULL
;
1064 token
= expect(token
, '(', "after address_space attribute");
1065 token
= conditional_expression(token
, &expr
);
1067 as
= const_expression_value(expr
);
1068 if (Waddress_space
&& as
)
1069 attr_set_as(&ctx
->ctype
, as
);
1071 token
= expect(token
, ')', "after address_space attribute");
1075 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1077 if (ctype
->ctype
.base_type
!= &int_type
)
1079 if (ctype
== &char_ctype
)
1081 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1085 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1087 if (ctype
->ctype
.base_type
!= &int_type
)
1089 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1093 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1095 if (ctype
->ctype
.base_type
!= &int_type
)
1097 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1101 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1103 if (ctype
->ctype
.base_type
!= &int_type
)
1105 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1109 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1111 if (ctype
->ctype
.base_type
!= &int_type
)
1113 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1117 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1119 if (ctype
->ctype
.base_type
!= &int_type
)
1121 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1125 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1127 token
= expect(token
, '(', "after mode attribute");
1128 if (token_type(token
) == TOKEN_IDENT
) {
1129 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1130 if (mode
&& mode
->op
->type
== KW_MODE
)
1131 ctx
->mode
= mode
->op
;
1133 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1134 token
= token
->next
;
1136 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1137 token
= expect(token
, ')', "after mode attribute");
1141 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1143 struct context
*context
= alloc_context();
1144 struct expression
*args
[3];
1147 token
= expect(token
, '(', "after context attribute");
1148 while (!match_op(token
, ')')) {
1149 struct expression
*expr
= NULL
;
1150 token
= conditional_expression(token
, &expr
);
1154 args
[argc
++] = expr
;
1155 if (!match_op(token
, ','))
1157 token
= token
->next
;
1162 sparse_error(token
->pos
, "expected context input/output values");
1165 context
->in
= get_expression_value(args
[0]);
1168 context
->in
= get_expression_value(args
[0]);
1169 context
->out
= get_expression_value(args
[1]);
1172 context
->context
= args
[0];
1173 context
->in
= get_expression_value(args
[1]);
1174 context
->out
= get_expression_value(args
[2]);
1179 attr_add_context(&ctx
->ctype
, context
);
1181 token
= expect(token
, ')', "after context attribute");
1185 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1187 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1188 ctx
->ctype
.base_type
->designated_init
= 1;
1190 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1194 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1196 if (Wtransparent_union
)
1197 warning(token
->pos
, "ignoring attribute __transparent_union__");
1201 static struct token
*recover_unknown_attribute(struct token
*token
)
1203 struct expression
*expr
= NULL
;
1205 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1206 token
= token
->next
;
1207 if (match_op(token
, '('))
1208 token
= parens_expression(token
, &expr
, "in attribute");
1212 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1214 token
= expect(token
, '(', "after attribute");
1215 token
= expect(token
, '(', "after attribute");
1218 struct ident
*attribute_name
;
1219 struct symbol
*attr
;
1221 if (eof_token(token
))
1223 if (match_op(token
, ';'))
1225 if (token_type(token
) != TOKEN_IDENT
)
1227 attribute_name
= token
->ident
;
1228 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1229 if (attr
&& attr
->op
->attribute
)
1230 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1232 token
= recover_unknown_attribute(token
);
1234 if (!match_op(token
, ','))
1236 token
= token
->next
;
1239 token
= expect(token
, ')', "after attribute");
1240 token
= expect(token
, ')', "after attribute");
1242 if (ctx
->ctype
.attribute
->is_packed
&& ctx
->ctype
.base_type
)
1243 set_attr_is_packed(&ctx
->ctype
.base_type
->ctype
);
1248 static const char *storage_class
[] =
1250 [STypedef
] = "typedef",
1252 [SExtern
] = "extern",
1253 [SStatic
] = "static",
1254 [SRegister
] = "register",
1255 [SForced
] = "[force]"
1258 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1260 static unsigned long mod
[] =
1263 [SExtern
] = MOD_EXTERN
,
1264 [SStatic
] = MOD_STATIC
,
1265 [SRegister
] = MOD_REGISTER
1267 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1268 | (ctx
->is_tls
? MOD_TLS
: 0);
1271 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1273 /* __thread can be used alone, or with extern or static */
1274 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1275 sparse_error(*pos
, "__thread can only be used alone, or with "
1276 "extern or static");
1280 if (!ctx
->storage_class
) {
1281 ctx
->storage_class
= class;
1284 if (ctx
->storage_class
== class)
1285 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1287 sparse_error(*pos
, "multiple storage classes");
1290 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1292 set_storage_class(&next
->pos
, ctx
, STypedef
);
1296 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1298 set_storage_class(&next
->pos
, ctx
, SAuto
);
1302 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1304 set_storage_class(&next
->pos
, ctx
, SRegister
);
1308 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1310 set_storage_class(&next
->pos
, ctx
, SStatic
);
1314 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1316 set_storage_class(&next
->pos
, ctx
, SExtern
);
1320 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1322 /* This GCC extension can be used alone, or with extern or static */
1323 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1324 || ctx
->storage_class
== SExtern
) {
1327 sparse_error(next
->pos
, "__thread can only be used alone, or "
1328 "with extern or static");
1334 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1336 set_storage_class(&token
->pos
, ctx
, SForced
);
1340 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1346 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1348 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1352 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1354 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1358 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1360 unsigned long mod
= thistype
->modifiers
;
1363 apply_qualifier(&pos
, ctype
, mod
);
1366 if (thistype
->alignment
> ctype
->alignment
)
1367 ctype
->alignment
= thistype
->alignment
;
1370 merge_attr(ctype
, thistype
);
1373 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1376 if (what
& (Set_S
| Set_T
))
1378 if (what
& Set_Char
)
1380 else if (what
& Set_Double
)
1382 else if (what
& Set_Float
)
1384 else if (what
& Set_Signed
)
1386 else if (what
& Set_Unsigned
)
1388 else if (what
& Set_Short
)
1390 else if (what
& Set_Long
)
1394 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1395 old
, show_ident(new));
1399 sparse_error(pos
, "two or more data types in declaration specifiers");
1402 static struct symbol
* const int_types
[] =
1403 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1404 static struct symbol
* const signed_types
[] =
1405 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1407 static struct symbol
* const unsigned_types
[] =
1408 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1410 static struct symbol
* const real_types
[] =
1411 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1412 static struct symbol
* const char_types
[] =
1413 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1414 static struct symbol
* const * const types
[] = {
1415 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1416 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1419 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1421 return types
[want_unsigned
? CUInt
: CInt
][size
];
1424 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1426 while (token_type(t
) == TOKEN_IDENT
) {
1427 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1430 if (s
->type
!= SYM_KEYWORD
)
1432 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1435 if (s
->op
->declarator
)
1436 t
= s
->op
->declarator(t
, ctx
);
1441 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1447 while (token_type(token
) == TOKEN_IDENT
) {
1448 struct symbol
*s
= lookup_symbol(token
->ident
,
1449 NS_TYPEDEF
| NS_SYMBOL
);
1450 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1452 if (s
->type
!= SYM_KEYWORD
) {
1455 seen
|= Set_S
| Set_T
;
1456 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1457 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1458 token
= token
->next
;
1461 if (s
->op
->type
& KW_SPECIFIER
) {
1462 if (seen
& s
->op
->test
) {
1463 specifier_conflict(token
->pos
,
1469 class += s
->op
->class;
1470 if (s
->op
->type
& KW_SHORT
) {
1472 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1473 if (class == CReal
) {
1474 specifier_conflict(token
->pos
,
1482 token
= token
->next
;
1483 if (s
->op
->declarator
)
1484 token
= s
->op
->declarator(token
, ctx
);
1485 if (s
->op
->type
& KW_EXACT
) {
1486 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1487 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1491 if (!(seen
& Set_S
)) { /* not set explicitly? */
1492 struct symbol
*base
= &incomplete_ctype
;
1494 base
= types
[class][size
];
1495 ctx
->ctype
.base_type
= base
;
1498 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1499 struct symbol
*type
;
1500 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1501 if (!is_int_type(ctx
->ctype
.base_type
)) {
1502 sparse_error(token
->pos
, "invalid modifier");
1505 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1506 *type
= *ctx
->ctype
.base_type
;
1507 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1508 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1509 type
->type
= SYM_RESTRICT
;
1510 ctx
->ctype
.base_type
= type
;
1511 create_fouled(type
);
1516 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1518 struct expression
*expr
= NULL
;
1520 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1521 token
= token
->next
;
1522 token
= parse_expression(token
, &expr
);
1523 sym
->array_size
= expr
;
1527 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1528 static struct token
*identifier_list(struct token
*, struct symbol
*);
1529 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1531 static struct token
*skip_attribute(struct token
*token
)
1533 token
= token
->next
;
1534 if (match_op(token
, '(')) {
1536 token
= token
->next
;
1537 while (depth
&& !eof_token(token
)) {
1538 if (token_type(token
) == TOKEN_SPECIAL
) {
1539 if (token
->special
== '(')
1541 else if (token
->special
== ')')
1544 token
= token
->next
;
1550 static struct token
*skip_attributes(struct token
*token
)
1552 struct symbol
*keyword
;
1554 if (token_type(token
) != TOKEN_IDENT
)
1556 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1557 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1559 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1561 token
= expect(token
->next
, '(', "after attribute");
1562 token
= expect(token
, '(', "after attribute");
1564 if (eof_token(token
))
1566 if (match_op(token
, ';'))
1568 if (token_type(token
) != TOKEN_IDENT
)
1570 token
= skip_attribute(token
);
1571 if (!match_op(token
, ','))
1573 token
= token
->next
;
1575 token
= expect(token
, ')', "after attribute");
1576 token
= expect(token
, ')', "after attribute");
1581 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1583 struct symbol
*keyword
;
1585 if (token_type(token
) != TOKEN_IDENT
)
1587 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1588 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1590 if (!(keyword
->op
->type
& keywords
))
1592 token
= keyword
->op
->declarator(token
->next
, ctx
);
1593 keywords
&= KW_ATTRIBUTE
;
1598 static int is_nested(struct token
*token
, struct token
**p
,
1599 int prefer_abstract
)
1602 * This can be either a parameter list or a grouping.
1603 * For the direct (non-abstract) case, we know if must be
1604 * a parameter list if we already saw the identifier.
1605 * For the abstract case, we know if must be a parameter
1606 * list if it is empty or starts with a type.
1608 struct token
*next
= token
->next
;
1610 *p
= next
= skip_attributes(next
);
1612 if (token_type(next
) == TOKEN_IDENT
) {
1613 if (lookup_type(next
))
1614 return !prefer_abstract
;
1618 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1625 Empty
, K_R
, Proto
, Bad_Func
,
1628 static enum kind
which_func(struct token
*token
,
1630 int prefer_abstract
)
1632 struct token
*next
= token
->next
;
1634 if (token_type(next
) == TOKEN_IDENT
) {
1635 if (lookup_type(next
))
1637 /* identifier list not in definition; complain */
1638 if (prefer_abstract
)
1640 "identifier list not in definition");
1644 if (token_type(next
) != TOKEN_SPECIAL
)
1647 if (next
->special
== ')') {
1648 /* don't complain about those */
1649 if (!n
|| match_op(next
->next
, ';'))
1652 "non-ANSI function declaration of function '%s'",
1657 if (next
->special
== SPECIAL_ELLIPSIS
) {
1659 "variadic functions must have one named argument");
1666 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1668 struct ctype
*ctype
= &ctx
->ctype
;
1670 struct ident
**p
= ctx
->ident
;
1672 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1673 *ctx
->ident
= token
->ident
;
1674 token
= token
->next
;
1675 } else if (match_op(token
, '(') &&
1676 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1677 struct symbol
*base_type
= ctype
->base_type
;
1678 if (token
->next
!= next
)
1679 next
= handle_attributes(token
->next
, ctx
,
1681 token
= declarator(next
, ctx
);
1682 token
= expect(token
, ')', "in nested declarator");
1683 while (ctype
->base_type
!= base_type
)
1684 ctype
= &ctype
->base_type
->ctype
;
1688 if (match_op(token
, '(')) {
1689 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1691 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1692 token
= token
->next
;
1694 token
= identifier_list(token
, fn
);
1695 else if (kind
== Proto
)
1696 token
= parameter_type_list(token
, fn
);
1697 token
= expect(token
, ')', "in function declarator");
1698 fn
->endpos
= token
->pos
;
1702 while (match_op(token
, '[')) {
1703 struct symbol
*array
;
1704 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1705 token
= abstract_array_declarator(token
->next
, array
);
1706 token
= expect(token
, ']', "in abstract_array_declarator");
1707 array
->endpos
= token
->pos
;
1708 ctype
= &array
->ctype
;
1713 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1715 while (match_op(token
,'*')) {
1716 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1717 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1718 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1719 merge_attr(&ptr
->ctype
, &ctx
->ctype
);
1720 ctx
->ctype
.modifiers
= 0;
1721 ctx
->ctype
.base_type
= ptr
;
1722 ctx
->ctype
.attribute
= &null_attr
;
1723 ctx
->ctype
.alignment
= 0;
1725 token
= handle_qualifiers(token
->next
, ctx
);
1726 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1731 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1733 token
= pointer(token
, ctx
);
1734 return direct_declarator(token
, ctx
);
1737 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1739 struct ctype
*ctype
= &ctx
->ctype
;
1740 struct expression
*expr
;
1741 struct symbol
*bitfield
;
1744 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1745 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1746 show_typename(ctype
->base_type
));
1747 // Parse this to recover gracefully.
1748 return conditional_expression(token
->next
, &expr
);
1751 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1752 token
= conditional_expression(token
->next
, &expr
);
1753 width
= const_expression_value(expr
);
1754 bitfield
->bit_size
= width
;
1756 if (width
< 0 || width
> INT_MAX
) {
1757 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1759 } else if (*ctx
->ident
&& width
== 0) {
1760 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1761 show_ident(*ctx
->ident
));
1763 } else if (*ctx
->ident
) {
1764 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1765 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1766 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1767 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1768 // Valid values are either {-1;0} or {0}, depending on integer
1769 // representation. The latter makes for very efficient code...
1770 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1772 if (Wdefault_bitfield_sign
&&
1773 bitfield_type
->type
!= SYM_ENUM
&&
1774 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1776 // The sign of bitfields is unspecified by default.
1777 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1780 bitfield
->bit_size
= width
;
1781 bitfield
->endpos
= token
->pos
;
1785 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1787 struct decl_state ctx
= {.prefer_abstract
= 0, .ctype
.attribute
= &null_attr
};
1791 token
= declaration_specifiers(token
, &ctx
);
1792 mod
= storage_modifiers(&ctx
);
1795 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1796 ctx
.ident
= &decl
->ident
;
1798 token
= declarator(token
, &ctx
);
1799 if (match_op(token
, ':'))
1800 token
= handle_bitfield(token
, &ctx
);
1802 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1803 apply_modifiers(token
->pos
, &ctx
);
1805 decl
->ctype
= ctx
.ctype
;
1806 decl
->ctype
.modifiers
|= mod
;
1807 decl
->endpos
= token
->pos
;
1808 add_symbol(list
, decl
);
1809 if (!match_op(token
, ','))
1811 token
= token
->next
;
1817 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1819 while (!match_op(token
, '}')) {
1820 if (!match_op(token
, ';'))
1821 token
= declaration_list(token
, list
);
1822 if (!match_op(token
, ';')) {
1823 sparse_error(token
->pos
, "expected ; at end of declaration");
1826 token
= token
->next
;
1831 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1833 struct decl_state ctx
= {.prefer_abstract
= 1, .ctype
.attribute
= &null_attr
};
1835 token
= declaration_specifiers(token
, &ctx
);
1836 ctx
.ident
= &sym
->ident
;
1837 token
= declarator(token
, &ctx
);
1838 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1839 apply_modifiers(token
->pos
, &ctx
);
1840 sym
->ctype
= ctx
.ctype
;
1841 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1842 sym
->endpos
= token
->pos
;
1846 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1848 struct decl_state ctx
= {.prefer_abstract
= 1, .ctype
.attribute
= &null_attr
};
1850 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1852 token
= declaration_specifiers(token
, &ctx
);
1853 token
= declarator(token
, &ctx
);
1854 apply_modifiers(token
->pos
, &ctx
);
1855 sym
->ctype
= ctx
.ctype
;
1856 sym
->endpos
= token
->pos
;
1857 class = ctx
.storage_class
;
1860 if (class == SForced
) {
1866 warning(sym
->pos
, "storage class in typename (%s %s)",
1867 storage_class
[class], show_typename(sym
));
1871 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1873 token
= parse_expression(token
, tree
);
1874 return expect(token
, ';', "at end of statement");
1877 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1878 struct expression_list
**inout
)
1880 struct expression
*expr
;
1882 /* Allow empty operands */
1883 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1886 struct ident
*ident
= NULL
;
1887 if (match_op(token
->next
, '[') &&
1888 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1889 match_op(token
->next
->next
->next
, ']')) {
1890 ident
= token
->next
->next
->ident
;
1891 token
= token
->next
->next
->next
;
1893 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1894 token
= primary_expression(token
->next
, &expr
);
1895 add_expression(inout
, expr
);
1896 token
= parens_expression(token
, &expr
, "in asm parameter");
1897 add_expression(inout
, expr
);
1898 } while (match_op(token
, ','));
1902 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1903 struct expression_list
**clobbers
)
1905 struct expression
*expr
;
1908 token
= primary_expression(token
->next
, &expr
);
1910 add_expression(clobbers
, expr
);
1911 } while (match_op(token
, ','));
1915 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1916 struct symbol_list
**labels
)
1918 struct symbol
*label
;
1921 token
= token
->next
; /* skip ':' and ',' */
1922 if (token_type(token
) != TOKEN_IDENT
)
1924 label
= label_symbol(token
);
1925 add_symbol(labels
, label
);
1926 token
= token
->next
;
1927 } while (match_op(token
, ','));
1931 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1935 token
= token
->next
;
1936 stmt
->type
= STMT_ASM
;
1937 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1938 token
= token
->next
;
1940 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
1942 token
= token
->next
;
1944 token
= expect(token
, '(', "after asm");
1945 token
= parse_expression(token
, &stmt
->asm_string
);
1946 if (match_op(token
, ':'))
1947 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1948 if (match_op(token
, ':'))
1949 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1950 if (match_op(token
, ':'))
1951 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1952 if (is_goto
&& match_op(token
, ':'))
1953 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
1954 token
= expect(token
, ')', "after asm");
1955 return expect(token
, ';', "at end of asm-statement");
1958 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1960 struct expression
*expr
;
1961 token
= expect(token
, '(', "after asm");
1962 token
= parse_expression(token
->next
, &expr
);
1963 token
= expect(token
, ')', "after asm");
1967 /* Make a statement out of an expression */
1968 static struct statement
*make_statement(struct expression
*expr
)
1970 struct statement
*stmt
;
1974 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1975 stmt
->expression
= expr
;
1980 * All iterators have two symbols associated with them:
1981 * the "continue" and "break" symbols, which are targets
1982 * for continue and break statements respectively.
1984 * They are in a special name-space, but they follow
1985 * all the normal visibility rules, so nested iterators
1986 * automatically work right.
1988 static void start_iterator(struct statement
*stmt
)
1990 struct symbol
*cont
, *brk
;
1992 start_symbol_scope(stmt
->pos
);
1993 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1995 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1996 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1998 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2000 stmt
->type
= STMT_ITERATOR
;
2001 stmt
->iterator_break
= brk
;
2002 stmt
->iterator_continue
= cont
;
2003 fn_local_symbol(brk
);
2004 fn_local_symbol(cont
);
2007 static void end_iterator(struct statement
*stmt
)
2012 static struct statement
*start_function(struct symbol
*sym
)
2015 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2017 start_function_scope(stmt
->pos
);
2018 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2019 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2020 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2021 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2022 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2024 fn_local_symbol(ret
);
2026 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2032 static void end_function(struct symbol
*sym
)
2035 end_function_scope();
2039 * A "switch()" statement, like an iterator, has a
2040 * the "break" symbol associated with it. It works
2041 * exactly like the iterator break - it's the target
2042 * for any break-statements in scope, and means that
2043 * "break" handling doesn't even need to know whether
2044 * it's breaking out of an iterator or a switch.
2046 * In addition, the "case" symbol is a marker for the
2047 * case/default statements to find the switch statement
2048 * that they are associated with.
2050 static void start_switch(struct statement
*stmt
)
2052 struct symbol
*brk
, *switch_case
;
2054 start_symbol_scope(stmt
->pos
);
2055 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2057 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2059 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2060 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2061 switch_case
->stmt
= stmt
;
2063 stmt
->type
= STMT_SWITCH
;
2064 stmt
->switch_break
= brk
;
2065 stmt
->switch_case
= switch_case
;
2067 fn_local_symbol(brk
);
2068 fn_local_symbol(switch_case
);
2071 static void end_switch(struct statement
*stmt
)
2073 if (!stmt
->switch_case
->symbol_list
)
2074 warning(stmt
->pos
, "switch with no cases");
2078 static void add_case_statement(struct statement
*stmt
)
2080 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2084 sparse_error(stmt
->pos
, "not in switch scope");
2085 stmt
->type
= STMT_NONE
;
2088 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2089 add_symbol(&target
->symbol_list
, sym
);
2091 stmt
->case_label
= sym
;
2092 fn_local_symbol(sym
);
2095 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2097 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2100 error_die(token
->pos
, "internal error: return without a function target");
2101 stmt
->type
= STMT_RETURN
;
2102 stmt
->ret_target
= target
;
2103 return expression_statement(token
->next
, &stmt
->ret_value
);
2106 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2108 struct symbol_list
*syms
;
2109 struct expression
*e1
, *e2
, *e3
;
2110 struct statement
*iterator
;
2112 start_iterator(stmt
);
2113 token
= expect(token
->next
, '(', "after 'for'");
2117 /* C99 variable declaration? */
2118 if (lookup_type(token
)) {
2119 token
= external_declaration(token
, &syms
);
2121 token
= parse_expression(token
, &e1
);
2122 token
= expect(token
, ';', "in 'for'");
2124 token
= parse_expression(token
, &e2
);
2125 token
= expect(token
, ';', "in 'for'");
2126 token
= parse_expression(token
, &e3
);
2127 token
= expect(token
, ')', "in 'for'");
2128 token
= statement(token
, &iterator
);
2130 stmt
->iterator_syms
= syms
;
2131 stmt
->iterator_pre_statement
= make_statement(e1
);
2132 stmt
->iterator_pre_condition
= e2
;
2133 stmt
->iterator_post_statement
= make_statement(e3
);
2134 stmt
->iterator_post_condition
= NULL
;
2135 stmt
->iterator_statement
= iterator
;
2141 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2143 struct expression
*expr
;
2144 struct statement
*iterator
;
2146 start_iterator(stmt
);
2147 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2148 token
= statement(token
, &iterator
);
2150 stmt
->iterator_pre_condition
= expr
;
2151 stmt
->iterator_post_condition
= NULL
;
2152 stmt
->iterator_statement
= iterator
;
2158 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2160 struct expression
*expr
;
2161 struct statement
*iterator
;
2163 start_iterator(stmt
);
2164 token
= statement(token
->next
, &iterator
);
2165 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2166 token
= token
->next
;
2168 sparse_error(token
->pos
, "expected 'while' after 'do'");
2169 token
= parens_expression(token
, &expr
, "after 'do-while'");
2171 stmt
->iterator_post_condition
= expr
;
2172 stmt
->iterator_statement
= iterator
;
2175 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2176 warning(iterator
->pos
, "do-while statement is not a compound statement");
2178 return expect(token
, ';', "after statement");
2181 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2183 stmt
->type
= STMT_IF
;
2184 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2185 token
= statement(token
, &stmt
->if_true
);
2186 if (token_type(token
) != TOKEN_IDENT
)
2188 if (token
->ident
!= &else_ident
)
2190 return statement(token
->next
, &stmt
->if_false
);
2193 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2195 stmt
->type
= STMT_CASE
;
2196 token
= expect(token
, ':', "after default/case");
2197 add_case_statement(stmt
);
2198 return statement(token
, &stmt
->case_statement
);
2201 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2203 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2204 if (match_op(token
, SPECIAL_ELLIPSIS
))
2205 token
= parse_expression(token
->next
, &stmt
->case_to
);
2206 return case_statement(token
, stmt
);
2209 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2211 return case_statement(token
->next
, stmt
);
2214 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2216 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2217 stmt
->type
= STMT_GOTO
;
2218 stmt
->goto_label
= target
;
2220 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2221 return expect(token
->next
, ';', "at end of statement");
2224 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2226 stmt
->type
= STMT_SWITCH
;
2228 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2229 token
= statement(token
, &stmt
->switch_statement
);
2234 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2236 stmt
->type
= STMT_GOTO
;
2237 token
= token
->next
;
2238 if (match_op(token
, '*')) {
2239 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2240 add_statement(&function_computed_goto_list
, stmt
);
2241 } else if (token_type(token
) == TOKEN_IDENT
) {
2242 stmt
->goto_label
= label_symbol(token
);
2243 token
= token
->next
;
2245 sparse_error(token
->pos
, "Expected identifier or goto expression");
2247 return expect(token
, ';', "at end of statement");
2250 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2252 stmt
->type
= STMT_CONTEXT
;
2253 token
= parse_expression(token
->next
, &stmt
->expression
);
2254 if (stmt
->expression
->type
== EXPR_PREOP
2255 && stmt
->expression
->op
== '('
2256 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2257 struct expression
*expr
;
2258 expr
= stmt
->expression
->unop
;
2259 stmt
->context
= expr
->left
;
2260 stmt
->expression
= expr
->right
;
2262 return expect(token
, ';', "at end of statement");
2265 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2267 stmt
->type
= STMT_RANGE
;
2268 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2269 token
= expect(token
, ',', "after range expression");
2270 token
= assignment_expression(token
, &stmt
->range_low
);
2271 token
= expect(token
, ',', "after low range");
2272 token
= assignment_expression(token
, &stmt
->range_high
);
2273 return expect(token
, ';', "after range statement");
2276 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2278 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2281 if (token_type(token
) == TOKEN_IDENT
) {
2282 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2283 if (s
&& s
->op
->statement
)
2284 return s
->op
->statement(token
, stmt
);
2286 if (match_op(token
->next
, ':')) {
2287 struct symbol
*s
= label_symbol(token
);
2288 stmt
->type
= STMT_LABEL
;
2289 stmt
->label_identifier
= s
;
2291 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(token
->ident
));
2293 token
= skip_attributes(token
->next
->next
);
2294 return statement(token
, &stmt
->label_statement
);
2298 if (match_op(token
, '{')) {
2299 stmt
->type
= STMT_COMPOUND
;
2300 start_symbol_scope(stmt
->pos
);
2301 token
= compound_statement(token
->next
, stmt
);
2304 return expect(token
, '}', "at end of compound statement");
2307 stmt
->type
= STMT_EXPRESSION
;
2308 return expression_statement(token
, &stmt
->expression
);
2311 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2312 static struct token
*label_statement(struct token
*token
)
2314 while (token_type(token
) == TOKEN_IDENT
) {
2315 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2316 /* it's block-scope, but we want label namespace */
2317 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2318 sym
->namespace = NS_LABEL
;
2319 fn_local_symbol(sym
);
2320 token
= token
->next
;
2321 if (!match_op(token
, ','))
2323 token
= token
->next
;
2325 return expect(token
, ';', "at end of label declaration");
2328 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2330 int seen_statement
= 0;
2331 while (token_type(token
) == TOKEN_IDENT
&&
2332 token
->ident
== &__label___ident
)
2333 token
= label_statement(token
->next
);
2335 struct statement
* stmt
;
2336 if (eof_token(token
))
2338 if (match_op(token
, '}'))
2340 if (lookup_type(token
)) {
2341 if (seen_statement
) {
2342 warning(token
->pos
, "mixing declarations and code");
2345 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2346 token
= external_declaration(token
, &stmt
->declaration
);
2348 seen_statement
= Wdeclarationafterstatement
;
2349 token
= statement(token
, &stmt
);
2351 add_statement(list
, stmt
);
2356 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2358 struct symbol_list
**list
= &fn
->arguments
;
2360 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2361 sym
->ident
= token
->ident
;
2362 token
= token
->next
;
2363 sym
->endpos
= token
->pos
;
2364 sym
->ctype
.base_type
= &incomplete_ctype
;
2365 add_symbol(list
, sym
);
2366 if (!match_op(token
, ',') ||
2367 token_type(token
->next
) != TOKEN_IDENT
||
2368 lookup_type(token
->next
))
2370 token
= token
->next
;
2375 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2377 struct symbol_list
**list
= &fn
->arguments
;
2382 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2384 token
= token
->next
;
2388 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2389 token
= parameter_declaration(token
, sym
);
2390 if (sym
->ctype
.base_type
== &void_ctype
) {
2391 /* Special case: (void) */
2392 if (!*list
&& !sym
->ident
)
2394 warning(token
->pos
, "void parameter");
2396 add_symbol(list
, sym
);
2397 if (!match_op(token
, ','))
2399 token
= token
->next
;
2404 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2406 token
= statement_list(token
, &stmt
->stmts
);
2410 static struct expression
*identifier_expression(struct token
*token
)
2412 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2413 expr
->expr_ident
= token
->ident
;
2417 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2419 int idx_from
, idx_to
;
2420 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2422 idx_from
= const_expression_value(from
);
2425 idx_to
= const_expression_value(to
);
2426 if (idx_to
< idx_from
|| idx_from
< 0)
2427 warning(from
->pos
, "nonsense array initializer index range");
2429 expr
->idx_from
= idx_from
;
2430 expr
->idx_to
= idx_to
;
2434 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2436 int expect_equal
= 0;
2437 struct token
*next
= token
->next
;
2438 struct expression
**tail
= ep
;
2443 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2444 struct expression
*expr
= identifier_expression(token
);
2445 if (Wold_initializer
)
2446 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2447 token
= initializer(&expr
->ident_expression
, next
->next
);
2448 if (expr
->ident_expression
)
2453 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2454 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2455 struct expression
*expr
= identifier_expression(next
);
2457 tail
= &expr
->ident_expression
;
2460 } else if (match_op(token
, '[')) {
2461 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2462 token
= constant_expression(token
->next
, &from
);
2464 sparse_error(token
->pos
, "Expected constant expression");
2467 if (match_op(token
, SPECIAL_ELLIPSIS
))
2468 token
= constant_expression(token
->next
, &to
);
2469 expr
= index_expression(from
, to
);
2471 tail
= &expr
->idx_expression
;
2472 token
= expect(token
, ']', "at end of initializer index");
2479 if (nested
&& !expect_equal
) {
2480 if (!match_op(token
, '='))
2481 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2486 token
= expect(token
, '=', "at end of initializer index");
2488 token
= initializer(tail
, token
);
2494 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2496 struct expression
*expr
;
2499 token
= single_initializer(&expr
, token
);
2502 add_expression(list
, expr
);
2503 if (!match_op(token
, ','))
2505 token
= token
->next
;
2510 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2512 if (match_op(token
, '{')) {
2513 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2515 token
= initializer_list(&expr
->expr_list
, token
->next
);
2516 return expect(token
, '}', "at end of initializer");
2518 return assignment_expression(token
, tree
);
2521 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2524 sparse_error(sym
->pos
, "no identifier for function argument");
2527 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2530 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2531 struct symbol_list
**list
)
2533 struct symbol_list
**old_symbol_list
;
2534 struct symbol
*base_type
= decl
->ctype
.base_type
;
2535 struct statement
*stmt
, **p
;
2536 struct symbol
*prev
;
2539 old_symbol_list
= function_symbol_list
;
2540 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2541 function_symbol_list
= &decl
->inline_symbol_list
;
2542 p
= &base_type
->inline_stmt
;
2544 function_symbol_list
= &decl
->symbol_list
;
2545 p
= &base_type
->stmt
;
2547 function_computed_target_list
= NULL
;
2548 function_computed_goto_list
= NULL
;
2550 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2551 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2552 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2554 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2555 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2557 stmt
= start_function(decl
);
2560 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2561 declare_argument(arg
, base_type
);
2562 } END_FOR_EACH_PTR(arg
);
2564 token
= compound_statement(token
->next
, stmt
);
2567 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2568 add_symbol(list
, decl
);
2569 check_declaration(decl
);
2570 decl
->definition
= decl
;
2571 prev
= decl
->same_symbol
;
2572 if (prev
&& prev
->definition
) {
2573 warning(decl
->pos
, "multiple definitions for function '%s'",
2574 show_ident(decl
->ident
));
2575 info(prev
->definition
->pos
, " the previous one is here");
2578 prev
->definition
= decl
;
2579 prev
= prev
->same_symbol
;
2582 function_symbol_list
= old_symbol_list
;
2583 if (function_computed_goto_list
) {
2584 if (!function_computed_target_list
)
2585 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2587 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2588 stmt
->target_list
= function_computed_target_list
;
2589 } END_FOR_EACH_PTR(stmt
);
2592 return expect(token
, '}', "at end of function");
2595 static void promote_k_r_types(struct symbol
*arg
)
2597 struct symbol
*base
= arg
->ctype
.base_type
;
2598 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2599 arg
->ctype
.base_type
= &int_ctype
;
2603 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2605 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2608 FOR_EACH_PTR(real_args
, arg
) {
2609 struct symbol
*type
;
2611 /* This is quadratic in the number of arguments. We _really_ don't care */
2612 FOR_EACH_PTR(argtypes
, type
) {
2613 if (type
->ident
== arg
->ident
)
2615 } END_FOR_EACH_PTR(type
);
2616 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2620 /* "char" and "short" promote to "int" */
2621 promote_k_r_types(type
);
2623 arg
->ctype
= type
->ctype
;
2624 } END_FOR_EACH_PTR(arg
);
2626 FOR_EACH_PTR(argtypes
, arg
) {
2628 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2629 } END_FOR_EACH_PTR(arg
);
2633 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2634 struct symbol_list
**list
)
2636 struct symbol_list
*args
= NULL
;
2638 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2640 token
= declaration_list(token
, &args
);
2641 if (!match_op(token
, ';')) {
2642 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2645 token
= token
->next
;
2646 } while (lookup_type(token
));
2648 apply_k_r_types(args
, decl
);
2650 if (!match_op(token
, '{')) {
2651 sparse_error(token
->pos
, "expected function body");
2654 return parse_function_body(token
, decl
, list
);
2657 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2659 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2660 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2661 struct statement
*stmt
;
2663 anon
->ctype
.base_type
= fn
;
2664 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2667 token
= parse_asm_statement(token
, stmt
);
2669 add_symbol(list
, anon
);
2673 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2675 struct ident
*ident
= NULL
;
2676 struct symbol
*decl
;
2677 struct decl_state ctx
= { .ident
= &ident
, .ctype
.attribute
= &null_attr
};
2679 struct symbol
*base_type
;
2683 /* Top-level inline asm? */
2684 if (token_type(token
) == TOKEN_IDENT
) {
2685 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2686 if (s
&& s
->op
->toplevel
)
2687 return s
->op
->toplevel(token
, list
);
2690 /* Parse declaration-specifiers, if any */
2691 token
= declaration_specifiers(token
, &ctx
);
2692 mod
= storage_modifiers(&ctx
);
2693 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2694 /* Just a type declaration? */
2695 if (match_op(token
, ';')) {
2696 apply_modifiers(token
->pos
, &ctx
);
2701 token
= declarator(token
, &ctx
);
2702 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2703 apply_modifiers(token
->pos
, &ctx
);
2705 decl
->ctype
= ctx
.ctype
;
2706 decl
->ctype
.modifiers
|= mod
;
2707 decl
->endpos
= token
->pos
;
2709 /* Just a type declaration? */
2711 warning(token
->pos
, "missing identifier in declaration");
2712 return expect(token
, ';', "at the end of type declaration");
2715 /* type define declaration? */
2716 is_typedef
= ctx
.storage_class
== STypedef
;
2718 /* Typedefs don't have meaningful storage */
2720 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2722 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2724 base_type
= decl
->ctype
.base_type
;
2727 if (base_type
&& !base_type
->ident
) {
2728 switch (base_type
->type
) {
2733 base_type
->ident
= ident
;
2736 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2737 /* K&R argument declaration? */
2738 if (lookup_type(token
))
2739 return parse_k_r_arguments(token
, decl
, list
);
2740 if (match_op(token
, '{'))
2741 return parse_function_body(token
, decl
, list
);
2743 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2744 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2745 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2746 sparse_error(token
->pos
, "void declaration");
2750 if (!is_typedef
&& match_op(token
, '=')) {
2751 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2752 warning(decl
->pos
, "symbol with external linkage has initializer");
2753 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2755 token
= initializer(&decl
->initializer
, token
->next
);
2758 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2759 add_symbol(list
, decl
);
2760 fn_local_symbol(decl
);
2763 check_declaration(decl
);
2764 if (decl
->same_symbol
)
2765 decl
->definition
= decl
->same_symbol
->definition
;
2767 if (!match_op(token
, ','))
2770 token
= token
->next
;
2772 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2774 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2775 token
= declarator(token
, &ctx
);
2776 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2777 apply_modifiers(token
->pos
, &ctx
);
2778 decl
->ctype
= ctx
.ctype
;
2779 decl
->ctype
.modifiers
|= mod
;
2780 decl
->endpos
= token
->pos
;
2782 sparse_error(token
->pos
, "expected identifier name in type definition");
2787 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2789 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2791 /* Function declarations are automatically extern unless specifically static */
2792 base_type
= decl
->ctype
.base_type
;
2793 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2794 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2795 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2798 return expect(token
, ';', "at end of declaration");