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 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 #include "expression.h"
47 static struct symbol_list
**function_symbol_list
;
48 struct symbol_list
*function_computed_target_list
;
49 struct statement_list
*function_computed_goto_list
;
51 static struct token
*statement(struct token
*token
, struct statement
**tree
);
52 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
);
54 typedef struct token
*declarator_t(struct token
*, struct decl_state
*);
56 struct_specifier
, union_specifier
, enum_specifier
,
57 attribute_specifier
, typeof_specifier
, parse_asm_declarator
,
58 typedef_specifier
, inline_specifier
, auto_specifier
,
59 register_specifier
, static_specifier
, extern_specifier
,
60 thread_specifier
, const_qualifier
, volatile_qualifier
;
62 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
63 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
64 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
65 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
66 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
67 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
68 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
69 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
70 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
71 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
72 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
73 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
74 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
75 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
77 typedef struct token
*attr_t(struct token
*, struct symbol
*,
81 attribute_packed
, attribute_aligned
, attribute_modifier
,
82 attribute_address_space
, attribute_context
,
83 attribute_designated_init
,
84 attribute_transparent_union
, ignore_attribute
,
85 attribute_mode
, attribute_force
;
87 typedef struct symbol
*to_mode_t(struct symbol
*);
90 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
, to_word_mode
;
104 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
108 CInt
= 0, CSInt
, CUInt
, CReal
, CChar
, CSChar
, CUChar
112 SNone
= 0, STypedef
, SAuto
, SRegister
, SExtern
, SStatic
, SForced
115 static struct symbol_op typedef_op
= {
117 .declarator
= typedef_specifier
,
120 static struct symbol_op inline_op
= {
122 .declarator
= inline_specifier
,
125 static struct symbol_op auto_op
= {
127 .declarator
= auto_specifier
,
130 static struct symbol_op register_op
= {
132 .declarator
= register_specifier
,
135 static struct symbol_op static_op
= {
137 .declarator
= static_specifier
,
140 static struct symbol_op extern_op
= {
142 .declarator
= extern_specifier
,
145 static struct symbol_op thread_op
= {
147 .declarator
= thread_specifier
,
150 static struct symbol_op const_op
= {
151 .type
= KW_QUALIFIER
,
152 .declarator
= const_qualifier
,
155 static struct symbol_op volatile_op
= {
156 .type
= KW_QUALIFIER
,
157 .declarator
= volatile_qualifier
,
160 static struct symbol_op restrict_op
= {
161 .type
= KW_QUALIFIER
,
164 static struct symbol_op typeof_op
= {
165 .type
= KW_SPECIFIER
,
166 .declarator
= typeof_specifier
,
171 static struct symbol_op attribute_op
= {
172 .type
= KW_ATTRIBUTE
,
173 .declarator
= attribute_specifier
,
176 static struct symbol_op struct_op
= {
177 .type
= KW_SPECIFIER
,
178 .declarator
= struct_specifier
,
183 static struct symbol_op union_op
= {
184 .type
= KW_SPECIFIER
,
185 .declarator
= union_specifier
,
190 static struct symbol_op enum_op
= {
191 .type
= KW_SPECIFIER
,
192 .declarator
= enum_specifier
,
197 static struct symbol_op spec_op
= {
198 .type
= KW_SPECIFIER
| KW_EXACT
,
203 static struct symbol_op char_op
= {
204 .type
= KW_SPECIFIER
,
205 .test
= Set_T
|Set_Long
|Set_Short
,
206 .set
= Set_T
|Set_Char
,
210 static struct symbol_op int_op
= {
211 .type
= KW_SPECIFIER
,
213 .set
= Set_T
|Set_Int
,
216 static struct symbol_op double_op
= {
217 .type
= KW_SPECIFIER
,
218 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
219 .set
= Set_T
|Set_Double
,
223 static struct symbol_op float_op
= {
224 .type
= KW_SPECIFIER
| KW_SHORT
,
225 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
226 .set
= Set_T
|Set_Float
,
230 static struct symbol_op short_op
= {
231 .type
= KW_SPECIFIER
| KW_SHORT
,
232 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
236 static struct symbol_op signed_op
= {
237 .type
= KW_SPECIFIER
,
238 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
243 static struct symbol_op unsigned_op
= {
244 .type
= KW_SPECIFIER
,
245 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
250 static struct symbol_op long_op
= {
251 .type
= KW_SPECIFIER
| KW_LONG
,
252 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
256 static struct symbol_op if_op
= {
257 .statement
= parse_if_statement
,
260 static struct symbol_op return_op
= {
261 .statement
= parse_return_statement
,
264 static struct symbol_op loop_iter_op
= {
265 .statement
= parse_loop_iterator
,
268 static struct symbol_op default_op
= {
269 .statement
= parse_default_statement
,
272 static struct symbol_op case_op
= {
273 .statement
= parse_case_statement
,
276 static struct symbol_op switch_op
= {
277 .statement
= parse_switch_statement
,
280 static struct symbol_op for_op
= {
281 .statement
= parse_for_statement
,
284 static struct symbol_op while_op
= {
285 .statement
= parse_while_statement
,
288 static struct symbol_op do_op
= {
289 .statement
= parse_do_statement
,
292 static struct symbol_op goto_op
= {
293 .statement
= parse_goto_statement
,
296 static struct symbol_op __context___op
= {
297 .statement
= parse_context_statement
,
300 static struct symbol_op range_op
= {
301 .statement
= parse_range_statement
,
304 static struct symbol_op asm_op
= {
306 .declarator
= parse_asm_declarator
,
307 .statement
= parse_asm_statement
,
308 .toplevel
= toplevel_asm_declaration
,
311 static struct symbol_op packed_op
= {
312 .attribute
= attribute_packed
,
315 static struct symbol_op aligned_op
= {
316 .attribute
= attribute_aligned
,
319 static struct symbol_op attr_mod_op
= {
320 .attribute
= attribute_modifier
,
323 static struct symbol_op attr_force_op
= {
324 .attribute
= attribute_force
,
327 static struct symbol_op address_space_op
= {
328 .attribute
= attribute_address_space
,
331 static struct symbol_op mode_op
= {
332 .attribute
= attribute_mode
,
335 static struct symbol_op context_op
= {
336 .attribute
= attribute_context
,
339 static struct symbol_op designated_init_op
= {
340 .attribute
= attribute_designated_init
,
343 static struct symbol_op transparent_union_op
= {
344 .attribute
= attribute_transparent_union
,
347 static struct symbol_op ignore_attr_op
= {
348 .attribute
= ignore_attribute
,
351 static struct symbol_op mode_QI_op
= {
353 .to_mode
= to_QI_mode
356 static struct symbol_op mode_HI_op
= {
358 .to_mode
= to_HI_mode
361 static struct symbol_op mode_SI_op
= {
363 .to_mode
= to_SI_mode
366 static struct symbol_op mode_DI_op
= {
368 .to_mode
= to_DI_mode
371 static struct symbol_op mode_TI_op
= {
373 .to_mode
= to_TI_mode
376 static struct symbol_op mode_word_op
= {
378 .to_mode
= to_word_mode
381 static struct init_keyword
{
384 unsigned long modifiers
;
385 struct symbol_op
*op
;
387 } keyword_table
[] = {
388 /* Type qualifiers */
389 { "const", NS_TYPEDEF
, .op
= &const_op
},
390 { "__const", NS_TYPEDEF
, .op
= &const_op
},
391 { "__const__", NS_TYPEDEF
, .op
= &const_op
},
392 { "volatile", NS_TYPEDEF
, .op
= &volatile_op
},
393 { "__volatile", NS_TYPEDEF
, .op
= &volatile_op
},
394 { "__volatile__", NS_TYPEDEF
, .op
= &volatile_op
},
397 { "typedef", NS_TYPEDEF
, .op
= &typedef_op
},
399 /* Type specifiers */
400 { "void", NS_TYPEDEF
, .type
= &void_ctype
, .op
= &spec_op
},
401 { "char", NS_TYPEDEF
, .op
= &char_op
},
402 { "short", NS_TYPEDEF
, .op
= &short_op
},
403 { "int", NS_TYPEDEF
, .op
= &int_op
},
404 { "long", NS_TYPEDEF
, .op
= &long_op
},
405 { "float", NS_TYPEDEF
, .op
= &float_op
},
406 { "double", NS_TYPEDEF
, .op
= &double_op
},
407 { "signed", NS_TYPEDEF
, .op
= &signed_op
},
408 { "__signed", NS_TYPEDEF
, .op
= &signed_op
},
409 { "__signed__", NS_TYPEDEF
, .op
= &signed_op
},
410 { "unsigned", NS_TYPEDEF
, .op
= &unsigned_op
},
411 { "_Bool", NS_TYPEDEF
, .type
= &bool_ctype
, .op
= &spec_op
},
413 /* Predeclared types */
414 { "__builtin_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
415 { "__builtin_ms_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
416 { "__int128_t", NS_TYPEDEF
, .type
= &lllong_ctype
, .op
= &spec_op
},
417 { "__uint128_t",NS_TYPEDEF
, .type
= &ulllong_ctype
, .op
= &spec_op
},
420 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
421 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
422 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
424 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
425 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
427 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
428 { "union", NS_TYPEDEF
, .op
= &union_op
},
429 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
431 { "inline", NS_TYPEDEF
, .op
= &inline_op
},
432 { "__inline", NS_TYPEDEF
, .op
= &inline_op
},
433 { "__inline__", NS_TYPEDEF
, .op
= &inline_op
},
435 /* Ignored for now.. */
436 { "restrict", NS_TYPEDEF
, .op
= &restrict_op
},
437 { "__restrict", NS_TYPEDEF
, .op
= &restrict_op
},
440 { "auto", NS_TYPEDEF
, .op
= &auto_op
},
441 { "register", NS_TYPEDEF
, .op
= ®ister_op
},
442 { "static", NS_TYPEDEF
, .op
= &static_op
},
443 { "extern", NS_TYPEDEF
, .op
= &extern_op
},
444 { "__thread", NS_TYPEDEF
, .op
= &thread_op
},
447 { "if", NS_KEYWORD
, .op
= &if_op
},
448 { "return", NS_KEYWORD
, .op
= &return_op
},
449 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
450 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
451 { "default", NS_KEYWORD
, .op
= &default_op
},
452 { "case", NS_KEYWORD
, .op
= &case_op
},
453 { "switch", NS_KEYWORD
, .op
= &switch_op
},
454 { "for", NS_KEYWORD
, .op
= &for_op
},
455 { "while", NS_KEYWORD
, .op
= &while_op
},
456 { "do", NS_KEYWORD
, .op
= &do_op
},
457 { "goto", NS_KEYWORD
, .op
= &goto_op
},
458 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
459 { "__range__", NS_KEYWORD
, .op
= &range_op
},
460 { "asm", NS_KEYWORD
, .op
= &asm_op
},
461 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
462 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
465 { "packed", NS_KEYWORD
, .op
= &packed_op
},
466 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
467 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
468 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
469 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
470 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
471 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
472 { "force", NS_KEYWORD
, .op
= &attr_force_op
},
473 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
474 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
475 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
476 { "mode", NS_KEYWORD
, .op
= &mode_op
},
477 { "context", NS_KEYWORD
, .op
= &context_op
},
478 { "designated_init", NS_KEYWORD
, .op
= &designated_init_op
},
479 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
480 { "noreturn", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
481 { "__noreturn__", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
482 { "pure", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
483 {"__pure__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
484 {"const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
485 {"__const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
486 {"__const__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
488 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
489 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
490 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
491 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
492 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
493 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
494 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
495 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
496 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
497 { "TI", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
498 { "__TI__", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
499 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
500 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
503 const char *ignored_attributes
[] = {
530 "externally_visible",
531 "__externally_visible__",
559 "__ms_hook_prologue__",
562 "no_instrument_function",
563 "__no_instrument_function__",
586 "__syscall_linkage__",
597 "warn_unused_result",
598 "__warn_unused_result__",
606 void init_parser(int stream
)
609 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
610 struct init_keyword
*ptr
= keyword_table
+ i
;
611 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
612 sym
->ident
->keyword
= 1;
613 if (ptr
->ns
== NS_TYPEDEF
)
614 sym
->ident
->reserved
= 1;
615 sym
->ctype
.modifiers
= ptr
->modifiers
;
616 sym
->ctype
.base_type
= ptr
->type
;
620 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
621 const char * name
= ignored_attributes
[i
];
622 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
624 sym
->ident
->keyword
= 1;
625 sym
->op
= &ignore_attr_op
;
630 // Add a symbol to the list of function-local symbols
631 static void fn_local_symbol(struct symbol
*sym
)
633 if (function_symbol_list
)
634 add_symbol(function_symbol_list
, sym
);
637 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
642 if (token_type(token
) != TOKEN_IDENT
)
645 va_start(args
, token
);
647 next
= va_arg(args
, struct ident
*);
648 } while (next
&& token
->ident
!= next
);
651 return next
&& token
->ident
== next
;
655 struct statement
*alloc_statement(struct position pos
, int type
)
657 struct statement
*stmt
= __alloc_statement(0);
663 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
665 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
667 struct symbol
*ctype
;
670 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
672 sparse_error(pos
, "don't know how to apply mode to %s",
673 show_typename(ctx
->ctype
.base_type
));
675 ctx
->ctype
.base_type
= ctype
;
679 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
681 struct symbol
*sym
= alloc_symbol(pos
, type
);
683 sym
->ctype
.base_type
= ctype
->base_type
;
684 sym
->ctype
.modifiers
= ctype
->modifiers
;
686 ctype
->base_type
= sym
;
687 ctype
->modifiers
= 0;
692 * NOTE! NS_LABEL is not just a different namespace,
693 * it also ends up using function scope instead of the
694 * regular symbol scope.
696 struct symbol
*label_symbol(struct token
*token
)
698 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
700 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
701 bind_symbol(sym
, token
->ident
, NS_LABEL
);
702 fn_local_symbol(sym
);
707 static struct token
*struct_union_enum_specifier(enum type type
,
708 struct token
*token
, struct decl_state
*ctx
,
709 struct token
*(*parse
)(struct token
*, struct symbol
*))
712 struct position
*repos
;
714 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
715 if (token_type(token
) == TOKEN_IDENT
) {
716 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
718 (is_outer_scope(sym
->scope
) &&
719 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
720 // Either a new symbol, or else an out-of-scope
721 // symbol being redefined.
722 sym
= alloc_symbol(token
->pos
, type
);
723 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
725 if (sym
->type
!= type
)
726 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
727 ctx
->ctype
.base_type
= sym
;
730 if (match_op(token
, '{')) {
731 // The following test is actually wrong for empty
732 // structs, but (1) they are not C99, (2) gcc does
733 // the same thing, and (3) it's easier.
734 if (sym
->symbol_list
)
735 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
737 token
= parse(token
->next
, sym
);
738 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
740 // Mark the structure as needing re-examination
742 sym
->endpos
= token
->pos
;
747 // private struct/union/enum type
748 if (!match_op(token
, '{')) {
749 sparse_error(token
->pos
, "expected declaration");
750 ctx
->ctype
.base_type
= &bad_ctype
;
754 sym
= alloc_symbol(token
->pos
, type
);
755 token
= parse(token
->next
, sym
);
756 ctx
->ctype
.base_type
= sym
;
757 token
= expect(token
, '}', "at end of specifier");
758 sym
->endpos
= token
->pos
;
763 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
765 struct symbol
*field
, *last
= NULL
;
767 res
= struct_declaration_list(token
, &sym
->symbol_list
);
768 FOR_EACH_PTR(sym
->symbol_list
, field
) {
770 struct symbol
*base
= field
->ctype
.base_type
;
771 if (base
&& base
->type
== SYM_BITFIELD
)
775 last
->next_subobject
= field
;
777 } END_FOR_EACH_PTR(field
);
781 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
783 return struct_declaration_list(token
, &sym
->symbol_list
);
786 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
788 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
791 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
793 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
799 unsigned long long y
;
802 static void upper_boundary(Num
*n
, Num
*v
)
814 static void lower_boundary(Num
*n
, Num
*v
)
826 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
828 int shift
= type
->bit_size
;
829 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
833 if (upper
->x
== 0 && upper
->y
>> shift
)
835 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
840 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
842 if (s1
->bit_size
< s2
->bit_size
) {
844 } else if (s1
->bit_size
== s2
->bit_size
) {
845 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
848 if (s1
->bit_size
< bits_in_int
)
853 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
857 FOR_EACH_PTR(list
, sym
) {
858 struct expression
*expr
= sym
->initializer
;
859 struct symbol
*ctype
;
860 if (expr
->type
!= EXPR_VALUE
)
863 if (ctype
->bit_size
== base_type
->bit_size
)
865 cast_value(expr
, base_type
, expr
, ctype
);
866 } END_FOR_EACH_PTR(sym
);
869 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
871 unsigned long long lastval
= 0;
872 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
873 Num upper
= {-1, 0}, lower
= {1, 0};
875 parent
->examined
= 1;
876 parent
->ctype
.base_type
= &int_ctype
;
877 while (token_type(token
) == TOKEN_IDENT
) {
878 struct expression
*expr
= NULL
;
879 struct token
*next
= token
->next
;
882 if (match_op(next
, '=')) {
883 next
= constant_expression(next
->next
, &expr
);
884 lastval
= get_expression_value(expr
);
886 if (expr
&& expr
->ctype
)
890 } else if (is_int_type(ctype
)) {
893 error_die(token
->pos
, "can't increment the last enum member");
897 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
898 expr
->value
= lastval
;
902 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
903 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
904 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
905 sym
->initializer
= expr
;
906 sym
->enum_member
= 1;
907 sym
->ctype
.base_type
= parent
;
908 add_ptr_list(&parent
->symbol_list
, sym
);
910 if (base_type
!= &bad_ctype
) {
911 if (ctype
->type
== SYM_NODE
)
912 ctype
= ctype
->ctype
.base_type
;
913 if (ctype
->type
== SYM_ENUM
) {
917 ctype
= ctype
->ctype
.base_type
;
921 * - if all enums are of the same type, then
922 * the base_type is that type (two first
924 * - if enums are of different types, they
925 * all have to be integer types, and the
926 * base type is at least "int_ctype".
927 * - otherwise the base_type is "bad_ctype".
931 } else if (ctype
== base_type
) {
933 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
934 base_type
= bigger_enum_type(base_type
, ctype
);
936 base_type
= &bad_ctype
;
937 parent
->ctype
.base_type
= base_type
;
939 if (is_int_type(base_type
)) {
940 Num v
= {.y
= lastval
};
941 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
943 else if ((long long)lastval
>= 0)
947 upper_boundary(&upper
, &v
);
948 lower_boundary(&lower
, &v
);
952 sym
->endpos
= token
->pos
;
954 if (!match_op(token
, ','))
959 sparse_error(token
->pos
, "bad enum definition");
960 base_type
= &bad_ctype
;
962 else if (!is_int_type(base_type
))
963 base_type
= base_type
;
964 else if (type_is_ok(base_type
, &upper
, &lower
))
965 base_type
= base_type
;
966 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
967 base_type
= &int_ctype
;
968 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
969 base_type
= &uint_ctype
;
970 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
971 base_type
= &long_ctype
;
972 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
973 base_type
= &ulong_ctype
;
974 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
975 base_type
= &llong_ctype
;
976 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
977 base_type
= &ullong_ctype
;
979 base_type
= &bad_ctype
;
980 parent
->ctype
.base_type
= base_type
;
981 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
982 parent
->examined
= 0;
984 cast_enum_list(parent
->symbol_list
, base_type
);
989 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
991 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
992 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
994 if (!ctype
->base_type
)
995 ctype
->base_type
= &incomplete_ctype
;
1000 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
1002 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
1006 if (!match_op(token
, '(')) {
1007 sparse_error(token
->pos
, "expected '(' after typeof");
1010 if (lookup_type(token
->next
)) {
1011 token
= typename(token
->next
, &sym
, NULL
);
1012 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1013 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
1015 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1016 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1018 typeof_sym
->endpos
= token
->pos
;
1019 if (!typeof_sym
->initializer
) {
1020 sparse_error(token
->pos
, "expected expression after the '(' token");
1021 typeof_sym
= &bad_ctype
;
1023 ctx
->ctype
.base_type
= typeof_sym
;
1025 return expect(token
, ')', "after typeof");
1028 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1030 struct expression
*expr
= NULL
;
1031 if (match_op(token
, '('))
1032 token
= parens_expression(token
, &expr
, "in attribute");
1036 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1038 if (!ctx
->ctype
.alignment
)
1039 ctx
->ctype
.alignment
= 1;
1043 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1045 int alignment
= max_alignment
;
1046 struct expression
*expr
= NULL
;
1048 if (match_op(token
, '(')) {
1049 token
= parens_expression(token
, &expr
, "in attribute");
1051 alignment
= const_expression_value(expr
);
1053 if (alignment
& (alignment
-1)) {
1054 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1056 } else if (alignment
> ctx
->ctype
.alignment
)
1057 ctx
->ctype
.alignment
= alignment
;
1061 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1063 if (ctx
->modifiers
& qual
)
1064 warning(*pos
, "duplicate %s", modifier_string(qual
));
1065 ctx
->modifiers
|= qual
;
1068 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1070 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1074 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1076 struct expression
*expr
= NULL
;
1078 token
= expect(token
, '(', "after address_space attribute");
1079 token
= conditional_expression(token
, &expr
);
1081 as
= const_expression_value(expr
);
1082 if (Waddress_space
&& as
)
1085 token
= expect(token
, ')', "after address_space attribute");
1089 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1091 if (ctype
->ctype
.base_type
!= &int_type
)
1093 if (ctype
== &char_ctype
)
1095 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1099 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1101 if (ctype
->ctype
.base_type
!= &int_type
)
1103 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1107 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1109 if (ctype
->ctype
.base_type
!= &int_type
)
1111 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1115 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1117 if (ctype
->ctype
.base_type
!= &int_type
)
1119 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1123 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1125 if (ctype
->ctype
.base_type
!= &int_type
)
1127 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1131 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1133 if (ctype
->ctype
.base_type
!= &int_type
)
1135 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1139 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1141 token
= expect(token
, '(', "after mode attribute");
1142 if (token_type(token
) == TOKEN_IDENT
) {
1143 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1144 if (mode
&& mode
->op
->type
== KW_MODE
)
1145 ctx
->mode
= mode
->op
;
1147 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1148 token
= token
->next
;
1150 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1151 token
= expect(token
, ')', "after mode attribute");
1155 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1157 struct context
*context
= alloc_context();
1158 struct expression
*args
[3];
1161 token
= expect(token
, '(', "after context attribute");
1162 while (!match_op(token
, ')')) {
1163 struct expression
*expr
= NULL
;
1164 token
= conditional_expression(token
, &expr
);
1168 args
[argc
++] = expr
;
1169 if (!match_op(token
, ','))
1171 token
= token
->next
;
1176 sparse_error(token
->pos
, "expected context input/output values");
1179 context
->in
= get_expression_value(args
[0]);
1182 context
->in
= get_expression_value(args
[0]);
1183 context
->out
= get_expression_value(args
[1]);
1186 context
->context
= args
[0];
1187 context
->in
= get_expression_value(args
[1]);
1188 context
->out
= get_expression_value(args
[2]);
1193 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1195 token
= expect(token
, ')', "after context attribute");
1199 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1201 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1202 ctx
->ctype
.base_type
->designated_init
= 1;
1204 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1208 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1210 if (Wtransparent_union
)
1211 warning(token
->pos
, "attribute __transparent_union__");
1213 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1214 ctx
->ctype
.base_type
->transparent_union
= 1;
1216 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1220 static struct token
*recover_unknown_attribute(struct token
*token
)
1222 struct expression
*expr
= NULL
;
1224 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1225 token
= token
->next
;
1226 if (match_op(token
, '('))
1227 token
= parens_expression(token
, &expr
, "in attribute");
1231 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1233 token
= expect(token
, '(', "after attribute");
1234 token
= expect(token
, '(', "after attribute");
1237 struct ident
*attribute_name
;
1238 struct symbol
*attr
;
1240 if (eof_token(token
))
1242 if (match_op(token
, ';'))
1244 if (token_type(token
) != TOKEN_IDENT
)
1246 attribute_name
= token
->ident
;
1247 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1248 if (attr
&& attr
->op
->attribute
)
1249 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1251 token
= recover_unknown_attribute(token
);
1253 if (!match_op(token
, ','))
1255 token
= token
->next
;
1258 token
= expect(token
, ')', "after attribute");
1259 token
= expect(token
, ')', "after attribute");
1263 static const char *storage_class
[] =
1265 [STypedef
] = "typedef",
1267 [SExtern
] = "extern",
1268 [SStatic
] = "static",
1269 [SRegister
] = "register",
1270 [SForced
] = "[force]"
1273 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1275 static unsigned long mod
[] =
1278 [SExtern
] = MOD_EXTERN
,
1279 [SStatic
] = MOD_STATIC
,
1280 [SRegister
] = MOD_REGISTER
1282 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1283 | (ctx
->is_tls
? MOD_TLS
: 0);
1286 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1288 /* __thread can be used alone, or with extern or static */
1289 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1290 sparse_error(*pos
, "__thread can only be used alone, or with "
1291 "extern or static");
1295 if (!ctx
->storage_class
) {
1296 ctx
->storage_class
= class;
1299 if (ctx
->storage_class
== class)
1300 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1302 sparse_error(*pos
, "multiple storage classes");
1305 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1307 set_storage_class(&next
->pos
, ctx
, STypedef
);
1311 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1313 set_storage_class(&next
->pos
, ctx
, SAuto
);
1317 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1319 set_storage_class(&next
->pos
, ctx
, SRegister
);
1323 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1325 set_storage_class(&next
->pos
, ctx
, SStatic
);
1329 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1331 set_storage_class(&next
->pos
, ctx
, SExtern
);
1335 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1337 /* This GCC extension can be used alone, or with extern or static */
1338 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1339 || ctx
->storage_class
== SExtern
) {
1342 sparse_error(next
->pos
, "__thread can only be used alone, or "
1343 "with extern or static");
1349 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1351 set_storage_class(&token
->pos
, ctx
, SForced
);
1355 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1361 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1363 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1367 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1369 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1373 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1375 unsigned long mod
= thistype
->modifiers
;
1378 apply_qualifier(&pos
, ctype
, mod
);
1381 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1382 (struct ptr_list
**)&ctype
->contexts
);
1385 if (thistype
->alignment
> ctype
->alignment
)
1386 ctype
->alignment
= thistype
->alignment
;
1390 ctype
->as
= thistype
->as
;
1393 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1396 if (what
& (Set_S
| Set_T
))
1398 if (what
& Set_Char
)
1400 else if (what
& Set_Double
)
1402 else if (what
& Set_Float
)
1404 else if (what
& Set_Signed
)
1406 else if (what
& Set_Unsigned
)
1408 else if (what
& Set_Short
)
1410 else if (what
& Set_Long
)
1414 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1415 old
, show_ident(new));
1419 sparse_error(pos
, "two or more data types in declaration specifiers");
1422 static struct symbol
* const int_types
[] =
1423 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1424 static struct symbol
* const signed_types
[] =
1425 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1427 static struct symbol
* const unsigned_types
[] =
1428 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1430 static struct symbol
* const real_types
[] =
1431 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1432 static struct symbol
* const char_types
[] =
1433 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1434 static struct symbol
* const * const types
[] = {
1435 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1436 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1439 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1441 return types
[want_unsigned
? CUInt
: CInt
][size
];
1444 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1446 while (token_type(t
) == TOKEN_IDENT
) {
1447 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1450 if (s
->type
!= SYM_KEYWORD
)
1452 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1455 if (s
->op
->declarator
)
1456 t
= s
->op
->declarator(t
, ctx
);
1461 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1467 while (token_type(token
) == TOKEN_IDENT
) {
1468 struct symbol
*s
= lookup_symbol(token
->ident
,
1469 NS_TYPEDEF
| NS_SYMBOL
);
1470 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1472 if (s
->type
!= SYM_KEYWORD
) {
1475 seen
|= Set_S
| Set_T
;
1476 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1477 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1478 token
= token
->next
;
1481 if (s
->op
->type
& KW_SPECIFIER
) {
1482 if (seen
& s
->op
->test
) {
1483 specifier_conflict(token
->pos
,
1489 class += s
->op
->class;
1490 if (s
->op
->type
& KW_SHORT
) {
1492 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1493 if (class == CReal
) {
1494 specifier_conflict(token
->pos
,
1502 token
= token
->next
;
1503 if (s
->op
->declarator
)
1504 token
= s
->op
->declarator(token
, ctx
);
1505 if (s
->op
->type
& KW_EXACT
) {
1506 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1507 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1511 if (!(seen
& Set_S
)) { /* not set explicitly? */
1512 struct symbol
*base
= &incomplete_ctype
;
1514 base
= types
[class][size
];
1515 ctx
->ctype
.base_type
= base
;
1518 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1519 struct symbol
*type
;
1520 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1521 if (!is_int_type(ctx
->ctype
.base_type
)) {
1522 sparse_error(token
->pos
, "invalid modifier");
1525 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1526 *type
= *ctx
->ctype
.base_type
;
1527 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1528 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1529 type
->type
= SYM_RESTRICT
;
1530 ctx
->ctype
.base_type
= type
;
1531 create_fouled(type
);
1536 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1538 struct expression
*expr
= NULL
;
1540 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1541 token
= token
->next
;
1542 token
= parse_expression(token
, &expr
);
1543 sym
->array_size
= expr
;
1547 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1548 static struct token
*identifier_list(struct token
*, struct symbol
*);
1549 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1551 static struct token
*skip_attribute(struct token
*token
)
1553 token
= token
->next
;
1554 if (match_op(token
, '(')) {
1556 token
= token
->next
;
1557 while (depth
&& !eof_token(token
)) {
1558 if (token_type(token
) == TOKEN_SPECIAL
) {
1559 if (token
->special
== '(')
1561 else if (token
->special
== ')')
1564 token
= token
->next
;
1570 static struct token
*skip_attributes(struct token
*token
)
1572 struct symbol
*keyword
;
1574 if (token_type(token
) != TOKEN_IDENT
)
1576 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1577 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1579 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1581 token
= expect(token
->next
, '(', "after attribute");
1582 token
= expect(token
, '(', "after attribute");
1584 if (eof_token(token
))
1586 if (match_op(token
, ';'))
1588 if (token_type(token
) != TOKEN_IDENT
)
1590 token
= skip_attribute(token
);
1591 if (!match_op(token
, ','))
1593 token
= token
->next
;
1595 token
= expect(token
, ')', "after attribute");
1596 token
= expect(token
, ')', "after attribute");
1601 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1603 struct symbol
*keyword
;
1605 if (token_type(token
) != TOKEN_IDENT
)
1607 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1608 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1610 if (!(keyword
->op
->type
& keywords
))
1612 token
= keyword
->op
->declarator(token
->next
, ctx
);
1613 keywords
&= KW_ATTRIBUTE
;
1618 static int is_nested(struct token
*token
, struct token
**p
,
1619 int prefer_abstract
)
1622 * This can be either a parameter list or a grouping.
1623 * For the direct (non-abstract) case, we know if must be
1624 * a parameter list if we already saw the identifier.
1625 * For the abstract case, we know if must be a parameter
1626 * list if it is empty or starts with a type.
1628 struct token
*next
= token
->next
;
1630 *p
= next
= skip_attributes(next
);
1632 if (token_type(next
) == TOKEN_IDENT
) {
1633 if (lookup_type(next
))
1634 return !prefer_abstract
;
1638 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1645 Empty
, K_R
, Proto
, Bad_Func
,
1648 static enum kind
which_func(struct token
*token
,
1650 int prefer_abstract
)
1652 struct token
*next
= token
->next
;
1654 if (token_type(next
) == TOKEN_IDENT
) {
1655 if (lookup_type(next
))
1657 /* identifier list not in definition; complain */
1658 if (prefer_abstract
)
1660 "identifier list not in definition");
1664 if (token_type(next
) != TOKEN_SPECIAL
)
1667 if (next
->special
== ')') {
1668 /* don't complain about those */
1669 if (!n
|| match_op(next
->next
, ';'))
1672 "non-ANSI function declaration of function '%s'",
1677 if (next
->special
== SPECIAL_ELLIPSIS
) {
1679 "variadic functions must have one named argument");
1686 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1688 struct ctype
*ctype
= &ctx
->ctype
;
1690 struct ident
**p
= ctx
->ident
;
1692 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1693 *ctx
->ident
= token
->ident
;
1694 token
= token
->next
;
1695 } else if (match_op(token
, '(') &&
1696 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1697 struct symbol
*base_type
= ctype
->base_type
;
1698 if (token
->next
!= next
)
1699 next
= handle_attributes(token
->next
, ctx
,
1701 token
= declarator(next
, ctx
);
1702 token
= expect(token
, ')', "in nested declarator");
1703 while (ctype
->base_type
!= base_type
)
1704 ctype
= &ctype
->base_type
->ctype
;
1708 if (match_op(token
, '(')) {
1709 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1711 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1712 token
= token
->next
;
1714 token
= identifier_list(token
, fn
);
1715 else if (kind
== Proto
)
1716 token
= parameter_type_list(token
, fn
);
1717 token
= expect(token
, ')', "in function declarator");
1718 fn
->endpos
= token
->pos
;
1722 while (match_op(token
, '[')) {
1723 struct symbol
*array
;
1724 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1725 token
= abstract_array_declarator(token
->next
, array
);
1726 token
= expect(token
, ']', "in abstract_array_declarator");
1727 array
->endpos
= token
->pos
;
1728 ctype
= &array
->ctype
;
1733 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1735 while (match_op(token
,'*')) {
1736 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1737 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1738 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1739 ptr
->ctype
.as
= ctx
->ctype
.as
;
1740 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1741 ctx
->ctype
.modifiers
= 0;
1742 ctx
->ctype
.base_type
= ptr
;
1744 ctx
->ctype
.contexts
= NULL
;
1745 ctx
->ctype
.alignment
= 0;
1747 token
= handle_qualifiers(token
->next
, ctx
);
1748 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1753 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1755 token
= pointer(token
, ctx
);
1756 return direct_declarator(token
, ctx
);
1759 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1761 struct ctype
*ctype
= &ctx
->ctype
;
1762 struct expression
*expr
;
1763 struct symbol
*bitfield
;
1766 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1767 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1768 show_typename(ctype
->base_type
));
1769 // Parse this to recover gracefully.
1770 return conditional_expression(token
->next
, &expr
);
1773 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1774 token
= conditional_expression(token
->next
, &expr
);
1775 width
= const_expression_value(expr
);
1776 bitfield
->bit_size
= width
;
1778 if (width
< 0 || width
> INT_MAX
) {
1779 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1781 } else if (*ctx
->ident
&& width
== 0) {
1782 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1783 show_ident(*ctx
->ident
));
1785 } else if (*ctx
->ident
) {
1786 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1787 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1788 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1789 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1790 // Valid values are either {-1;0} or {0}, depending on integer
1791 // representation. The latter makes for very efficient code...
1792 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1794 if (Wdefault_bitfield_sign
&&
1795 bitfield_type
->type
!= SYM_ENUM
&&
1796 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1798 // The sign of bitfields is unspecified by default.
1799 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1802 bitfield
->bit_size
= width
;
1803 bitfield
->endpos
= token
->pos
;
1807 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1809 struct decl_state ctx
= {.prefer_abstract
= 0};
1813 token
= declaration_specifiers(token
, &ctx
);
1814 mod
= storage_modifiers(&ctx
);
1817 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1818 ctx
.ident
= &decl
->ident
;
1820 token
= declarator(token
, &ctx
);
1821 if (match_op(token
, ':'))
1822 token
= handle_bitfield(token
, &ctx
);
1824 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1825 apply_modifiers(token
->pos
, &ctx
);
1827 decl
->ctype
= ctx
.ctype
;
1828 decl
->ctype
.modifiers
|= mod
;
1829 decl
->endpos
= token
->pos
;
1830 add_symbol(list
, decl
);
1831 if (!match_op(token
, ','))
1833 token
= token
->next
;
1839 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1841 while (!match_op(token
, '}')) {
1842 if (!match_op(token
, ';'))
1843 token
= declaration_list(token
, list
);
1844 if (!match_op(token
, ';')) {
1845 sparse_error(token
->pos
, "expected ; at end of declaration");
1848 token
= token
->next
;
1853 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1855 struct decl_state ctx
= {.prefer_abstract
= 1};
1857 token
= declaration_specifiers(token
, &ctx
);
1858 ctx
.ident
= &sym
->ident
;
1859 token
= declarator(token
, &ctx
);
1860 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1861 apply_modifiers(token
->pos
, &ctx
);
1862 sym
->ctype
= ctx
.ctype
;
1863 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1864 sym
->endpos
= token
->pos
;
1865 sym
->forced_arg
= ctx
.storage_class
== SForced
;
1869 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1871 struct decl_state ctx
= {.prefer_abstract
= 1};
1873 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1875 token
= declaration_specifiers(token
, &ctx
);
1876 token
= declarator(token
, &ctx
);
1877 apply_modifiers(token
->pos
, &ctx
);
1878 sym
->ctype
= ctx
.ctype
;
1879 sym
->endpos
= token
->pos
;
1880 class = ctx
.storage_class
;
1883 if (class == SForced
) {
1889 warning(sym
->pos
, "storage class in typename (%s %s)",
1890 storage_class
[class], show_typename(sym
));
1894 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1896 token
= parse_expression(token
, tree
);
1897 return expect(token
, ';', "at end of statement");
1900 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1901 struct expression_list
**inout
)
1903 struct expression
*expr
;
1905 /* Allow empty operands */
1906 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1909 struct ident
*ident
= NULL
;
1910 if (match_op(token
->next
, '[') &&
1911 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1912 match_op(token
->next
->next
->next
, ']')) {
1913 ident
= token
->next
->next
->ident
;
1914 token
= token
->next
->next
->next
;
1916 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1917 token
= primary_expression(token
->next
, &expr
);
1918 add_expression(inout
, expr
);
1919 token
= parens_expression(token
, &expr
, "in asm parameter");
1920 add_expression(inout
, expr
);
1921 } while (match_op(token
, ','));
1925 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1926 struct expression_list
**clobbers
)
1928 struct expression
*expr
;
1931 token
= primary_expression(token
->next
, &expr
);
1933 add_expression(clobbers
, expr
);
1934 } while (match_op(token
, ','));
1938 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1939 struct symbol_list
**labels
)
1941 struct symbol
*label
;
1944 token
= token
->next
; /* skip ':' and ',' */
1945 if (token_type(token
) != TOKEN_IDENT
)
1947 label
= label_symbol(token
);
1948 add_symbol(labels
, label
);
1949 token
= token
->next
;
1950 } while (match_op(token
, ','));
1954 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1958 token
= token
->next
;
1959 stmt
->type
= STMT_ASM
;
1960 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1961 token
= token
->next
;
1963 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
1965 token
= token
->next
;
1967 token
= expect(token
, '(', "after asm");
1968 token
= parse_expression(token
, &stmt
->asm_string
);
1969 if (match_op(token
, ':'))
1970 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1971 if (match_op(token
, ':'))
1972 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1973 if (match_op(token
, ':'))
1974 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1975 if (is_goto
&& match_op(token
, ':'))
1976 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
1977 token
= expect(token
, ')', "after asm");
1978 return expect(token
, ';', "at end of asm-statement");
1981 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1983 struct expression
*expr
;
1984 token
= expect(token
, '(', "after asm");
1985 token
= parse_expression(token
->next
, &expr
);
1986 token
= expect(token
, ')', "after asm");
1990 /* Make a statement out of an expression */
1991 static struct statement
*make_statement(struct expression
*expr
)
1993 struct statement
*stmt
;
1997 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1998 stmt
->expression
= expr
;
2003 * All iterators have two symbols associated with them:
2004 * the "continue" and "break" symbols, which are targets
2005 * for continue and break statements respectively.
2007 * They are in a special name-space, but they follow
2008 * all the normal visibility rules, so nested iterators
2009 * automatically work right.
2011 static void start_iterator(struct statement
*stmt
)
2013 struct symbol
*cont
, *brk
;
2015 start_symbol_scope();
2016 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2017 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2018 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2019 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2021 stmt
->type
= STMT_ITERATOR
;
2022 stmt
->iterator_break
= brk
;
2023 stmt
->iterator_continue
= cont
;
2024 fn_local_symbol(brk
);
2025 fn_local_symbol(cont
);
2028 static void end_iterator(struct statement
*stmt
)
2033 static struct statement
*start_function(struct symbol
*sym
)
2036 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2038 start_function_scope();
2039 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2040 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2041 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2042 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2043 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2045 fn_local_symbol(ret
);
2047 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2053 static void end_function(struct symbol
*sym
)
2056 end_function_scope();
2060 * A "switch()" statement, like an iterator, has a
2061 * the "break" symbol associated with it. It works
2062 * exactly like the iterator break - it's the target
2063 * for any break-statements in scope, and means that
2064 * "break" handling doesn't even need to know whether
2065 * it's breaking out of an iterator or a switch.
2067 * In addition, the "case" symbol is a marker for the
2068 * case/default statements to find the switch statement
2069 * that they are associated with.
2071 static void start_switch(struct statement
*stmt
)
2073 struct symbol
*brk
, *switch_case
;
2075 start_symbol_scope();
2076 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2077 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2079 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2080 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2081 switch_case
->stmt
= stmt
;
2083 stmt
->type
= STMT_SWITCH
;
2084 stmt
->switch_break
= brk
;
2085 stmt
->switch_case
= switch_case
;
2087 fn_local_symbol(brk
);
2088 fn_local_symbol(switch_case
);
2091 static void end_switch(struct statement
*stmt
)
2093 if (!stmt
->switch_case
->symbol_list
)
2094 warning(stmt
->pos
, "switch with no cases");
2098 static void add_case_statement(struct statement
*stmt
)
2100 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2104 sparse_error(stmt
->pos
, "not in switch scope");
2105 stmt
->type
= STMT_NONE
;
2108 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2109 add_symbol(&target
->symbol_list
, sym
);
2111 stmt
->case_label
= sym
;
2112 fn_local_symbol(sym
);
2115 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2117 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2120 error_die(token
->pos
, "internal error: return without a function target");
2121 stmt
->type
= STMT_RETURN
;
2122 stmt
->ret_target
= target
;
2123 return expression_statement(token
->next
, &stmt
->ret_value
);
2126 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2128 struct symbol_list
*syms
;
2129 struct expression
*e1
, *e2
, *e3
;
2130 struct statement
*iterator
;
2132 start_iterator(stmt
);
2133 token
= expect(token
->next
, '(', "after 'for'");
2137 /* C99 variable declaration? */
2138 if (lookup_type(token
)) {
2139 token
= external_declaration(token
, &syms
);
2141 token
= parse_expression(token
, &e1
);
2142 token
= expect(token
, ';', "in 'for'");
2144 token
= parse_expression(token
, &e2
);
2145 token
= expect(token
, ';', "in 'for'");
2146 token
= parse_expression(token
, &e3
);
2147 token
= expect(token
, ')', "in 'for'");
2148 token
= statement(token
, &iterator
);
2150 stmt
->iterator_syms
= syms
;
2151 stmt
->iterator_pre_statement
= make_statement(e1
);
2152 stmt
->iterator_pre_condition
= e2
;
2153 stmt
->iterator_post_statement
= make_statement(e3
);
2154 stmt
->iterator_post_condition
= NULL
;
2155 stmt
->iterator_statement
= iterator
;
2161 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2163 struct expression
*expr
;
2164 struct statement
*iterator
;
2166 start_iterator(stmt
);
2167 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2168 token
= statement(token
, &iterator
);
2170 stmt
->iterator_pre_condition
= expr
;
2171 stmt
->iterator_post_condition
= NULL
;
2172 stmt
->iterator_statement
= iterator
;
2178 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2180 struct expression
*expr
;
2181 struct statement
*iterator
;
2183 start_iterator(stmt
);
2184 token
= statement(token
->next
, &iterator
);
2185 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2186 token
= token
->next
;
2188 sparse_error(token
->pos
, "expected 'while' after 'do'");
2189 token
= parens_expression(token
, &expr
, "after 'do-while'");
2191 stmt
->iterator_post_condition
= expr
;
2192 stmt
->iterator_statement
= iterator
;
2195 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2196 warning(iterator
->pos
, "do-while statement is not a compound statement");
2198 return expect(token
, ';', "after statement");
2201 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2203 stmt
->type
= STMT_IF
;
2204 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2205 token
= statement(token
, &stmt
->if_true
);
2206 if (token_type(token
) != TOKEN_IDENT
)
2208 if (token
->ident
!= &else_ident
)
2210 return statement(token
->next
, &stmt
->if_false
);
2213 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2215 stmt
->type
= STMT_CASE
;
2216 token
= expect(token
, ':', "after default/case");
2217 add_case_statement(stmt
);
2218 return statement(token
, &stmt
->case_statement
);
2221 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2223 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2224 if (match_op(token
, SPECIAL_ELLIPSIS
))
2225 token
= parse_expression(token
->next
, &stmt
->case_to
);
2226 return case_statement(token
, stmt
);
2229 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2231 return case_statement(token
->next
, stmt
);
2234 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2236 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2237 stmt
->type
= STMT_GOTO
;
2238 stmt
->goto_label
= target
;
2240 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2241 return expect(token
->next
, ';', "at end of statement");
2244 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2246 stmt
->type
= STMT_SWITCH
;
2248 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2249 token
= statement(token
, &stmt
->switch_statement
);
2254 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2256 stmt
->type
= STMT_GOTO
;
2257 token
= token
->next
;
2258 if (match_op(token
, '*')) {
2259 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2260 add_statement(&function_computed_goto_list
, stmt
);
2261 } else if (token_type(token
) == TOKEN_IDENT
) {
2262 stmt
->goto_label
= label_symbol(token
);
2263 token
= token
->next
;
2265 sparse_error(token
->pos
, "Expected identifier or goto expression");
2267 return expect(token
, ';', "at end of statement");
2270 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2272 stmt
->type
= STMT_CONTEXT
;
2273 token
= parse_expression(token
->next
, &stmt
->expression
);
2274 if (stmt
->expression
->type
== EXPR_PREOP
2275 && stmt
->expression
->op
== '('
2276 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2277 struct expression
*expr
;
2278 expr
= stmt
->expression
->unop
;
2279 stmt
->context
= expr
->left
;
2280 stmt
->expression
= expr
->right
;
2282 return expect(token
, ';', "at end of statement");
2285 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2287 stmt
->type
= STMT_RANGE
;
2288 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2289 token
= expect(token
, ',', "after range expression");
2290 token
= assignment_expression(token
, &stmt
->range_low
);
2291 token
= expect(token
, ',', "after low range");
2292 token
= assignment_expression(token
, &stmt
->range_high
);
2293 return expect(token
, ';', "after range statement");
2296 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2298 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2301 if (token_type(token
) == TOKEN_IDENT
) {
2302 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2303 if (s
&& s
->op
->statement
)
2304 return s
->op
->statement(token
, stmt
);
2306 if (match_op(token
->next
, ':')) {
2307 struct symbol
*s
= label_symbol(token
);
2308 stmt
->type
= STMT_LABEL
;
2309 stmt
->label_identifier
= s
;
2311 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(token
->ident
));
2313 token
= skip_attributes(token
->next
->next
);
2314 return statement(token
, &stmt
->label_statement
);
2318 if (match_op(token
, '{')) {
2319 stmt
->type
= STMT_COMPOUND
;
2320 start_symbol_scope();
2321 token
= compound_statement(token
->next
, stmt
);
2324 return expect(token
, '}', "at end of compound statement");
2327 stmt
->type
= STMT_EXPRESSION
;
2328 return expression_statement(token
, &stmt
->expression
);
2331 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2332 static struct token
*label_statement(struct token
*token
)
2334 while (token_type(token
) == TOKEN_IDENT
) {
2335 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2336 /* it's block-scope, but we want label namespace */
2337 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2338 sym
->namespace = NS_LABEL
;
2339 fn_local_symbol(sym
);
2340 token
= token
->next
;
2341 if (!match_op(token
, ','))
2343 token
= token
->next
;
2345 return expect(token
, ';', "at end of label declaration");
2348 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2350 int seen_statement
= 0;
2351 while (token_type(token
) == TOKEN_IDENT
&&
2352 token
->ident
== &__label___ident
)
2353 token
= label_statement(token
->next
);
2355 struct statement
* stmt
;
2356 if (eof_token(token
))
2358 if (match_op(token
, '}'))
2360 if (lookup_type(token
)) {
2361 if (seen_statement
) {
2362 warning(token
->pos
, "mixing declarations and code");
2365 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2366 token
= external_declaration(token
, &stmt
->declaration
);
2368 seen_statement
= Wdeclarationafterstatement
;
2369 token
= statement(token
, &stmt
);
2371 add_statement(list
, stmt
);
2376 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2378 struct symbol_list
**list
= &fn
->arguments
;
2380 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2381 sym
->ident
= token
->ident
;
2382 token
= token
->next
;
2383 sym
->endpos
= token
->pos
;
2384 sym
->ctype
.base_type
= &incomplete_ctype
;
2385 add_symbol(list
, sym
);
2386 if (!match_op(token
, ',') ||
2387 token_type(token
->next
) != TOKEN_IDENT
||
2388 lookup_type(token
->next
))
2390 token
= token
->next
;
2395 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2397 struct symbol_list
**list
= &fn
->arguments
;
2402 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2404 token
= token
->next
;
2408 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2409 token
= parameter_declaration(token
, sym
);
2410 if (sym
->ctype
.base_type
== &void_ctype
) {
2411 /* Special case: (void) */
2412 if (!*list
&& !sym
->ident
)
2414 warning(token
->pos
, "void parameter");
2416 add_symbol(list
, sym
);
2417 if (!match_op(token
, ','))
2419 token
= token
->next
;
2424 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2426 token
= statement_list(token
, &stmt
->stmts
);
2430 static struct expression
*identifier_expression(struct token
*token
)
2432 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2433 expr
->expr_ident
= token
->ident
;
2437 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2439 int idx_from
, idx_to
;
2440 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2442 idx_from
= const_expression_value(from
);
2445 idx_to
= const_expression_value(to
);
2446 if (idx_to
< idx_from
|| idx_from
< 0)
2447 warning(from
->pos
, "nonsense array initializer index range");
2449 expr
->idx_from
= idx_from
;
2450 expr
->idx_to
= idx_to
;
2454 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2456 int expect_equal
= 0;
2457 struct token
*next
= token
->next
;
2458 struct expression
**tail
= ep
;
2463 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2464 struct expression
*expr
= identifier_expression(token
);
2465 if (Wold_initializer
)
2466 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2467 token
= initializer(&expr
->ident_expression
, next
->next
);
2468 if (expr
->ident_expression
)
2473 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2474 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2475 struct expression
*expr
= identifier_expression(next
);
2477 tail
= &expr
->ident_expression
;
2480 } else if (match_op(token
, '[')) {
2481 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2482 token
= constant_expression(token
->next
, &from
);
2484 sparse_error(token
->pos
, "Expected constant expression");
2487 if (match_op(token
, SPECIAL_ELLIPSIS
))
2488 token
= constant_expression(token
->next
, &to
);
2489 expr
= index_expression(from
, to
);
2491 tail
= &expr
->idx_expression
;
2492 token
= expect(token
, ']', "at end of initializer index");
2499 if (nested
&& !expect_equal
) {
2500 if (!match_op(token
, '='))
2501 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2506 token
= expect(token
, '=', "at end of initializer index");
2508 token
= initializer(tail
, token
);
2514 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2516 struct expression
*expr
;
2519 token
= single_initializer(&expr
, token
);
2522 add_expression(list
, expr
);
2523 if (!match_op(token
, ','))
2525 token
= token
->next
;
2530 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2532 if (match_op(token
, '{')) {
2533 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2535 token
= initializer_list(&expr
->expr_list
, token
->next
);
2536 return expect(token
, '}', "at end of initializer");
2538 return assignment_expression(token
, tree
);
2541 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2544 sparse_error(sym
->pos
, "no identifier for function argument");
2547 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2550 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2551 struct symbol_list
**list
)
2553 struct symbol_list
**old_symbol_list
;
2554 struct symbol
*base_type
= decl
->ctype
.base_type
;
2555 struct statement
*stmt
, **p
;
2556 struct symbol
*prev
;
2559 old_symbol_list
= function_symbol_list
;
2560 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2561 function_symbol_list
= &decl
->inline_symbol_list
;
2562 p
= &base_type
->inline_stmt
;
2564 function_symbol_list
= &decl
->symbol_list
;
2565 p
= &base_type
->stmt
;
2567 function_computed_target_list
= NULL
;
2568 function_computed_goto_list
= NULL
;
2570 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2571 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2572 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2574 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2575 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2577 stmt
= start_function(decl
);
2580 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2581 declare_argument(arg
, base_type
);
2582 } END_FOR_EACH_PTR(arg
);
2584 token
= compound_statement(token
->next
, stmt
);
2587 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2588 add_symbol(list
, decl
);
2589 check_declaration(decl
);
2590 decl
->definition
= decl
;
2591 prev
= decl
->same_symbol
;
2592 if (prev
&& prev
->definition
) {
2593 warning(decl
->pos
, "multiple definitions for function '%s'",
2594 show_ident(decl
->ident
));
2595 info(prev
->definition
->pos
, " the previous one is here");
2598 prev
->definition
= decl
;
2599 prev
= prev
->same_symbol
;
2602 function_symbol_list
= old_symbol_list
;
2603 if (function_computed_goto_list
) {
2604 if (!function_computed_target_list
)
2605 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2607 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2608 stmt
->target_list
= function_computed_target_list
;
2609 } END_FOR_EACH_PTR(stmt
);
2612 return expect(token
, '}', "at end of function");
2615 static void promote_k_r_types(struct symbol
*arg
)
2617 struct symbol
*base
= arg
->ctype
.base_type
;
2618 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2619 arg
->ctype
.base_type
= &int_ctype
;
2623 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2625 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2628 FOR_EACH_PTR(real_args
, arg
) {
2629 struct symbol
*type
;
2631 /* This is quadratic in the number of arguments. We _really_ don't care */
2632 FOR_EACH_PTR(argtypes
, type
) {
2633 if (type
->ident
== arg
->ident
)
2635 } END_FOR_EACH_PTR(type
);
2636 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2640 /* "char" and "short" promote to "int" */
2641 promote_k_r_types(type
);
2643 arg
->ctype
= type
->ctype
;
2644 } END_FOR_EACH_PTR(arg
);
2646 FOR_EACH_PTR(argtypes
, arg
) {
2648 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2649 } END_FOR_EACH_PTR(arg
);
2653 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2654 struct symbol_list
**list
)
2656 struct symbol_list
*args
= NULL
;
2658 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2660 token
= declaration_list(token
, &args
);
2661 if (!match_op(token
, ';')) {
2662 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2665 token
= token
->next
;
2666 } while (lookup_type(token
));
2668 apply_k_r_types(args
, decl
);
2670 if (!match_op(token
, '{')) {
2671 sparse_error(token
->pos
, "expected function body");
2674 return parse_function_body(token
, decl
, list
);
2677 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2679 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2680 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2681 struct statement
*stmt
;
2683 anon
->ctype
.base_type
= fn
;
2684 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2687 token
= parse_asm_statement(token
, stmt
);
2689 add_symbol(list
, anon
);
2693 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2695 struct ident
*ident
= NULL
;
2696 struct symbol
*decl
;
2697 struct decl_state ctx
= { .ident
= &ident
};
2699 struct symbol
*base_type
;
2703 /* Top-level inline asm? */
2704 if (token_type(token
) == TOKEN_IDENT
) {
2705 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2706 if (s
&& s
->op
->toplevel
)
2707 return s
->op
->toplevel(token
, list
);
2710 /* Parse declaration-specifiers, if any */
2711 token
= declaration_specifiers(token
, &ctx
);
2712 mod
= storage_modifiers(&ctx
);
2713 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2714 /* Just a type declaration? */
2715 if (match_op(token
, ';')) {
2716 apply_modifiers(token
->pos
, &ctx
);
2721 token
= declarator(token
, &ctx
);
2722 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2723 apply_modifiers(token
->pos
, &ctx
);
2725 decl
->ctype
= ctx
.ctype
;
2726 decl
->ctype
.modifiers
|= mod
;
2727 decl
->endpos
= token
->pos
;
2729 /* Just a type declaration? */
2731 warning(token
->pos
, "missing identifier in declaration");
2732 return expect(token
, ';', "at the end of type declaration");
2735 /* type define declaration? */
2736 is_typedef
= ctx
.storage_class
== STypedef
;
2738 /* Typedefs don't have meaningful storage */
2740 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2742 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2744 base_type
= decl
->ctype
.base_type
;
2747 if (base_type
&& !base_type
->ident
) {
2748 switch (base_type
->type
) {
2753 base_type
->ident
= ident
;
2756 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2757 /* K&R argument declaration? */
2758 if (lookup_type(token
))
2759 return parse_k_r_arguments(token
, decl
, list
);
2760 if (match_op(token
, '{'))
2761 return parse_function_body(token
, decl
, list
);
2763 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2764 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2765 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2766 sparse_error(token
->pos
, "void declaration");
2770 if (!is_typedef
&& match_op(token
, '=')) {
2771 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2772 warning(decl
->pos
, "symbol with external linkage has initializer");
2773 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2775 token
= initializer(&decl
->initializer
, token
->next
);
2778 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2779 add_symbol(list
, decl
);
2780 fn_local_symbol(decl
);
2783 check_declaration(decl
);
2784 if (decl
->same_symbol
)
2785 decl
->definition
= decl
->same_symbol
->definition
;
2787 if (!match_op(token
, ','))
2790 token
= token
->next
;
2792 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2794 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2795 token
= declarator(token
, &ctx
);
2796 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2797 apply_modifiers(token
->pos
, &ctx
);
2798 decl
->ctype
= ctx
.ctype
;
2799 decl
->ctype
.modifiers
|= mod
;
2800 decl
->endpos
= token
->pos
;
2802 sparse_error(token
->pos
, "expected identifier name in type definition");
2807 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2809 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2811 /* Function declarations are automatically extern unless specifically static */
2812 base_type
= decl
->ctype
.base_type
;
2813 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2814 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2815 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2818 return expect(token
, ';', "at end of declaration");