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
);
76 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
);
78 typedef struct token
*attr_t(struct token
*, struct symbol
*,
82 attribute_packed
, attribute_aligned
, attribute_modifier
,
84 attribute_address_space
, attribute_context
,
85 attribute_designated_init
,
86 attribute_transparent_union
, ignore_attribute
,
87 attribute_mode
, attribute_force
;
89 typedef struct symbol
*to_mode_t(struct symbol
*);
92 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
, to_word_mode
;
107 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
111 CInt
= 0, CSInt
, CUInt
, CReal
, CChar
, CSChar
, CUChar
,
115 SNone
= 0, STypedef
, SAuto
, SRegister
, SExtern
, SStatic
, SForced
, SMax
,
118 static struct symbol_op typedef_op
= {
120 .declarator
= typedef_specifier
,
123 static struct symbol_op inline_op
= {
125 .declarator
= inline_specifier
,
128 static declarator_t noreturn_specifier
;
129 static struct symbol_op noreturn_op
= {
131 .declarator
= noreturn_specifier
,
134 static declarator_t alignas_specifier
;
135 static struct symbol_op alignas_op
= {
137 .declarator
= alignas_specifier
,
140 static struct symbol_op auto_op
= {
142 .declarator
= auto_specifier
,
145 static struct symbol_op register_op
= {
147 .declarator
= register_specifier
,
150 static struct symbol_op static_op
= {
152 .declarator
= static_specifier
,
155 static struct symbol_op extern_op
= {
157 .declarator
= extern_specifier
,
160 static struct symbol_op thread_op
= {
162 .declarator
= thread_specifier
,
165 static struct symbol_op const_op
= {
166 .type
= KW_QUALIFIER
,
167 .declarator
= const_qualifier
,
170 static struct symbol_op volatile_op
= {
171 .type
= KW_QUALIFIER
,
172 .declarator
= volatile_qualifier
,
175 static struct symbol_op restrict_op
= {
176 .type
= KW_QUALIFIER
,
179 static struct symbol_op typeof_op
= {
180 .type
= KW_SPECIFIER
,
181 .declarator
= typeof_specifier
,
186 static struct symbol_op attribute_op
= {
187 .type
= KW_ATTRIBUTE
,
188 .declarator
= attribute_specifier
,
191 static struct symbol_op struct_op
= {
192 .type
= KW_SPECIFIER
,
193 .declarator
= struct_specifier
,
198 static struct symbol_op union_op
= {
199 .type
= KW_SPECIFIER
,
200 .declarator
= union_specifier
,
205 static struct symbol_op enum_op
= {
206 .type
= KW_SPECIFIER
,
207 .declarator
= enum_specifier
,
212 static struct symbol_op spec_op
= {
213 .type
= KW_SPECIFIER
| KW_EXACT
,
218 static struct symbol_op char_op
= {
219 .type
= KW_SPECIFIER
,
220 .test
= Set_T
|Set_Long
|Set_Short
,
221 .set
= Set_T
|Set_Char
,
225 static struct symbol_op int_op
= {
226 .type
= KW_SPECIFIER
,
228 .set
= Set_T
|Set_Int
,
231 static struct symbol_op double_op
= {
232 .type
= KW_SPECIFIER
,
233 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
234 .set
= Set_T
|Set_Double
,
238 static struct symbol_op float_op
= {
239 .type
= KW_SPECIFIER
| KW_SHORT
,
240 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
241 .set
= Set_T
|Set_Float
,
245 static struct symbol_op short_op
= {
246 .type
= KW_SPECIFIER
| KW_SHORT
,
247 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
251 static struct symbol_op signed_op
= {
252 .type
= KW_SPECIFIER
,
253 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
258 static struct symbol_op unsigned_op
= {
259 .type
= KW_SPECIFIER
,
260 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
265 static struct symbol_op long_op
= {
266 .type
= KW_SPECIFIER
| KW_LONG
,
267 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
271 static struct symbol_op int128_op
= {
272 .type
= KW_SPECIFIER
| KW_LONG
,
273 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
274 .set
= Set_T
|Set_Int128
,
277 static struct symbol_op if_op
= {
278 .statement
= parse_if_statement
,
281 static struct symbol_op return_op
= {
282 .statement
= parse_return_statement
,
285 static struct symbol_op loop_iter_op
= {
286 .statement
= parse_loop_iterator
,
289 static struct symbol_op default_op
= {
290 .statement
= parse_default_statement
,
293 static struct symbol_op case_op
= {
294 .statement
= parse_case_statement
,
297 static struct symbol_op switch_op
= {
298 .statement
= parse_switch_statement
,
301 static struct symbol_op for_op
= {
302 .statement
= parse_for_statement
,
305 static struct symbol_op while_op
= {
306 .statement
= parse_while_statement
,
309 static struct symbol_op do_op
= {
310 .statement
= parse_do_statement
,
313 static struct symbol_op goto_op
= {
314 .statement
= parse_goto_statement
,
317 static struct symbol_op __context___op
= {
318 .statement
= parse_context_statement
,
321 static struct symbol_op range_op
= {
322 .statement
= parse_range_statement
,
325 static struct symbol_op asm_op
= {
327 .declarator
= parse_asm_declarator
,
328 .statement
= parse_asm_statement
,
329 .toplevel
= toplevel_asm_declaration
,
332 static struct symbol_op static_assert_op
= {
333 .toplevel
= parse_static_assert
,
336 static struct symbol_op packed_op
= {
337 .attribute
= attribute_packed
,
340 static struct symbol_op aligned_op
= {
341 .attribute
= attribute_aligned
,
344 static struct symbol_op attr_mod_op
= {
345 .attribute
= attribute_modifier
,
348 static struct symbol_op attr_bitwise_op
= {
349 .attribute
= attribute_bitwise
,
352 static struct symbol_op attr_force_op
= {
353 .attribute
= attribute_force
,
356 static struct symbol_op address_space_op
= {
357 .attribute
= attribute_address_space
,
360 static struct symbol_op mode_op
= {
361 .attribute
= attribute_mode
,
364 static struct symbol_op context_op
= {
365 .attribute
= attribute_context
,
368 static struct symbol_op designated_init_op
= {
369 .attribute
= attribute_designated_init
,
372 static struct symbol_op transparent_union_op
= {
373 .attribute
= attribute_transparent_union
,
376 static struct symbol_op ignore_attr_op
= {
377 .attribute
= ignore_attribute
,
380 static struct symbol_op mode_QI_op
= {
382 .to_mode
= to_QI_mode
385 static struct symbol_op mode_HI_op
= {
387 .to_mode
= to_HI_mode
390 static struct symbol_op mode_SI_op
= {
392 .to_mode
= to_SI_mode
395 static struct symbol_op mode_DI_op
= {
397 .to_mode
= to_DI_mode
400 static struct symbol_op mode_TI_op
= {
402 .to_mode
= to_TI_mode
405 static struct symbol_op mode_word_op
= {
407 .to_mode
= to_word_mode
410 /* Using NS_TYPEDEF will also make the keyword a reserved one */
411 static struct init_keyword
{
414 unsigned long modifiers
;
415 struct symbol_op
*op
;
417 } keyword_table
[] = {
418 /* Type qualifiers */
419 { "const", NS_TYPEDEF
, .op
= &const_op
},
420 { "__const", NS_TYPEDEF
, .op
= &const_op
},
421 { "__const__", NS_TYPEDEF
, .op
= &const_op
},
422 { "volatile", NS_TYPEDEF
, .op
= &volatile_op
},
423 { "__volatile", NS_TYPEDEF
, .op
= &volatile_op
},
424 { "__volatile__", NS_TYPEDEF
, .op
= &volatile_op
},
427 { "typedef", NS_TYPEDEF
, .op
= &typedef_op
},
429 /* Type specifiers */
430 { "void", NS_TYPEDEF
, .type
= &void_ctype
, .op
= &spec_op
},
431 { "char", NS_TYPEDEF
, .op
= &char_op
},
432 { "short", NS_TYPEDEF
, .op
= &short_op
},
433 { "int", NS_TYPEDEF
, .op
= &int_op
},
434 { "long", NS_TYPEDEF
, .op
= &long_op
},
435 { "float", NS_TYPEDEF
, .op
= &float_op
},
436 { "double", NS_TYPEDEF
, .op
= &double_op
},
437 { "signed", NS_TYPEDEF
, .op
= &signed_op
},
438 { "__signed", NS_TYPEDEF
, .op
= &signed_op
},
439 { "__signed__", NS_TYPEDEF
, .op
= &signed_op
},
440 { "unsigned", NS_TYPEDEF
, .op
= &unsigned_op
},
441 { "__int128", NS_TYPEDEF
, .op
= &int128_op
},
442 { "_Bool", NS_TYPEDEF
, .type
= &bool_ctype
, .op
= &spec_op
},
444 /* Predeclared types */
445 { "__builtin_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
446 { "__builtin_ms_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
447 { "__int128_t", NS_TYPEDEF
, .type
= &lllong_ctype
, .op
= &spec_op
},
448 { "__uint128_t",NS_TYPEDEF
, .type
= &ulllong_ctype
, .op
= &spec_op
},
451 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
452 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
453 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
455 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
456 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
458 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
459 { "union", NS_TYPEDEF
, .op
= &union_op
},
460 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
462 { "inline", NS_TYPEDEF
, .op
= &inline_op
},
463 { "__inline", NS_TYPEDEF
, .op
= &inline_op
},
464 { "__inline__", NS_TYPEDEF
, .op
= &inline_op
},
466 { "_Noreturn", NS_TYPEDEF
, .op
= &noreturn_op
},
468 { "_Alignas", NS_TYPEDEF
, .op
= &alignas_op
},
470 /* Ignored for now.. */
471 { "restrict", NS_TYPEDEF
, .op
= &restrict_op
},
472 { "__restrict", NS_TYPEDEF
, .op
= &restrict_op
},
473 { "__restrict__", NS_TYPEDEF
, .op
= &restrict_op
},
475 /* Static assertion */
476 { "_Static_assert", NS_KEYWORD
, .op
= &static_assert_op
},
479 { "auto", NS_TYPEDEF
, .op
= &auto_op
},
480 { "register", NS_TYPEDEF
, .op
= ®ister_op
},
481 { "static", NS_TYPEDEF
, .op
= &static_op
},
482 { "extern", NS_TYPEDEF
, .op
= &extern_op
},
483 { "__thread", NS_TYPEDEF
, .op
= &thread_op
},
484 { "_Thread_local", NS_TYPEDEF
, .op
= &thread_op
},
487 { "if", NS_KEYWORD
, .op
= &if_op
},
488 { "return", NS_KEYWORD
, .op
= &return_op
},
489 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
490 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
491 { "default", NS_KEYWORD
, .op
= &default_op
},
492 { "case", NS_KEYWORD
, .op
= &case_op
},
493 { "switch", NS_KEYWORD
, .op
= &switch_op
},
494 { "for", NS_KEYWORD
, .op
= &for_op
},
495 { "while", NS_KEYWORD
, .op
= &while_op
},
496 { "do", NS_KEYWORD
, .op
= &do_op
},
497 { "goto", NS_KEYWORD
, .op
= &goto_op
},
498 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
499 { "__range__", NS_KEYWORD
, .op
= &range_op
},
500 { "asm", NS_KEYWORD
, .op
= &asm_op
},
501 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
502 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
505 { "packed", NS_KEYWORD
, .op
= &packed_op
},
506 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
507 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
508 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
509 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
510 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
511 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
512 { "force", NS_KEYWORD
, .op
= &attr_force_op
},
513 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_bitwise_op
},
514 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_bitwise_op
},
515 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
516 { "mode", NS_KEYWORD
, .op
= &mode_op
},
517 { "context", NS_KEYWORD
, .op
= &context_op
},
518 { "designated_init", NS_KEYWORD
, .op
= &designated_init_op
},
519 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
520 { "noreturn", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
521 { "__noreturn__", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
522 { "pure", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
523 {"__pure__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
524 {"const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
525 {"__const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
526 {"__const__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
528 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
529 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
530 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
531 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
532 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
533 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
534 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
535 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
536 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
537 { "TI", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
538 { "__TI__", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
539 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
540 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
543 static const char *ignored_attributes
[] = {
555 "__assume_aligned__",
574 "externally_visible",
575 "__externally_visible__",
605 "__ms_hook_prologue__",
608 "no_instrument_function",
609 "__no_instrument_function__",
632 "__syscall_linkage__",
643 "warn_unused_result",
644 "__warn_unused_result__",
649 "no_sanitize_address",
650 "__no_sanitize_address__",
654 void init_parser(int stream
)
657 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
658 struct init_keyword
*ptr
= keyword_table
+ i
;
659 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
660 sym
->ident
->keyword
= 1;
661 if (ptr
->ns
== NS_TYPEDEF
)
662 sym
->ident
->reserved
= 1;
663 sym
->ctype
.modifiers
= ptr
->modifiers
;
664 sym
->ctype
.base_type
= ptr
->type
;
668 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
669 const char * name
= ignored_attributes
[i
];
670 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
672 sym
->ident
->keyword
= 1;
673 sym
->op
= &ignore_attr_op
;
678 // Add a symbol to the list of function-local symbols
679 static void fn_local_symbol(struct symbol
*sym
)
681 if (function_symbol_list
)
682 add_symbol(function_symbol_list
, sym
);
685 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
690 if (token_type(token
) != TOKEN_IDENT
)
693 va_start(args
, token
);
695 next
= va_arg(args
, struct ident
*);
696 } while (next
&& token
->ident
!= next
);
699 return next
&& token
->ident
== next
;
703 struct statement
*alloc_statement(struct position pos
, int type
)
705 struct statement
*stmt
= __alloc_statement(0);
711 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
713 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
715 struct symbol
*ctype
;
718 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
720 sparse_error(pos
, "don't know how to apply mode to %s",
721 show_typename(ctx
->ctype
.base_type
));
723 ctx
->ctype
.base_type
= ctype
;
727 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
729 struct symbol
*sym
= alloc_symbol(pos
, type
);
731 sym
->ctype
.base_type
= ctype
->base_type
;
732 sym
->ctype
.modifiers
= ctype
->modifiers
;
734 ctype
->base_type
= sym
;
735 ctype
->modifiers
= 0;
740 * NOTE! NS_LABEL is not just a different namespace,
741 * it also ends up using function scope instead of the
742 * regular symbol scope.
744 struct symbol
*label_symbol(struct token
*token
)
746 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
748 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
749 bind_symbol(sym
, token
->ident
, NS_LABEL
);
750 fn_local_symbol(sym
);
755 static struct token
*struct_union_enum_specifier(enum type type
,
756 struct token
*token
, struct decl_state
*ctx
,
757 struct token
*(*parse
)(struct token
*, struct symbol
*))
760 struct position
*repos
;
762 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
763 if (token_type(token
) == TOKEN_IDENT
) {
764 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
766 (is_outer_scope(sym
->scope
) &&
767 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
768 // Either a new symbol, or else an out-of-scope
769 // symbol being redefined.
770 sym
= alloc_symbol(token
->pos
, type
);
771 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
773 if (sym
->type
!= type
)
774 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
775 ctx
->ctype
.base_type
= sym
;
778 if (match_op(token
, '{')) {
779 // The following test is actually wrong for empty
780 // structs, but (1) they are not C99, (2) gcc does
781 // the same thing, and (3) it's easier.
782 if (sym
->symbol_list
)
783 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
785 token
= parse(token
->next
, sym
);
786 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
788 // Mark the structure as needing re-examination
790 sym
->endpos
= token
->pos
;
795 // private struct/union/enum type
796 if (!match_op(token
, '{')) {
797 sparse_error(token
->pos
, "expected declaration");
798 ctx
->ctype
.base_type
= &bad_ctype
;
802 sym
= alloc_symbol(token
->pos
, type
);
803 token
= parse(token
->next
, sym
);
804 ctx
->ctype
.base_type
= sym
;
805 token
= expect(token
, '}', "at end of specifier");
806 sym
->endpos
= token
->pos
;
811 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
813 struct symbol
*field
, *last
= NULL
;
815 res
= struct_declaration_list(token
, &sym
->symbol_list
);
816 FOR_EACH_PTR(sym
->symbol_list
, field
) {
818 struct symbol
*base
= field
->ctype
.base_type
;
819 if (base
&& base
->type
== SYM_BITFIELD
)
823 last
->next_subobject
= field
;
825 } END_FOR_EACH_PTR(field
);
829 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
831 return struct_declaration_list(token
, &sym
->symbol_list
);
834 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
836 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
839 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
841 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
847 unsigned long long y
;
850 static void upper_boundary(Num
*n
, Num
*v
)
862 static void lower_boundary(Num
*n
, Num
*v
)
874 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
876 int shift
= type
->bit_size
;
877 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
881 if (upper
->x
== 0 && upper
->y
>> shift
)
883 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
888 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
890 if (s1
->bit_size
< s2
->bit_size
) {
892 } else if (s1
->bit_size
== s2
->bit_size
) {
893 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
896 if (s1
->bit_size
< bits_in_int
)
901 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
905 FOR_EACH_PTR(list
, sym
) {
906 struct expression
*expr
= sym
->initializer
;
907 struct symbol
*ctype
;
908 if (expr
->type
!= EXPR_VALUE
)
911 if (ctype
->bit_size
== base_type
->bit_size
)
913 cast_value(expr
, base_type
, expr
, ctype
);
914 } END_FOR_EACH_PTR(sym
);
917 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
919 unsigned long long lastval
= 0;
920 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
921 Num upper
= {-1, 0}, lower
= {1, 0};
923 parent
->examined
= 1;
924 parent
->ctype
.base_type
= &int_ctype
;
925 while (token_type(token
) == TOKEN_IDENT
) {
926 struct expression
*expr
= NULL
;
927 struct token
*next
= token
->next
;
930 if (match_op(next
, '=')) {
931 next
= constant_expression(next
->next
, &expr
);
932 lastval
= get_expression_value(expr
);
934 if (expr
&& expr
->ctype
)
938 } else if (is_int_type(ctype
)) {
941 error_die(token
->pos
, "can't increment the last enum member");
945 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
946 expr
->value
= lastval
;
950 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
951 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
952 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
953 sym
->initializer
= expr
;
954 sym
->enum_member
= 1;
955 sym
->ctype
.base_type
= parent
;
956 add_ptr_list(&parent
->symbol_list
, sym
);
958 if (base_type
!= &bad_ctype
) {
959 if (ctype
->type
== SYM_NODE
)
960 ctype
= ctype
->ctype
.base_type
;
961 if (ctype
->type
== SYM_ENUM
) {
965 ctype
= ctype
->ctype
.base_type
;
969 * - if all enums are of the same type, then
970 * the base_type is that type (two first
972 * - if enums are of different types, they
973 * all have to be integer types, and the
974 * base type is at least "int_ctype".
975 * - otherwise the base_type is "bad_ctype".
979 } else if (ctype
== base_type
) {
981 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
982 base_type
= bigger_enum_type(base_type
, ctype
);
984 base_type
= &bad_ctype
;
985 parent
->ctype
.base_type
= base_type
;
987 if (is_int_type(base_type
)) {
988 Num v
= {.y
= lastval
};
989 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
991 else if ((long long)lastval
>= 0)
995 upper_boundary(&upper
, &v
);
996 lower_boundary(&lower
, &v
);
1000 sym
->endpos
= token
->pos
;
1002 if (!match_op(token
, ','))
1004 token
= token
->next
;
1007 sparse_error(token
->pos
, "bad enum definition");
1008 base_type
= &bad_ctype
;
1010 else if (!is_int_type(base_type
))
1011 base_type
= base_type
;
1012 else if (type_is_ok(base_type
, &upper
, &lower
))
1013 base_type
= base_type
;
1014 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
1015 base_type
= &int_ctype
;
1016 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
1017 base_type
= &uint_ctype
;
1018 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
1019 base_type
= &long_ctype
;
1020 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
1021 base_type
= &ulong_ctype
;
1022 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
1023 base_type
= &llong_ctype
;
1024 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
1025 base_type
= &ullong_ctype
;
1027 base_type
= &bad_ctype
;
1028 parent
->ctype
.base_type
= base_type
;
1029 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1030 parent
->examined
= 0;
1032 cast_enum_list(parent
->symbol_list
, base_type
);
1037 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
1039 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1040 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1042 if (!ctype
->base_type
)
1043 ctype
->base_type
= &incomplete_ctype
;
1048 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
1050 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
1054 if (!match_op(token
, '(')) {
1055 sparse_error(token
->pos
, "expected '(' after typeof");
1058 if (lookup_type(token
->next
)) {
1059 token
= typename(token
->next
, &sym
, NULL
);
1060 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1061 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
1063 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1064 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1066 typeof_sym
->endpos
= token
->pos
;
1067 if (!typeof_sym
->initializer
) {
1068 sparse_error(token
->pos
, "expected expression after the '(' token");
1069 typeof_sym
= &bad_ctype
;
1071 ctx
->ctype
.base_type
= typeof_sym
;
1073 return expect(token
, ')', "after typeof");
1076 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1078 struct expression
*expr
= NULL
;
1079 if (match_op(token
, '('))
1080 token
= parens_expression(token
, &expr
, "in attribute");
1084 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1086 if (!ctx
->ctype
.alignment
)
1087 ctx
->ctype
.alignment
= 1;
1091 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1093 int alignment
= max_alignment
;
1094 struct expression
*expr
= NULL
;
1096 if (match_op(token
, '(')) {
1097 token
= parens_expression(token
, &expr
, "in attribute");
1099 alignment
= const_expression_value(expr
);
1101 if (alignment
& (alignment
-1)) {
1102 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1104 } else if (alignment
> ctx
->ctype
.alignment
)
1105 ctx
->ctype
.alignment
= alignment
;
1109 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1111 if (ctx
->modifiers
& qual
)
1112 warning(*pos
, "duplicate %s", modifier_string(qual
));
1113 ctx
->modifiers
|= qual
;
1116 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1118 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1122 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1125 attribute_modifier(token
, attr
, ctx
);
1129 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1131 struct expression
*expr
= NULL
;
1133 token
= expect(token
, '(', "after address_space attribute");
1134 token
= conditional_expression(token
, &expr
);
1136 as
= const_expression_value(expr
);
1137 if (Waddress_space
&& as
)
1140 token
= expect(token
, ')', "after address_space attribute");
1144 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1146 if (ctype
->ctype
.base_type
!= &int_type
)
1148 if (ctype
== &char_ctype
)
1150 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1154 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1156 if (ctype
->ctype
.base_type
!= &int_type
)
1158 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1162 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1164 if (ctype
->ctype
.base_type
!= &int_type
)
1166 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1170 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1172 if (ctype
->ctype
.base_type
!= &int_type
)
1174 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1178 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1180 if (ctype
->ctype
.base_type
!= &int_type
)
1182 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1186 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1188 if (ctype
->ctype
.base_type
!= &int_type
)
1190 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1194 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1196 token
= expect(token
, '(', "after mode attribute");
1197 if (token_type(token
) == TOKEN_IDENT
) {
1198 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1199 if (mode
&& mode
->op
->type
== KW_MODE
)
1200 ctx
->mode
= mode
->op
;
1202 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1203 token
= token
->next
;
1205 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1206 token
= expect(token
, ')', "after mode attribute");
1210 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1212 struct context
*context
= alloc_context();
1213 struct expression
*args
[3];
1216 token
= expect(token
, '(', "after context attribute");
1217 while (!match_op(token
, ')')) {
1218 struct expression
*expr
= NULL
;
1219 token
= conditional_expression(token
, &expr
);
1223 args
[argc
++] = expr
;
1224 if (!match_op(token
, ','))
1226 token
= token
->next
;
1231 sparse_error(token
->pos
, "expected context input/output values");
1234 context
->in
= get_expression_value(args
[0]);
1237 context
->in
= get_expression_value(args
[0]);
1238 context
->out
= get_expression_value(args
[1]);
1241 context
->context
= args
[0];
1242 context
->in
= get_expression_value(args
[1]);
1243 context
->out
= get_expression_value(args
[2]);
1248 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1250 token
= expect(token
, ')', "after context attribute");
1254 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1256 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1257 ctx
->ctype
.base_type
->designated_init
= 1;
1259 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1263 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1265 if (Wtransparent_union
)
1266 warning(token
->pos
, "attribute __transparent_union__");
1268 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1269 ctx
->ctype
.base_type
->transparent_union
= 1;
1271 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1275 static struct token
*recover_unknown_attribute(struct token
*token
)
1277 struct expression
*expr
= NULL
;
1279 if (Wunknown_attribute
)
1280 warning(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1281 token
= token
->next
;
1282 if (match_op(token
, '('))
1283 token
= parens_expression(token
, &expr
, "in attribute");
1287 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1289 token
= expect(token
, '(', "after attribute");
1290 token
= expect(token
, '(', "after attribute");
1293 struct ident
*attribute_name
;
1294 struct symbol
*attr
;
1296 if (eof_token(token
))
1298 if (match_op(token
, ';'))
1300 if (token_type(token
) != TOKEN_IDENT
)
1302 attribute_name
= token
->ident
;
1303 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1304 if (attr
&& attr
->op
->attribute
)
1305 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1307 token
= recover_unknown_attribute(token
);
1309 if (!match_op(token
, ','))
1311 token
= token
->next
;
1314 token
= expect(token
, ')', "after attribute");
1315 token
= expect(token
, ')', "after attribute");
1319 static const char *storage_class
[] =
1321 [STypedef
] = "typedef",
1323 [SExtern
] = "extern",
1324 [SStatic
] = "static",
1325 [SRegister
] = "register",
1326 [SForced
] = "[force]"
1329 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1331 static unsigned long mod
[SMax
] =
1334 [SExtern
] = MOD_EXTERN
,
1335 [SStatic
] = MOD_STATIC
,
1336 [SRegister
] = MOD_REGISTER
1338 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1339 | (ctx
->is_tls
? MOD_TLS
: 0);
1342 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1344 /* __thread can be used alone, or with extern or static */
1345 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1346 sparse_error(*pos
, "__thread can only be used alone, or with "
1347 "extern or static");
1351 if (!ctx
->storage_class
) {
1352 ctx
->storage_class
= class;
1355 if (ctx
->storage_class
== class)
1356 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1358 sparse_error(*pos
, "multiple storage classes");
1361 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1363 set_storage_class(&next
->pos
, ctx
, STypedef
);
1367 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1369 set_storage_class(&next
->pos
, ctx
, SAuto
);
1373 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1375 set_storage_class(&next
->pos
, ctx
, SRegister
);
1379 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1381 set_storage_class(&next
->pos
, ctx
, SStatic
);
1385 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1387 set_storage_class(&next
->pos
, ctx
, SExtern
);
1391 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1393 /* This GCC extension can be used alone, or with extern or static */
1394 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1395 || ctx
->storage_class
== SExtern
) {
1398 sparse_error(next
->pos
, "__thread can only be used alone, or "
1399 "with extern or static");
1405 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1407 set_storage_class(&token
->pos
, ctx
, SForced
);
1411 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1417 static struct token
*noreturn_specifier(struct token
*next
, struct decl_state
*ctx
)
1419 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_NORETURN
);
1423 static struct token
*alignas_specifier(struct token
*token
, struct decl_state
*ctx
)
1427 if (!match_op(token
, '(')) {
1428 sparse_error(token
->pos
, "expected '(' after _Alignas");
1431 if (lookup_type(token
->next
)) {
1432 struct symbol
*sym
= NULL
;
1433 token
= typename(token
->next
, &sym
, NULL
);
1434 sym
= examine_symbol_type(sym
);
1435 alignment
= sym
->ctype
.alignment
;
1436 token
= expect(token
, ')', "after _Alignas(...");
1438 struct expression
*expr
= NULL
;
1439 token
= parens_expression(token
, &expr
, "after _Alignas");
1442 alignment
= const_expression_value(expr
);
1445 if (alignment
< 0) {
1446 warning(token
->pos
, "non-positive alignment");
1449 if (alignment
& (alignment
-1)) {
1450 warning(token
->pos
, "non-power-of-2 alignment");
1453 if (alignment
> ctx
->ctype
.alignment
)
1454 ctx
->ctype
.alignment
= alignment
;
1458 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1460 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1464 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1466 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1470 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1472 unsigned long mod
= thistype
->modifiers
;
1475 apply_qualifier(&pos
, ctype
, mod
);
1478 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1479 (struct ptr_list
**)&ctype
->contexts
);
1482 if (thistype
->alignment
> ctype
->alignment
)
1483 ctype
->alignment
= thistype
->alignment
;
1487 ctype
->as
= thistype
->as
;
1490 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1493 if (what
& (Set_S
| Set_T
))
1495 if (what
& Set_Char
)
1497 else if (what
& Set_Double
)
1499 else if (what
& Set_Float
)
1501 else if (what
& Set_Signed
)
1503 else if (what
& Set_Unsigned
)
1505 else if (what
& Set_Short
)
1507 else if (what
& Set_Long
)
1511 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1512 old
, show_ident(new));
1516 sparse_error(pos
, "two or more data types in declaration specifiers");
1519 static struct symbol
* const int_types
[] =
1520 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &lllong_ctype
};
1521 static struct symbol
* const signed_types
[] =
1522 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1524 static struct symbol
* const unsigned_types
[] =
1525 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1527 static struct symbol
* const real_types
[] =
1528 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1529 static struct symbol
* const char_types
[] =
1530 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1531 static struct symbol
* const * const types
[] = {
1532 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1533 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1536 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1538 return types
[want_unsigned
? CUInt
: CInt
][size
];
1541 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1543 while (token_type(t
) == TOKEN_IDENT
) {
1544 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1547 if (s
->type
!= SYM_KEYWORD
)
1549 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1552 if (s
->op
->declarator
)
1553 t
= s
->op
->declarator(t
, ctx
);
1558 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1564 while (token_type(token
) == TOKEN_IDENT
) {
1565 struct symbol
*s
= lookup_symbol(token
->ident
,
1566 NS_TYPEDEF
| NS_SYMBOL
);
1567 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1569 if (s
->type
!= SYM_KEYWORD
) {
1572 seen
|= Set_S
| Set_T
;
1573 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1574 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1575 token
= token
->next
;
1578 if (s
->op
->type
& KW_SPECIFIER
) {
1579 if (seen
& s
->op
->test
) {
1580 specifier_conflict(token
->pos
,
1586 class += s
->op
->class;
1587 if (s
->op
->set
& Set_Int128
)
1589 if (s
->op
->type
& KW_SHORT
) {
1591 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1592 if (class == CReal
) {
1593 specifier_conflict(token
->pos
,
1601 token
= token
->next
;
1602 if (s
->op
->declarator
)
1603 token
= s
->op
->declarator(token
, ctx
);
1604 if (s
->op
->type
& KW_EXACT
) {
1605 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1606 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1610 if (!(seen
& Set_S
)) { /* not set explicitly? */
1611 struct symbol
*base
= &incomplete_ctype
;
1613 base
= types
[class][size
];
1614 ctx
->ctype
.base_type
= base
;
1617 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1618 struct symbol
*type
;
1619 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1620 if (!is_int_type(ctx
->ctype
.base_type
)) {
1621 sparse_error(token
->pos
, "invalid modifier");
1624 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1625 *type
= *ctx
->ctype
.base_type
;
1626 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1627 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1628 type
->type
= SYM_RESTRICT
;
1629 ctx
->ctype
.base_type
= type
;
1630 create_fouled(type
);
1635 static struct token
*abstract_array_static_declarator(struct token
*token
, int *has_static
)
1637 while (token
->ident
== &static_ident
) {
1639 sparse_error(token
->pos
, "duplicate array static declarator");
1642 token
= token
->next
;
1648 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1650 struct expression
*expr
= NULL
;
1653 token
= abstract_array_static_declarator(token
, &has_static
);
1655 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, &__restrict___ident
, NULL
))
1656 token
= abstract_array_static_declarator(token
->next
, &has_static
);
1657 token
= parse_expression(token
, &expr
);
1658 sym
->array_size
= expr
;
1662 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1663 static struct token
*identifier_list(struct token
*, struct symbol
*);
1664 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1666 static struct token
*skip_attribute(struct token
*token
)
1668 token
= token
->next
;
1669 if (match_op(token
, '(')) {
1671 token
= token
->next
;
1672 while (depth
&& !eof_token(token
)) {
1673 if (token_type(token
) == TOKEN_SPECIAL
) {
1674 if (token
->special
== '(')
1676 else if (token
->special
== ')')
1679 token
= token
->next
;
1685 static struct token
*skip_attributes(struct token
*token
)
1687 struct symbol
*keyword
;
1689 if (token_type(token
) != TOKEN_IDENT
)
1691 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1692 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1694 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1696 token
= expect(token
->next
, '(', "after attribute");
1697 token
= expect(token
, '(', "after attribute");
1699 if (eof_token(token
))
1701 if (match_op(token
, ';'))
1703 if (token_type(token
) != TOKEN_IDENT
)
1705 token
= skip_attribute(token
);
1706 if (!match_op(token
, ','))
1708 token
= token
->next
;
1710 token
= expect(token
, ')', "after attribute");
1711 token
= expect(token
, ')', "after attribute");
1716 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1718 struct symbol
*keyword
;
1720 if (token_type(token
) != TOKEN_IDENT
)
1722 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1723 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1725 if (!(keyword
->op
->type
& keywords
))
1727 token
= keyword
->op
->declarator(token
->next
, ctx
);
1728 keywords
&= KW_ATTRIBUTE
;
1733 static int is_nested(struct token
*token
, struct token
**p
,
1734 int prefer_abstract
)
1737 * This can be either a parameter list or a grouping.
1738 * For the direct (non-abstract) case, we know if must be
1739 * a parameter list if we already saw the identifier.
1740 * For the abstract case, we know if must be a parameter
1741 * list if it is empty or starts with a type.
1743 struct token
*next
= token
->next
;
1745 *p
= next
= skip_attributes(next
);
1747 if (token_type(next
) == TOKEN_IDENT
) {
1748 if (lookup_type(next
))
1749 return !prefer_abstract
;
1753 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1760 Empty
, K_R
, Proto
, Bad_Func
,
1763 static enum kind
which_func(struct token
*token
,
1765 int prefer_abstract
)
1767 struct token
*next
= token
->next
;
1769 if (token_type(next
) == TOKEN_IDENT
) {
1770 if (lookup_type(next
))
1772 /* identifier list not in definition; complain */
1773 if (prefer_abstract
)
1775 "identifier list not in definition");
1779 if (token_type(next
) != TOKEN_SPECIAL
)
1782 if (next
->special
== ')') {
1783 /* don't complain about those */
1784 if (!n
|| match_op(next
->next
, ';'))
1787 "non-ANSI function declaration of function '%s'",
1792 if (next
->special
== SPECIAL_ELLIPSIS
) {
1794 "variadic functions must have one named argument");
1801 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1803 struct ctype
*ctype
= &ctx
->ctype
;
1805 struct ident
**p
= ctx
->ident
;
1807 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1808 *ctx
->ident
= token
->ident
;
1809 token
= token
->next
;
1810 } else if (match_op(token
, '(') &&
1811 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1812 struct symbol
*base_type
= ctype
->base_type
;
1813 if (token
->next
!= next
)
1814 next
= handle_attributes(token
->next
, ctx
,
1816 token
= declarator(next
, ctx
);
1817 token
= expect(token
, ')', "in nested declarator");
1818 while (ctype
->base_type
!= base_type
)
1819 ctype
= &ctype
->base_type
->ctype
;
1823 if (match_op(token
, '(')) {
1824 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1826 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1827 token
= token
->next
;
1829 token
= identifier_list(token
, fn
);
1830 else if (kind
== Proto
)
1831 token
= parameter_type_list(token
, fn
);
1832 token
= expect(token
, ')', "in function declarator");
1833 fn
->endpos
= token
->pos
;
1837 while (match_op(token
, '[')) {
1838 struct symbol
*array
;
1839 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1840 token
= abstract_array_declarator(token
->next
, array
);
1841 token
= expect(token
, ']', "in abstract_array_declarator");
1842 array
->endpos
= token
->pos
;
1843 ctype
= &array
->ctype
;
1848 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1850 while (match_op(token
,'*')) {
1851 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1852 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1853 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1854 ptr
->ctype
.as
= ctx
->ctype
.as
;
1855 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1856 ctx
->ctype
.modifiers
= 0;
1857 ctx
->ctype
.base_type
= ptr
;
1859 ctx
->ctype
.contexts
= NULL
;
1860 ctx
->ctype
.alignment
= 0;
1862 token
= handle_qualifiers(token
->next
, ctx
);
1863 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1868 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1870 token
= pointer(token
, ctx
);
1871 return direct_declarator(token
, ctx
);
1874 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1876 struct ctype
*ctype
= &ctx
->ctype
;
1877 struct expression
*expr
;
1878 struct symbol
*bitfield
;
1881 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1882 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1883 show_typename(ctype
->base_type
));
1884 // Parse this to recover gracefully.
1885 return conditional_expression(token
->next
, &expr
);
1888 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1889 token
= conditional_expression(token
->next
, &expr
);
1890 width
= const_expression_value(expr
);
1891 bitfield
->bit_size
= width
;
1893 if (width
< 0 || width
> INT_MAX
) {
1894 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1896 } else if (*ctx
->ident
&& width
== 0) {
1897 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1898 show_ident(*ctx
->ident
));
1900 } else if (*ctx
->ident
) {
1901 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1902 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1903 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1904 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1905 // Valid values are either {-1;0} or {0}, depending on integer
1906 // representation. The latter makes for very efficient code...
1907 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1909 if (Wdefault_bitfield_sign
&&
1910 bitfield_type
->type
!= SYM_ENUM
&&
1911 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1913 // The sign of bitfields is unspecified by default.
1914 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1917 bitfield
->bit_size
= width
;
1918 bitfield
->endpos
= token
->pos
;
1922 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1924 struct decl_state ctx
= {.prefer_abstract
= 0};
1928 token
= declaration_specifiers(token
, &ctx
);
1929 mod
= storage_modifiers(&ctx
);
1932 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1933 ctx
.ident
= &decl
->ident
;
1935 token
= declarator(token
, &ctx
);
1936 if (match_op(token
, ':'))
1937 token
= handle_bitfield(token
, &ctx
);
1939 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1940 apply_modifiers(token
->pos
, &ctx
);
1942 decl
->ctype
= ctx
.ctype
;
1943 decl
->ctype
.modifiers
|= mod
;
1944 decl
->endpos
= token
->pos
;
1945 add_symbol(list
, decl
);
1946 if (!match_op(token
, ','))
1948 token
= token
->next
;
1954 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1956 while (!match_op(token
, '}')) {
1957 if (match_ident(token
, &_Static_assert_ident
)) {
1958 token
= parse_static_assert(token
, NULL
);
1961 if (!match_op(token
, ';'))
1962 token
= declaration_list(token
, list
);
1963 if (!match_op(token
, ';')) {
1964 sparse_error(token
->pos
, "expected ; at end of declaration");
1967 token
= token
->next
;
1972 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1974 struct decl_state ctx
= {.prefer_abstract
= 1};
1976 token
= declaration_specifiers(token
, &ctx
);
1977 ctx
.ident
= &sym
->ident
;
1978 token
= declarator(token
, &ctx
);
1979 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1980 apply_modifiers(token
->pos
, &ctx
);
1981 sym
->ctype
= ctx
.ctype
;
1982 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1983 sym
->endpos
= token
->pos
;
1984 sym
->forced_arg
= ctx
.storage_class
== SForced
;
1988 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1990 struct decl_state ctx
= {.prefer_abstract
= 1};
1992 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1994 token
= declaration_specifiers(token
, &ctx
);
1995 token
= declarator(token
, &ctx
);
1996 apply_modifiers(token
->pos
, &ctx
);
1997 sym
->ctype
= ctx
.ctype
;
1998 sym
->endpos
= token
->pos
;
1999 class = ctx
.storage_class
;
2002 if (class == SForced
) {
2008 warning(sym
->pos
, "storage class in typename (%s %s)",
2009 storage_class
[class], show_typename(sym
));
2013 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
2015 token
= parse_expression(token
, tree
);
2016 return expect(token
, ';', "at end of statement");
2019 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2020 struct expression_list
**inout
)
2022 struct expression
*expr
;
2024 /* Allow empty operands */
2025 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2028 struct ident
*ident
= NULL
;
2029 if (match_op(token
->next
, '[') &&
2030 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2031 match_op(token
->next
->next
->next
, ']')) {
2032 ident
= token
->next
->next
->ident
;
2033 token
= token
->next
->next
->next
;
2035 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
2036 token
= primary_expression(token
->next
, &expr
);
2037 add_expression(inout
, expr
);
2038 token
= parens_expression(token
, &expr
, "in asm parameter");
2039 add_expression(inout
, expr
);
2040 } while (match_op(token
, ','));
2044 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2045 struct expression_list
**clobbers
)
2047 struct expression
*expr
;
2050 token
= primary_expression(token
->next
, &expr
);
2052 add_expression(clobbers
, expr
);
2053 } while (match_op(token
, ','));
2057 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2058 struct symbol_list
**labels
)
2060 struct symbol
*label
;
2063 token
= token
->next
; /* skip ':' and ',' */
2064 if (token_type(token
) != TOKEN_IDENT
)
2066 label
= label_symbol(token
);
2067 add_symbol(labels
, label
);
2068 token
= token
->next
;
2069 } while (match_op(token
, ','));
2073 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2077 token
= token
->next
;
2078 stmt
->type
= STMT_ASM
;
2079 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
2080 token
= token
->next
;
2082 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
2084 token
= token
->next
;
2086 token
= expect(token
, '(', "after asm");
2087 token
= parse_expression(token
, &stmt
->asm_string
);
2088 if (match_op(token
, ':'))
2089 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2090 if (match_op(token
, ':'))
2091 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2092 if (match_op(token
, ':'))
2093 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2094 if (is_goto
&& match_op(token
, ':'))
2095 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2096 token
= expect(token
, ')', "after asm");
2097 return expect(token
, ';', "at end of asm-statement");
2100 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
2102 struct expression
*expr
;
2103 token
= expect(token
, '(', "after asm");
2104 token
= parse_expression(token
->next
, &expr
);
2105 token
= expect(token
, ')', "after asm");
2109 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2111 struct expression
*cond
= NULL
, *message
= NULL
;
2113 token
= expect(token
->next
, '(', "after _Static_assert");
2114 token
= constant_expression(token
, &cond
);
2116 sparse_error(token
->pos
, "Expected constant expression");
2117 token
= expect(token
, ',', "after conditional expression in _Static_assert");
2118 token
= parse_expression(token
, &message
);
2119 if (!message
|| message
->type
!= EXPR_STRING
) {
2120 struct position pos
;
2122 pos
= message
? message
->pos
: token
->pos
;
2123 sparse_error(pos
, "bad or missing string literal");
2126 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2128 token
= expect(token
, ';', "after _Static_assert()");
2130 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
)
2131 sparse_error(cond
->pos
, "static assertion failed: %s",
2132 show_string(message
->string
));
2136 /* Make a statement out of an expression */
2137 static struct statement
*make_statement(struct expression
*expr
)
2139 struct statement
*stmt
;
2143 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2144 stmt
->expression
= expr
;
2149 * All iterators have two symbols associated with them:
2150 * the "continue" and "break" symbols, which are targets
2151 * for continue and break statements respectively.
2153 * They are in a special name-space, but they follow
2154 * all the normal visibility rules, so nested iterators
2155 * automatically work right.
2157 static void start_iterator(struct statement
*stmt
)
2159 struct symbol
*cont
, *brk
;
2161 start_symbol_scope();
2162 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2163 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2164 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2165 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2167 stmt
->type
= STMT_ITERATOR
;
2168 stmt
->iterator_break
= brk
;
2169 stmt
->iterator_continue
= cont
;
2170 fn_local_symbol(brk
);
2171 fn_local_symbol(cont
);
2174 static void end_iterator(struct statement
*stmt
)
2179 static struct statement
*start_function(struct symbol
*sym
)
2182 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2184 start_function_scope();
2185 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2186 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2187 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2188 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2189 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2191 fn_local_symbol(ret
);
2193 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2199 static void end_function(struct symbol
*sym
)
2202 end_function_scope();
2206 * A "switch()" statement, like an iterator, has a
2207 * the "break" symbol associated with it. It works
2208 * exactly like the iterator break - it's the target
2209 * for any break-statements in scope, and means that
2210 * "break" handling doesn't even need to know whether
2211 * it's breaking out of an iterator or a switch.
2213 * In addition, the "case" symbol is a marker for the
2214 * case/default statements to find the switch statement
2215 * that they are associated with.
2217 static void start_switch(struct statement
*stmt
)
2219 struct symbol
*brk
, *switch_case
;
2221 start_symbol_scope();
2222 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2223 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2225 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2226 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2227 switch_case
->stmt
= stmt
;
2229 stmt
->type
= STMT_SWITCH
;
2230 stmt
->switch_break
= brk
;
2231 stmt
->switch_case
= switch_case
;
2233 fn_local_symbol(brk
);
2234 fn_local_symbol(switch_case
);
2237 static void end_switch(struct statement
*stmt
)
2239 if (!stmt
->switch_case
->symbol_list
)
2240 warning(stmt
->pos
, "switch with no cases");
2244 static void add_case_statement(struct statement
*stmt
)
2246 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2250 sparse_error(stmt
->pos
, "not in switch scope");
2251 stmt
->type
= STMT_NONE
;
2254 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2255 add_symbol(&target
->symbol_list
, sym
);
2257 stmt
->case_label
= sym
;
2258 fn_local_symbol(sym
);
2261 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2263 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2266 error_die(token
->pos
, "internal error: return without a function target");
2267 stmt
->type
= STMT_RETURN
;
2268 stmt
->ret_target
= target
;
2269 return expression_statement(token
->next
, &stmt
->ret_value
);
2272 static void validate_for_loop_decl(struct symbol
*sym
)
2274 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2276 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2277 const char *name
= show_ident(sym
->ident
);
2278 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2279 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2283 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2285 struct symbol_list
*syms
;
2286 struct expression
*e1
, *e2
, *e3
;
2287 struct statement
*iterator
;
2289 start_iterator(stmt
);
2290 token
= expect(token
->next
, '(', "after 'for'");
2294 /* C99 variable declaration? */
2295 if (lookup_type(token
)) {
2296 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2298 token
= parse_expression(token
, &e1
);
2299 token
= expect(token
, ';', "in 'for'");
2301 token
= parse_expression(token
, &e2
);
2302 token
= expect(token
, ';', "in 'for'");
2303 token
= parse_expression(token
, &e3
);
2304 token
= expect(token
, ')', "in 'for'");
2305 token
= statement(token
, &iterator
);
2307 stmt
->iterator_syms
= syms
;
2308 stmt
->iterator_pre_statement
= make_statement(e1
);
2309 stmt
->iterator_pre_condition
= e2
;
2310 stmt
->iterator_post_statement
= make_statement(e3
);
2311 stmt
->iterator_post_condition
= NULL
;
2312 stmt
->iterator_statement
= iterator
;
2318 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2320 struct expression
*expr
;
2321 struct statement
*iterator
;
2323 start_iterator(stmt
);
2324 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2325 token
= statement(token
, &iterator
);
2327 stmt
->iterator_pre_condition
= expr
;
2328 stmt
->iterator_post_condition
= NULL
;
2329 stmt
->iterator_statement
= iterator
;
2335 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2337 struct expression
*expr
;
2338 struct statement
*iterator
;
2340 start_iterator(stmt
);
2341 token
= statement(token
->next
, &iterator
);
2342 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2343 token
= token
->next
;
2345 sparse_error(token
->pos
, "expected 'while' after 'do'");
2346 token
= parens_expression(token
, &expr
, "after 'do-while'");
2348 stmt
->iterator_post_condition
= expr
;
2349 stmt
->iterator_statement
= iterator
;
2352 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2353 warning(iterator
->pos
, "do-while statement is not a compound statement");
2355 return expect(token
, ';', "after statement");
2358 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2360 stmt
->type
= STMT_IF
;
2361 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2362 token
= statement(token
, &stmt
->if_true
);
2363 if (token_type(token
) != TOKEN_IDENT
)
2365 if (token
->ident
!= &else_ident
)
2367 return statement(token
->next
, &stmt
->if_false
);
2370 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2372 stmt
->type
= STMT_CASE
;
2373 token
= expect(token
, ':', "after default/case");
2374 add_case_statement(stmt
);
2375 return statement(token
, &stmt
->case_statement
);
2378 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2380 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2381 if (match_op(token
, SPECIAL_ELLIPSIS
))
2382 token
= parse_expression(token
->next
, &stmt
->case_to
);
2383 return case_statement(token
, stmt
);
2386 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2388 return case_statement(token
->next
, stmt
);
2391 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2393 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2394 stmt
->type
= STMT_GOTO
;
2395 stmt
->goto_label
= target
;
2397 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2398 return expect(token
->next
, ';', "at end of statement");
2401 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2403 stmt
->type
= STMT_SWITCH
;
2405 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2406 token
= statement(token
, &stmt
->switch_statement
);
2411 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2413 stmt
->type
= STMT_GOTO
;
2414 token
= token
->next
;
2415 if (match_op(token
, '*')) {
2416 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2417 add_statement(&function_computed_goto_list
, stmt
);
2418 } else if (token_type(token
) == TOKEN_IDENT
) {
2419 stmt
->goto_label
= label_symbol(token
);
2420 token
= token
->next
;
2422 sparse_error(token
->pos
, "Expected identifier or goto expression");
2424 return expect(token
, ';', "at end of statement");
2427 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2429 stmt
->type
= STMT_CONTEXT
;
2430 token
= parse_expression(token
->next
, &stmt
->expression
);
2431 if (stmt
->expression
->type
== EXPR_PREOP
2432 && stmt
->expression
->op
== '('
2433 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2434 struct expression
*expr
;
2435 expr
= stmt
->expression
->unop
;
2436 stmt
->context
= expr
->left
;
2437 stmt
->expression
= expr
->right
;
2439 return expect(token
, ';', "at end of statement");
2442 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2444 stmt
->type
= STMT_RANGE
;
2445 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2446 token
= expect(token
, ',', "after range expression");
2447 token
= assignment_expression(token
, &stmt
->range_low
);
2448 token
= expect(token
, ',', "after low range");
2449 token
= assignment_expression(token
, &stmt
->range_high
);
2450 return expect(token
, ';', "after range statement");
2453 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2455 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2458 if (token_type(token
) == TOKEN_IDENT
) {
2459 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2460 if (s
&& s
->op
->statement
)
2461 return s
->op
->statement(token
, stmt
);
2463 if (match_op(token
->next
, ':')) {
2464 struct symbol
*s
= label_symbol(token
);
2465 stmt
->type
= STMT_LABEL
;
2466 stmt
->label_identifier
= s
;
2468 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(token
->ident
));
2470 token
= skip_attributes(token
->next
->next
);
2471 return statement(token
, &stmt
->label_statement
);
2475 if (match_op(token
, '{')) {
2476 stmt
->type
= STMT_COMPOUND
;
2477 start_symbol_scope();
2478 token
= compound_statement(token
->next
, stmt
);
2481 return expect(token
, '}', "at end of compound statement");
2484 stmt
->type
= STMT_EXPRESSION
;
2485 return expression_statement(token
, &stmt
->expression
);
2488 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2489 static struct token
*label_statement(struct token
*token
)
2491 while (token_type(token
) == TOKEN_IDENT
) {
2492 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2493 /* it's block-scope, but we want label namespace */
2494 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2495 sym
->namespace = NS_LABEL
;
2496 fn_local_symbol(sym
);
2497 token
= token
->next
;
2498 if (!match_op(token
, ','))
2500 token
= token
->next
;
2502 return expect(token
, ';', "at end of label declaration");
2505 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2507 int seen_statement
= 0;
2508 while (token_type(token
) == TOKEN_IDENT
&&
2509 token
->ident
== &__label___ident
)
2510 token
= label_statement(token
->next
);
2512 struct statement
* stmt
;
2513 if (eof_token(token
))
2515 if (match_op(token
, '}'))
2517 if (match_ident(token
, &_Static_assert_ident
)) {
2518 token
= parse_static_assert(token
, NULL
);
2521 if (lookup_type(token
)) {
2522 if (seen_statement
) {
2523 warning(token
->pos
, "mixing declarations and code");
2526 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2527 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2529 seen_statement
= Wdeclarationafterstatement
;
2530 token
= statement(token
, &stmt
);
2532 add_statement(list
, stmt
);
2537 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2539 struct symbol_list
**list
= &fn
->arguments
;
2541 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2542 sym
->ident
= token
->ident
;
2543 token
= token
->next
;
2544 sym
->endpos
= token
->pos
;
2545 sym
->ctype
.base_type
= &incomplete_ctype
;
2546 add_symbol(list
, sym
);
2547 if (!match_op(token
, ',') ||
2548 token_type(token
->next
) != TOKEN_IDENT
||
2549 lookup_type(token
->next
))
2551 token
= token
->next
;
2556 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2558 struct symbol_list
**list
= &fn
->arguments
;
2563 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2565 token
= token
->next
;
2569 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2570 token
= parameter_declaration(token
, sym
);
2571 if (sym
->ctype
.base_type
== &void_ctype
) {
2572 /* Special case: (void) */
2573 if (!*list
&& !sym
->ident
)
2575 warning(token
->pos
, "void parameter");
2577 add_symbol(list
, sym
);
2578 if (!match_op(token
, ','))
2580 token
= token
->next
;
2585 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2587 token
= statement_list(token
, &stmt
->stmts
);
2591 static struct expression
*identifier_expression(struct token
*token
)
2593 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2594 expr
->expr_ident
= token
->ident
;
2598 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2600 int idx_from
, idx_to
;
2601 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2603 idx_from
= const_expression_value(from
);
2606 idx_to
= const_expression_value(to
);
2607 if (idx_to
< idx_from
|| idx_from
< 0)
2608 warning(from
->pos
, "nonsense array initializer index range");
2610 expr
->idx_from
= idx_from
;
2611 expr
->idx_to
= idx_to
;
2615 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2617 int expect_equal
= 0;
2618 struct token
*next
= token
->next
;
2619 struct expression
**tail
= ep
;
2624 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2625 struct expression
*expr
= identifier_expression(token
);
2626 if (Wold_initializer
)
2627 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2628 token
= initializer(&expr
->ident_expression
, next
->next
);
2629 if (expr
->ident_expression
)
2634 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2635 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2636 struct expression
*expr
= identifier_expression(next
);
2638 tail
= &expr
->ident_expression
;
2641 } else if (match_op(token
, '[')) {
2642 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2643 token
= constant_expression(token
->next
, &from
);
2645 sparse_error(token
->pos
, "Expected constant expression");
2648 if (match_op(token
, SPECIAL_ELLIPSIS
))
2649 token
= constant_expression(token
->next
, &to
);
2650 expr
= index_expression(from
, to
);
2652 tail
= &expr
->idx_expression
;
2653 token
= expect(token
, ']', "at end of initializer index");
2660 if (nested
&& !expect_equal
) {
2661 if (!match_op(token
, '='))
2662 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2667 token
= expect(token
, '=', "at end of initializer index");
2669 token
= initializer(tail
, token
);
2675 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2677 struct expression
*expr
;
2680 token
= single_initializer(&expr
, token
);
2683 add_expression(list
, expr
);
2684 if (!match_op(token
, ','))
2686 token
= token
->next
;
2691 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2693 if (match_op(token
, '{')) {
2694 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2696 token
= initializer_list(&expr
->expr_list
, token
->next
);
2697 return expect(token
, '}', "at end of initializer");
2699 return assignment_expression(token
, tree
);
2702 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2705 sparse_error(sym
->pos
, "no identifier for function argument");
2708 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2711 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2712 struct symbol_list
**list
)
2714 struct symbol_list
**old_symbol_list
;
2715 struct symbol
*base_type
= decl
->ctype
.base_type
;
2716 struct statement
*stmt
, **p
;
2717 struct symbol
*prev
;
2720 old_symbol_list
= function_symbol_list
;
2721 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2722 function_symbol_list
= &decl
->inline_symbol_list
;
2723 p
= &base_type
->inline_stmt
;
2725 function_symbol_list
= &decl
->symbol_list
;
2726 p
= &base_type
->stmt
;
2728 function_computed_target_list
= NULL
;
2729 function_computed_goto_list
= NULL
;
2731 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2732 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2733 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2735 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2736 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2738 stmt
= start_function(decl
);
2741 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2742 declare_argument(arg
, base_type
);
2743 } END_FOR_EACH_PTR(arg
);
2745 token
= compound_statement(token
->next
, stmt
);
2748 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2749 add_symbol(list
, decl
);
2750 check_declaration(decl
);
2751 decl
->definition
= decl
;
2752 prev
= decl
->same_symbol
;
2753 if (prev
&& prev
->definition
) {
2754 warning(decl
->pos
, "multiple definitions for function '%s'",
2755 show_ident(decl
->ident
));
2756 info(prev
->definition
->pos
, " the previous one is here");
2759 rebind_scope(prev
, decl
->scope
);
2760 prev
->definition
= decl
;
2761 prev
= prev
->same_symbol
;
2764 function_symbol_list
= old_symbol_list
;
2765 if (function_computed_goto_list
) {
2766 if (!function_computed_target_list
)
2767 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2769 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2770 stmt
->target_list
= function_computed_target_list
;
2771 } END_FOR_EACH_PTR(stmt
);
2774 return expect(token
, '}', "at end of function");
2777 static void promote_k_r_types(struct symbol
*arg
)
2779 struct symbol
*base
= arg
->ctype
.base_type
;
2780 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2781 arg
->ctype
.base_type
= &int_ctype
;
2785 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2787 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2790 FOR_EACH_PTR(real_args
, arg
) {
2791 struct symbol
*type
;
2793 /* This is quadratic in the number of arguments. We _really_ don't care */
2794 FOR_EACH_PTR(argtypes
, type
) {
2795 if (type
->ident
== arg
->ident
)
2797 } END_FOR_EACH_PTR(type
);
2798 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2802 /* "char" and "short" promote to "int" */
2803 promote_k_r_types(type
);
2805 arg
->ctype
= type
->ctype
;
2806 } END_FOR_EACH_PTR(arg
);
2808 FOR_EACH_PTR(argtypes
, arg
) {
2810 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2811 } END_FOR_EACH_PTR(arg
);
2815 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2816 struct symbol_list
**list
)
2818 struct symbol_list
*args
= NULL
;
2820 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2822 token
= declaration_list(token
, &args
);
2823 if (!match_op(token
, ';')) {
2824 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2827 token
= token
->next
;
2828 } while (lookup_type(token
));
2830 apply_k_r_types(args
, decl
);
2832 if (!match_op(token
, '{')) {
2833 sparse_error(token
->pos
, "expected function body");
2836 return parse_function_body(token
, decl
, list
);
2839 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2841 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2842 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2843 struct statement
*stmt
;
2845 anon
->ctype
.base_type
= fn
;
2846 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2849 token
= parse_asm_statement(token
, stmt
);
2851 add_symbol(list
, anon
);
2855 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2856 validate_decl_t validate_decl
)
2858 struct ident
*ident
= NULL
;
2859 struct symbol
*decl
;
2860 struct decl_state ctx
= { .ident
= &ident
};
2862 struct symbol
*base_type
;
2866 /* Top-level inline asm or static assertion? */
2867 if (token_type(token
) == TOKEN_IDENT
) {
2868 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2869 if (s
&& s
->op
->toplevel
)
2870 return s
->op
->toplevel(token
, list
);
2873 /* Parse declaration-specifiers, if any */
2874 token
= declaration_specifiers(token
, &ctx
);
2875 mod
= storage_modifiers(&ctx
);
2876 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2877 /* Just a type declaration? */
2878 if (match_op(token
, ';')) {
2879 apply_modifiers(token
->pos
, &ctx
);
2884 token
= declarator(token
, &ctx
);
2885 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2886 apply_modifiers(token
->pos
, &ctx
);
2888 decl
->ctype
= ctx
.ctype
;
2889 decl
->ctype
.modifiers
|= mod
;
2890 decl
->endpos
= token
->pos
;
2892 /* Just a type declaration? */
2894 warning(token
->pos
, "missing identifier in declaration");
2895 return expect(token
, ';', "at the end of type declaration");
2898 /* type define declaration? */
2899 is_typedef
= ctx
.storage_class
== STypedef
;
2901 /* Typedefs don't have meaningful storage */
2903 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2905 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2907 base_type
= decl
->ctype
.base_type
;
2910 if (base_type
&& !base_type
->ident
) {
2911 switch (base_type
->type
) {
2916 base_type
->ident
= ident
;
2922 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2923 /* K&R argument declaration? */
2924 if (lookup_type(token
))
2925 return parse_k_r_arguments(token
, decl
, list
);
2926 if (match_op(token
, '{'))
2927 return parse_function_body(token
, decl
, list
);
2929 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2930 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2931 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2932 sparse_error(token
->pos
, "void declaration");
2936 if (!is_typedef
&& match_op(token
, '=')) {
2937 token
= initializer(&decl
->initializer
, token
->next
);
2941 validate_decl(decl
);
2943 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
2944 warning(decl
->pos
, "symbol with external linkage has initializer");
2945 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2948 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2949 add_symbol(list
, decl
);
2950 fn_local_symbol(decl
);
2953 check_declaration(decl
);
2954 if (decl
->same_symbol
) {
2955 decl
->definition
= decl
->same_symbol
->definition
;
2956 decl
->op
= decl
->same_symbol
->op
;
2959 if (!match_op(token
, ','))
2962 token
= token
->next
;
2964 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2966 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2967 token
= declarator(token
, &ctx
);
2968 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2969 apply_modifiers(token
->pos
, &ctx
);
2970 decl
->ctype
= ctx
.ctype
;
2971 decl
->ctype
.modifiers
|= mod
;
2972 decl
->endpos
= token
->pos
;
2974 sparse_error(token
->pos
, "expected identifier name in type definition");
2979 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2981 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2983 /* Function declarations are automatically extern unless specifically static */
2984 base_type
= decl
->ctype
.base_type
;
2985 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2986 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2987 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2990 return expect(token
, ';', "at end of declaration");