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
},
544 static const char *ignored_attributes
[] = {
546 #define GCC_ATTR(x) \
548 STRINGIFY(__##x##__),
550 #include "gcc-attr-list.h"
562 void init_parser(int stream
)
565 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
566 struct init_keyword
*ptr
= keyword_table
+ i
;
567 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
568 sym
->ident
->keyword
= 1;
569 if (ptr
->ns
== NS_TYPEDEF
)
570 sym
->ident
->reserved
= 1;
571 sym
->ctype
.modifiers
= ptr
->modifiers
;
572 sym
->ctype
.base_type
= ptr
->type
;
576 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
577 const char * name
= ignored_attributes
[i
];
578 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
581 sym
->ident
->keyword
= 1;
582 sym
->op
= &ignore_attr_op
;
588 // Add a symbol to the list of function-local symbols
589 static void fn_local_symbol(struct symbol
*sym
)
591 if (function_symbol_list
)
592 add_symbol(function_symbol_list
, sym
);
595 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
600 if (token_type(token
) != TOKEN_IDENT
)
603 va_start(args
, token
);
605 next
= va_arg(args
, struct ident
*);
606 } while (next
&& token
->ident
!= next
);
609 return next
&& token
->ident
== next
;
613 struct statement
*alloc_statement(struct position pos
, int type
)
615 struct statement
*stmt
= __alloc_statement(0);
621 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
623 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
625 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
627 struct symbol
*ctype
;
630 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
632 sparse_error(pos
, "don't know how to apply mode to %s",
633 show_typename(ctx
->ctype
.base_type
));
635 ctx
->ctype
.base_type
= ctype
;
639 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
641 struct symbol
*sym
= alloc_symbol(pos
, type
);
643 sym
->ctype
.base_type
= ctype
->base_type
;
644 sym
->ctype
.modifiers
= ctype
->modifiers
;
646 ctype
->base_type
= sym
;
647 ctype
->modifiers
= 0;
652 * NOTE! NS_LABEL is not just a different namespace,
653 * it also ends up using function scope instead of the
654 * regular symbol scope.
656 struct symbol
*label_symbol(struct token
*token
)
658 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
660 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
661 bind_symbol(sym
, token
->ident
, NS_LABEL
);
662 fn_local_symbol(sym
);
667 static struct token
*struct_union_enum_specifier(enum type type
,
668 struct token
*token
, struct decl_state
*ctx
,
669 struct token
*(*parse
)(struct token
*, struct symbol
*))
672 struct position
*repos
;
674 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
675 if (token_type(token
) == TOKEN_IDENT
) {
676 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
678 (is_outer_scope(sym
->scope
) &&
679 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
680 // Either a new symbol, or else an out-of-scope
681 // symbol being redefined.
682 sym
= alloc_symbol(token
->pos
, type
);
683 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
685 if (sym
->type
!= type
)
686 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
687 ctx
->ctype
.base_type
= sym
;
690 if (match_op(token
, '{')) {
691 struct decl_state attr
= { .ctype
.base_type
= sym
, };
693 // The following test is actually wrong for empty
694 // structs, but (1) they are not C99, (2) gcc does
695 // the same thing, and (3) it's easier.
696 if (sym
->symbol_list
)
697 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
699 token
= parse(token
->next
, sym
);
700 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
702 token
= handle_attributes(token
, &attr
, KW_ATTRIBUTE
);
703 apply_ctype(token
->pos
, &attr
.ctype
, &sym
->ctype
);
705 // Mark the structure as needing re-examination
707 sym
->endpos
= token
->pos
;
712 // private struct/union/enum type
713 if (!match_op(token
, '{')) {
714 sparse_error(token
->pos
, "expected declaration");
715 ctx
->ctype
.base_type
= &bad_ctype
;
719 sym
= alloc_symbol(token
->pos
, type
);
720 token
= parse(token
->next
, sym
);
721 ctx
->ctype
.base_type
= sym
;
722 token
= expect(token
, '}', "at end of specifier");
723 sym
->endpos
= token
->pos
;
728 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
730 struct symbol
*field
, *last
= NULL
;
732 res
= struct_declaration_list(token
, &sym
->symbol_list
);
733 FOR_EACH_PTR(sym
->symbol_list
, field
) {
735 struct symbol
*base
= field
->ctype
.base_type
;
736 if (base
&& base
->type
== SYM_BITFIELD
)
740 last
->next_subobject
= field
;
742 } END_FOR_EACH_PTR(field
);
746 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
748 return struct_declaration_list(token
, &sym
->symbol_list
);
751 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
753 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
756 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
758 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
764 unsigned long long y
;
767 static void upper_boundary(Num
*n
, Num
*v
)
779 static void lower_boundary(Num
*n
, Num
*v
)
791 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
793 int shift
= type
->bit_size
;
794 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
798 if (upper
->x
== 0 && upper
->y
>> shift
)
800 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
805 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
807 if (s1
->bit_size
< s2
->bit_size
) {
809 } else if (s1
->bit_size
== s2
->bit_size
) {
810 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
813 if (s1
->bit_size
< bits_in_int
)
818 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
822 FOR_EACH_PTR(list
, sym
) {
823 struct expression
*expr
= sym
->initializer
;
824 struct symbol
*ctype
;
825 if (expr
->type
!= EXPR_VALUE
)
828 if (ctype
->bit_size
== base_type
->bit_size
)
830 cast_value(expr
, base_type
, expr
, ctype
);
831 } END_FOR_EACH_PTR(sym
);
834 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
836 unsigned long long lastval
= 0;
837 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
838 Num upper
= {-1, 0}, lower
= {1, 0};
840 parent
->examined
= 1;
841 parent
->ctype
.base_type
= &int_ctype
;
842 while (token_type(token
) == TOKEN_IDENT
) {
843 struct expression
*expr
= NULL
;
844 struct token
*next
= token
->next
;
847 if (match_op(next
, '=')) {
848 next
= constant_expression(next
->next
, &expr
);
849 lastval
= get_expression_value(expr
);
851 if (expr
&& expr
->ctype
)
855 } else if (is_int_type(ctype
)) {
858 error_die(token
->pos
, "can't increment the last enum member");
862 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
863 expr
->value
= lastval
;
867 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
868 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
869 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
870 sym
->initializer
= expr
;
871 sym
->enum_member
= 1;
872 sym
->ctype
.base_type
= parent
;
873 add_ptr_list(&parent
->symbol_list
, sym
);
875 if (base_type
!= &bad_ctype
) {
876 if (ctype
->type
== SYM_NODE
)
877 ctype
= ctype
->ctype
.base_type
;
878 if (ctype
->type
== SYM_ENUM
) {
882 ctype
= ctype
->ctype
.base_type
;
886 * - if all enums are of the same type, then
887 * the base_type is that type (two first
889 * - if enums are of different types, they
890 * all have to be integer types, and the
891 * base type is at least "int_ctype".
892 * - otherwise the base_type is "bad_ctype".
896 } else if (ctype
== base_type
) {
898 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
899 base_type
= bigger_enum_type(base_type
, ctype
);
901 base_type
= &bad_ctype
;
902 parent
->ctype
.base_type
= base_type
;
904 if (is_int_type(base_type
)) {
905 Num v
= {.y
= lastval
};
906 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
908 else if ((long long)lastval
>= 0)
912 upper_boundary(&upper
, &v
);
913 lower_boundary(&lower
, &v
);
917 sym
->endpos
= token
->pos
;
919 if (!match_op(token
, ','))
924 sparse_error(token
->pos
, "bad enum definition");
925 base_type
= &bad_ctype
;
927 else if (!is_int_type(base_type
))
928 base_type
= base_type
;
929 else if (type_is_ok(base_type
, &upper
, &lower
))
930 base_type
= base_type
;
931 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
932 base_type
= &int_ctype
;
933 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
934 base_type
= &uint_ctype
;
935 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
936 base_type
= &long_ctype
;
937 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
938 base_type
= &ulong_ctype
;
939 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
940 base_type
= &llong_ctype
;
941 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
942 base_type
= &ullong_ctype
;
944 base_type
= &bad_ctype
;
945 parent
->ctype
.base_type
= base_type
;
946 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
947 parent
->examined
= 0;
949 cast_enum_list(parent
->symbol_list
, base_type
);
954 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
956 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
957 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
959 if (!ctype
->base_type
)
960 ctype
->base_type
= &incomplete_ctype
;
965 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
969 if (!match_op(token
, '(')) {
970 sparse_error(token
->pos
, "expected '(' after typeof");
973 if (lookup_type(token
->next
)) {
974 token
= typename(token
->next
, &sym
, NULL
);
975 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
976 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
978 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
979 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
981 typeof_sym
->endpos
= token
->pos
;
982 if (!typeof_sym
->initializer
) {
983 sparse_error(token
->pos
, "expected expression after the '(' token");
984 typeof_sym
= &bad_ctype
;
986 ctx
->ctype
.base_type
= typeof_sym
;
988 return expect(token
, ')', "after typeof");
991 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
993 struct expression
*expr
= NULL
;
994 if (match_op(token
, '('))
995 token
= parens_expression(token
, &expr
, "in attribute");
999 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1001 if (!ctx
->ctype
.alignment
)
1002 ctx
->ctype
.alignment
= 1;
1006 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1008 int alignment
= max_alignment
;
1009 struct expression
*expr
= NULL
;
1011 if (match_op(token
, '(')) {
1012 token
= parens_expression(token
, &expr
, "in attribute");
1014 alignment
= const_expression_value(expr
);
1016 if (alignment
& (alignment
-1)) {
1017 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1019 } else if (alignment
> ctx
->ctype
.alignment
)
1020 ctx
->ctype
.alignment
= alignment
;
1024 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1026 if (ctx
->modifiers
& qual
)
1027 warning(*pos
, "duplicate %s", modifier_string(qual
));
1028 ctx
->modifiers
|= qual
;
1031 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1033 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1037 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1040 attribute_modifier(token
, attr
, ctx
);
1044 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1046 struct expression
*expr
= NULL
;
1048 token
= expect(token
, '(', "after address_space attribute");
1049 token
= conditional_expression(token
, &expr
);
1051 as
= const_expression_value(expr
);
1052 if (Waddress_space
&& as
)
1055 token
= expect(token
, ')', "after address_space attribute");
1059 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1061 if (ctype
->ctype
.base_type
!= &int_type
)
1063 if (ctype
== &char_ctype
)
1065 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1069 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1071 if (ctype
->ctype
.base_type
!= &int_type
)
1073 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1077 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1079 if (ctype
->ctype
.base_type
!= &int_type
)
1081 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1085 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1087 if (ctype
->ctype
.base_type
!= &int_type
)
1089 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1093 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1095 if (ctype
->ctype
.base_type
!= &int_type
)
1097 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1101 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1103 if (ctype
->ctype
.base_type
!= &int_type
)
1105 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1109 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1111 token
= expect(token
, '(', "after mode attribute");
1112 if (token_type(token
) == TOKEN_IDENT
) {
1113 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1114 if (mode
&& mode
->op
->type
== KW_MODE
)
1115 ctx
->mode
= mode
->op
;
1117 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1118 token
= token
->next
;
1120 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1121 token
= expect(token
, ')', "after mode attribute");
1125 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1127 struct context
*context
= alloc_context();
1128 struct expression
*args
[3];
1131 token
= expect(token
, '(', "after context attribute");
1132 while (!match_op(token
, ')')) {
1133 struct expression
*expr
= NULL
;
1134 token
= conditional_expression(token
, &expr
);
1138 args
[argc
++] = expr
;
1139 if (!match_op(token
, ','))
1141 token
= token
->next
;
1146 sparse_error(token
->pos
, "expected context input/output values");
1149 context
->in
= get_expression_value(args
[0]);
1152 context
->in
= get_expression_value(args
[0]);
1153 context
->out
= get_expression_value(args
[1]);
1156 context
->context
= args
[0];
1157 context
->in
= get_expression_value(args
[1]);
1158 context
->out
= get_expression_value(args
[2]);
1163 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1165 token
= expect(token
, ')', "after context attribute");
1169 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1171 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1172 ctx
->ctype
.base_type
->designated_init
= 1;
1174 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1178 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1180 if (Wtransparent_union
)
1181 warning(token
->pos
, "attribute __transparent_union__");
1183 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1184 ctx
->ctype
.base_type
->transparent_union
= 1;
1186 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1190 static struct token
*recover_unknown_attribute(struct token
*token
)
1192 struct expression
*expr
= NULL
;
1194 if (Wunknown_attribute
)
1195 warning(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1196 token
= token
->next
;
1197 if (match_op(token
, '('))
1198 token
= parens_expression(token
, &expr
, "in attribute");
1202 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1204 token
= expect(token
, '(', "after attribute");
1205 token
= expect(token
, '(', "after attribute");
1208 struct ident
*attribute_name
;
1209 struct symbol
*attr
;
1211 if (eof_token(token
))
1213 if (match_op(token
, ';'))
1215 if (token_type(token
) != TOKEN_IDENT
)
1217 attribute_name
= token
->ident
;
1218 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1219 if (attr
&& attr
->op
->attribute
)
1220 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1222 token
= recover_unknown_attribute(token
);
1224 if (!match_op(token
, ','))
1226 token
= token
->next
;
1229 token
= expect(token
, ')', "after attribute");
1230 token
= expect(token
, ')', "after attribute");
1234 static const char *storage_class
[] =
1236 [STypedef
] = "typedef",
1238 [SExtern
] = "extern",
1239 [SStatic
] = "static",
1240 [SRegister
] = "register",
1241 [SForced
] = "[force]"
1244 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1246 static unsigned long mod
[SMax
] =
1249 [SExtern
] = MOD_EXTERN
,
1250 [SStatic
] = MOD_STATIC
,
1251 [SRegister
] = MOD_REGISTER
1253 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1254 | (ctx
->is_tls
? MOD_TLS
: 0);
1257 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1259 /* __thread can be used alone, or with extern or static */
1260 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1261 sparse_error(*pos
, "__thread can only be used alone, or with "
1262 "extern or static");
1266 if (!ctx
->storage_class
) {
1267 ctx
->storage_class
= class;
1270 if (ctx
->storage_class
== class)
1271 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1273 sparse_error(*pos
, "multiple storage classes");
1276 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1278 set_storage_class(&next
->pos
, ctx
, STypedef
);
1282 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1284 set_storage_class(&next
->pos
, ctx
, SAuto
);
1288 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1290 set_storage_class(&next
->pos
, ctx
, SRegister
);
1294 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1296 set_storage_class(&next
->pos
, ctx
, SStatic
);
1300 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1302 set_storage_class(&next
->pos
, ctx
, SExtern
);
1306 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1308 /* This GCC extension can be used alone, or with extern or static */
1309 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1310 || ctx
->storage_class
== SExtern
) {
1313 sparse_error(next
->pos
, "__thread can only be used alone, or "
1314 "with extern or static");
1320 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1322 set_storage_class(&token
->pos
, ctx
, SForced
);
1326 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1332 static struct token
*noreturn_specifier(struct token
*next
, struct decl_state
*ctx
)
1334 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_NORETURN
);
1338 static struct token
*alignas_specifier(struct token
*token
, struct decl_state
*ctx
)
1342 if (!match_op(token
, '(')) {
1343 sparse_error(token
->pos
, "expected '(' after _Alignas");
1346 if (lookup_type(token
->next
)) {
1347 struct symbol
*sym
= NULL
;
1348 token
= typename(token
->next
, &sym
, NULL
);
1349 sym
= examine_symbol_type(sym
);
1350 alignment
= sym
->ctype
.alignment
;
1351 token
= expect(token
, ')', "after _Alignas(...");
1353 struct expression
*expr
= NULL
;
1354 token
= parens_expression(token
, &expr
, "after _Alignas");
1357 alignment
= const_expression_value(expr
);
1360 if (alignment
< 0) {
1361 warning(token
->pos
, "non-positive alignment");
1364 if (alignment
& (alignment
-1)) {
1365 warning(token
->pos
, "non-power-of-2 alignment");
1368 if (alignment
> ctx
->ctype
.alignment
)
1369 ctx
->ctype
.alignment
= alignment
;
1373 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1375 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1379 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1381 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1385 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1387 unsigned long mod
= thistype
->modifiers
;
1390 apply_qualifier(&pos
, ctype
, mod
);
1393 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1394 (struct ptr_list
**)&ctype
->contexts
);
1397 if (thistype
->alignment
> ctype
->alignment
)
1398 ctype
->alignment
= thistype
->alignment
;
1402 ctype
->as
= thistype
->as
;
1405 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1408 if (what
& (Set_S
| Set_T
))
1410 if (what
& Set_Char
)
1412 else if (what
& Set_Double
)
1414 else if (what
& Set_Float
)
1416 else if (what
& Set_Signed
)
1418 else if (what
& Set_Unsigned
)
1420 else if (what
& Set_Short
)
1422 else if (what
& Set_Long
)
1426 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1427 old
, show_ident(new));
1431 sparse_error(pos
, "two or more data types in declaration specifiers");
1434 static struct symbol
* const int_types
[] =
1435 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &lllong_ctype
};
1436 static struct symbol
* const signed_types
[] =
1437 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1439 static struct symbol
* const unsigned_types
[] =
1440 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1442 static struct symbol
* const real_types
[] =
1443 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1444 static struct symbol
* const char_types
[] =
1445 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1446 static struct symbol
* const * const types
[] = {
1447 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1448 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1451 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1453 return types
[want_unsigned
? CUInt
: CInt
][size
];
1456 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1458 while (token_type(t
) == TOKEN_IDENT
) {
1459 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1462 if (s
->type
!= SYM_KEYWORD
)
1464 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1467 if (s
->op
->declarator
)
1468 t
= s
->op
->declarator(t
, ctx
);
1473 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1479 while (token_type(token
) == TOKEN_IDENT
) {
1480 struct symbol
*s
= lookup_symbol(token
->ident
,
1481 NS_TYPEDEF
| NS_SYMBOL
);
1482 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1484 if (s
->type
!= SYM_KEYWORD
) {
1487 seen
|= Set_S
| Set_T
;
1488 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1489 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1490 token
= token
->next
;
1493 if (s
->op
->type
& KW_SPECIFIER
) {
1494 if (seen
& s
->op
->test
) {
1495 specifier_conflict(token
->pos
,
1501 class += s
->op
->class;
1502 if (s
->op
->set
& Set_Int128
)
1504 if (s
->op
->type
& KW_SHORT
) {
1506 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1507 if (class == CReal
) {
1508 specifier_conflict(token
->pos
,
1516 token
= token
->next
;
1517 if (s
->op
->declarator
)
1518 token
= s
->op
->declarator(token
, ctx
);
1519 if (s
->op
->type
& KW_EXACT
) {
1520 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1521 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1525 if (!(seen
& Set_S
)) { /* not set explicitly? */
1526 struct symbol
*base
= &incomplete_ctype
;
1528 base
= types
[class][size
];
1529 ctx
->ctype
.base_type
= base
;
1532 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1533 struct symbol
*type
;
1534 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1535 if (!is_int_type(ctx
->ctype
.base_type
)) {
1536 sparse_error(token
->pos
, "invalid modifier");
1539 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1540 *type
= *ctx
->ctype
.base_type
;
1541 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1542 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1543 type
->type
= SYM_RESTRICT
;
1544 ctx
->ctype
.base_type
= type
;
1545 create_fouled(type
);
1550 static struct token
*abstract_array_static_declarator(struct token
*token
, int *has_static
)
1552 while (token
->ident
== &static_ident
) {
1554 sparse_error(token
->pos
, "duplicate array static declarator");
1557 token
= token
->next
;
1563 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1565 struct expression
*expr
= NULL
;
1568 token
= abstract_array_static_declarator(token
, &has_static
);
1570 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, &__restrict___ident
, NULL
))
1571 token
= abstract_array_static_declarator(token
->next
, &has_static
);
1572 token
= parse_expression(token
, &expr
);
1573 sym
->array_size
= expr
;
1577 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1578 static struct token
*identifier_list(struct token
*, struct symbol
*);
1579 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1581 static struct token
*skip_attribute(struct token
*token
)
1583 token
= token
->next
;
1584 if (match_op(token
, '(')) {
1586 token
= token
->next
;
1587 while (depth
&& !eof_token(token
)) {
1588 if (token_type(token
) == TOKEN_SPECIAL
) {
1589 if (token
->special
== '(')
1591 else if (token
->special
== ')')
1594 token
= token
->next
;
1600 static struct token
*skip_attributes(struct token
*token
)
1602 struct symbol
*keyword
;
1604 if (token_type(token
) != TOKEN_IDENT
)
1606 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1607 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1609 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1611 token
= expect(token
->next
, '(', "after attribute");
1612 token
= expect(token
, '(', "after attribute");
1614 if (eof_token(token
))
1616 if (match_op(token
, ';'))
1618 if (token_type(token
) != TOKEN_IDENT
)
1620 token
= skip_attribute(token
);
1621 if (!match_op(token
, ','))
1623 token
= token
->next
;
1625 token
= expect(token
, ')', "after attribute");
1626 token
= expect(token
, ')', "after attribute");
1631 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1633 struct symbol
*keyword
;
1635 if (token_type(token
) != TOKEN_IDENT
)
1637 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1638 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1640 if (!(keyword
->op
->type
& keywords
))
1642 token
= keyword
->op
->declarator(token
->next
, ctx
);
1643 keywords
&= KW_ATTRIBUTE
;
1648 static int is_nested(struct token
*token
, struct token
**p
,
1649 int prefer_abstract
)
1652 * This can be either a parameter list or a grouping.
1653 * For the direct (non-abstract) case, we know if must be
1654 * a parameter list if we already saw the identifier.
1655 * For the abstract case, we know if must be a parameter
1656 * list if it is empty or starts with a type.
1658 struct token
*next
= token
->next
;
1660 *p
= next
= skip_attributes(next
);
1662 if (token_type(next
) == TOKEN_IDENT
) {
1663 if (lookup_type(next
))
1664 return !prefer_abstract
;
1668 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1675 Empty
, K_R
, Proto
, Bad_Func
,
1678 static enum kind
which_func(struct token
*token
,
1680 int prefer_abstract
)
1682 struct token
*next
= token
->next
;
1684 if (token_type(next
) == TOKEN_IDENT
) {
1685 if (lookup_type(next
))
1687 /* identifier list not in definition; complain */
1688 if (prefer_abstract
)
1690 "identifier list not in definition");
1694 if (token_type(next
) != TOKEN_SPECIAL
)
1697 if (next
->special
== ')') {
1698 /* don't complain about those */
1699 if (!n
|| match_op(next
->next
, ';'))
1702 "non-ANSI function declaration of function '%s'",
1707 if (next
->special
== SPECIAL_ELLIPSIS
) {
1709 "variadic functions must have one named argument");
1716 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1718 struct ctype
*ctype
= &ctx
->ctype
;
1720 struct ident
**p
= ctx
->ident
;
1722 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1723 *ctx
->ident
= token
->ident
;
1724 token
= token
->next
;
1725 } else if (match_op(token
, '(') &&
1726 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1727 struct symbol
*base_type
= ctype
->base_type
;
1728 if (token
->next
!= next
)
1729 next
= handle_attributes(token
->next
, ctx
,
1731 token
= declarator(next
, ctx
);
1732 token
= expect(token
, ')', "in nested declarator");
1733 while (ctype
->base_type
!= base_type
)
1734 ctype
= &ctype
->base_type
->ctype
;
1738 if (match_op(token
, '(')) {
1739 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1741 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1742 token
= token
->next
;
1744 token
= identifier_list(token
, fn
);
1745 else if (kind
== Proto
)
1746 token
= parameter_type_list(token
, fn
);
1747 token
= expect(token
, ')', "in function declarator");
1748 fn
->endpos
= token
->pos
;
1752 while (match_op(token
, '[')) {
1753 struct symbol
*array
;
1754 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1755 token
= abstract_array_declarator(token
->next
, array
);
1756 token
= expect(token
, ']', "in abstract_array_declarator");
1757 array
->endpos
= token
->pos
;
1758 ctype
= &array
->ctype
;
1763 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1765 while (match_op(token
,'*')) {
1766 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1767 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1768 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1769 ptr
->ctype
.as
= ctx
->ctype
.as
;
1770 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1771 ctx
->ctype
.modifiers
= 0;
1772 ctx
->ctype
.base_type
= ptr
;
1774 ctx
->ctype
.contexts
= NULL
;
1775 ctx
->ctype
.alignment
= 0;
1777 token
= handle_qualifiers(token
->next
, ctx
);
1778 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1783 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1785 token
= pointer(token
, ctx
);
1786 return direct_declarator(token
, ctx
);
1789 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1791 struct ctype
*ctype
= &ctx
->ctype
;
1792 struct expression
*expr
;
1793 struct symbol
*bitfield
;
1796 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1797 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1798 show_typename(ctype
->base_type
));
1799 // Parse this to recover gracefully.
1800 return conditional_expression(token
->next
, &expr
);
1803 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1804 token
= conditional_expression(token
->next
, &expr
);
1805 width
= const_expression_value(expr
);
1806 bitfield
->bit_size
= width
;
1808 if (width
< 0 || width
> INT_MAX
) {
1809 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1811 } else if (*ctx
->ident
&& width
== 0) {
1812 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1813 show_ident(*ctx
->ident
));
1815 } else if (*ctx
->ident
) {
1816 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1817 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1818 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1819 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1820 // Valid values are either {-1;0} or {0}, depending on integer
1821 // representation. The latter makes for very efficient code...
1822 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1824 if (Wdefault_bitfield_sign
&&
1825 bitfield_type
->type
!= SYM_ENUM
&&
1826 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1828 // The sign of bitfields is unspecified by default.
1829 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1832 bitfield
->bit_size
= width
;
1833 bitfield
->endpos
= token
->pos
;
1837 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1839 struct decl_state ctx
= {.prefer_abstract
= 0};
1843 token
= declaration_specifiers(token
, &ctx
);
1844 mod
= storage_modifiers(&ctx
);
1847 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1848 ctx
.ident
= &decl
->ident
;
1850 token
= declarator(token
, &ctx
);
1851 if (match_op(token
, ':'))
1852 token
= handle_bitfield(token
, &ctx
);
1854 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1855 apply_modifiers(token
->pos
, &ctx
);
1857 decl
->ctype
= ctx
.ctype
;
1858 decl
->ctype
.modifiers
|= mod
;
1859 decl
->endpos
= token
->pos
;
1860 add_symbol(list
, decl
);
1861 if (!match_op(token
, ','))
1863 token
= token
->next
;
1869 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1871 while (!match_op(token
, '}')) {
1872 if (match_ident(token
, &_Static_assert_ident
)) {
1873 token
= parse_static_assert(token
, NULL
);
1876 if (!match_op(token
, ';'))
1877 token
= declaration_list(token
, list
);
1878 if (!match_op(token
, ';')) {
1879 sparse_error(token
->pos
, "expected ; at end of declaration");
1882 token
= token
->next
;
1887 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1889 struct decl_state ctx
= {.prefer_abstract
= 1};
1891 token
= declaration_specifiers(token
, &ctx
);
1892 ctx
.ident
= &sym
->ident
;
1893 token
= declarator(token
, &ctx
);
1894 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1895 apply_modifiers(token
->pos
, &ctx
);
1896 sym
->ctype
= ctx
.ctype
;
1897 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1898 sym
->endpos
= token
->pos
;
1899 sym
->forced_arg
= ctx
.storage_class
== SForced
;
1903 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1905 struct decl_state ctx
= {.prefer_abstract
= 1};
1907 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1909 token
= declaration_specifiers(token
, &ctx
);
1910 token
= declarator(token
, &ctx
);
1911 apply_modifiers(token
->pos
, &ctx
);
1912 sym
->ctype
= ctx
.ctype
;
1913 sym
->endpos
= token
->pos
;
1914 class = ctx
.storage_class
;
1917 if (class == SForced
) {
1923 warning(sym
->pos
, "storage class in typename (%s %s)",
1924 storage_class
[class], show_typename(sym
));
1928 static struct token
*parse_underscore_Pragma(struct token
*token
)
1933 if (!match_op(next
, '('))
1936 if (next
->pos
.type
!= TOKEN_STRING
)
1939 if (!match_op(next
, ')'))
1944 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1946 if (match_ident(token
, &_Pragma_ident
))
1947 return parse_underscore_Pragma(token
);
1949 token
= parse_expression(token
, tree
);
1950 return expect(token
, ';', "at end of statement");
1953 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1954 struct expression_list
**inout
)
1956 struct expression
*expr
;
1958 /* Allow empty operands */
1959 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1962 struct ident
*ident
= NULL
;
1963 if (match_op(token
->next
, '[') &&
1964 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1965 match_op(token
->next
->next
->next
, ']')) {
1966 ident
= token
->next
->next
->ident
;
1967 token
= token
->next
->next
->next
;
1969 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1970 token
= primary_expression(token
->next
, &expr
);
1971 add_expression(inout
, expr
);
1972 token
= parens_expression(token
, &expr
, "in asm parameter");
1973 add_expression(inout
, expr
);
1974 } while (match_op(token
, ','));
1978 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1979 struct expression_list
**clobbers
)
1981 struct expression
*expr
;
1984 token
= primary_expression(token
->next
, &expr
);
1986 add_expression(clobbers
, expr
);
1987 } while (match_op(token
, ','));
1991 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1992 struct symbol_list
**labels
)
1994 struct symbol
*label
;
1997 token
= token
->next
; /* skip ':' and ',' */
1998 if (token_type(token
) != TOKEN_IDENT
)
2000 label
= label_symbol(token
);
2001 add_symbol(labels
, label
);
2002 token
= token
->next
;
2003 } while (match_op(token
, ','));
2007 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2011 token
= token
->next
;
2012 stmt
->type
= STMT_ASM
;
2013 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
2014 token
= token
->next
;
2016 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
2018 token
= token
->next
;
2020 token
= expect(token
, '(', "after asm");
2021 token
= parse_expression(token
, &stmt
->asm_string
);
2022 if (match_op(token
, ':'))
2023 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2024 if (match_op(token
, ':'))
2025 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2026 if (match_op(token
, ':'))
2027 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2028 if (is_goto
&& match_op(token
, ':'))
2029 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2030 token
= expect(token
, ')', "after asm");
2031 return expect(token
, ';', "at end of asm-statement");
2034 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
2036 struct expression
*expr
;
2037 token
= expect(token
, '(', "after asm");
2038 token
= parse_expression(token
->next
, &expr
);
2039 token
= expect(token
, ')', "after asm");
2043 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2045 struct expression
*cond
= NULL
, *message
= NULL
;
2047 token
= expect(token
->next
, '(', "after _Static_assert");
2048 token
= constant_expression(token
, &cond
);
2050 sparse_error(token
->pos
, "Expected constant expression");
2051 token
= expect(token
, ',', "after conditional expression in _Static_assert");
2052 token
= parse_expression(token
, &message
);
2053 if (!message
|| message
->type
!= EXPR_STRING
) {
2054 struct position pos
;
2056 pos
= message
? message
->pos
: token
->pos
;
2057 sparse_error(pos
, "bad or missing string literal");
2060 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2062 token
= expect(token
, ';', "after _Static_assert()");
2064 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
)
2065 sparse_error(cond
->pos
, "static assertion failed: %s",
2066 show_string(message
->string
));
2070 /* Make a statement out of an expression */
2071 static struct statement
*make_statement(struct expression
*expr
)
2073 struct statement
*stmt
;
2077 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2078 stmt
->expression
= expr
;
2083 * All iterators have two symbols associated with them:
2084 * the "continue" and "break" symbols, which are targets
2085 * for continue and break statements respectively.
2087 * They are in a special name-space, but they follow
2088 * all the normal visibility rules, so nested iterators
2089 * automatically work right.
2091 static void start_iterator(struct statement
*stmt
)
2093 struct symbol
*cont
, *brk
;
2095 start_symbol_scope(stmt
->pos
);
2096 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2097 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2098 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2099 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2101 stmt
->type
= STMT_ITERATOR
;
2102 stmt
->iterator_break
= brk
;
2103 stmt
->iterator_continue
= cont
;
2104 fn_local_symbol(brk
);
2105 fn_local_symbol(cont
);
2108 static void end_iterator(struct statement
*stmt
)
2113 static struct statement
*start_function(struct symbol
*sym
)
2116 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2118 start_function_scope(sym
->pos
);
2119 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2120 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2121 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2122 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2123 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2125 fn_local_symbol(ret
);
2127 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2133 static void end_function(struct symbol
*sym
)
2136 end_function_scope();
2140 * A "switch()" statement, like an iterator, has a
2141 * the "break" symbol associated with it. It works
2142 * exactly like the iterator break - it's the target
2143 * for any break-statements in scope, and means that
2144 * "break" handling doesn't even need to know whether
2145 * it's breaking out of an iterator or a switch.
2147 * In addition, the "case" symbol is a marker for the
2148 * case/default statements to find the switch statement
2149 * that they are associated with.
2151 static void start_switch(struct statement
*stmt
)
2153 struct symbol
*brk
, *switch_case
;
2155 start_symbol_scope(stmt
->pos
);
2156 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2157 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2159 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2160 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2161 switch_case
->stmt
= stmt
;
2163 stmt
->type
= STMT_SWITCH
;
2164 stmt
->switch_break
= brk
;
2165 stmt
->switch_case
= switch_case
;
2167 fn_local_symbol(brk
);
2168 fn_local_symbol(switch_case
);
2171 static void end_switch(struct statement
*stmt
)
2173 if (!stmt
->switch_case
->symbol_list
)
2174 warning(stmt
->pos
, "switch with no cases");
2178 static void add_case_statement(struct statement
*stmt
)
2180 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2184 sparse_error(stmt
->pos
, "not in switch scope");
2185 stmt
->type
= STMT_NONE
;
2188 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2189 add_symbol(&target
->symbol_list
, sym
);
2191 stmt
->case_label
= sym
;
2192 fn_local_symbol(sym
);
2195 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2197 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2200 error_die(token
->pos
, "internal error: return without a function target");
2201 stmt
->type
= STMT_RETURN
;
2202 stmt
->ret_target
= target
;
2203 return expression_statement(token
->next
, &stmt
->ret_value
);
2206 static void validate_for_loop_decl(struct symbol
*sym
)
2208 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2210 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2211 const char *name
= show_ident(sym
->ident
);
2212 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2213 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2217 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2219 struct symbol_list
*syms
;
2220 struct expression
*e1
, *e2
, *e3
;
2221 struct statement
*iterator
;
2223 start_iterator(stmt
);
2224 token
= expect(token
->next
, '(', "after 'for'");
2228 /* C99 variable declaration? */
2229 if (lookup_type(token
)) {
2230 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2232 token
= parse_expression(token
, &e1
);
2233 token
= expect(token
, ';', "in 'for'");
2235 token
= parse_expression(token
, &e2
);
2236 token
= expect(token
, ';', "in 'for'");
2237 token
= parse_expression(token
, &e3
);
2238 token
= expect(token
, ')', "in 'for'");
2239 token
= statement(token
, &iterator
);
2241 stmt
->iterator_syms
= syms
;
2242 stmt
->iterator_pre_statement
= make_statement(e1
);
2243 stmt
->iterator_pre_condition
= e2
;
2244 stmt
->iterator_post_statement
= make_statement(e3
);
2245 stmt
->iterator_post_condition
= NULL
;
2246 stmt
->iterator_statement
= iterator
;
2252 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2254 struct expression
*expr
;
2255 struct statement
*iterator
;
2257 start_iterator(stmt
);
2258 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2259 token
= statement(token
, &iterator
);
2261 stmt
->iterator_pre_condition
= expr
;
2262 stmt
->iterator_post_condition
= NULL
;
2263 stmt
->iterator_statement
= iterator
;
2269 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2271 struct expression
*expr
;
2272 struct statement
*iterator
;
2274 start_iterator(stmt
);
2275 token
= statement(token
->next
, &iterator
);
2276 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2277 token
= token
->next
;
2279 sparse_error(token
->pos
, "expected 'while' after 'do'");
2280 token
= parens_expression(token
, &expr
, "after 'do-while'");
2282 stmt
->iterator_post_condition
= expr
;
2283 stmt
->iterator_statement
= iterator
;
2286 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2287 warning(iterator
->pos
, "do-while statement is not a compound statement");
2289 return expect(token
, ';', "after statement");
2292 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2294 stmt
->type
= STMT_IF
;
2295 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2296 token
= statement(token
, &stmt
->if_true
);
2297 if (token_type(token
) != TOKEN_IDENT
)
2299 if (token
->ident
!= &else_ident
)
2301 return statement(token
->next
, &stmt
->if_false
);
2304 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2306 stmt
->type
= STMT_CASE
;
2307 token
= expect(token
, ':', "after default/case");
2308 add_case_statement(stmt
);
2309 return statement(token
, &stmt
->case_statement
);
2312 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2314 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2315 if (match_op(token
, SPECIAL_ELLIPSIS
))
2316 token
= parse_expression(token
->next
, &stmt
->case_to
);
2317 return case_statement(token
, stmt
);
2320 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2322 return case_statement(token
->next
, stmt
);
2325 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2327 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2328 stmt
->type
= STMT_GOTO
;
2329 stmt
->goto_label
= target
;
2331 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2332 return expect(token
->next
, ';', "at end of statement");
2335 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2337 stmt
->type
= STMT_SWITCH
;
2339 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2340 token
= statement(token
, &stmt
->switch_statement
);
2345 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2347 stmt
->type
= STMT_GOTO
;
2348 token
= token
->next
;
2349 if (match_op(token
, '*')) {
2350 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2351 add_statement(&function_computed_goto_list
, stmt
);
2352 } else if (token_type(token
) == TOKEN_IDENT
) {
2353 stmt
->goto_label
= label_symbol(token
);
2354 token
= token
->next
;
2356 sparse_error(token
->pos
, "Expected identifier or goto expression");
2358 return expect(token
, ';', "at end of statement");
2361 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2363 stmt
->type
= STMT_CONTEXT
;
2364 token
= parse_expression(token
->next
, &stmt
->expression
);
2365 if (stmt
->expression
->type
== EXPR_PREOP
2366 && stmt
->expression
->op
== '('
2367 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2368 struct expression
*expr
;
2369 expr
= stmt
->expression
->unop
;
2370 stmt
->context
= expr
->left
;
2371 stmt
->expression
= expr
->right
;
2373 return expect(token
, ';', "at end of statement");
2376 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2378 stmt
->type
= STMT_RANGE
;
2379 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2380 token
= expect(token
, ',', "after range expression");
2381 token
= assignment_expression(token
, &stmt
->range_low
);
2382 token
= expect(token
, ',', "after low range");
2383 token
= assignment_expression(token
, &stmt
->range_high
);
2384 return expect(token
, ';', "after range statement");
2387 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2389 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2392 if (token_type(token
) == TOKEN_IDENT
) {
2393 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2394 if (s
&& s
->op
->statement
)
2395 return s
->op
->statement(token
, stmt
);
2397 if (match_op(token
->next
, ':')) {
2398 struct symbol
*s
= label_symbol(token
);
2399 stmt
->type
= STMT_LABEL
;
2400 stmt
->label_identifier
= s
;
2402 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(token
->ident
));
2404 token
= skip_attributes(token
->next
->next
);
2405 return statement(token
, &stmt
->label_statement
);
2409 if (match_op(token
, '{')) {
2410 stmt
->type
= STMT_COMPOUND
;
2411 start_symbol_scope(stmt
->pos
);
2412 token
= compound_statement(token
->next
, stmt
);
2415 return expect(token
, '}', "at end of compound statement");
2418 stmt
->type
= STMT_EXPRESSION
;
2419 return expression_statement(token
, &stmt
->expression
);
2422 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2423 static struct token
*label_statement(struct token
*token
)
2425 while (token_type(token
) == TOKEN_IDENT
) {
2426 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2427 /* it's block-scope, but we want label namespace */
2428 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2429 sym
->namespace = NS_LABEL
;
2430 fn_local_symbol(sym
);
2431 token
= token
->next
;
2432 if (!match_op(token
, ','))
2434 token
= token
->next
;
2436 return expect(token
, ';', "at end of label declaration");
2439 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2441 int seen_statement
= 0;
2442 while (token_type(token
) == TOKEN_IDENT
&&
2443 token
->ident
== &__label___ident
)
2444 token
= label_statement(token
->next
);
2446 struct statement
* stmt
;
2447 if (eof_token(token
))
2449 if (match_op(token
, '}'))
2451 if (match_ident(token
, &_Static_assert_ident
)) {
2452 token
= parse_static_assert(token
, NULL
);
2455 if (lookup_type(token
)) {
2456 if (seen_statement
) {
2457 warning(token
->pos
, "mixing declarations and code");
2460 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2461 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2463 seen_statement
= Wdeclarationafterstatement
;
2464 token
= statement(token
, &stmt
);
2466 add_statement(list
, stmt
);
2471 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2473 struct symbol_list
**list
= &fn
->arguments
;
2475 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2476 sym
->ident
= token
->ident
;
2477 token
= token
->next
;
2478 sym
->endpos
= token
->pos
;
2479 sym
->ctype
.base_type
= &incomplete_ctype
;
2480 add_symbol(list
, sym
);
2481 if (!match_op(token
, ',') ||
2482 token_type(token
->next
) != TOKEN_IDENT
||
2483 lookup_type(token
->next
))
2485 token
= token
->next
;
2490 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2492 struct symbol_list
**list
= &fn
->arguments
;
2497 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2499 token
= token
->next
;
2503 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2504 token
= parameter_declaration(token
, sym
);
2505 if (sym
->ctype
.base_type
== &void_ctype
) {
2506 /* Special case: (void) */
2507 if (!*list
&& !sym
->ident
)
2509 warning(token
->pos
, "void parameter");
2511 add_symbol(list
, sym
);
2512 if (!match_op(token
, ','))
2514 token
= token
->next
;
2519 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2521 token
= statement_list(token
, &stmt
->stmts
);
2525 static struct expression
*identifier_expression(struct token
*token
)
2527 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2528 expr
->expr_ident
= token
->ident
;
2532 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2534 int idx_from
, idx_to
;
2535 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2537 idx_from
= const_expression_value(from
);
2540 idx_to
= const_expression_value(to
);
2541 if (idx_to
< idx_from
|| idx_from
< 0)
2542 warning(from
->pos
, "nonsense array initializer index range");
2544 expr
->idx_from
= idx_from
;
2545 expr
->idx_to
= idx_to
;
2549 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2551 int expect_equal
= 0;
2552 struct token
*next
= token
->next
;
2553 struct expression
**tail
= ep
;
2558 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2559 struct expression
*expr
= identifier_expression(token
);
2560 if (Wold_initializer
)
2561 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2562 token
= initializer(&expr
->ident_expression
, next
->next
);
2563 if (expr
->ident_expression
)
2568 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2569 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2570 struct expression
*expr
= identifier_expression(next
);
2572 tail
= &expr
->ident_expression
;
2575 } else if (match_op(token
, '[')) {
2576 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2577 token
= constant_expression(token
->next
, &from
);
2579 sparse_error(token
->pos
, "Expected constant expression");
2582 if (match_op(token
, SPECIAL_ELLIPSIS
))
2583 token
= constant_expression(token
->next
, &to
);
2584 expr
= index_expression(from
, to
);
2586 tail
= &expr
->idx_expression
;
2587 token
= expect(token
, ']', "at end of initializer index");
2594 if (nested
&& !expect_equal
) {
2595 if (!match_op(token
, '='))
2596 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2601 token
= expect(token
, '=', "at end of initializer index");
2603 token
= initializer(tail
, token
);
2609 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2611 struct expression
*expr
;
2614 token
= single_initializer(&expr
, token
);
2617 add_expression(list
, expr
);
2618 if (!match_op(token
, ','))
2620 token
= token
->next
;
2625 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2627 if (match_op(token
, '{')) {
2628 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2630 token
= initializer_list(&expr
->expr_list
, token
->next
);
2631 return expect(token
, '}', "at end of initializer");
2633 return assignment_expression(token
, tree
);
2636 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2639 sparse_error(sym
->pos
, "no identifier for function argument");
2642 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2645 static int is_syscall(struct symbol
*sym
)
2651 macro
= get_macro_name(sym
->pos
);
2653 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2654 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2657 name
= sym
->ident
->name
;
2659 if (name
&& strncmp(name
, "sys_", 4) ==0)
2662 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2669 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2670 struct symbol_list
**list
)
2672 struct symbol_list
**old_symbol_list
;
2673 struct symbol
*base_type
= decl
->ctype
.base_type
;
2674 struct statement
*stmt
, **p
;
2675 struct symbol
*prev
;
2678 old_symbol_list
= function_symbol_list
;
2679 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2680 function_symbol_list
= &decl
->inline_symbol_list
;
2681 p
= &base_type
->inline_stmt
;
2683 function_symbol_list
= &decl
->symbol_list
;
2684 p
= &base_type
->stmt
;
2686 function_computed_target_list
= NULL
;
2687 function_computed_goto_list
= NULL
;
2689 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2690 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2691 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2693 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2694 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2696 stmt
= start_function(decl
);
2699 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2700 declare_argument(arg
, base_type
);
2701 } END_FOR_EACH_PTR(arg
);
2703 token
= compound_statement(token
->next
, stmt
);
2706 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2707 add_symbol(list
, decl
);
2708 else if (is_syscall(decl
)) {
2709 add_symbol(list
, decl
);
2711 printf("parse.c decl: %s\n", decl->ident->name);
2712 char *macro = get_macro_name(decl->pos);
2713 printf("decl macro: %s\n", macro);
2716 check_declaration(decl
);
2717 decl
->definition
= decl
;
2718 prev
= decl
->same_symbol
;
2719 if (prev
&& prev
->definition
) {
2720 warning(decl
->pos
, "multiple definitions for function '%s'",
2721 show_ident(decl
->ident
));
2722 info(prev
->definition
->pos
, " the previous one is here");
2725 rebind_scope(prev
, decl
->scope
);
2726 prev
->definition
= decl
;
2727 prev
= prev
->same_symbol
;
2730 function_symbol_list
= old_symbol_list
;
2731 if (function_computed_goto_list
) {
2732 if (!function_computed_target_list
)
2733 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2735 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2736 stmt
->target_list
= function_computed_target_list
;
2737 } END_FOR_EACH_PTR(stmt
);
2740 return expect(token
, '}', "at end of function");
2743 static void promote_k_r_types(struct symbol
*arg
)
2745 struct symbol
*base
= arg
->ctype
.base_type
;
2746 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2747 arg
->ctype
.base_type
= &int_ctype
;
2751 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2753 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2756 FOR_EACH_PTR(real_args
, arg
) {
2757 struct symbol
*type
;
2759 /* This is quadratic in the number of arguments. We _really_ don't care */
2760 FOR_EACH_PTR(argtypes
, type
) {
2761 if (type
->ident
== arg
->ident
)
2763 } END_FOR_EACH_PTR(type
);
2764 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2768 /* "char" and "short" promote to "int" */
2769 promote_k_r_types(type
);
2771 arg
->ctype
= type
->ctype
;
2772 } END_FOR_EACH_PTR(arg
);
2774 FOR_EACH_PTR(argtypes
, arg
) {
2776 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2777 } END_FOR_EACH_PTR(arg
);
2781 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2782 struct symbol_list
**list
)
2784 struct symbol_list
*args
= NULL
;
2786 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2788 token
= declaration_list(token
, &args
);
2789 if (!match_op(token
, ';')) {
2790 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2793 token
= token
->next
;
2794 } while (lookup_type(token
));
2796 apply_k_r_types(args
, decl
);
2798 if (!match_op(token
, '{')) {
2799 sparse_error(token
->pos
, "expected function body");
2802 return parse_function_body(token
, decl
, list
);
2805 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2807 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2808 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2809 struct statement
*stmt
;
2811 anon
->ctype
.base_type
= fn
;
2812 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2815 token
= parse_asm_statement(token
, stmt
);
2817 add_symbol(list
, anon
);
2821 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2822 validate_decl_t validate_decl
)
2824 struct ident
*ident
= NULL
;
2825 struct symbol
*decl
;
2826 struct decl_state ctx
= { .ident
= &ident
};
2828 struct symbol
*base_type
;
2832 if (match_ident(token
, &_Pragma_ident
))
2833 return parse_underscore_Pragma(token
);
2835 /* Top-level inline asm or static assertion? */
2836 if (token_type(token
) == TOKEN_IDENT
) {
2837 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2838 if (s
&& s
->op
->toplevel
)
2839 return s
->op
->toplevel(token
, list
);
2842 /* Parse declaration-specifiers, if any */
2843 token
= declaration_specifiers(token
, &ctx
);
2844 mod
= storage_modifiers(&ctx
);
2845 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2846 /* Just a type declaration? */
2847 if (match_op(token
, ';')) {
2848 apply_modifiers(token
->pos
, &ctx
);
2853 token
= declarator(token
, &ctx
);
2854 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2855 apply_modifiers(token
->pos
, &ctx
);
2857 decl
->ctype
= ctx
.ctype
;
2858 decl
->ctype
.modifiers
|= mod
;
2859 decl
->endpos
= token
->pos
;
2861 /* Just a type declaration? */
2863 warning(token
->pos
, "missing identifier in declaration");
2864 return expect(token
, ';', "at the end of type declaration");
2867 /* type define declaration? */
2868 is_typedef
= ctx
.storage_class
== STypedef
;
2870 /* Typedefs don't have meaningful storage */
2872 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2874 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2876 base_type
= decl
->ctype
.base_type
;
2879 if (base_type
&& !base_type
->ident
) {
2880 switch (base_type
->type
) {
2885 base_type
->ident
= ident
;
2891 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2892 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
2893 warning(decl
->pos
, "'%s()' has implicit return type",
2894 show_ident(decl
->ident
));
2895 base_type
->ctype
.base_type
= &int_ctype
;
2897 /* K&R argument declaration? */
2898 if (lookup_type(token
))
2899 return parse_k_r_arguments(token
, decl
, list
);
2900 if (match_op(token
, '{'))
2901 return parse_function_body(token
, decl
, list
);
2903 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2904 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2905 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2906 sparse_error(token
->pos
, "void declaration");
2908 if (base_type
== &incomplete_ctype
) {
2909 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
2910 decl
->ctype
.base_type
= &int_ctype
;;
2914 if (!is_typedef
&& match_op(token
, '=')) {
2915 token
= initializer(&decl
->initializer
, token
->next
);
2919 validate_decl(decl
);
2921 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
2922 warning(decl
->pos
, "symbol with external linkage has initializer");
2923 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2926 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2927 add_symbol(list
, decl
);
2928 fn_local_symbol(decl
);
2931 check_declaration(decl
);
2932 if (decl
->same_symbol
) {
2933 decl
->definition
= decl
->same_symbol
->definition
;
2934 decl
->op
= decl
->same_symbol
->op
;
2937 if (!match_op(token
, ','))
2940 token
= token
->next
;
2942 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2944 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2945 token
= declarator(token
, &ctx
);
2946 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2947 apply_modifiers(token
->pos
, &ctx
);
2948 decl
->ctype
= ctx
.ctype
;
2949 decl
->ctype
.modifiers
|= mod
;
2950 decl
->endpos
= token
->pos
;
2952 sparse_error(token
->pos
, "expected identifier name in type definition");
2957 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2959 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2961 /* Function declarations are automatically extern unless specifically static */
2962 base_type
= decl
->ctype
.base_type
;
2963 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2964 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2965 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2968 return expect(token
, ';', "at end of declaration");