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 // The following test is actually wrong for empty
692 // structs, but (1) they are not C99, (2) gcc does
693 // the same thing, and (3) it's easier.
694 if (sym
->symbol_list
)
695 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
697 token
= parse(token
->next
, sym
);
698 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
700 // Mark the structure as needing re-examination
702 sym
->endpos
= token
->pos
;
707 // private struct/union/enum type
708 if (!match_op(token
, '{')) {
709 sparse_error(token
->pos
, "expected declaration");
710 ctx
->ctype
.base_type
= &bad_ctype
;
714 sym
= alloc_symbol(token
->pos
, type
);
715 token
= parse(token
->next
, sym
);
716 ctx
->ctype
.base_type
= sym
;
717 token
= expect(token
, '}', "at end of specifier");
718 sym
->endpos
= token
->pos
;
723 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
725 struct symbol
*field
, *last
= NULL
;
727 res
= struct_declaration_list(token
, &sym
->symbol_list
);
728 FOR_EACH_PTR(sym
->symbol_list
, field
) {
730 struct symbol
*base
= field
->ctype
.base_type
;
731 if (base
&& base
->type
== SYM_BITFIELD
)
735 last
->next_subobject
= field
;
737 } END_FOR_EACH_PTR(field
);
741 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
743 return struct_declaration_list(token
, &sym
->symbol_list
);
746 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
748 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
751 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
753 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
759 unsigned long long y
;
762 static void upper_boundary(Num
*n
, Num
*v
)
774 static void lower_boundary(Num
*n
, Num
*v
)
786 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
788 int shift
= type
->bit_size
;
789 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
793 if (upper
->x
== 0 && upper
->y
>> shift
)
795 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
800 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
802 if (s1
->bit_size
< s2
->bit_size
) {
804 } else if (s1
->bit_size
== s2
->bit_size
) {
805 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
808 if (s1
->bit_size
< bits_in_int
)
813 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
817 FOR_EACH_PTR(list
, sym
) {
818 struct expression
*expr
= sym
->initializer
;
819 struct symbol
*ctype
;
820 if (expr
->type
!= EXPR_VALUE
)
823 if (ctype
->bit_size
== base_type
->bit_size
)
825 cast_value(expr
, base_type
, expr
, ctype
);
826 } END_FOR_EACH_PTR(sym
);
829 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
831 unsigned long long lastval
= 0;
832 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
833 Num upper
= {-1, 0}, lower
= {1, 0};
835 parent
->examined
= 1;
836 parent
->ctype
.base_type
= &int_ctype
;
837 while (token_type(token
) == TOKEN_IDENT
) {
838 struct expression
*expr
= NULL
;
839 struct token
*next
= token
->next
;
842 if (match_op(next
, '=')) {
843 next
= constant_expression(next
->next
, &expr
);
844 lastval
= get_expression_value(expr
);
846 if (expr
&& expr
->ctype
)
850 } else if (is_int_type(ctype
)) {
853 error_die(token
->pos
, "can't increment the last enum member");
857 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
858 expr
->value
= lastval
;
862 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
863 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
864 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
865 sym
->initializer
= expr
;
866 sym
->enum_member
= 1;
867 sym
->ctype
.base_type
= parent
;
868 add_ptr_list(&parent
->symbol_list
, sym
);
870 if (base_type
!= &bad_ctype
) {
871 if (ctype
->type
== SYM_NODE
)
872 ctype
= ctype
->ctype
.base_type
;
873 if (ctype
->type
== SYM_ENUM
) {
877 ctype
= ctype
->ctype
.base_type
;
881 * - if all enums are of the same type, then
882 * the base_type is that type (two first
884 * - if enums are of different types, they
885 * all have to be integer types, and the
886 * base type is at least "int_ctype".
887 * - otherwise the base_type is "bad_ctype".
891 } else if (ctype
== base_type
) {
893 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
894 base_type
= bigger_enum_type(base_type
, ctype
);
896 base_type
= &bad_ctype
;
897 parent
->ctype
.base_type
= base_type
;
899 if (is_int_type(base_type
)) {
900 Num v
= {.y
= lastval
};
901 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
903 else if ((long long)lastval
>= 0)
907 upper_boundary(&upper
, &v
);
908 lower_boundary(&lower
, &v
);
912 sym
->endpos
= token
->pos
;
914 if (!match_op(token
, ','))
919 sparse_error(token
->pos
, "bad enum definition");
920 base_type
= &bad_ctype
;
922 else if (!is_int_type(base_type
))
923 base_type
= base_type
;
924 else if (type_is_ok(base_type
, &upper
, &lower
))
925 base_type
= base_type
;
926 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
927 base_type
= &int_ctype
;
928 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
929 base_type
= &uint_ctype
;
930 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
931 base_type
= &long_ctype
;
932 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
933 base_type
= &ulong_ctype
;
934 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
935 base_type
= &llong_ctype
;
936 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
937 base_type
= &ullong_ctype
;
939 base_type
= &bad_ctype
;
940 parent
->ctype
.base_type
= base_type
;
941 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
942 parent
->examined
= 0;
944 cast_enum_list(parent
->symbol_list
, base_type
);
949 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
951 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
952 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
954 if (!ctype
->base_type
)
955 ctype
->base_type
= &incomplete_ctype
;
960 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
964 if (!match_op(token
, '(')) {
965 sparse_error(token
->pos
, "expected '(' after typeof");
968 if (lookup_type(token
->next
)) {
969 token
= typename(token
->next
, &sym
, NULL
);
970 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
971 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
973 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
974 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
976 typeof_sym
->endpos
= token
->pos
;
977 if (!typeof_sym
->initializer
) {
978 sparse_error(token
->pos
, "expected expression after the '(' token");
979 typeof_sym
= &bad_ctype
;
981 ctx
->ctype
.base_type
= typeof_sym
;
983 return expect(token
, ')', "after typeof");
986 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
988 struct expression
*expr
= NULL
;
989 if (match_op(token
, '('))
990 token
= parens_expression(token
, &expr
, "in attribute");
994 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
996 if (!ctx
->ctype
.alignment
)
997 ctx
->ctype
.alignment
= 1;
1001 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1003 int alignment
= max_alignment
;
1004 struct expression
*expr
= NULL
;
1006 if (match_op(token
, '(')) {
1007 token
= parens_expression(token
, &expr
, "in attribute");
1009 alignment
= const_expression_value(expr
);
1011 if (alignment
& (alignment
-1)) {
1012 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1014 } else if (alignment
> ctx
->ctype
.alignment
)
1015 ctx
->ctype
.alignment
= alignment
;
1019 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1021 if (ctx
->modifiers
& qual
)
1022 warning(*pos
, "duplicate %s", modifier_string(qual
));
1023 ctx
->modifiers
|= qual
;
1026 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1028 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1032 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1035 attribute_modifier(token
, attr
, ctx
);
1039 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1041 struct expression
*expr
= NULL
;
1043 token
= expect(token
, '(', "after address_space attribute");
1044 token
= conditional_expression(token
, &expr
);
1046 as
= const_expression_value(expr
);
1047 if (Waddress_space
&& as
)
1050 token
= expect(token
, ')', "after address_space attribute");
1054 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1056 if (ctype
->ctype
.base_type
!= &int_type
)
1058 if (ctype
== &char_ctype
)
1060 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1064 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1066 if (ctype
->ctype
.base_type
!= &int_type
)
1068 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1072 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1074 if (ctype
->ctype
.base_type
!= &int_type
)
1076 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1080 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1082 if (ctype
->ctype
.base_type
!= &int_type
)
1084 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1088 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1090 if (ctype
->ctype
.base_type
!= &int_type
)
1092 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1096 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1098 if (ctype
->ctype
.base_type
!= &int_type
)
1100 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1104 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1106 token
= expect(token
, '(', "after mode attribute");
1107 if (token_type(token
) == TOKEN_IDENT
) {
1108 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1109 if (mode
&& mode
->op
->type
== KW_MODE
)
1110 ctx
->mode
= mode
->op
;
1112 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1113 token
= token
->next
;
1115 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1116 token
= expect(token
, ')', "after mode attribute");
1120 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1122 struct context
*context
= alloc_context();
1123 struct expression
*args
[3];
1126 token
= expect(token
, '(', "after context attribute");
1127 while (!match_op(token
, ')')) {
1128 struct expression
*expr
= NULL
;
1129 token
= conditional_expression(token
, &expr
);
1133 args
[argc
++] = expr
;
1134 if (!match_op(token
, ','))
1136 token
= token
->next
;
1141 sparse_error(token
->pos
, "expected context input/output values");
1144 context
->in
= get_expression_value(args
[0]);
1147 context
->in
= get_expression_value(args
[0]);
1148 context
->out
= get_expression_value(args
[1]);
1151 context
->context
= args
[0];
1152 context
->in
= get_expression_value(args
[1]);
1153 context
->out
= get_expression_value(args
[2]);
1158 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1160 token
= expect(token
, ')', "after context attribute");
1164 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1166 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1167 ctx
->ctype
.base_type
->designated_init
= 1;
1169 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1173 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1175 if (Wtransparent_union
)
1176 warning(token
->pos
, "attribute __transparent_union__");
1178 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1179 ctx
->ctype
.base_type
->transparent_union
= 1;
1181 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1185 static struct token
*recover_unknown_attribute(struct token
*token
)
1187 struct expression
*expr
= NULL
;
1189 if (Wunknown_attribute
)
1190 warning(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1191 token
= token
->next
;
1192 if (match_op(token
, '('))
1193 token
= parens_expression(token
, &expr
, "in attribute");
1197 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1199 token
= expect(token
, '(', "after attribute");
1200 token
= expect(token
, '(', "after attribute");
1203 struct ident
*attribute_name
;
1204 struct symbol
*attr
;
1206 if (eof_token(token
))
1208 if (match_op(token
, ';'))
1210 if (token_type(token
) != TOKEN_IDENT
)
1212 attribute_name
= token
->ident
;
1213 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1214 if (attr
&& attr
->op
->attribute
)
1215 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1217 token
= recover_unknown_attribute(token
);
1219 if (!match_op(token
, ','))
1221 token
= token
->next
;
1224 token
= expect(token
, ')', "after attribute");
1225 token
= expect(token
, ')', "after attribute");
1229 static const char *storage_class
[] =
1231 [STypedef
] = "typedef",
1233 [SExtern
] = "extern",
1234 [SStatic
] = "static",
1235 [SRegister
] = "register",
1236 [SForced
] = "[force]"
1239 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1241 static unsigned long mod
[SMax
] =
1244 [SExtern
] = MOD_EXTERN
,
1245 [SStatic
] = MOD_STATIC
,
1246 [SRegister
] = MOD_REGISTER
1248 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1249 | (ctx
->is_tls
? MOD_TLS
: 0);
1252 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1254 /* __thread can be used alone, or with extern or static */
1255 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1256 sparse_error(*pos
, "__thread can only be used alone, or with "
1257 "extern or static");
1261 if (!ctx
->storage_class
) {
1262 ctx
->storage_class
= class;
1265 if (ctx
->storage_class
== class)
1266 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1268 sparse_error(*pos
, "multiple storage classes");
1271 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1273 set_storage_class(&next
->pos
, ctx
, STypedef
);
1277 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1279 set_storage_class(&next
->pos
, ctx
, SAuto
);
1283 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1285 set_storage_class(&next
->pos
, ctx
, SRegister
);
1289 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1291 set_storage_class(&next
->pos
, ctx
, SStatic
);
1295 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1297 set_storage_class(&next
->pos
, ctx
, SExtern
);
1301 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1303 /* This GCC extension can be used alone, or with extern or static */
1304 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1305 || ctx
->storage_class
== SExtern
) {
1308 sparse_error(next
->pos
, "__thread can only be used alone, or "
1309 "with extern or static");
1315 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1317 set_storage_class(&token
->pos
, ctx
, SForced
);
1321 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1327 static struct token
*noreturn_specifier(struct token
*next
, struct decl_state
*ctx
)
1329 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_NORETURN
);
1333 static struct token
*alignas_specifier(struct token
*token
, struct decl_state
*ctx
)
1337 if (!match_op(token
, '(')) {
1338 sparse_error(token
->pos
, "expected '(' after _Alignas");
1341 if (lookup_type(token
->next
)) {
1342 struct symbol
*sym
= NULL
;
1343 token
= typename(token
->next
, &sym
, NULL
);
1344 sym
= examine_symbol_type(sym
);
1345 alignment
= sym
->ctype
.alignment
;
1346 token
= expect(token
, ')', "after _Alignas(...");
1348 struct expression
*expr
= NULL
;
1349 token
= parens_expression(token
, &expr
, "after _Alignas");
1352 alignment
= const_expression_value(expr
);
1355 if (alignment
< 0) {
1356 warning(token
->pos
, "non-positive alignment");
1359 if (alignment
& (alignment
-1)) {
1360 warning(token
->pos
, "non-power-of-2 alignment");
1363 if (alignment
> ctx
->ctype
.alignment
)
1364 ctx
->ctype
.alignment
= alignment
;
1368 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1370 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1374 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1376 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1380 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1382 unsigned long mod
= thistype
->modifiers
;
1385 apply_qualifier(&pos
, ctype
, mod
);
1388 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1389 (struct ptr_list
**)&ctype
->contexts
);
1392 if (thistype
->alignment
> ctype
->alignment
)
1393 ctype
->alignment
= thistype
->alignment
;
1397 ctype
->as
= thistype
->as
;
1400 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1403 if (what
& (Set_S
| Set_T
))
1405 if (what
& Set_Char
)
1407 else if (what
& Set_Double
)
1409 else if (what
& Set_Float
)
1411 else if (what
& Set_Signed
)
1413 else if (what
& Set_Unsigned
)
1415 else if (what
& Set_Short
)
1417 else if (what
& Set_Long
)
1421 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1422 old
, show_ident(new));
1426 sparse_error(pos
, "two or more data types in declaration specifiers");
1429 static struct symbol
* const int_types
[] =
1430 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &lllong_ctype
};
1431 static struct symbol
* const signed_types
[] =
1432 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1434 static struct symbol
* const unsigned_types
[] =
1435 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1437 static struct symbol
* const real_types
[] =
1438 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1439 static struct symbol
* const char_types
[] =
1440 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1441 static struct symbol
* const * const types
[] = {
1442 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1443 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1446 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1448 return types
[want_unsigned
? CUInt
: CInt
][size
];
1451 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1453 while (token_type(t
) == TOKEN_IDENT
) {
1454 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1457 if (s
->type
!= SYM_KEYWORD
)
1459 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1462 if (s
->op
->declarator
)
1463 t
= s
->op
->declarator(t
, ctx
);
1468 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1474 while (token_type(token
) == TOKEN_IDENT
) {
1475 struct symbol
*s
= lookup_symbol(token
->ident
,
1476 NS_TYPEDEF
| NS_SYMBOL
);
1477 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1479 if (s
->type
!= SYM_KEYWORD
) {
1482 seen
|= Set_S
| Set_T
;
1483 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1484 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1485 token
= token
->next
;
1488 if (s
->op
->type
& KW_SPECIFIER
) {
1489 if (seen
& s
->op
->test
) {
1490 specifier_conflict(token
->pos
,
1496 class += s
->op
->class;
1497 if (s
->op
->set
& Set_Int128
)
1499 if (s
->op
->type
& KW_SHORT
) {
1501 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1502 if (class == CReal
) {
1503 specifier_conflict(token
->pos
,
1511 token
= token
->next
;
1512 if (s
->op
->declarator
)
1513 token
= s
->op
->declarator(token
, ctx
);
1514 if (s
->op
->type
& KW_EXACT
) {
1515 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1516 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1520 if (!(seen
& Set_S
)) { /* not set explicitly? */
1521 struct symbol
*base
= &incomplete_ctype
;
1523 base
= types
[class][size
];
1524 ctx
->ctype
.base_type
= base
;
1527 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1528 struct symbol
*type
;
1529 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1530 if (!is_int_type(ctx
->ctype
.base_type
)) {
1531 sparse_error(token
->pos
, "invalid modifier");
1534 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1535 *type
= *ctx
->ctype
.base_type
;
1536 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1537 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1538 type
->type
= SYM_RESTRICT
;
1539 ctx
->ctype
.base_type
= type
;
1540 create_fouled(type
);
1545 static struct token
*abstract_array_static_declarator(struct token
*token
, int *has_static
)
1547 while (token
->ident
== &static_ident
) {
1549 sparse_error(token
->pos
, "duplicate array static declarator");
1552 token
= token
->next
;
1558 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1560 struct expression
*expr
= NULL
;
1563 token
= abstract_array_static_declarator(token
, &has_static
);
1565 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, &__restrict___ident
, NULL
))
1566 token
= abstract_array_static_declarator(token
->next
, &has_static
);
1567 token
= parse_expression(token
, &expr
);
1568 sym
->array_size
= expr
;
1572 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1573 static struct token
*identifier_list(struct token
*, struct symbol
*);
1574 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1576 static struct token
*skip_attribute(struct token
*token
)
1578 token
= token
->next
;
1579 if (match_op(token
, '(')) {
1581 token
= token
->next
;
1582 while (depth
&& !eof_token(token
)) {
1583 if (token_type(token
) == TOKEN_SPECIAL
) {
1584 if (token
->special
== '(')
1586 else if (token
->special
== ')')
1589 token
= token
->next
;
1595 static struct token
*skip_attributes(struct token
*token
)
1597 struct symbol
*keyword
;
1599 if (token_type(token
) != TOKEN_IDENT
)
1601 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1602 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1604 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1606 token
= expect(token
->next
, '(', "after attribute");
1607 token
= expect(token
, '(', "after attribute");
1609 if (eof_token(token
))
1611 if (match_op(token
, ';'))
1613 if (token_type(token
) != TOKEN_IDENT
)
1615 token
= skip_attribute(token
);
1616 if (!match_op(token
, ','))
1618 token
= token
->next
;
1620 token
= expect(token
, ')', "after attribute");
1621 token
= expect(token
, ')', "after attribute");
1626 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1628 struct symbol
*keyword
;
1630 if (token_type(token
) != TOKEN_IDENT
)
1632 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1633 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1635 if (!(keyword
->op
->type
& keywords
))
1637 token
= keyword
->op
->declarator(token
->next
, ctx
);
1638 keywords
&= KW_ATTRIBUTE
;
1643 static int is_nested(struct token
*token
, struct token
**p
,
1644 int prefer_abstract
)
1647 * This can be either a parameter list or a grouping.
1648 * For the direct (non-abstract) case, we know if must be
1649 * a parameter list if we already saw the identifier.
1650 * For the abstract case, we know if must be a parameter
1651 * list if it is empty or starts with a type.
1653 struct token
*next
= token
->next
;
1655 *p
= next
= skip_attributes(next
);
1657 if (token_type(next
) == TOKEN_IDENT
) {
1658 if (lookup_type(next
))
1659 return !prefer_abstract
;
1663 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1670 Empty
, K_R
, Proto
, Bad_Func
,
1673 static enum kind
which_func(struct token
*token
,
1675 int prefer_abstract
)
1677 struct token
*next
= token
->next
;
1679 if (token_type(next
) == TOKEN_IDENT
) {
1680 if (lookup_type(next
))
1682 /* identifier list not in definition; complain */
1683 if (prefer_abstract
)
1685 "identifier list not in definition");
1689 if (token_type(next
) != TOKEN_SPECIAL
)
1692 if (next
->special
== ')') {
1693 /* don't complain about those */
1694 if (!n
|| match_op(next
->next
, ';'))
1697 "non-ANSI function declaration of function '%s'",
1702 if (next
->special
== SPECIAL_ELLIPSIS
) {
1704 "variadic functions must have one named argument");
1711 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1713 struct ctype
*ctype
= &ctx
->ctype
;
1715 struct ident
**p
= ctx
->ident
;
1717 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1718 *ctx
->ident
= token
->ident
;
1719 token
= token
->next
;
1720 } else if (match_op(token
, '(') &&
1721 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1722 struct symbol
*base_type
= ctype
->base_type
;
1723 if (token
->next
!= next
)
1724 next
= handle_attributes(token
->next
, ctx
,
1726 token
= declarator(next
, ctx
);
1727 token
= expect(token
, ')', "in nested declarator");
1728 while (ctype
->base_type
!= base_type
)
1729 ctype
= &ctype
->base_type
->ctype
;
1733 if (match_op(token
, '(')) {
1734 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1736 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1737 token
= token
->next
;
1739 token
= identifier_list(token
, fn
);
1740 else if (kind
== Proto
)
1741 token
= parameter_type_list(token
, fn
);
1742 token
= expect(token
, ')', "in function declarator");
1743 fn
->endpos
= token
->pos
;
1747 while (match_op(token
, '[')) {
1748 struct symbol
*array
;
1749 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1750 token
= abstract_array_declarator(token
->next
, array
);
1751 token
= expect(token
, ']', "in abstract_array_declarator");
1752 array
->endpos
= token
->pos
;
1753 ctype
= &array
->ctype
;
1758 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1760 while (match_op(token
,'*')) {
1761 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1762 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1763 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1764 ptr
->ctype
.as
= ctx
->ctype
.as
;
1765 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1766 ctx
->ctype
.modifiers
= 0;
1767 ctx
->ctype
.base_type
= ptr
;
1769 ctx
->ctype
.contexts
= NULL
;
1770 ctx
->ctype
.alignment
= 0;
1772 token
= handle_qualifiers(token
->next
, ctx
);
1773 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1778 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1780 token
= pointer(token
, ctx
);
1781 return direct_declarator(token
, ctx
);
1784 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1786 struct ctype
*ctype
= &ctx
->ctype
;
1787 struct expression
*expr
;
1788 struct symbol
*bitfield
;
1791 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1792 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1793 show_typename(ctype
->base_type
));
1794 // Parse this to recover gracefully.
1795 return conditional_expression(token
->next
, &expr
);
1798 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1799 token
= conditional_expression(token
->next
, &expr
);
1800 width
= const_expression_value(expr
);
1801 bitfield
->bit_size
= width
;
1803 if (width
< 0 || width
> INT_MAX
) {
1804 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1806 } else if (*ctx
->ident
&& width
== 0) {
1807 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1808 show_ident(*ctx
->ident
));
1810 } else if (*ctx
->ident
) {
1811 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1812 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1813 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1814 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1815 // Valid values are either {-1;0} or {0}, depending on integer
1816 // representation. The latter makes for very efficient code...
1817 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1819 if (Wdefault_bitfield_sign
&&
1820 bitfield_type
->type
!= SYM_ENUM
&&
1821 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1823 // The sign of bitfields is unspecified by default.
1824 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1827 bitfield
->bit_size
= width
;
1828 bitfield
->endpos
= token
->pos
;
1832 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1834 struct decl_state ctx
= {.prefer_abstract
= 0};
1838 token
= declaration_specifiers(token
, &ctx
);
1839 mod
= storage_modifiers(&ctx
);
1842 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1843 ctx
.ident
= &decl
->ident
;
1845 token
= declarator(token
, &ctx
);
1846 if (match_op(token
, ':'))
1847 token
= handle_bitfield(token
, &ctx
);
1849 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1850 apply_modifiers(token
->pos
, &ctx
);
1852 decl
->ctype
= ctx
.ctype
;
1853 decl
->ctype
.modifiers
|= mod
;
1854 decl
->endpos
= token
->pos
;
1855 add_symbol(list
, decl
);
1856 if (!match_op(token
, ','))
1858 token
= token
->next
;
1864 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1866 while (!match_op(token
, '}')) {
1867 if (match_ident(token
, &_Static_assert_ident
)) {
1868 token
= parse_static_assert(token
, NULL
);
1871 if (!match_op(token
, ';'))
1872 token
= declaration_list(token
, list
);
1873 if (!match_op(token
, ';')) {
1874 sparse_error(token
->pos
, "expected ; at end of declaration");
1877 token
= token
->next
;
1882 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1884 struct decl_state ctx
= {.prefer_abstract
= 1};
1886 token
= declaration_specifiers(token
, &ctx
);
1887 ctx
.ident
= &sym
->ident
;
1888 token
= declarator(token
, &ctx
);
1889 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1890 apply_modifiers(token
->pos
, &ctx
);
1891 sym
->ctype
= ctx
.ctype
;
1892 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1893 sym
->endpos
= token
->pos
;
1894 sym
->forced_arg
= ctx
.storage_class
== SForced
;
1898 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1900 struct decl_state ctx
= {.prefer_abstract
= 1};
1902 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1904 token
= declaration_specifiers(token
, &ctx
);
1905 token
= declarator(token
, &ctx
);
1906 apply_modifiers(token
->pos
, &ctx
);
1907 sym
->ctype
= ctx
.ctype
;
1908 sym
->endpos
= token
->pos
;
1909 class = ctx
.storage_class
;
1912 if (class == SForced
) {
1918 warning(sym
->pos
, "storage class in typename (%s %s)",
1919 storage_class
[class], show_typename(sym
));
1923 static struct token
*parse_underscore_Pragma(struct token
*token
)
1928 if (!match_op(next
, '('))
1931 if (next
->pos
.type
!= TOKEN_STRING
)
1934 if (!match_op(next
, ')'))
1939 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1941 if (match_ident(token
, &_Pragma_ident
))
1942 return parse_underscore_Pragma(token
);
1944 token
= parse_expression(token
, tree
);
1945 return expect(token
, ';', "at end of statement");
1948 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1949 struct expression_list
**inout
)
1951 struct expression
*expr
;
1953 /* Allow empty operands */
1954 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1957 struct ident
*ident
= NULL
;
1958 if (match_op(token
->next
, '[') &&
1959 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1960 match_op(token
->next
->next
->next
, ']')) {
1961 ident
= token
->next
->next
->ident
;
1962 token
= token
->next
->next
->next
;
1964 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1965 token
= primary_expression(token
->next
, &expr
);
1966 add_expression(inout
, expr
);
1967 token
= parens_expression(token
, &expr
, "in asm parameter");
1968 add_expression(inout
, expr
);
1969 } while (match_op(token
, ','));
1973 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1974 struct expression_list
**clobbers
)
1976 struct expression
*expr
;
1979 token
= primary_expression(token
->next
, &expr
);
1981 add_expression(clobbers
, expr
);
1982 } while (match_op(token
, ','));
1986 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1987 struct symbol_list
**labels
)
1989 struct symbol
*label
;
1992 token
= token
->next
; /* skip ':' and ',' */
1993 if (token_type(token
) != TOKEN_IDENT
)
1995 label
= label_symbol(token
);
1996 add_symbol(labels
, label
);
1997 token
= token
->next
;
1998 } while (match_op(token
, ','));
2002 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2006 token
= token
->next
;
2007 stmt
->type
= STMT_ASM
;
2008 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
2009 token
= token
->next
;
2011 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
2013 token
= token
->next
;
2015 token
= expect(token
, '(', "after asm");
2016 token
= parse_expression(token
, &stmt
->asm_string
);
2017 if (match_op(token
, ':'))
2018 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2019 if (match_op(token
, ':'))
2020 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2021 if (match_op(token
, ':'))
2022 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2023 if (is_goto
&& match_op(token
, ':'))
2024 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2025 token
= expect(token
, ')', "after asm");
2026 return expect(token
, ';', "at end of asm-statement");
2029 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
2031 struct expression
*expr
;
2032 token
= expect(token
, '(', "after asm");
2033 token
= parse_expression(token
->next
, &expr
);
2034 token
= expect(token
, ')', "after asm");
2038 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2040 struct expression
*cond
= NULL
, *message
= NULL
;
2042 token
= expect(token
->next
, '(', "after _Static_assert");
2043 token
= constant_expression(token
, &cond
);
2045 sparse_error(token
->pos
, "Expected constant expression");
2046 token
= expect(token
, ',', "after conditional expression in _Static_assert");
2047 token
= parse_expression(token
, &message
);
2048 if (!message
|| message
->type
!= EXPR_STRING
) {
2049 struct position pos
;
2051 pos
= message
? message
->pos
: token
->pos
;
2052 sparse_error(pos
, "bad or missing string literal");
2055 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2057 token
= expect(token
, ';', "after _Static_assert()");
2059 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
)
2060 sparse_error(cond
->pos
, "static assertion failed: %s",
2061 show_string(message
->string
));
2065 /* Make a statement out of an expression */
2066 static struct statement
*make_statement(struct expression
*expr
)
2068 struct statement
*stmt
;
2072 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2073 stmt
->expression
= expr
;
2078 * All iterators have two symbols associated with them:
2079 * the "continue" and "break" symbols, which are targets
2080 * for continue and break statements respectively.
2082 * They are in a special name-space, but they follow
2083 * all the normal visibility rules, so nested iterators
2084 * automatically work right.
2086 static void start_iterator(struct statement
*stmt
)
2088 struct symbol
*cont
, *brk
;
2090 start_symbol_scope(stmt
->pos
);
2091 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2092 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2093 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2094 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2096 stmt
->type
= STMT_ITERATOR
;
2097 stmt
->iterator_break
= brk
;
2098 stmt
->iterator_continue
= cont
;
2099 fn_local_symbol(brk
);
2100 fn_local_symbol(cont
);
2103 static void end_iterator(struct statement
*stmt
)
2108 static struct statement
*start_function(struct symbol
*sym
)
2111 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2113 start_function_scope(sym
->pos
);
2114 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2115 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2116 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2117 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2118 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2120 fn_local_symbol(ret
);
2122 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2128 static void end_function(struct symbol
*sym
)
2131 end_function_scope();
2135 * A "switch()" statement, like an iterator, has a
2136 * the "break" symbol associated with it. It works
2137 * exactly like the iterator break - it's the target
2138 * for any break-statements in scope, and means that
2139 * "break" handling doesn't even need to know whether
2140 * it's breaking out of an iterator or a switch.
2142 * In addition, the "case" symbol is a marker for the
2143 * case/default statements to find the switch statement
2144 * that they are associated with.
2146 static void start_switch(struct statement
*stmt
)
2148 struct symbol
*brk
, *switch_case
;
2150 start_symbol_scope(stmt
->pos
);
2151 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2152 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2154 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2155 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2156 switch_case
->stmt
= stmt
;
2158 stmt
->type
= STMT_SWITCH
;
2159 stmt
->switch_break
= brk
;
2160 stmt
->switch_case
= switch_case
;
2162 fn_local_symbol(brk
);
2163 fn_local_symbol(switch_case
);
2166 static void end_switch(struct statement
*stmt
)
2168 if (!stmt
->switch_case
->symbol_list
)
2169 warning(stmt
->pos
, "switch with no cases");
2173 static void add_case_statement(struct statement
*stmt
)
2175 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2179 sparse_error(stmt
->pos
, "not in switch scope");
2180 stmt
->type
= STMT_NONE
;
2183 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2184 add_symbol(&target
->symbol_list
, sym
);
2186 stmt
->case_label
= sym
;
2187 fn_local_symbol(sym
);
2190 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2192 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2195 error_die(token
->pos
, "internal error: return without a function target");
2196 stmt
->type
= STMT_RETURN
;
2197 stmt
->ret_target
= target
;
2198 return expression_statement(token
->next
, &stmt
->ret_value
);
2201 static void validate_for_loop_decl(struct symbol
*sym
)
2203 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2205 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2206 const char *name
= show_ident(sym
->ident
);
2207 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2208 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2212 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2214 struct symbol_list
*syms
;
2215 struct expression
*e1
, *e2
, *e3
;
2216 struct statement
*iterator
;
2218 start_iterator(stmt
);
2219 token
= expect(token
->next
, '(', "after 'for'");
2223 /* C99 variable declaration? */
2224 if (lookup_type(token
)) {
2225 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2227 token
= parse_expression(token
, &e1
);
2228 token
= expect(token
, ';', "in 'for'");
2230 token
= parse_expression(token
, &e2
);
2231 token
= expect(token
, ';', "in 'for'");
2232 token
= parse_expression(token
, &e3
);
2233 token
= expect(token
, ')', "in 'for'");
2234 token
= statement(token
, &iterator
);
2236 stmt
->iterator_syms
= syms
;
2237 stmt
->iterator_pre_statement
= make_statement(e1
);
2238 stmt
->iterator_pre_condition
= e2
;
2239 stmt
->iterator_post_statement
= make_statement(e3
);
2240 stmt
->iterator_post_condition
= NULL
;
2241 stmt
->iterator_statement
= iterator
;
2247 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2249 struct expression
*expr
;
2250 struct statement
*iterator
;
2252 start_iterator(stmt
);
2253 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2254 token
= statement(token
, &iterator
);
2256 stmt
->iterator_pre_condition
= expr
;
2257 stmt
->iterator_post_condition
= NULL
;
2258 stmt
->iterator_statement
= iterator
;
2264 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2266 struct expression
*expr
;
2267 struct statement
*iterator
;
2269 start_iterator(stmt
);
2270 token
= statement(token
->next
, &iterator
);
2271 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2272 token
= token
->next
;
2274 sparse_error(token
->pos
, "expected 'while' after 'do'");
2275 token
= parens_expression(token
, &expr
, "after 'do-while'");
2277 stmt
->iterator_post_condition
= expr
;
2278 stmt
->iterator_statement
= iterator
;
2281 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2282 warning(iterator
->pos
, "do-while statement is not a compound statement");
2284 return expect(token
, ';', "after statement");
2287 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2289 stmt
->type
= STMT_IF
;
2290 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2291 token
= statement(token
, &stmt
->if_true
);
2292 if (token_type(token
) != TOKEN_IDENT
)
2294 if (token
->ident
!= &else_ident
)
2296 return statement(token
->next
, &stmt
->if_false
);
2299 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2301 stmt
->type
= STMT_CASE
;
2302 token
= expect(token
, ':', "after default/case");
2303 add_case_statement(stmt
);
2304 return statement(token
, &stmt
->case_statement
);
2307 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2309 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2310 if (match_op(token
, SPECIAL_ELLIPSIS
))
2311 token
= parse_expression(token
->next
, &stmt
->case_to
);
2312 return case_statement(token
, stmt
);
2315 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2317 return case_statement(token
->next
, stmt
);
2320 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2322 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2323 stmt
->type
= STMT_GOTO
;
2324 stmt
->goto_label
= target
;
2326 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2327 return expect(token
->next
, ';', "at end of statement");
2330 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2332 stmt
->type
= STMT_SWITCH
;
2334 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2335 token
= statement(token
, &stmt
->switch_statement
);
2340 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2342 stmt
->type
= STMT_GOTO
;
2343 token
= token
->next
;
2344 if (match_op(token
, '*')) {
2345 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2346 add_statement(&function_computed_goto_list
, stmt
);
2347 } else if (token_type(token
) == TOKEN_IDENT
) {
2348 stmt
->goto_label
= label_symbol(token
);
2349 token
= token
->next
;
2351 sparse_error(token
->pos
, "Expected identifier or goto expression");
2353 return expect(token
, ';', "at end of statement");
2356 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2358 stmt
->type
= STMT_CONTEXT
;
2359 token
= parse_expression(token
->next
, &stmt
->expression
);
2360 if (stmt
->expression
->type
== EXPR_PREOP
2361 && stmt
->expression
->op
== '('
2362 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2363 struct expression
*expr
;
2364 expr
= stmt
->expression
->unop
;
2365 stmt
->context
= expr
->left
;
2366 stmt
->expression
= expr
->right
;
2368 return expect(token
, ';', "at end of statement");
2371 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2373 stmt
->type
= STMT_RANGE
;
2374 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2375 token
= expect(token
, ',', "after range expression");
2376 token
= assignment_expression(token
, &stmt
->range_low
);
2377 token
= expect(token
, ',', "after low range");
2378 token
= assignment_expression(token
, &stmt
->range_high
);
2379 return expect(token
, ';', "after range statement");
2382 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2384 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2387 if (token_type(token
) == TOKEN_IDENT
) {
2388 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2389 if (s
&& s
->op
->statement
)
2390 return s
->op
->statement(token
, stmt
);
2392 if (match_op(token
->next
, ':')) {
2393 struct symbol
*s
= label_symbol(token
);
2394 stmt
->type
= STMT_LABEL
;
2395 stmt
->label_identifier
= s
;
2397 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(token
->ident
));
2399 token
= skip_attributes(token
->next
->next
);
2400 return statement(token
, &stmt
->label_statement
);
2404 if (match_op(token
, '{')) {
2405 stmt
->type
= STMT_COMPOUND
;
2406 start_symbol_scope(stmt
->pos
);
2407 token
= compound_statement(token
->next
, stmt
);
2410 return expect(token
, '}', "at end of compound statement");
2413 stmt
->type
= STMT_EXPRESSION
;
2414 return expression_statement(token
, &stmt
->expression
);
2417 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2418 static struct token
*label_statement(struct token
*token
)
2420 while (token_type(token
) == TOKEN_IDENT
) {
2421 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2422 /* it's block-scope, but we want label namespace */
2423 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2424 sym
->namespace = NS_LABEL
;
2425 fn_local_symbol(sym
);
2426 token
= token
->next
;
2427 if (!match_op(token
, ','))
2429 token
= token
->next
;
2431 return expect(token
, ';', "at end of label declaration");
2434 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2436 int seen_statement
= 0;
2437 while (token_type(token
) == TOKEN_IDENT
&&
2438 token
->ident
== &__label___ident
)
2439 token
= label_statement(token
->next
);
2441 struct statement
* stmt
;
2442 if (eof_token(token
))
2444 if (match_op(token
, '}'))
2446 if (match_ident(token
, &_Static_assert_ident
)) {
2447 token
= parse_static_assert(token
, NULL
);
2450 if (lookup_type(token
)) {
2451 if (seen_statement
) {
2452 warning(token
->pos
, "mixing declarations and code");
2455 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2456 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2458 seen_statement
= Wdeclarationafterstatement
;
2459 token
= statement(token
, &stmt
);
2461 add_statement(list
, stmt
);
2466 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2468 struct symbol_list
**list
= &fn
->arguments
;
2470 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2471 sym
->ident
= token
->ident
;
2472 token
= token
->next
;
2473 sym
->endpos
= token
->pos
;
2474 sym
->ctype
.base_type
= &incomplete_ctype
;
2475 add_symbol(list
, sym
);
2476 if (!match_op(token
, ',') ||
2477 token_type(token
->next
) != TOKEN_IDENT
||
2478 lookup_type(token
->next
))
2480 token
= token
->next
;
2485 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2487 struct symbol_list
**list
= &fn
->arguments
;
2492 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2494 token
= token
->next
;
2498 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2499 token
= parameter_declaration(token
, sym
);
2500 if (sym
->ctype
.base_type
== &void_ctype
) {
2501 /* Special case: (void) */
2502 if (!*list
&& !sym
->ident
)
2504 warning(token
->pos
, "void parameter");
2506 add_symbol(list
, sym
);
2507 if (!match_op(token
, ','))
2509 token
= token
->next
;
2514 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2516 token
= statement_list(token
, &stmt
->stmts
);
2520 static struct expression
*identifier_expression(struct token
*token
)
2522 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2523 expr
->expr_ident
= token
->ident
;
2527 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2529 int idx_from
, idx_to
;
2530 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2532 idx_from
= const_expression_value(from
);
2535 idx_to
= const_expression_value(to
);
2536 if (idx_to
< idx_from
|| idx_from
< 0)
2537 warning(from
->pos
, "nonsense array initializer index range");
2539 expr
->idx_from
= idx_from
;
2540 expr
->idx_to
= idx_to
;
2544 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2546 int expect_equal
= 0;
2547 struct token
*next
= token
->next
;
2548 struct expression
**tail
= ep
;
2553 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2554 struct expression
*expr
= identifier_expression(token
);
2555 if (Wold_initializer
)
2556 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2557 token
= initializer(&expr
->ident_expression
, next
->next
);
2558 if (expr
->ident_expression
)
2563 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2564 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2565 struct expression
*expr
= identifier_expression(next
);
2567 tail
= &expr
->ident_expression
;
2570 } else if (match_op(token
, '[')) {
2571 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2572 token
= constant_expression(token
->next
, &from
);
2574 sparse_error(token
->pos
, "Expected constant expression");
2577 if (match_op(token
, SPECIAL_ELLIPSIS
))
2578 token
= constant_expression(token
->next
, &to
);
2579 expr
= index_expression(from
, to
);
2581 tail
= &expr
->idx_expression
;
2582 token
= expect(token
, ']', "at end of initializer index");
2589 if (nested
&& !expect_equal
) {
2590 if (!match_op(token
, '='))
2591 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2596 token
= expect(token
, '=', "at end of initializer index");
2598 token
= initializer(tail
, token
);
2604 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2606 struct expression
*expr
;
2609 token
= single_initializer(&expr
, token
);
2612 add_expression(list
, expr
);
2613 if (!match_op(token
, ','))
2615 token
= token
->next
;
2620 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2622 if (match_op(token
, '{')) {
2623 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2625 token
= initializer_list(&expr
->expr_list
, token
->next
);
2626 return expect(token
, '}', "at end of initializer");
2628 return assignment_expression(token
, tree
);
2631 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2634 sparse_error(sym
->pos
, "no identifier for function argument");
2637 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2640 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2641 struct symbol_list
**list
)
2643 struct symbol_list
**old_symbol_list
;
2644 struct symbol
*base_type
= decl
->ctype
.base_type
;
2645 struct statement
*stmt
, **p
;
2646 struct symbol
*prev
;
2649 old_symbol_list
= function_symbol_list
;
2650 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2651 function_symbol_list
= &decl
->inline_symbol_list
;
2652 p
= &base_type
->inline_stmt
;
2654 function_symbol_list
= &decl
->symbol_list
;
2655 p
= &base_type
->stmt
;
2657 function_computed_target_list
= NULL
;
2658 function_computed_goto_list
= NULL
;
2660 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2661 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2662 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2664 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2665 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2667 stmt
= start_function(decl
);
2670 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2671 declare_argument(arg
, base_type
);
2672 } END_FOR_EACH_PTR(arg
);
2674 token
= compound_statement(token
->next
, stmt
);
2677 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2678 add_symbol(list
, decl
);
2679 check_declaration(decl
);
2680 decl
->definition
= decl
;
2681 prev
= decl
->same_symbol
;
2682 if (prev
&& prev
->definition
) {
2683 warning(decl
->pos
, "multiple definitions for function '%s'",
2684 show_ident(decl
->ident
));
2685 info(prev
->definition
->pos
, " the previous one is here");
2688 rebind_scope(prev
, decl
->scope
);
2689 prev
->definition
= decl
;
2690 prev
= prev
->same_symbol
;
2693 function_symbol_list
= old_symbol_list
;
2694 if (function_computed_goto_list
) {
2695 if (!function_computed_target_list
)
2696 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2698 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2699 stmt
->target_list
= function_computed_target_list
;
2700 } END_FOR_EACH_PTR(stmt
);
2703 return expect(token
, '}', "at end of function");
2706 static void promote_k_r_types(struct symbol
*arg
)
2708 struct symbol
*base
= arg
->ctype
.base_type
;
2709 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2710 arg
->ctype
.base_type
= &int_ctype
;
2714 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2716 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2719 FOR_EACH_PTR(real_args
, arg
) {
2720 struct symbol
*type
;
2722 /* This is quadratic in the number of arguments. We _really_ don't care */
2723 FOR_EACH_PTR(argtypes
, type
) {
2724 if (type
->ident
== arg
->ident
)
2726 } END_FOR_EACH_PTR(type
);
2727 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2731 /* "char" and "short" promote to "int" */
2732 promote_k_r_types(type
);
2734 arg
->ctype
= type
->ctype
;
2735 } END_FOR_EACH_PTR(arg
);
2737 FOR_EACH_PTR(argtypes
, arg
) {
2739 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2740 } END_FOR_EACH_PTR(arg
);
2744 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2745 struct symbol_list
**list
)
2747 struct symbol_list
*args
= NULL
;
2749 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2751 token
= declaration_list(token
, &args
);
2752 if (!match_op(token
, ';')) {
2753 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2756 token
= token
->next
;
2757 } while (lookup_type(token
));
2759 apply_k_r_types(args
, decl
);
2761 if (!match_op(token
, '{')) {
2762 sparse_error(token
->pos
, "expected function body");
2765 return parse_function_body(token
, decl
, list
);
2768 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2770 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2771 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2772 struct statement
*stmt
;
2774 anon
->ctype
.base_type
= fn
;
2775 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2778 token
= parse_asm_statement(token
, stmt
);
2780 add_symbol(list
, anon
);
2784 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2785 validate_decl_t validate_decl
)
2787 struct ident
*ident
= NULL
;
2788 struct symbol
*decl
;
2789 struct decl_state ctx
= { .ident
= &ident
};
2791 struct symbol
*base_type
;
2795 if (match_ident(token
, &_Pragma_ident
))
2796 return parse_underscore_Pragma(token
);
2798 /* Top-level inline asm or static assertion? */
2799 if (token_type(token
) == TOKEN_IDENT
) {
2800 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2801 if (s
&& s
->op
->toplevel
)
2802 return s
->op
->toplevel(token
, list
);
2805 /* Parse declaration-specifiers, if any */
2806 token
= declaration_specifiers(token
, &ctx
);
2807 mod
= storage_modifiers(&ctx
);
2808 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2809 /* Just a type declaration? */
2810 if (match_op(token
, ';')) {
2811 apply_modifiers(token
->pos
, &ctx
);
2816 token
= declarator(token
, &ctx
);
2817 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2818 apply_modifiers(token
->pos
, &ctx
);
2820 decl
->ctype
= ctx
.ctype
;
2821 decl
->ctype
.modifiers
|= mod
;
2822 decl
->endpos
= token
->pos
;
2824 /* Just a type declaration? */
2826 warning(token
->pos
, "missing identifier in declaration");
2827 return expect(token
, ';', "at the end of type declaration");
2830 /* type define declaration? */
2831 is_typedef
= ctx
.storage_class
== STypedef
;
2833 /* Typedefs don't have meaningful storage */
2835 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2837 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2839 base_type
= decl
->ctype
.base_type
;
2842 if (base_type
&& !base_type
->ident
) {
2843 switch (base_type
->type
) {
2848 base_type
->ident
= ident
;
2854 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2855 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
2856 warning(decl
->pos
, "'%s()' has implicit return type",
2857 show_ident(decl
->ident
));
2858 base_type
->ctype
.base_type
= &int_ctype
;
2860 /* K&R argument declaration? */
2861 if (lookup_type(token
))
2862 return parse_k_r_arguments(token
, decl
, list
);
2863 if (match_op(token
, '{'))
2864 return parse_function_body(token
, decl
, list
);
2866 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2867 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2868 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2869 sparse_error(token
->pos
, "void declaration");
2871 if (base_type
== &incomplete_ctype
) {
2872 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
2873 decl
->ctype
.base_type
= &int_ctype
;;
2877 if (!is_typedef
&& match_op(token
, '=')) {
2878 token
= initializer(&decl
->initializer
, token
->next
);
2882 validate_decl(decl
);
2884 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
2885 warning(decl
->pos
, "symbol with external linkage has initializer");
2886 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2889 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2890 add_symbol(list
, decl
);
2891 fn_local_symbol(decl
);
2894 check_declaration(decl
);
2895 if (decl
->same_symbol
) {
2896 decl
->definition
= decl
->same_symbol
->definition
;
2897 decl
->op
= decl
->same_symbol
->op
;
2900 if (!match_op(token
, ','))
2903 token
= token
->next
;
2905 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2907 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2908 token
= declarator(token
, &ctx
);
2909 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2910 apply_modifiers(token
->pos
, &ctx
);
2911 decl
->ctype
= ctx
.ctype
;
2912 decl
->ctype
.modifiers
|= mod
;
2913 decl
->endpos
= token
->pos
;
2915 sparse_error(token
->pos
, "expected identifier name in type definition");
2920 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2922 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2924 /* Function declarations are automatically extern unless specifically static */
2925 base_type
= decl
->ctype
.base_type
;
2926 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2927 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2928 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2931 return expect(token
, ';', "at end of declaration");