2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
8 * Copyright (C) 2004 Christopher Li
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 #include "expression.h"
47 static struct symbol_list
**function_symbol_list
;
48 struct symbol_list
*function_computed_target_list
;
49 struct statement_list
*function_computed_goto_list
;
51 static struct token
*statement(struct token
*token
, struct statement
**tree
);
52 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
);
54 typedef struct token
*declarator_t(struct token
*, struct decl_state
*);
56 struct_specifier
, union_specifier
, enum_specifier
,
57 attribute_specifier
, typeof_specifier
, parse_asm_declarator
,
58 typedef_specifier
, inline_specifier
, auto_specifier
,
59 register_specifier
, static_specifier
, extern_specifier
,
60 thread_specifier
, const_qualifier
, volatile_qualifier
;
62 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
63 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
64 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
65 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
66 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
67 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
68 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
69 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
70 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
71 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
72 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
73 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
74 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
75 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
77 typedef struct token
*attr_t(struct token
*, struct symbol
*,
81 attribute_packed
, attribute_aligned
, attribute_modifier
,
82 attribute_address_space
, attribute_context
,
83 attribute_designated_init
,
84 attribute_transparent_union
, ignore_attribute
,
85 attribute_mode
, attribute_force
;
87 typedef struct symbol
*to_mode_t(struct symbol
*);
90 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
, to_word_mode
;
105 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
109 CInt
= 0, CSInt
, CUInt
, CReal
, CChar
, CSChar
, CUChar
,
113 SNone
= 0, STypedef
, SAuto
, SRegister
, SExtern
, SStatic
, SForced
, SMax
,
116 static struct symbol_op typedef_op
= {
118 .declarator
= typedef_specifier
,
121 static struct symbol_op inline_op
= {
123 .declarator
= inline_specifier
,
126 static struct symbol_op auto_op
= {
128 .declarator
= auto_specifier
,
131 static struct symbol_op register_op
= {
133 .declarator
= register_specifier
,
136 static struct symbol_op static_op
= {
138 .declarator
= static_specifier
,
141 static struct symbol_op extern_op
= {
143 .declarator
= extern_specifier
,
146 static struct symbol_op thread_op
= {
148 .declarator
= thread_specifier
,
151 static struct symbol_op const_op
= {
152 .type
= KW_QUALIFIER
,
153 .declarator
= const_qualifier
,
156 static struct symbol_op volatile_op
= {
157 .type
= KW_QUALIFIER
,
158 .declarator
= volatile_qualifier
,
161 static struct symbol_op restrict_op
= {
162 .type
= KW_QUALIFIER
,
165 static struct symbol_op typeof_op
= {
166 .type
= KW_SPECIFIER
,
167 .declarator
= typeof_specifier
,
172 static struct symbol_op attribute_op
= {
173 .type
= KW_ATTRIBUTE
,
174 .declarator
= attribute_specifier
,
177 static struct symbol_op struct_op
= {
178 .type
= KW_SPECIFIER
,
179 .declarator
= struct_specifier
,
184 static struct symbol_op union_op
= {
185 .type
= KW_SPECIFIER
,
186 .declarator
= union_specifier
,
191 static struct symbol_op enum_op
= {
192 .type
= KW_SPECIFIER
,
193 .declarator
= enum_specifier
,
198 static struct symbol_op spec_op
= {
199 .type
= KW_SPECIFIER
| KW_EXACT
,
204 static struct symbol_op char_op
= {
205 .type
= KW_SPECIFIER
,
206 .test
= Set_T
|Set_Long
|Set_Short
,
207 .set
= Set_T
|Set_Char
,
211 static struct symbol_op int_op
= {
212 .type
= KW_SPECIFIER
,
214 .set
= Set_T
|Set_Int
,
217 static struct symbol_op double_op
= {
218 .type
= KW_SPECIFIER
,
219 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
220 .set
= Set_T
|Set_Double
,
224 static struct symbol_op float_op
= {
225 .type
= KW_SPECIFIER
| KW_SHORT
,
226 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
227 .set
= Set_T
|Set_Float
,
231 static struct symbol_op short_op
= {
232 .type
= KW_SPECIFIER
| KW_SHORT
,
233 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
237 static struct symbol_op signed_op
= {
238 .type
= KW_SPECIFIER
,
239 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
244 static struct symbol_op unsigned_op
= {
245 .type
= KW_SPECIFIER
,
246 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
251 static struct symbol_op long_op
= {
252 .type
= KW_SPECIFIER
| KW_LONG
,
253 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
257 static struct symbol_op int128_op
= {
258 .type
= KW_SPECIFIER
| KW_LONG
,
259 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
260 .set
= Set_T
|Set_Int128
,
263 static struct symbol_op if_op
= {
264 .statement
= parse_if_statement
,
267 static struct symbol_op return_op
= {
268 .statement
= parse_return_statement
,
271 static struct symbol_op loop_iter_op
= {
272 .statement
= parse_loop_iterator
,
275 static struct symbol_op default_op
= {
276 .statement
= parse_default_statement
,
279 static struct symbol_op case_op
= {
280 .statement
= parse_case_statement
,
283 static struct symbol_op switch_op
= {
284 .statement
= parse_switch_statement
,
287 static struct symbol_op for_op
= {
288 .statement
= parse_for_statement
,
291 static struct symbol_op while_op
= {
292 .statement
= parse_while_statement
,
295 static struct symbol_op do_op
= {
296 .statement
= parse_do_statement
,
299 static struct symbol_op goto_op
= {
300 .statement
= parse_goto_statement
,
303 static struct symbol_op __context___op
= {
304 .statement
= parse_context_statement
,
307 static struct symbol_op range_op
= {
308 .statement
= parse_range_statement
,
311 static struct symbol_op asm_op
= {
313 .declarator
= parse_asm_declarator
,
314 .statement
= parse_asm_statement
,
315 .toplevel
= toplevel_asm_declaration
,
318 static struct symbol_op packed_op
= {
319 .attribute
= attribute_packed
,
322 static struct symbol_op aligned_op
= {
323 .attribute
= attribute_aligned
,
326 static struct symbol_op attr_mod_op
= {
327 .attribute
= attribute_modifier
,
330 static struct symbol_op attr_force_op
= {
331 .attribute
= attribute_force
,
334 static struct symbol_op address_space_op
= {
335 .attribute
= attribute_address_space
,
338 static struct symbol_op mode_op
= {
339 .attribute
= attribute_mode
,
342 static struct symbol_op context_op
= {
343 .attribute
= attribute_context
,
346 static struct symbol_op designated_init_op
= {
347 .attribute
= attribute_designated_init
,
350 static struct symbol_op transparent_union_op
= {
351 .attribute
= attribute_transparent_union
,
354 static struct symbol_op ignore_attr_op
= {
355 .attribute
= ignore_attribute
,
358 static struct symbol_op mode_QI_op
= {
360 .to_mode
= to_QI_mode
363 static struct symbol_op mode_HI_op
= {
365 .to_mode
= to_HI_mode
368 static struct symbol_op mode_SI_op
= {
370 .to_mode
= to_SI_mode
373 static struct symbol_op mode_DI_op
= {
375 .to_mode
= to_DI_mode
378 static struct symbol_op mode_TI_op
= {
380 .to_mode
= to_TI_mode
383 static struct symbol_op mode_word_op
= {
385 .to_mode
= to_word_mode
388 static struct init_keyword
{
391 unsigned long modifiers
;
392 struct symbol_op
*op
;
394 } keyword_table
[] = {
395 /* Type qualifiers */
396 { "const", NS_TYPEDEF
, .op
= &const_op
},
397 { "__const", NS_TYPEDEF
, .op
= &const_op
},
398 { "__const__", NS_TYPEDEF
, .op
= &const_op
},
399 { "volatile", NS_TYPEDEF
, .op
= &volatile_op
},
400 { "__volatile", NS_TYPEDEF
, .op
= &volatile_op
},
401 { "__volatile__", NS_TYPEDEF
, .op
= &volatile_op
},
404 { "typedef", NS_TYPEDEF
, .op
= &typedef_op
},
406 /* Type specifiers */
407 { "void", NS_TYPEDEF
, .type
= &void_ctype
, .op
= &spec_op
},
408 { "char", NS_TYPEDEF
, .op
= &char_op
},
409 { "short", NS_TYPEDEF
, .op
= &short_op
},
410 { "int", NS_TYPEDEF
, .op
= &int_op
},
411 { "long", NS_TYPEDEF
, .op
= &long_op
},
412 { "float", NS_TYPEDEF
, .op
= &float_op
},
413 { "double", NS_TYPEDEF
, .op
= &double_op
},
414 { "signed", NS_TYPEDEF
, .op
= &signed_op
},
415 { "__signed", NS_TYPEDEF
, .op
= &signed_op
},
416 { "__signed__", NS_TYPEDEF
, .op
= &signed_op
},
417 { "unsigned", NS_TYPEDEF
, .op
= &unsigned_op
},
418 { "__int128", NS_TYPEDEF
, .op
= &int128_op
},
419 { "_Bool", NS_TYPEDEF
, .type
= &bool_ctype
, .op
= &spec_op
},
421 /* Predeclared types */
422 { "__builtin_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
423 { "__builtin_ms_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
424 { "__int128_t", NS_TYPEDEF
, .type
= &lllong_ctype
, .op
= &spec_op
},
425 { "__uint128_t",NS_TYPEDEF
, .type
= &ulllong_ctype
, .op
= &spec_op
},
428 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
429 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
430 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
432 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
433 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
435 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
436 { "union", NS_TYPEDEF
, .op
= &union_op
},
437 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
439 { "inline", NS_TYPEDEF
, .op
= &inline_op
},
440 { "__inline", NS_TYPEDEF
, .op
= &inline_op
},
441 { "__inline__", NS_TYPEDEF
, .op
= &inline_op
},
443 /* Ignored for now.. */
444 { "restrict", NS_TYPEDEF
, .op
= &restrict_op
},
445 { "__restrict", NS_TYPEDEF
, .op
= &restrict_op
},
446 { "__restrict__", NS_TYPEDEF
, .op
= &restrict_op
},
449 { "auto", NS_TYPEDEF
, .op
= &auto_op
},
450 { "register", NS_TYPEDEF
, .op
= ®ister_op
},
451 { "static", NS_TYPEDEF
, .op
= &static_op
},
452 { "extern", NS_TYPEDEF
, .op
= &extern_op
},
453 { "__thread", NS_TYPEDEF
, .op
= &thread_op
},
456 { "if", NS_KEYWORD
, .op
= &if_op
},
457 { "return", NS_KEYWORD
, .op
= &return_op
},
458 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
459 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
460 { "default", NS_KEYWORD
, .op
= &default_op
},
461 { "case", NS_KEYWORD
, .op
= &case_op
},
462 { "switch", NS_KEYWORD
, .op
= &switch_op
},
463 { "for", NS_KEYWORD
, .op
= &for_op
},
464 { "while", NS_KEYWORD
, .op
= &while_op
},
465 { "do", NS_KEYWORD
, .op
= &do_op
},
466 { "goto", NS_KEYWORD
, .op
= &goto_op
},
467 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
468 { "__range__", NS_KEYWORD
, .op
= &range_op
},
469 { "asm", NS_KEYWORD
, .op
= &asm_op
},
470 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
471 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
474 { "packed", NS_KEYWORD
, .op
= &packed_op
},
475 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
476 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
477 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
478 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
479 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
480 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
481 { "force", NS_KEYWORD
, .op
= &attr_force_op
},
482 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
483 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
484 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
485 { "mode", NS_KEYWORD
, .op
= &mode_op
},
486 { "context", NS_KEYWORD
, .op
= &context_op
},
487 { "designated_init", NS_KEYWORD
, .op
= &designated_init_op
},
488 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
489 { "noreturn", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
490 { "__noreturn__", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
491 { "pure", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
492 {"__pure__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
493 {"const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
494 {"__const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
495 {"__const__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
497 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
498 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
499 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
500 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
501 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
502 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
503 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
504 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
505 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
506 { "TI", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
507 { "__TI__", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
508 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
509 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
512 static const char *ignored_attributes
[] = {
524 "__assume_aligned__",
543 "externally_visible",
544 "__externally_visible__",
574 "__ms_hook_prologue__",
577 "no_instrument_function",
578 "__no_instrument_function__",
601 "__syscall_linkage__",
612 "warn_unused_result",
613 "__warn_unused_result__",
618 "no_sanitize_address",
619 "__no_sanitize_address__",
623 void init_parser(int stream
)
626 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
627 struct init_keyword
*ptr
= keyword_table
+ i
;
628 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
629 sym
->ident
->keyword
= 1;
630 if (ptr
->ns
== NS_TYPEDEF
)
631 sym
->ident
->reserved
= 1;
632 sym
->ctype
.modifiers
= ptr
->modifiers
;
633 sym
->ctype
.base_type
= ptr
->type
;
637 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
638 const char * name
= ignored_attributes
[i
];
639 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
641 sym
->ident
->keyword
= 1;
642 sym
->op
= &ignore_attr_op
;
647 // Add a symbol to the list of function-local symbols
648 static void fn_local_symbol(struct symbol
*sym
)
650 if (function_symbol_list
)
651 add_symbol(function_symbol_list
, sym
);
654 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
659 if (token_type(token
) != TOKEN_IDENT
)
662 va_start(args
, token
);
664 next
= va_arg(args
, struct ident
*);
665 } while (next
&& token
->ident
!= next
);
668 return next
&& token
->ident
== next
;
672 struct statement
*alloc_statement(struct position pos
, int type
)
674 struct statement
*stmt
= __alloc_statement(0);
680 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
682 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
684 struct symbol
*ctype
;
687 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
689 sparse_error(pos
, "don't know how to apply mode to %s",
690 show_typename(ctx
->ctype
.base_type
));
692 ctx
->ctype
.base_type
= ctype
;
696 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
698 struct symbol
*sym
= alloc_symbol(pos
, type
);
700 sym
->ctype
.base_type
= ctype
->base_type
;
701 sym
->ctype
.modifiers
= ctype
->modifiers
;
703 ctype
->base_type
= sym
;
704 ctype
->modifiers
= 0;
709 * NOTE! NS_LABEL is not just a different namespace,
710 * it also ends up using function scope instead of the
711 * regular symbol scope.
713 struct symbol
*label_symbol(struct token
*token
)
715 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
717 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
718 bind_symbol(sym
, token
->ident
, NS_LABEL
);
719 fn_local_symbol(sym
);
724 static struct token
*struct_union_enum_specifier(enum type type
,
725 struct token
*token
, struct decl_state
*ctx
,
726 struct token
*(*parse
)(struct token
*, struct symbol
*))
729 struct position
*repos
;
731 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
732 if (token_type(token
) == TOKEN_IDENT
) {
733 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
735 (is_outer_scope(sym
->scope
) &&
736 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
737 // Either a new symbol, or else an out-of-scope
738 // symbol being redefined.
739 sym
= alloc_symbol(token
->pos
, type
);
740 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
742 if (sym
->type
!= type
)
743 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
744 ctx
->ctype
.base_type
= sym
;
747 if (match_op(token
, '{')) {
748 // The following test is actually wrong for empty
749 // structs, but (1) they are not C99, (2) gcc does
750 // the same thing, and (3) it's easier.
751 if (sym
->symbol_list
)
752 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
754 token
= parse(token
->next
, sym
);
755 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
757 // Mark the structure as needing re-examination
759 sym
->endpos
= token
->pos
;
764 // private struct/union/enum type
765 if (!match_op(token
, '{')) {
766 sparse_error(token
->pos
, "expected declaration");
767 ctx
->ctype
.base_type
= &bad_ctype
;
771 sym
= alloc_symbol(token
->pos
, type
);
772 token
= parse(token
->next
, sym
);
773 ctx
->ctype
.base_type
= sym
;
774 token
= expect(token
, '}', "at end of specifier");
775 sym
->endpos
= token
->pos
;
780 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
782 struct symbol
*field
, *last
= NULL
;
784 res
= struct_declaration_list(token
, &sym
->symbol_list
);
785 FOR_EACH_PTR(sym
->symbol_list
, field
) {
787 struct symbol
*base
= field
->ctype
.base_type
;
788 if (base
&& base
->type
== SYM_BITFIELD
)
792 last
->next_subobject
= field
;
794 } END_FOR_EACH_PTR(field
);
798 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
800 return struct_declaration_list(token
, &sym
->symbol_list
);
803 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
805 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
808 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
810 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
816 unsigned long long y
;
819 static void upper_boundary(Num
*n
, Num
*v
)
831 static void lower_boundary(Num
*n
, Num
*v
)
843 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
845 int shift
= type
->bit_size
;
846 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
850 if (upper
->x
== 0 && upper
->y
>> shift
)
852 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
857 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
859 if (s1
->bit_size
< s2
->bit_size
) {
861 } else if (s1
->bit_size
== s2
->bit_size
) {
862 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
865 if (s1
->bit_size
< bits_in_int
)
870 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
874 FOR_EACH_PTR(list
, sym
) {
875 struct expression
*expr
= sym
->initializer
;
876 struct symbol
*ctype
;
877 if (expr
->type
!= EXPR_VALUE
)
880 if (ctype
->bit_size
== base_type
->bit_size
)
882 cast_value(expr
, base_type
, expr
, ctype
);
883 } END_FOR_EACH_PTR(sym
);
886 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
888 unsigned long long lastval
= 0;
889 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
890 Num upper
= {-1, 0}, lower
= {1, 0};
892 parent
->examined
= 1;
893 parent
->ctype
.base_type
= &int_ctype
;
894 while (token_type(token
) == TOKEN_IDENT
) {
895 struct expression
*expr
= NULL
;
896 struct token
*next
= token
->next
;
899 if (match_op(next
, '=')) {
900 next
= constant_expression(next
->next
, &expr
);
901 lastval
= get_expression_value(expr
);
903 if (expr
&& expr
->ctype
)
907 } else if (is_int_type(ctype
)) {
910 error_die(token
->pos
, "can't increment the last enum member");
914 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
915 expr
->value
= lastval
;
919 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
920 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
921 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
922 sym
->initializer
= expr
;
923 sym
->enum_member
= 1;
924 sym
->ctype
.base_type
= parent
;
925 add_ptr_list(&parent
->symbol_list
, sym
);
927 if (base_type
!= &bad_ctype
) {
928 if (ctype
->type
== SYM_NODE
)
929 ctype
= ctype
->ctype
.base_type
;
930 if (ctype
->type
== SYM_ENUM
) {
934 ctype
= ctype
->ctype
.base_type
;
938 * - if all enums are of the same type, then
939 * the base_type is that type (two first
941 * - if enums are of different types, they
942 * all have to be integer types, and the
943 * base type is at least "int_ctype".
944 * - otherwise the base_type is "bad_ctype".
948 } else if (ctype
== base_type
) {
950 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
951 base_type
= bigger_enum_type(base_type
, ctype
);
953 base_type
= &bad_ctype
;
954 parent
->ctype
.base_type
= base_type
;
956 if (is_int_type(base_type
)) {
957 Num v
= {.y
= lastval
};
958 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
960 else if ((long long)lastval
>= 0)
964 upper_boundary(&upper
, &v
);
965 lower_boundary(&lower
, &v
);
969 sym
->endpos
= token
->pos
;
971 if (!match_op(token
, ','))
976 sparse_error(token
->pos
, "bad enum definition");
977 base_type
= &bad_ctype
;
979 else if (!is_int_type(base_type
))
980 base_type
= base_type
;
981 else if (type_is_ok(base_type
, &upper
, &lower
))
982 base_type
= base_type
;
983 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
984 base_type
= &int_ctype
;
985 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
986 base_type
= &uint_ctype
;
987 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
988 base_type
= &long_ctype
;
989 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
990 base_type
= &ulong_ctype
;
991 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
992 base_type
= &llong_ctype
;
993 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
994 base_type
= &ullong_ctype
;
996 base_type
= &bad_ctype
;
997 parent
->ctype
.base_type
= base_type
;
998 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
999 parent
->examined
= 0;
1001 cast_enum_list(parent
->symbol_list
, base_type
);
1006 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
1008 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1009 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1011 if (!ctype
->base_type
)
1012 ctype
->base_type
= &incomplete_ctype
;
1017 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
1019 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
1023 if (!match_op(token
, '(')) {
1024 sparse_error(token
->pos
, "expected '(' after typeof");
1027 if (lookup_type(token
->next
)) {
1028 token
= typename(token
->next
, &sym
, NULL
);
1029 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1030 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
1032 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1033 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1035 typeof_sym
->endpos
= token
->pos
;
1036 if (!typeof_sym
->initializer
) {
1037 sparse_error(token
->pos
, "expected expression after the '(' token");
1038 typeof_sym
= &bad_ctype
;
1040 ctx
->ctype
.base_type
= typeof_sym
;
1042 return expect(token
, ')', "after typeof");
1045 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1047 struct expression
*expr
= NULL
;
1048 if (match_op(token
, '('))
1049 token
= parens_expression(token
, &expr
, "in attribute");
1053 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1055 if (!ctx
->ctype
.alignment
)
1056 ctx
->ctype
.alignment
= 1;
1060 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1062 int alignment
= max_alignment
;
1063 struct expression
*expr
= NULL
;
1065 if (match_op(token
, '(')) {
1066 token
= parens_expression(token
, &expr
, "in attribute");
1068 alignment
= const_expression_value(expr
);
1070 if (alignment
& (alignment
-1)) {
1071 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1073 } else if (alignment
> ctx
->ctype
.alignment
)
1074 ctx
->ctype
.alignment
= alignment
;
1078 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1080 if (ctx
->modifiers
& qual
)
1081 warning(*pos
, "duplicate %s", modifier_string(qual
));
1082 ctx
->modifiers
|= qual
;
1085 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1087 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1091 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1093 struct expression
*expr
= NULL
;
1095 token
= expect(token
, '(', "after address_space attribute");
1096 token
= conditional_expression(token
, &expr
);
1098 as
= const_expression_value(expr
);
1099 if (Waddress_space
&& as
)
1102 token
= expect(token
, ')', "after address_space attribute");
1106 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1108 if (ctype
->ctype
.base_type
!= &int_type
)
1110 if (ctype
== &char_ctype
)
1112 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1116 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1118 if (ctype
->ctype
.base_type
!= &int_type
)
1120 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1124 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1126 if (ctype
->ctype
.base_type
!= &int_type
)
1128 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1132 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1134 if (ctype
->ctype
.base_type
!= &int_type
)
1136 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1140 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1142 if (ctype
->ctype
.base_type
!= &int_type
)
1144 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1148 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1150 if (ctype
->ctype
.base_type
!= &int_type
)
1152 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1156 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1158 token
= expect(token
, '(', "after mode attribute");
1159 if (token_type(token
) == TOKEN_IDENT
) {
1160 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1161 if (mode
&& mode
->op
->type
== KW_MODE
)
1162 ctx
->mode
= mode
->op
;
1164 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1165 token
= token
->next
;
1167 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1168 token
= expect(token
, ')', "after mode attribute");
1172 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1174 struct context
*context
= alloc_context();
1175 struct expression
*args
[3];
1178 token
= expect(token
, '(', "after context attribute");
1179 while (!match_op(token
, ')')) {
1180 struct expression
*expr
= NULL
;
1181 token
= conditional_expression(token
, &expr
);
1185 args
[argc
++] = expr
;
1186 if (!match_op(token
, ','))
1188 token
= token
->next
;
1193 sparse_error(token
->pos
, "expected context input/output values");
1196 context
->in
= get_expression_value(args
[0]);
1199 context
->in
= get_expression_value(args
[0]);
1200 context
->out
= get_expression_value(args
[1]);
1203 context
->context
= args
[0];
1204 context
->in
= get_expression_value(args
[1]);
1205 context
->out
= get_expression_value(args
[2]);
1210 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1212 token
= expect(token
, ')', "after context attribute");
1216 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1218 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1219 ctx
->ctype
.base_type
->designated_init
= 1;
1221 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1225 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1227 if (Wtransparent_union
)
1228 warning(token
->pos
, "attribute __transparent_union__");
1230 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1231 ctx
->ctype
.base_type
->transparent_union
= 1;
1233 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1237 static struct token
*recover_unknown_attribute(struct token
*token
)
1239 struct expression
*expr
= NULL
;
1241 if (Wunknown_attribute
)
1242 warning(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1243 token
= token
->next
;
1244 if (match_op(token
, '('))
1245 token
= parens_expression(token
, &expr
, "in attribute");
1249 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1251 token
= expect(token
, '(', "after attribute");
1252 token
= expect(token
, '(', "after attribute");
1255 struct ident
*attribute_name
;
1256 struct symbol
*attr
;
1258 if (eof_token(token
))
1260 if (match_op(token
, ';'))
1262 if (token_type(token
) != TOKEN_IDENT
)
1264 attribute_name
= token
->ident
;
1265 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1266 if (attr
&& attr
->op
->attribute
)
1267 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1269 token
= recover_unknown_attribute(token
);
1271 if (!match_op(token
, ','))
1273 token
= token
->next
;
1276 token
= expect(token
, ')', "after attribute");
1277 token
= expect(token
, ')', "after attribute");
1281 static const char *storage_class
[] =
1283 [STypedef
] = "typedef",
1285 [SExtern
] = "extern",
1286 [SStatic
] = "static",
1287 [SRegister
] = "register",
1288 [SForced
] = "[force]"
1291 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1293 static unsigned long mod
[SMax
] =
1296 [SExtern
] = MOD_EXTERN
,
1297 [SStatic
] = MOD_STATIC
,
1298 [SRegister
] = MOD_REGISTER
1300 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1301 | (ctx
->is_tls
? MOD_TLS
: 0);
1304 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1306 /* __thread can be used alone, or with extern or static */
1307 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1308 sparse_error(*pos
, "__thread can only be used alone, or with "
1309 "extern or static");
1313 if (!ctx
->storage_class
) {
1314 ctx
->storage_class
= class;
1317 if (ctx
->storage_class
== class)
1318 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1320 sparse_error(*pos
, "multiple storage classes");
1323 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1325 set_storage_class(&next
->pos
, ctx
, STypedef
);
1329 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1331 set_storage_class(&next
->pos
, ctx
, SAuto
);
1335 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1337 set_storage_class(&next
->pos
, ctx
, SRegister
);
1341 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1343 set_storage_class(&next
->pos
, ctx
, SStatic
);
1347 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1349 set_storage_class(&next
->pos
, ctx
, SExtern
);
1353 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1355 /* This GCC extension can be used alone, or with extern or static */
1356 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1357 || ctx
->storage_class
== SExtern
) {
1360 sparse_error(next
->pos
, "__thread can only be used alone, or "
1361 "with extern or static");
1367 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1369 set_storage_class(&token
->pos
, ctx
, SForced
);
1373 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1379 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1381 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1385 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1387 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1391 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1393 unsigned long mod
= thistype
->modifiers
;
1396 apply_qualifier(&pos
, ctype
, mod
);
1399 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1400 (struct ptr_list
**)&ctype
->contexts
);
1403 if (thistype
->alignment
> ctype
->alignment
)
1404 ctype
->alignment
= thistype
->alignment
;
1408 ctype
->as
= thistype
->as
;
1411 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1414 if (what
& (Set_S
| Set_T
))
1416 if (what
& Set_Char
)
1418 else if (what
& Set_Double
)
1420 else if (what
& Set_Float
)
1422 else if (what
& Set_Signed
)
1424 else if (what
& Set_Unsigned
)
1426 else if (what
& Set_Short
)
1428 else if (what
& Set_Long
)
1432 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1433 old
, show_ident(new));
1437 sparse_error(pos
, "two or more data types in declaration specifiers");
1440 static struct symbol
* const int_types
[] =
1441 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &lllong_ctype
};
1442 static struct symbol
* const signed_types
[] =
1443 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1445 static struct symbol
* const unsigned_types
[] =
1446 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1448 static struct symbol
* const real_types
[] =
1449 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1450 static struct symbol
* const char_types
[] =
1451 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1452 static struct symbol
* const * const types
[] = {
1453 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1454 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1457 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1459 return types
[want_unsigned
? CUInt
: CInt
][size
];
1462 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1464 while (token_type(t
) == TOKEN_IDENT
) {
1465 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1468 if (s
->type
!= SYM_KEYWORD
)
1470 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1473 if (s
->op
->declarator
)
1474 t
= s
->op
->declarator(t
, ctx
);
1479 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1485 while (token_type(token
) == TOKEN_IDENT
) {
1486 struct symbol
*s
= lookup_symbol(token
->ident
,
1487 NS_TYPEDEF
| NS_SYMBOL
);
1488 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1490 if (s
->type
!= SYM_KEYWORD
) {
1493 seen
|= Set_S
| Set_T
;
1494 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1495 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1496 token
= token
->next
;
1499 if (s
->op
->type
& KW_SPECIFIER
) {
1500 if (seen
& s
->op
->test
) {
1501 specifier_conflict(token
->pos
,
1507 class += s
->op
->class;
1508 if (s
->op
->set
& Set_Int128
)
1510 if (s
->op
->type
& KW_SHORT
) {
1512 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1513 if (class == CReal
) {
1514 specifier_conflict(token
->pos
,
1522 token
= token
->next
;
1523 if (s
->op
->declarator
)
1524 token
= s
->op
->declarator(token
, ctx
);
1525 if (s
->op
->type
& KW_EXACT
) {
1526 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1527 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1531 if (!(seen
& Set_S
)) { /* not set explicitly? */
1532 struct symbol
*base
= &incomplete_ctype
;
1534 base
= types
[class][size
];
1535 ctx
->ctype
.base_type
= base
;
1538 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1539 struct symbol
*type
;
1540 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1541 if (!is_int_type(ctx
->ctype
.base_type
)) {
1542 sparse_error(token
->pos
, "invalid modifier");
1545 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1546 *type
= *ctx
->ctype
.base_type
;
1547 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1548 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1549 type
->type
= SYM_RESTRICT
;
1550 ctx
->ctype
.base_type
= type
;
1551 create_fouled(type
);
1556 static struct token
*abstract_array_static_declarator(struct token
*token
, int *has_static
)
1558 while (token
->ident
== &static_ident
) {
1560 sparse_error(token
->pos
, "duplicate array static declarator");
1563 token
= token
->next
;
1569 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1571 struct expression
*expr
= NULL
;
1574 token
= abstract_array_static_declarator(token
, &has_static
);
1576 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, &__restrict___ident
, NULL
))
1577 token
= abstract_array_static_declarator(token
->next
, &has_static
);
1578 token
= parse_expression(token
, &expr
);
1579 sym
->array_size
= expr
;
1583 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1584 static struct token
*identifier_list(struct token
*, struct symbol
*);
1585 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1587 static struct token
*skip_attribute(struct token
*token
)
1589 token
= token
->next
;
1590 if (match_op(token
, '(')) {
1592 token
= token
->next
;
1593 while (depth
&& !eof_token(token
)) {
1594 if (token_type(token
) == TOKEN_SPECIAL
) {
1595 if (token
->special
== '(')
1597 else if (token
->special
== ')')
1600 token
= token
->next
;
1606 static struct token
*skip_attributes(struct token
*token
)
1608 struct symbol
*keyword
;
1610 if (token_type(token
) != TOKEN_IDENT
)
1612 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1613 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1615 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1617 token
= expect(token
->next
, '(', "after attribute");
1618 token
= expect(token
, '(', "after attribute");
1620 if (eof_token(token
))
1622 if (match_op(token
, ';'))
1624 if (token_type(token
) != TOKEN_IDENT
)
1626 token
= skip_attribute(token
);
1627 if (!match_op(token
, ','))
1629 token
= token
->next
;
1631 token
= expect(token
, ')', "after attribute");
1632 token
= expect(token
, ')', "after attribute");
1637 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1639 struct symbol
*keyword
;
1641 if (token_type(token
) != TOKEN_IDENT
)
1643 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1644 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1646 if (!(keyword
->op
->type
& keywords
))
1648 token
= keyword
->op
->declarator(token
->next
, ctx
);
1649 keywords
&= KW_ATTRIBUTE
;
1654 static int is_nested(struct token
*token
, struct token
**p
,
1655 int prefer_abstract
)
1658 * This can be either a parameter list or a grouping.
1659 * For the direct (non-abstract) case, we know if must be
1660 * a parameter list if we already saw the identifier.
1661 * For the abstract case, we know if must be a parameter
1662 * list if it is empty or starts with a type.
1664 struct token
*next
= token
->next
;
1666 *p
= next
= skip_attributes(next
);
1668 if (token_type(next
) == TOKEN_IDENT
) {
1669 if (lookup_type(next
))
1670 return !prefer_abstract
;
1674 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1681 Empty
, K_R
, Proto
, Bad_Func
,
1684 static enum kind
which_func(struct token
*token
,
1686 int prefer_abstract
)
1688 struct token
*next
= token
->next
;
1690 if (token_type(next
) == TOKEN_IDENT
) {
1691 if (lookup_type(next
))
1693 /* identifier list not in definition; complain */
1694 if (prefer_abstract
)
1696 "identifier list not in definition");
1700 if (token_type(next
) != TOKEN_SPECIAL
)
1703 if (next
->special
== ')') {
1704 /* don't complain about those */
1705 if (!n
|| match_op(next
->next
, ';'))
1708 "non-ANSI function declaration of function '%s'",
1713 if (next
->special
== SPECIAL_ELLIPSIS
) {
1715 "variadic functions must have one named argument");
1722 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1724 struct ctype
*ctype
= &ctx
->ctype
;
1726 struct ident
**p
= ctx
->ident
;
1728 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1729 *ctx
->ident
= token
->ident
;
1730 token
= token
->next
;
1731 } else if (match_op(token
, '(') &&
1732 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1733 struct symbol
*base_type
= ctype
->base_type
;
1734 if (token
->next
!= next
)
1735 next
= handle_attributes(token
->next
, ctx
,
1737 token
= declarator(next
, ctx
);
1738 token
= expect(token
, ')', "in nested declarator");
1739 while (ctype
->base_type
!= base_type
)
1740 ctype
= &ctype
->base_type
->ctype
;
1744 if (match_op(token
, '(')) {
1745 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1747 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1748 token
= token
->next
;
1750 token
= identifier_list(token
, fn
);
1751 else if (kind
== Proto
)
1752 token
= parameter_type_list(token
, fn
);
1753 token
= expect(token
, ')', "in function declarator");
1754 fn
->endpos
= token
->pos
;
1758 while (match_op(token
, '[')) {
1759 struct symbol
*array
;
1760 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1761 token
= abstract_array_declarator(token
->next
, array
);
1762 token
= expect(token
, ']', "in abstract_array_declarator");
1763 array
->endpos
= token
->pos
;
1764 ctype
= &array
->ctype
;
1769 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1771 while (match_op(token
,'*')) {
1772 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1773 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1774 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1775 ptr
->ctype
.as
= ctx
->ctype
.as
;
1776 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1777 ctx
->ctype
.modifiers
= 0;
1778 ctx
->ctype
.base_type
= ptr
;
1780 ctx
->ctype
.contexts
= NULL
;
1781 ctx
->ctype
.alignment
= 0;
1783 token
= handle_qualifiers(token
->next
, ctx
);
1784 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1789 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1791 token
= pointer(token
, ctx
);
1792 return direct_declarator(token
, ctx
);
1795 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1797 struct ctype
*ctype
= &ctx
->ctype
;
1798 struct expression
*expr
;
1799 struct symbol
*bitfield
;
1802 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1803 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1804 show_typename(ctype
->base_type
));
1805 // Parse this to recover gracefully.
1806 return conditional_expression(token
->next
, &expr
);
1809 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1810 token
= conditional_expression(token
->next
, &expr
);
1811 width
= const_expression_value(expr
);
1812 bitfield
->bit_size
= width
;
1814 if (width
< 0 || width
> INT_MAX
) {
1815 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1817 } else if (*ctx
->ident
&& width
== 0) {
1818 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1819 show_ident(*ctx
->ident
));
1821 } else if (*ctx
->ident
) {
1822 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1823 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1824 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1825 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1826 // Valid values are either {-1;0} or {0}, depending on integer
1827 // representation. The latter makes for very efficient code...
1828 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1830 if (Wdefault_bitfield_sign
&&
1831 bitfield_type
->type
!= SYM_ENUM
&&
1832 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1834 // The sign of bitfields is unspecified by default.
1835 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1838 bitfield
->bit_size
= width
;
1839 bitfield
->endpos
= token
->pos
;
1843 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1845 struct decl_state ctx
= {.prefer_abstract
= 0};
1849 token
= declaration_specifiers(token
, &ctx
);
1850 mod
= storage_modifiers(&ctx
);
1853 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1854 ctx
.ident
= &decl
->ident
;
1856 token
= declarator(token
, &ctx
);
1857 if (match_op(token
, ':'))
1858 token
= handle_bitfield(token
, &ctx
);
1860 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1861 apply_modifiers(token
->pos
, &ctx
);
1863 decl
->ctype
= ctx
.ctype
;
1864 decl
->ctype
.modifiers
|= mod
;
1865 decl
->endpos
= token
->pos
;
1866 add_symbol(list
, decl
);
1867 if (!match_op(token
, ','))
1869 token
= token
->next
;
1875 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1877 while (!match_op(token
, '}')) {
1878 if (!match_op(token
, ';'))
1879 token
= declaration_list(token
, list
);
1880 if (!match_op(token
, ';')) {
1881 sparse_error(token
->pos
, "expected ; at end of declaration");
1884 token
= token
->next
;
1889 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1891 struct decl_state ctx
= {.prefer_abstract
= 1};
1893 token
= declaration_specifiers(token
, &ctx
);
1894 ctx
.ident
= &sym
->ident
;
1895 token
= declarator(token
, &ctx
);
1896 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1897 apply_modifiers(token
->pos
, &ctx
);
1898 sym
->ctype
= ctx
.ctype
;
1899 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1900 sym
->endpos
= token
->pos
;
1901 sym
->forced_arg
= ctx
.storage_class
== SForced
;
1905 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1907 struct decl_state ctx
= {.prefer_abstract
= 1};
1909 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1911 token
= declaration_specifiers(token
, &ctx
);
1912 token
= declarator(token
, &ctx
);
1913 apply_modifiers(token
->pos
, &ctx
);
1914 sym
->ctype
= ctx
.ctype
;
1915 sym
->endpos
= token
->pos
;
1916 class = ctx
.storage_class
;
1919 if (class == SForced
) {
1925 warning(sym
->pos
, "storage class in typename (%s %s)",
1926 storage_class
[class], show_typename(sym
));
1930 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1932 token
= parse_expression(token
, tree
);
1933 return expect(token
, ';', "at end of statement");
1936 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1937 struct expression_list
**inout
)
1939 struct expression
*expr
;
1941 /* Allow empty operands */
1942 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1945 struct ident
*ident
= NULL
;
1946 if (match_op(token
->next
, '[') &&
1947 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1948 match_op(token
->next
->next
->next
, ']')) {
1949 ident
= token
->next
->next
->ident
;
1950 token
= token
->next
->next
->next
;
1952 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1953 token
= primary_expression(token
->next
, &expr
);
1954 add_expression(inout
, expr
);
1955 token
= parens_expression(token
, &expr
, "in asm parameter");
1956 add_expression(inout
, expr
);
1957 } while (match_op(token
, ','));
1961 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1962 struct expression_list
**clobbers
)
1964 struct expression
*expr
;
1967 token
= primary_expression(token
->next
, &expr
);
1969 add_expression(clobbers
, expr
);
1970 } while (match_op(token
, ','));
1974 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1975 struct symbol_list
**labels
)
1977 struct symbol
*label
;
1980 token
= token
->next
; /* skip ':' and ',' */
1981 if (token_type(token
) != TOKEN_IDENT
)
1983 label
= label_symbol(token
);
1984 add_symbol(labels
, label
);
1985 token
= token
->next
;
1986 } while (match_op(token
, ','));
1990 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1994 token
= token
->next
;
1995 stmt
->type
= STMT_ASM
;
1996 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1997 token
= token
->next
;
1999 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
2001 token
= token
->next
;
2003 token
= expect(token
, '(', "after asm");
2004 token
= parse_expression(token
, &stmt
->asm_string
);
2005 if (match_op(token
, ':'))
2006 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2007 if (match_op(token
, ':'))
2008 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2009 if (match_op(token
, ':'))
2010 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2011 if (is_goto
&& match_op(token
, ':'))
2012 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2013 token
= expect(token
, ')', "after asm");
2014 return expect(token
, ';', "at end of asm-statement");
2017 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
2019 struct expression
*expr
;
2020 token
= expect(token
, '(', "after asm");
2021 token
= parse_expression(token
->next
, &expr
);
2022 token
= expect(token
, ')', "after asm");
2026 /* Make a statement out of an expression */
2027 static struct statement
*make_statement(struct expression
*expr
)
2029 struct statement
*stmt
;
2033 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2034 stmt
->expression
= expr
;
2039 * All iterators have two symbols associated with them:
2040 * the "continue" and "break" symbols, which are targets
2041 * for continue and break statements respectively.
2043 * They are in a special name-space, but they follow
2044 * all the normal visibility rules, so nested iterators
2045 * automatically work right.
2047 static void start_iterator(struct statement
*stmt
)
2049 struct symbol
*cont
, *brk
;
2051 start_symbol_scope();
2052 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2053 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2054 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2055 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2057 stmt
->type
= STMT_ITERATOR
;
2058 stmt
->iterator_break
= brk
;
2059 stmt
->iterator_continue
= cont
;
2060 fn_local_symbol(brk
);
2061 fn_local_symbol(cont
);
2064 static void end_iterator(struct statement
*stmt
)
2069 static struct statement
*start_function(struct symbol
*sym
)
2072 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2074 start_function_scope();
2075 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2076 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2077 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2078 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2079 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2081 fn_local_symbol(ret
);
2083 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2089 static void end_function(struct symbol
*sym
)
2092 end_function_scope();
2096 * A "switch()" statement, like an iterator, has a
2097 * the "break" symbol associated with it. It works
2098 * exactly like the iterator break - it's the target
2099 * for any break-statements in scope, and means that
2100 * "break" handling doesn't even need to know whether
2101 * it's breaking out of an iterator or a switch.
2103 * In addition, the "case" symbol is a marker for the
2104 * case/default statements to find the switch statement
2105 * that they are associated with.
2107 static void start_switch(struct statement
*stmt
)
2109 struct symbol
*brk
, *switch_case
;
2111 start_symbol_scope();
2112 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2113 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2115 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2116 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2117 switch_case
->stmt
= stmt
;
2119 stmt
->type
= STMT_SWITCH
;
2120 stmt
->switch_break
= brk
;
2121 stmt
->switch_case
= switch_case
;
2123 fn_local_symbol(brk
);
2124 fn_local_symbol(switch_case
);
2127 static void end_switch(struct statement
*stmt
)
2129 if (!stmt
->switch_case
->symbol_list
)
2130 warning(stmt
->pos
, "switch with no cases");
2134 static void add_case_statement(struct statement
*stmt
)
2136 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2140 sparse_error(stmt
->pos
, "not in switch scope");
2141 stmt
->type
= STMT_NONE
;
2144 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2145 add_symbol(&target
->symbol_list
, sym
);
2147 stmt
->case_label
= sym
;
2148 fn_local_symbol(sym
);
2151 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2153 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2156 error_die(token
->pos
, "internal error: return without a function target");
2157 stmt
->type
= STMT_RETURN
;
2158 stmt
->ret_target
= target
;
2159 return expression_statement(token
->next
, &stmt
->ret_value
);
2162 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2164 struct symbol_list
*syms
;
2165 struct expression
*e1
, *e2
, *e3
;
2166 struct statement
*iterator
;
2168 start_iterator(stmt
);
2169 token
= expect(token
->next
, '(', "after 'for'");
2173 /* C99 variable declaration? */
2174 if (lookup_type(token
)) {
2175 token
= external_declaration(token
, &syms
);
2177 token
= parse_expression(token
, &e1
);
2178 token
= expect(token
, ';', "in 'for'");
2180 token
= parse_expression(token
, &e2
);
2181 token
= expect(token
, ';', "in 'for'");
2182 token
= parse_expression(token
, &e3
);
2183 token
= expect(token
, ')', "in 'for'");
2184 token
= statement(token
, &iterator
);
2186 stmt
->iterator_syms
= syms
;
2187 stmt
->iterator_pre_statement
= make_statement(e1
);
2188 stmt
->iterator_pre_condition
= e2
;
2189 stmt
->iterator_post_statement
= make_statement(e3
);
2190 stmt
->iterator_post_condition
= NULL
;
2191 stmt
->iterator_statement
= iterator
;
2197 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2199 struct expression
*expr
;
2200 struct statement
*iterator
;
2202 start_iterator(stmt
);
2203 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2204 token
= statement(token
, &iterator
);
2206 stmt
->iterator_pre_condition
= expr
;
2207 stmt
->iterator_post_condition
= NULL
;
2208 stmt
->iterator_statement
= iterator
;
2214 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2216 struct expression
*expr
;
2217 struct statement
*iterator
;
2219 start_iterator(stmt
);
2220 token
= statement(token
->next
, &iterator
);
2221 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2222 token
= token
->next
;
2224 sparse_error(token
->pos
, "expected 'while' after 'do'");
2225 token
= parens_expression(token
, &expr
, "after 'do-while'");
2227 stmt
->iterator_post_condition
= expr
;
2228 stmt
->iterator_statement
= iterator
;
2231 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2232 warning(iterator
->pos
, "do-while statement is not a compound statement");
2234 return expect(token
, ';', "after statement");
2237 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2239 stmt
->type
= STMT_IF
;
2240 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2241 token
= statement(token
, &stmt
->if_true
);
2242 if (token_type(token
) != TOKEN_IDENT
)
2244 if (token
->ident
!= &else_ident
)
2246 return statement(token
->next
, &stmt
->if_false
);
2249 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2251 stmt
->type
= STMT_CASE
;
2252 token
= expect(token
, ':', "after default/case");
2253 add_case_statement(stmt
);
2254 return statement(token
, &stmt
->case_statement
);
2257 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2259 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2260 if (match_op(token
, SPECIAL_ELLIPSIS
))
2261 token
= parse_expression(token
->next
, &stmt
->case_to
);
2262 return case_statement(token
, stmt
);
2265 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2267 return case_statement(token
->next
, stmt
);
2270 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2272 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2273 stmt
->type
= STMT_GOTO
;
2274 stmt
->goto_label
= target
;
2276 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2277 return expect(token
->next
, ';', "at end of statement");
2280 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2282 stmt
->type
= STMT_SWITCH
;
2284 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2285 token
= statement(token
, &stmt
->switch_statement
);
2290 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2292 stmt
->type
= STMT_GOTO
;
2293 token
= token
->next
;
2294 if (match_op(token
, '*')) {
2295 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2296 add_statement(&function_computed_goto_list
, stmt
);
2297 } else if (token_type(token
) == TOKEN_IDENT
) {
2298 stmt
->goto_label
= label_symbol(token
);
2299 token
= token
->next
;
2301 sparse_error(token
->pos
, "Expected identifier or goto expression");
2303 return expect(token
, ';', "at end of statement");
2306 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2308 stmt
->type
= STMT_CONTEXT
;
2309 token
= parse_expression(token
->next
, &stmt
->expression
);
2310 if (stmt
->expression
->type
== EXPR_PREOP
2311 && stmt
->expression
->op
== '('
2312 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2313 struct expression
*expr
;
2314 expr
= stmt
->expression
->unop
;
2315 stmt
->context
= expr
->left
;
2316 stmt
->expression
= expr
->right
;
2318 return expect(token
, ';', "at end of statement");
2321 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2323 stmt
->type
= STMT_RANGE
;
2324 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2325 token
= expect(token
, ',', "after range expression");
2326 token
= assignment_expression(token
, &stmt
->range_low
);
2327 token
= expect(token
, ',', "after low range");
2328 token
= assignment_expression(token
, &stmt
->range_high
);
2329 return expect(token
, ';', "after range statement");
2332 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2334 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2337 if (token_type(token
) == TOKEN_IDENT
) {
2338 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2339 if (s
&& s
->op
->statement
)
2340 return s
->op
->statement(token
, stmt
);
2342 if (match_op(token
->next
, ':')) {
2343 struct symbol
*s
= label_symbol(token
);
2344 stmt
->type
= STMT_LABEL
;
2345 stmt
->label_identifier
= s
;
2347 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(token
->ident
));
2349 token
= skip_attributes(token
->next
->next
);
2350 return statement(token
, &stmt
->label_statement
);
2354 if (match_op(token
, '{')) {
2355 stmt
->type
= STMT_COMPOUND
;
2356 start_symbol_scope();
2357 token
= compound_statement(token
->next
, stmt
);
2360 return expect(token
, '}', "at end of compound statement");
2363 stmt
->type
= STMT_EXPRESSION
;
2364 return expression_statement(token
, &stmt
->expression
);
2367 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2368 static struct token
*label_statement(struct token
*token
)
2370 while (token_type(token
) == TOKEN_IDENT
) {
2371 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2372 /* it's block-scope, but we want label namespace */
2373 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2374 sym
->namespace = NS_LABEL
;
2375 fn_local_symbol(sym
);
2376 token
= token
->next
;
2377 if (!match_op(token
, ','))
2379 token
= token
->next
;
2381 return expect(token
, ';', "at end of label declaration");
2384 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2386 int seen_statement
= 0;
2387 while (token_type(token
) == TOKEN_IDENT
&&
2388 token
->ident
== &__label___ident
)
2389 token
= label_statement(token
->next
);
2391 struct statement
* stmt
;
2392 if (eof_token(token
))
2394 if (match_op(token
, '}'))
2396 if (lookup_type(token
)) {
2397 if (seen_statement
) {
2398 warning(token
->pos
, "mixing declarations and code");
2401 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2402 token
= external_declaration(token
, &stmt
->declaration
);
2404 seen_statement
= Wdeclarationafterstatement
;
2405 token
= statement(token
, &stmt
);
2407 add_statement(list
, stmt
);
2412 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2414 struct symbol_list
**list
= &fn
->arguments
;
2416 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2417 sym
->ident
= token
->ident
;
2418 token
= token
->next
;
2419 sym
->endpos
= token
->pos
;
2420 sym
->ctype
.base_type
= &incomplete_ctype
;
2421 add_symbol(list
, sym
);
2422 if (!match_op(token
, ',') ||
2423 token_type(token
->next
) != TOKEN_IDENT
||
2424 lookup_type(token
->next
))
2426 token
= token
->next
;
2431 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2433 struct symbol_list
**list
= &fn
->arguments
;
2438 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2440 token
= token
->next
;
2444 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2445 token
= parameter_declaration(token
, sym
);
2446 if (sym
->ctype
.base_type
== &void_ctype
) {
2447 /* Special case: (void) */
2448 if (!*list
&& !sym
->ident
)
2450 warning(token
->pos
, "void parameter");
2452 add_symbol(list
, sym
);
2453 if (!match_op(token
, ','))
2455 token
= token
->next
;
2460 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2462 token
= statement_list(token
, &stmt
->stmts
);
2466 static struct expression
*identifier_expression(struct token
*token
)
2468 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2469 expr
->expr_ident
= token
->ident
;
2473 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2475 int idx_from
, idx_to
;
2476 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2478 idx_from
= const_expression_value(from
);
2481 idx_to
= const_expression_value(to
);
2482 if (idx_to
< idx_from
|| idx_from
< 0)
2483 warning(from
->pos
, "nonsense array initializer index range");
2485 expr
->idx_from
= idx_from
;
2486 expr
->idx_to
= idx_to
;
2490 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2492 int expect_equal
= 0;
2493 struct token
*next
= token
->next
;
2494 struct expression
**tail
= ep
;
2499 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2500 struct expression
*expr
= identifier_expression(token
);
2501 if (Wold_initializer
)
2502 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2503 token
= initializer(&expr
->ident_expression
, next
->next
);
2504 if (expr
->ident_expression
)
2509 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2510 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2511 struct expression
*expr
= identifier_expression(next
);
2513 tail
= &expr
->ident_expression
;
2516 } else if (match_op(token
, '[')) {
2517 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2518 token
= constant_expression(token
->next
, &from
);
2520 sparse_error(token
->pos
, "Expected constant expression");
2523 if (match_op(token
, SPECIAL_ELLIPSIS
))
2524 token
= constant_expression(token
->next
, &to
);
2525 expr
= index_expression(from
, to
);
2527 tail
= &expr
->idx_expression
;
2528 token
= expect(token
, ']', "at end of initializer index");
2535 if (nested
&& !expect_equal
) {
2536 if (!match_op(token
, '='))
2537 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2542 token
= expect(token
, '=', "at end of initializer index");
2544 token
= initializer(tail
, token
);
2550 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2552 struct expression
*expr
;
2555 token
= single_initializer(&expr
, token
);
2558 add_expression(list
, expr
);
2559 if (!match_op(token
, ','))
2561 token
= token
->next
;
2566 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2568 if (match_op(token
, '{')) {
2569 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2571 token
= initializer_list(&expr
->expr_list
, token
->next
);
2572 return expect(token
, '}', "at end of initializer");
2574 return assignment_expression(token
, tree
);
2577 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2580 sparse_error(sym
->pos
, "no identifier for function argument");
2583 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2586 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2587 struct symbol_list
**list
)
2589 struct symbol_list
**old_symbol_list
;
2590 struct symbol
*base_type
= decl
->ctype
.base_type
;
2591 struct statement
*stmt
, **p
;
2592 struct symbol
*prev
;
2595 old_symbol_list
= function_symbol_list
;
2596 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2597 function_symbol_list
= &decl
->inline_symbol_list
;
2598 p
= &base_type
->inline_stmt
;
2600 function_symbol_list
= &decl
->symbol_list
;
2601 p
= &base_type
->stmt
;
2603 function_computed_target_list
= NULL
;
2604 function_computed_goto_list
= NULL
;
2606 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2607 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2608 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2610 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2611 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2613 stmt
= start_function(decl
);
2616 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2617 declare_argument(arg
, base_type
);
2618 } END_FOR_EACH_PTR(arg
);
2620 token
= compound_statement(token
->next
, stmt
);
2623 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2624 add_symbol(list
, decl
);
2625 check_declaration(decl
);
2626 decl
->definition
= decl
;
2627 prev
= decl
->same_symbol
;
2628 if (prev
&& prev
->definition
) {
2629 warning(decl
->pos
, "multiple definitions for function '%s'",
2630 show_ident(decl
->ident
));
2631 info(prev
->definition
->pos
, " the previous one is here");
2634 rebind_scope(prev
, decl
->scope
);
2635 prev
->definition
= decl
;
2636 prev
= prev
->same_symbol
;
2639 function_symbol_list
= old_symbol_list
;
2640 if (function_computed_goto_list
) {
2641 if (!function_computed_target_list
)
2642 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2644 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2645 stmt
->target_list
= function_computed_target_list
;
2646 } END_FOR_EACH_PTR(stmt
);
2649 return expect(token
, '}', "at end of function");
2652 static void promote_k_r_types(struct symbol
*arg
)
2654 struct symbol
*base
= arg
->ctype
.base_type
;
2655 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2656 arg
->ctype
.base_type
= &int_ctype
;
2660 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2662 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2665 FOR_EACH_PTR(real_args
, arg
) {
2666 struct symbol
*type
;
2668 /* This is quadratic in the number of arguments. We _really_ don't care */
2669 FOR_EACH_PTR(argtypes
, type
) {
2670 if (type
->ident
== arg
->ident
)
2672 } END_FOR_EACH_PTR(type
);
2673 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2677 /* "char" and "short" promote to "int" */
2678 promote_k_r_types(type
);
2680 arg
->ctype
= type
->ctype
;
2681 } END_FOR_EACH_PTR(arg
);
2683 FOR_EACH_PTR(argtypes
, arg
) {
2685 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2686 } END_FOR_EACH_PTR(arg
);
2690 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2691 struct symbol_list
**list
)
2693 struct symbol_list
*args
= NULL
;
2695 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2697 token
= declaration_list(token
, &args
);
2698 if (!match_op(token
, ';')) {
2699 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2702 token
= token
->next
;
2703 } while (lookup_type(token
));
2705 apply_k_r_types(args
, decl
);
2707 if (!match_op(token
, '{')) {
2708 sparse_error(token
->pos
, "expected function body");
2711 return parse_function_body(token
, decl
, list
);
2714 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2716 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2717 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2718 struct statement
*stmt
;
2720 anon
->ctype
.base_type
= fn
;
2721 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2724 token
= parse_asm_statement(token
, stmt
);
2726 add_symbol(list
, anon
);
2730 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2732 struct ident
*ident
= NULL
;
2733 struct symbol
*decl
;
2734 struct decl_state ctx
= { .ident
= &ident
};
2736 struct symbol
*base_type
;
2740 /* Top-level inline asm? */
2741 if (token_type(token
) == TOKEN_IDENT
) {
2742 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2743 if (s
&& s
->op
->toplevel
)
2744 return s
->op
->toplevel(token
, list
);
2747 /* Parse declaration-specifiers, if any */
2748 token
= declaration_specifiers(token
, &ctx
);
2749 mod
= storage_modifiers(&ctx
);
2750 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2751 /* Just a type declaration? */
2752 if (match_op(token
, ';')) {
2753 apply_modifiers(token
->pos
, &ctx
);
2758 token
= declarator(token
, &ctx
);
2759 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2760 apply_modifiers(token
->pos
, &ctx
);
2762 decl
->ctype
= ctx
.ctype
;
2763 decl
->ctype
.modifiers
|= mod
;
2764 decl
->endpos
= token
->pos
;
2766 /* Just a type declaration? */
2768 warning(token
->pos
, "missing identifier in declaration");
2769 return expect(token
, ';', "at the end of type declaration");
2772 /* type define declaration? */
2773 is_typedef
= ctx
.storage_class
== STypedef
;
2775 /* Typedefs don't have meaningful storage */
2777 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2779 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2781 base_type
= decl
->ctype
.base_type
;
2784 if (base_type
&& !base_type
->ident
) {
2785 switch (base_type
->type
) {
2790 base_type
->ident
= ident
;
2796 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2797 /* K&R argument declaration? */
2798 if (lookup_type(token
))
2799 return parse_k_r_arguments(token
, decl
, list
);
2800 if (match_op(token
, '{'))
2801 return parse_function_body(token
, decl
, list
);
2803 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2804 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2805 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2806 sparse_error(token
->pos
, "void declaration");
2810 if (!is_typedef
&& match_op(token
, '=')) {
2811 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2812 warning(decl
->pos
, "symbol with external linkage has initializer");
2813 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2815 token
= initializer(&decl
->initializer
, token
->next
);
2818 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2819 add_symbol(list
, decl
);
2820 fn_local_symbol(decl
);
2823 check_declaration(decl
);
2824 if (decl
->same_symbol
)
2825 decl
->definition
= decl
->same_symbol
->definition
;
2827 if (!match_op(token
, ','))
2830 token
= token
->next
;
2832 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2834 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2835 token
= declarator(token
, &ctx
);
2836 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2837 apply_modifiers(token
->pos
, &ctx
);
2838 decl
->ctype
= ctx
.ctype
;
2839 decl
->ctype
.modifiers
|= mod
;
2840 decl
->endpos
= token
->pos
;
2842 sparse_error(token
->pos
, "expected identifier name in type definition");
2847 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2849 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2851 /* Function declarations are automatically extern unless specifically static */
2852 base_type
= decl
->ctype
.base_type
;
2853 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2854 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2855 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2858 return expect(token
, ';', "at end of declaration");