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
;
61 static declarator_t restrict_qualifier
;
62 static declarator_t atomic_qualifier
;
63 static declarator_t autotype_specifier
;
65 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
66 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
67 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
68 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
69 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
70 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
71 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
72 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
73 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
74 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
75 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
76 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
77 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
78 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
79 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
);
81 typedef struct token
*attr_t(struct token
*, struct symbol
*,
85 attribute_packed
, attribute_aligned
, attribute_modifier
,
88 attribute_address_space
, attribute_context
,
89 attribute_designated_init
,
90 attribute_transparent_union
, ignore_attribute
,
91 attribute_mode
, attribute_force
;
93 typedef struct symbol
*to_mode_t(struct symbol
*);
96 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
;
97 static to_mode_t to_pointer_mode
, to_word_mode
;
112 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
116 CInt
= 0, CSInt
, CUInt
, CReal
,
120 SNone
= 0, STypedef
, SAuto
, SRegister
, SExtern
, SStatic
, SForced
, SMax
,
123 static void asm_modifier(struct token
*token
, unsigned long *mods
, unsigned long mod
)
126 warning(token
->pos
, "duplicated asm modifier");
130 static void asm_modifier_volatile(struct token
*token
, unsigned long *mods
)
132 asm_modifier(token
, mods
, MOD_VOLATILE
);
135 static void asm_modifier_inline(struct token
*token
, unsigned long *mods
)
137 asm_modifier(token
, mods
, MOD_INLINE
);
140 static struct symbol_op typedef_op
= {
142 .declarator
= typedef_specifier
,
145 static struct symbol_op inline_op
= {
147 .declarator
= inline_specifier
,
148 .asm_modifier
= asm_modifier_inline
,
151 static declarator_t noreturn_specifier
;
152 static struct symbol_op noreturn_op
= {
154 .declarator
= noreturn_specifier
,
157 static declarator_t alignas_specifier
;
158 static struct symbol_op alignas_op
= {
160 .declarator
= alignas_specifier
,
163 static struct symbol_op auto_op
= {
165 .declarator
= auto_specifier
,
168 static struct symbol_op register_op
= {
170 .declarator
= register_specifier
,
173 static struct symbol_op static_op
= {
175 .declarator
= static_specifier
,
178 static struct symbol_op extern_op
= {
180 .declarator
= extern_specifier
,
183 static struct symbol_op thread_op
= {
185 .declarator
= thread_specifier
,
188 static struct symbol_op const_op
= {
189 .type
= KW_QUALIFIER
,
190 .declarator
= const_qualifier
,
193 static struct symbol_op volatile_op
= {
194 .type
= KW_QUALIFIER
,
195 .declarator
= volatile_qualifier
,
196 .asm_modifier
= asm_modifier_volatile
,
199 static struct symbol_op restrict_op
= {
200 .type
= KW_QUALIFIER
,
201 .declarator
= restrict_qualifier
,
204 static struct symbol_op atomic_op
= {
205 .type
= KW_QUALIFIER
,
206 .declarator
= atomic_qualifier
,
209 static struct symbol_op typeof_op
= {
210 .type
= KW_SPECIFIER
,
211 .declarator
= typeof_specifier
,
216 static struct symbol_op autotype_op
= {
217 .type
= KW_SPECIFIER
,
218 .declarator
= autotype_specifier
,
223 static struct symbol_op attribute_op
= {
224 .type
= KW_ATTRIBUTE
,
225 .declarator
= attribute_specifier
,
228 static struct symbol_op struct_op
= {
229 .type
= KW_SPECIFIER
,
230 .declarator
= struct_specifier
,
235 static struct symbol_op union_op
= {
236 .type
= KW_SPECIFIER
,
237 .declarator
= union_specifier
,
242 static struct symbol_op enum_op
= {
243 .type
= KW_SPECIFIER
,
244 .declarator
= enum_specifier
,
249 static struct symbol_op spec_op
= {
250 .type
= KW_SPECIFIER
| KW_EXACT
,
255 static struct symbol_op char_op
= {
256 .type
= KW_SPECIFIER
,
257 .test
= Set_T
|Set_Long
|Set_Short
,
258 .set
= Set_T
|Set_Char
,
262 static struct symbol_op int_op
= {
263 .type
= KW_SPECIFIER
,
265 .set
= Set_T
|Set_Int
,
268 static struct symbol_op double_op
= {
269 .type
= KW_SPECIFIER
,
270 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
271 .set
= Set_T
|Set_Double
,
275 static struct symbol_op float_op
= {
276 .type
= KW_SPECIFIER
,
277 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
278 .set
= Set_T
|Set_Float
,
282 static struct symbol_op short_op
= {
283 .type
= KW_SPECIFIER
,
284 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
288 static struct symbol_op signed_op
= {
289 .type
= KW_SPECIFIER
,
290 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
295 static struct symbol_op unsigned_op
= {
296 .type
= KW_SPECIFIER
,
297 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
302 static struct symbol_op long_op
= {
303 .type
= KW_SPECIFIER
,
304 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
308 static struct symbol_op int128_op
= {
309 .type
= KW_SPECIFIER
,
310 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
311 .set
= Set_T
|Set_Int128
|Set_Vlong
,
315 static struct symbol_op if_op
= {
316 .statement
= parse_if_statement
,
319 static struct symbol_op return_op
= {
320 .statement
= parse_return_statement
,
323 static struct symbol_op loop_iter_op
= {
324 .statement
= parse_loop_iterator
,
327 static struct symbol_op default_op
= {
328 .statement
= parse_default_statement
,
331 static struct symbol_op case_op
= {
332 .statement
= parse_case_statement
,
335 static struct symbol_op switch_op
= {
336 .statement
= parse_switch_statement
,
339 static struct symbol_op for_op
= {
340 .statement
= parse_for_statement
,
343 static struct symbol_op while_op
= {
344 .statement
= parse_while_statement
,
347 static struct symbol_op do_op
= {
348 .statement
= parse_do_statement
,
351 static struct symbol_op goto_op
= {
352 .statement
= parse_goto_statement
,
355 static struct symbol_op __context___op
= {
356 .statement
= parse_context_statement
,
357 .attribute
= attribute_context
,
360 static struct symbol_op range_op
= {
361 .statement
= parse_range_statement
,
364 static struct symbol_op asm_op
= {
366 .declarator
= parse_asm_declarator
,
367 .statement
= parse_asm_statement
,
368 .toplevel
= toplevel_asm_declaration
,
371 static struct symbol_op static_assert_op
= {
372 .toplevel
= parse_static_assert
,
375 static struct symbol_op packed_op
= {
376 .attribute
= attribute_packed
,
379 static struct symbol_op aligned_op
= {
380 .attribute
= attribute_aligned
,
383 static struct symbol_op attr_mod_op
= {
384 .attribute
= attribute_modifier
,
387 static struct symbol_op attr_fun_op
= {
388 .attribute
= attribute_function
,
391 static struct symbol_op attr_bitwise_op
= {
392 .attribute
= attribute_bitwise
,
395 static struct symbol_op attr_force_op
= {
396 .attribute
= attribute_force
,
399 static struct symbol_op address_space_op
= {
400 .attribute
= attribute_address_space
,
403 static struct symbol_op mode_op
= {
404 .attribute
= attribute_mode
,
407 static struct symbol_op context_op
= {
408 .attribute
= attribute_context
,
411 static struct symbol_op designated_init_op
= {
412 .attribute
= attribute_designated_init
,
415 static struct symbol_op transparent_union_op
= {
416 .attribute
= attribute_transparent_union
,
419 static struct symbol_op ignore_attr_op
= {
420 .attribute
= ignore_attribute
,
423 static struct symbol_op mode_QI_op
= {
425 .to_mode
= to_QI_mode
428 static struct symbol_op mode_HI_op
= {
430 .to_mode
= to_HI_mode
433 static struct symbol_op mode_SI_op
= {
435 .to_mode
= to_SI_mode
438 static struct symbol_op mode_DI_op
= {
440 .to_mode
= to_DI_mode
443 static struct symbol_op mode_TI_op
= {
445 .to_mode
= to_TI_mode
448 static struct symbol_op mode_pointer_op
= {
450 .to_mode
= to_pointer_mode
453 static struct symbol_op mode_word_op
= {
455 .to_mode
= to_word_mode
458 /* Using NS_TYPEDEF will also make the keyword a reserved one */
459 static struct init_keyword
{
462 unsigned long modifiers
;
463 struct symbol_op
*op
;
465 } keyword_table
[] = {
466 /* Type qualifiers */
467 { "const", NS_TYPEDEF
, .op
= &const_op
},
468 { "__const", NS_TYPEDEF
, .op
= &const_op
},
469 { "__const__", NS_TYPEDEF
, .op
= &const_op
},
470 { "volatile", NS_TYPEDEF
, .op
= &volatile_op
},
471 { "__volatile", NS_TYPEDEF
, .op
= &volatile_op
},
472 { "__volatile__", NS_TYPEDEF
, .op
= &volatile_op
},
473 { "restrict", NS_TYPEDEF
, .op
= &restrict_op
},
474 { "__restrict", NS_TYPEDEF
, .op
= &restrict_op
},
475 { "__restrict__", NS_TYPEDEF
, .op
= &restrict_op
},
476 { "_Atomic", NS_TYPEDEF
, .op
= &atomic_op
},
479 { "typedef", NS_TYPEDEF
, .op
= &typedef_op
},
481 /* Type specifiers */
482 { "void", NS_TYPEDEF
, .type
= &void_ctype
, .op
= &spec_op
},
483 { "char", NS_TYPEDEF
, .op
= &char_op
},
484 { "short", NS_TYPEDEF
, .op
= &short_op
},
485 { "int", NS_TYPEDEF
, .op
= &int_op
},
486 { "long", NS_TYPEDEF
, .op
= &long_op
},
487 { "float", NS_TYPEDEF
, .op
= &float_op
},
488 { "double", NS_TYPEDEF
, .op
= &double_op
},
489 { "signed", NS_TYPEDEF
, .op
= &signed_op
},
490 { "__signed", NS_TYPEDEF
, .op
= &signed_op
},
491 { "__signed__", NS_TYPEDEF
, .op
= &signed_op
},
492 { "unsigned", NS_TYPEDEF
, .op
= &unsigned_op
},
493 { "__int128", NS_TYPEDEF
, .op
= &int128_op
},
494 { "_Bool", NS_TYPEDEF
, .type
= &bool_ctype
, .op
= &spec_op
},
496 /* Predeclared types */
497 { "__builtin_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
498 { "__builtin_ms_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
499 { "__int128_t", NS_TYPEDEF
, .type
= &sint128_ctype
, .op
= &spec_op
},
500 { "__uint128_t",NS_TYPEDEF
, .type
= &uint128_ctype
, .op
= &spec_op
},
501 { "_Float32", NS_TYPEDEF
, .type
= &float32_ctype
, .op
= &spec_op
},
502 { "_Float32x", NS_TYPEDEF
, .type
= &float32x_ctype
, .op
= &spec_op
},
503 { "_Float64", NS_TYPEDEF
, .type
= &float64_ctype
, .op
= &spec_op
},
504 { "_Float64x", NS_TYPEDEF
, .type
= &float64x_ctype
, .op
= &spec_op
},
505 { "_Float128", NS_TYPEDEF
, .type
= &float128_ctype
, .op
= &spec_op
},
508 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
509 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
510 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
511 { "__auto_type",NS_TYPEDEF
, .op
= &autotype_op
},
513 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
514 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
516 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
517 { "union", NS_TYPEDEF
, .op
= &union_op
},
518 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
520 { "inline", NS_TYPEDEF
, .op
= &inline_op
},
521 { "__inline", NS_TYPEDEF
, .op
= &inline_op
},
522 { "__inline__", NS_TYPEDEF
, .op
= &inline_op
},
524 { "_Noreturn", NS_TYPEDEF
, .op
= &noreturn_op
},
526 { "_Alignas", NS_TYPEDEF
, .op
= &alignas_op
},
528 /* Static assertion */
529 { "_Static_assert", NS_KEYWORD
, .op
= &static_assert_op
},
532 { "auto", NS_TYPEDEF
, .op
= &auto_op
},
533 { "register", NS_TYPEDEF
, .op
= ®ister_op
},
534 { "static", NS_TYPEDEF
, .op
= &static_op
},
535 { "extern", NS_TYPEDEF
, .op
= &extern_op
},
536 { "__thread", NS_TYPEDEF
, .op
= &thread_op
},
537 { "_Thread_local", NS_TYPEDEF
, .op
= &thread_op
},
540 { "if", NS_KEYWORD
, .op
= &if_op
},
541 { "return", NS_KEYWORD
, .op
= &return_op
},
542 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
543 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
544 { "default", NS_KEYWORD
, .op
= &default_op
},
545 { "case", NS_KEYWORD
, .op
= &case_op
},
546 { "switch", NS_KEYWORD
, .op
= &switch_op
},
547 { "for", NS_KEYWORD
, .op
= &for_op
},
548 { "while", NS_KEYWORD
, .op
= &while_op
},
549 { "do", NS_KEYWORD
, .op
= &do_op
},
550 { "goto", NS_KEYWORD
, .op
= &goto_op
},
551 { "context", NS_KEYWORD
, .op
= &context_op
},
552 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
553 { "__range__", NS_KEYWORD
, .op
= &range_op
},
554 { "asm", NS_KEYWORD
, .op
= &asm_op
},
555 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
556 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
559 { "packed", NS_KEYWORD
, .op
= &packed_op
},
560 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
561 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
562 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
563 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
564 { "__nocast__", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
565 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
566 { "__noderef__",NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
567 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
568 { "__safe__", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
569 { "unused", NS_KEYWORD
, MOD_UNUSED
, .op
= &attr_mod_op
},
570 { "__unused__", NS_KEYWORD
, MOD_UNUSED
, .op
= &attr_mod_op
},
571 { "externally_visible", NS_KEYWORD
, MOD_EXT_VISIBLE
,.op
= &attr_mod_op
},
572 { "__externally_visible__", NS_KEYWORD
,MOD_EXT_VISIBLE
,.op
= &attr_mod_op
},
573 { "force", NS_KEYWORD
, .op
= &attr_force_op
},
574 { "__force__", NS_KEYWORD
, .op
= &attr_force_op
},
575 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_bitwise_op
},
576 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_bitwise_op
},
577 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
578 { "__address_space__",NS_KEYWORD
, .op
= &address_space_op
},
579 { "designated_init", NS_KEYWORD
, .op
= &designated_init_op
},
580 { "__designated_init__", NS_KEYWORD
, .op
= &designated_init_op
},
581 { "transparent_union", NS_KEYWORD
, .op
= &transparent_union_op
},
582 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
583 { "noreturn", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_fun_op
},
584 { "__noreturn__", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_fun_op
},
585 { "pure", NS_KEYWORD
, MOD_PURE
, .op
= &attr_fun_op
},
586 {"__pure__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_fun_op
},
587 {"const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_fun_op
},
588 {"__const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_fun_op
},
589 {"__const__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_fun_op
},
590 {"gnu_inline", NS_KEYWORD
, MOD_GNU_INLINE
, .op
= &attr_fun_op
},
591 {"__gnu_inline__",NS_KEYWORD
, MOD_GNU_INLINE
, .op
= &attr_fun_op
},
593 { "mode", NS_KEYWORD
, .op
= &mode_op
},
594 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
595 { "QI", NS_KEYWORD
, .op
= &mode_QI_op
},
596 { "__QI__", NS_KEYWORD
, .op
= &mode_QI_op
},
597 { "HI", NS_KEYWORD
, .op
= &mode_HI_op
},
598 { "__HI__", NS_KEYWORD
, .op
= &mode_HI_op
},
599 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
600 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
601 { "DI", NS_KEYWORD
, .op
= &mode_DI_op
},
602 { "__DI__", NS_KEYWORD
, .op
= &mode_DI_op
},
603 { "TI", NS_KEYWORD
, .op
= &mode_TI_op
},
604 { "__TI__", NS_KEYWORD
, .op
= &mode_TI_op
},
605 { "byte", NS_KEYWORD
, .op
= &mode_QI_op
},
606 { "__byte__", NS_KEYWORD
, .op
= &mode_QI_op
},
607 { "pointer", NS_KEYWORD
, .op
= &mode_pointer_op
},
608 { "__pointer__",NS_KEYWORD
, .op
= &mode_pointer_op
},
609 { "word", NS_KEYWORD
, .op
= &mode_word_op
},
610 { "__word__", NS_KEYWORD
, .op
= &mode_word_op
},
614 static const char *ignored_attributes
[] = {
616 #define GCC_ATTR(x) \
618 STRINGIFY(__##x##__),
620 #include "gcc-attr-list.h"
632 void init_parser(int stream
)
635 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
636 struct init_keyword
*ptr
= keyword_table
+ i
;
637 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
638 sym
->ident
->keyword
= 1;
639 if (ptr
->ns
== NS_TYPEDEF
)
640 sym
->ident
->reserved
= 1;
641 sym
->ctype
.modifiers
= ptr
->modifiers
;
642 sym
->ctype
.base_type
= ptr
->type
;
646 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
647 const char * name
= ignored_attributes
[i
];
648 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
651 sym
->ident
->keyword
= 1;
652 sym
->op
= &ignore_attr_op
;
658 static struct token
*skip_to(struct token
*token
, int op
)
660 while (!match_op(token
, op
) && !eof_token(token
))
665 static struct token bad_token
= { .pos
.type
= TOKEN_BAD
};
666 struct token
*expect(struct token
*token
, int op
, const char *where
)
668 if (!match_op(token
, op
)) {
669 if (token
!= &bad_token
) {
670 bad_token
.next
= token
;
671 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
672 sparse_error(token
->pos
, "got %s", show_token(token
));
675 return skip_to(token
, op
);
682 // issue an error message on new parsing errors
683 // @token: the current token
684 // @errmsg: the error message
685 // If the current token is from a previous error, an error message
686 // has already been issued, so nothing more is done.
687 // Otherwise, @errmsg is displayed followed by the current token.
688 static void unexpected(struct token
*token
, const char *errmsg
)
690 if (token
== &bad_token
)
692 sparse_error(token
->pos
, "%s", errmsg
);
693 sparse_error(token
->pos
, "got %s", show_token(token
));
696 // Add a symbol to the list of function-local symbols
697 static void fn_local_symbol(struct symbol
*sym
)
699 if (function_symbol_list
)
700 add_symbol(function_symbol_list
, sym
);
703 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
708 if (token_type(token
) != TOKEN_IDENT
)
711 va_start(args
, token
);
713 next
= va_arg(args
, struct ident
*);
714 } while (next
&& token
->ident
!= next
);
717 return next
&& token
->ident
== next
;
721 struct statement
*alloc_statement(struct position pos
, int type
)
723 struct statement
*stmt
= __alloc_statement(0);
729 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
731 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
733 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
735 struct symbol
*ctype
;
738 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
740 sparse_error(pos
, "don't know how to apply mode to %s",
741 show_typename(ctx
->ctype
.base_type
));
743 ctx
->ctype
.base_type
= ctype
;
747 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
749 struct symbol
*sym
= alloc_symbol(pos
, type
);
751 sym
->ctype
.base_type
= ctype
->base_type
;
752 sym
->ctype
.modifiers
= ctype
->modifiers
;
754 ctype
->base_type
= sym
;
755 ctype
->modifiers
= 0;
760 * NOTE! NS_LABEL is not just a different namespace,
761 * it also ends up using function scope instead of the
762 * regular symbol scope.
764 struct symbol
*label_symbol(struct token
*token
, int used
)
766 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
768 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
769 bind_symbol(sym
, token
->ident
, NS_LABEL
);
772 fn_local_symbol(sym
);
777 static struct token
*struct_union_enum_specifier(enum type type
,
778 struct token
*token
, struct decl_state
*ctx
,
779 struct token
*(*parse
)(struct token
*, struct symbol
*))
782 struct position
*repos
;
784 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
785 if (token_type(token
) == TOKEN_IDENT
) {
786 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
788 (is_outer_scope(sym
->scope
) &&
789 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
790 // Either a new symbol, or else an out-of-scope
791 // symbol being redefined.
792 sym
= alloc_symbol(token
->pos
, type
);
793 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
795 if (sym
->type
!= type
)
796 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
797 ctx
->ctype
.base_type
= sym
;
800 if (match_op(token
, '{')) {
801 struct decl_state attr
= { .ctype
.base_type
= sym
, };
803 // The following test is actually wrong for empty
804 // structs, but (1) they are not C99, (2) gcc does
805 // the same thing, and (3) it's easier.
806 if (sym
->symbol_list
)
807 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
809 token
= parse(token
->next
, sym
);
810 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
812 token
= handle_attributes(token
, &attr
, KW_ATTRIBUTE
);
813 apply_ctype(token
->pos
, &attr
.ctype
, &sym
->ctype
);
815 // Mark the structure as needing re-examination
817 sym
->endpos
= token
->pos
;
822 // private struct/union/enum type
823 if (!match_op(token
, '{')) {
824 sparse_error(token
->pos
, "expected declaration");
825 ctx
->ctype
.base_type
= &bad_ctype
;
829 sym
= alloc_symbol(token
->pos
, type
);
830 set_current_scope(sym
); // used by dissect
831 token
= parse(token
->next
, sym
);
832 ctx
->ctype
.base_type
= sym
;
833 token
= expect(token
, '}', "at end of specifier");
834 sym
->endpos
= token
->pos
;
839 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
841 struct symbol
*field
, *last
= NULL
;
843 res
= struct_declaration_list(token
, &sym
->symbol_list
);
844 FOR_EACH_PTR(sym
->symbol_list
, field
) {
846 struct symbol
*base
= field
->ctype
.base_type
;
847 if (base
&& base
->type
== SYM_BITFIELD
)
851 last
->next_subobject
= field
;
853 } END_FOR_EACH_PTR(field
);
857 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
859 return struct_declaration_list(token
, &sym
->symbol_list
);
862 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
864 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
867 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
869 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
875 // This allow to use a shift amount as big (or bigger)
876 // than the width of the value to be shifted, in which case
877 // the result is, of course, 0.
878 static unsigned long long rshift(unsigned long long val
, unsigned int n
)
880 if (n
>= (sizeof(val
) * 8))
887 unsigned long long pos
;
890 static void update_range(struct range
*range
, unsigned long long uval
, struct symbol
*vtype
)
892 long long sval
= uval
;
894 if (is_signed_type(vtype
) && (sval
< 0)) {
895 if (sval
< range
->neg
)
898 if (uval
> range
->pos
)
903 static int type_is_ok(struct symbol
*type
, struct range range
)
905 int shift
= type
->bit_size
;
906 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
910 if (rshift(range
.pos
, shift
))
916 if (rshift(~range
.neg
, shift
))
921 static struct range
type_range(struct symbol
*type
)
924 unsigned int size
= type
->bit_size
;
925 unsigned long long max
;
928 if (is_signed_type(type
)) {
929 min
= sign_bit(size
);
933 max
= bits_mask(size
);
941 static int val_in_range(struct range
*range
, long long sval
, struct symbol
*vtype
)
943 unsigned long long uval
= sval
;
945 if (is_signed_type(vtype
) && (sval
< 0))
946 return range
->neg
<= sval
;
948 return uval
<= range
->pos
;
951 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
953 struct range irange
= type_range(&int_ctype
);
956 FOR_EACH_PTR(list
, sym
) {
957 struct expression
*expr
= sym
->initializer
;
958 struct symbol
*ctype
;
960 if (expr
->type
!= EXPR_VALUE
)
963 val
= get_expression_value(expr
);
964 if (is_int_type(ctype
) && val_in_range(&irange
, val
, ctype
)) {
965 expr
->ctype
= &int_ctype
;
968 cast_value(expr
, base_type
, expr
, ctype
);
969 expr
->ctype
= base_type
;
970 } END_FOR_EACH_PTR(sym
);
973 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
975 unsigned long long lastval
= 0;
976 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
977 struct range range
= { };
980 parent
->examined
= 1;
981 parent
->ctype
.base_type
= &int_ctype
;
982 while (token_type(token
) == TOKEN_IDENT
) {
983 struct expression
*expr
= NULL
;
984 struct token
*next
= token
->next
;
985 struct decl_state ctx
= { };
988 // FIXME: only 'deprecated' should be accepted
989 next
= handle_attributes(next
, &ctx
, KW_ATTRIBUTE
);
991 if (match_op(next
, '=')) {
992 next
= constant_expression(next
->next
, &expr
);
993 lastval
= get_expression_value(expr
);
995 if (expr
&& expr
->ctype
)
999 } else if (is_int_type(ctype
)) {
1002 error_die(token
->pos
, "can't increment the last enum member");
1006 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
1007 expr
->value
= lastval
;
1008 expr
->ctype
= ctype
;
1011 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1012 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
1013 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
1014 sym
->initializer
= expr
;
1015 sym
->enum_member
= 1;
1016 sym
->ctype
.base_type
= parent
;
1017 add_ptr_list(&parent
->symbol_list
, sym
);
1019 if (base_type
!= &bad_ctype
) {
1020 if (ctype
->type
== SYM_NODE
)
1021 ctype
= ctype
->ctype
.base_type
;
1022 if (ctype
->type
== SYM_ENUM
) {
1023 if (ctype
== parent
)
1026 ctype
= ctype
->ctype
.base_type
;
1030 * - if all enums are of the same type, then
1031 * the base_type is that type (two first
1033 * - if enums are of different types, they
1034 * all have to be integer types, and the
1035 * base type is at least "int_ctype".
1036 * - otherwise the base_type is "bad_ctype".
1038 if (!base_type
|| ctype
== &bad_ctype
) {
1040 } else if (ctype
== base_type
) {
1042 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
1043 base_type
= &int_ctype
;
1044 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
1045 if (!mix_bitwise
++) {
1046 warning(expr
->pos
, "mixed bitwiseness");
1048 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
1049 sparse_error(expr
->pos
, "incompatible restricted type");
1050 info(expr
->pos
, " expected: %s", show_typename(base_type
));
1051 info(expr
->pos
, " got: %s", show_typename(ctype
));
1052 base_type
= &bad_ctype
;
1053 } else if (base_type
!= &bad_ctype
) {
1054 sparse_error(token
->pos
, "bad enum definition");
1055 base_type
= &bad_ctype
;
1057 parent
->ctype
.base_type
= base_type
;
1059 if (is_int_type(base_type
)) {
1060 update_range(&range
, lastval
, ctype
);
1064 sym
->endpos
= token
->pos
;
1066 if (!match_op(token
, ','))
1068 token
= token
->next
;
1071 sparse_error(token
->pos
, "empty enum definition");
1072 base_type
= &bad_ctype
;
1074 else if (!is_int_type(base_type
))
1076 else if (type_is_ok(&uint_ctype
, range
))
1077 base_type
= &uint_ctype
;
1078 else if (type_is_ok(&int_ctype
, range
))
1079 base_type
= &int_ctype
;
1080 else if (type_is_ok(&ulong_ctype
, range
))
1081 base_type
= &ulong_ctype
;
1082 else if (type_is_ok(&long_ctype
, range
))
1083 base_type
= &long_ctype
;
1084 else if (type_is_ok(&ullong_ctype
, range
))
1085 base_type
= &ullong_ctype
;
1086 else if (type_is_ok(&llong_ctype
, range
))
1087 base_type
= &llong_ctype
;
1089 base_type
= &bad_ctype
;
1090 parent
->ctype
.base_type
= base_type
;
1091 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1092 parent
->examined
= 0;
1096 cast_enum_list(parent
->symbol_list
, base_type
);
1101 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
1103 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1104 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1106 if (!ctype
->base_type
)
1107 ctype
->base_type
= &incomplete_ctype
;
1112 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
1116 if (!match_op(token
, '(')) {
1117 sparse_error(token
->pos
, "expected '(' after typeof");
1120 if (lookup_type(token
->next
)) {
1121 token
= typename(token
->next
, &sym
, NULL
);
1122 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1123 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
1125 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1126 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1128 typeof_sym
->endpos
= token
->pos
;
1129 if (!typeof_sym
->initializer
) {
1130 sparse_error(token
->pos
, "expected expression after the '(' token");
1131 typeof_sym
= &bad_ctype
;
1133 ctx
->ctype
.base_type
= typeof_sym
;
1135 return expect(token
, ')', "after typeof");
1138 static struct token
*autotype_specifier(struct token
*token
, struct decl_state
*ctx
)
1140 ctx
->ctype
.base_type
= &autotype_ctype
;
1145 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1147 struct expression
*expr
= NULL
;
1148 if (match_op(token
, '('))
1149 token
= parens_expression(token
, &expr
, "in attribute");
1153 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1155 if (!ctx
->ctype
.alignment
)
1156 ctx
->ctype
.alignment
= 1;
1160 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1162 int alignment
= max_alignment
;
1163 struct expression
*expr
= NULL
;
1165 if (match_op(token
, '(')) {
1166 token
= parens_expression(token
, &expr
, "in attribute");
1168 alignment
= const_expression_value(expr
);
1170 if (alignment
& (alignment
-1)) {
1171 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1173 } else if (alignment
> ctx
->ctype
.alignment
)
1174 ctx
->ctype
.alignment
= alignment
;
1178 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1180 if (*mods
& mod
& ~MOD_DUP_OK
)
1181 warning(*pos
, "duplicate %s", modifier_string(mod
));
1185 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1187 apply_mod(pos
, &ctx
->modifiers
, qual
);
1190 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1192 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1196 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1198 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1202 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1205 attribute_modifier(token
, attr
, ctx
);
1209 static struct ident
*numerical_address_space(int asn
)
1215 sprintf(buff
, "<asn:%d>", asn
);
1216 return built_in_ident(buff
);
1219 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1221 struct expression
*expr
= NULL
;
1222 struct ident
*as
= NULL
;
1225 token
= expect(token
, '(', "after address_space attribute");
1226 switch (token_type(token
)) {
1228 next
= primary_expression(token
, &expr
);
1229 if (expr
->type
!= EXPR_VALUE
)
1231 as
= numerical_address_space(expr
->value
);
1241 warning(token
->pos
, "invalid address space name");
1244 if (Waddress_space
&& as
) {
1246 sparse_error(token
->pos
,
1247 "multiple address space given: %s & %s",
1248 show_as(ctx
->ctype
.as
), show_as(as
));
1251 token
= expect(next
, ')', "after address_space attribute");
1255 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1257 if (ctype
->ctype
.base_type
!= &int_type
)
1259 if (ctype
== &char_ctype
)
1261 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1265 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1267 if (ctype
->ctype
.base_type
!= &int_type
)
1269 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1273 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1275 if (ctype
->ctype
.base_type
!= &int_type
)
1277 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1281 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1283 if (ctype
->ctype
.base_type
!= &int_type
)
1285 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1289 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1291 if (ctype
->ctype
.base_type
!= &int_type
)
1293 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1297 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1299 if (ctype
->ctype
.base_type
!= &int_type
)
1301 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1305 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1307 if (ctype
->ctype
.base_type
!= &int_type
)
1309 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1313 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1315 token
= expect(token
, '(', "after mode attribute");
1316 if (token_type(token
) == TOKEN_IDENT
) {
1317 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1318 if (mode
&& mode
->op
->type
== KW_MODE
)
1319 ctx
->mode
= mode
->op
;
1321 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1322 token
= token
->next
;
1324 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1325 token
= expect(token
, ')', "after mode attribute");
1329 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1331 struct context
*context
= alloc_context();
1332 struct expression
*args
[3];
1335 token
= expect(token
, '(', "after context attribute");
1336 token
= conditional_expression(token
, &args
[0]);
1337 token
= expect(token
, ',', "after context 1st argument");
1338 token
= conditional_expression(token
, &args
[1]);
1339 if (match_op(token
, ',')) {
1340 token
= token
->next
;
1341 token
= conditional_expression(token
, &args
[2]);
1342 token
= expect(token
, ')', "after context 3rd argument");
1343 context
->context
= args
[0];
1346 token
= expect(token
, ')', "after context 2nd argument");
1348 context
->in
= get_expression_value(args
[idx
++]);
1349 context
->out
= get_expression_value(args
[idx
++]);
1350 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1354 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1356 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1357 ctx
->ctype
.base_type
->designated_init
= 1;
1359 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1363 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1365 if (Wtransparent_union
)
1366 warning(token
->pos
, "attribute __transparent_union__");
1368 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1369 ctx
->ctype
.base_type
->transparent_union
= 1;
1371 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1375 static struct token
*recover_unknown_attribute(struct token
*token
)
1377 struct expression
*expr
= NULL
;
1379 if (Wunknown_attribute
)
1380 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1381 token
= token
->next
;
1382 if (match_op(token
, '('))
1383 token
= parens_expression(token
, &expr
, "in attribute");
1387 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1389 token
= expect(token
, '(', "after attribute");
1390 token
= expect(token
, '(', "after attribute");
1393 struct ident
*attribute_name
;
1394 struct symbol
*attr
;
1396 if (eof_token(token
))
1398 if (match_op(token
, ';'))
1400 if (token_type(token
) != TOKEN_IDENT
)
1402 attribute_name
= token
->ident
;
1403 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1404 if (attr
&& attr
->op
->attribute
)
1405 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1407 token
= recover_unknown_attribute(token
);
1409 if (!match_op(token
, ','))
1411 token
= token
->next
;
1414 token
= expect(token
, ')', "after attribute");
1415 token
= expect(token
, ')', "after attribute");
1419 static const char *storage_class
[] =
1421 [STypedef
] = "typedef",
1423 [SExtern
] = "extern",
1424 [SStatic
] = "static",
1425 [SRegister
] = "register",
1426 [SForced
] = "[force]"
1429 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1431 static unsigned long mod
[SMax
] =
1434 [SExtern
] = MOD_EXTERN
,
1435 [SStatic
] = MOD_STATIC
,
1436 [SRegister
] = MOD_REGISTER
1438 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1439 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1440 return mod
[ctx
->storage_class
] | mods
;
1443 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1445 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1446 /* __thread can be used alone, or with extern or static */
1447 if (is_tls
&& (class != SStatic
&& class != SExtern
)) {
1448 sparse_error(*pos
, "__thread can only be used alone, or with "
1449 "extern or static");
1453 if (!ctx
->storage_class
) {
1454 ctx
->storage_class
= class;
1457 if (ctx
->storage_class
== class)
1458 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1460 sparse_error(*pos
, "multiple storage classes");
1463 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1465 set_storage_class(&next
->pos
, ctx
, STypedef
);
1469 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1471 set_storage_class(&next
->pos
, ctx
, SAuto
);
1475 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1477 set_storage_class(&next
->pos
, ctx
, SRegister
);
1481 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1483 set_storage_class(&next
->pos
, ctx
, SStatic
);
1487 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1489 set_storage_class(&next
->pos
, ctx
, SExtern
);
1493 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1495 /* This GCC extension can be used alone, or with extern or static */
1496 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1497 || ctx
->storage_class
== SExtern
) {
1498 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1500 sparse_error(next
->pos
, "__thread can only be used alone, or "
1501 "with extern or static");
1507 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1509 set_storage_class(&token
->pos
, ctx
, SForced
);
1513 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1515 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_INLINE
);
1519 static struct token
*noreturn_specifier(struct token
*next
, struct decl_state
*ctx
)
1521 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_NORETURN
);
1525 static struct token
*alignas_specifier(struct token
*token
, struct decl_state
*ctx
)
1529 if (!match_op(token
, '(')) {
1530 sparse_error(token
->pos
, "expected '(' after _Alignas");
1533 if (lookup_type(token
->next
)) {
1534 struct symbol
*sym
= NULL
;
1535 token
= typename(token
->next
, &sym
, NULL
);
1536 sym
= examine_symbol_type(sym
);
1537 alignment
= sym
->ctype
.alignment
;
1538 token
= expect(token
, ')', "after _Alignas(...");
1540 struct expression
*expr
= NULL
;
1541 token
= parens_expression(token
, &expr
, "after _Alignas");
1544 alignment
= const_expression_value(expr
);
1547 if (alignment
< 0) {
1548 warning(token
->pos
, "non-positive alignment");
1551 if (alignment
& (alignment
-1)) {
1552 warning(token
->pos
, "non-power-of-2 alignment");
1555 if (alignment
> ctx
->ctype
.alignment
)
1556 ctx
->ctype
.alignment
= alignment
;
1560 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1562 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1566 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1568 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1572 static struct token
*restrict_qualifier(struct token
*next
, struct decl_state
*ctx
)
1574 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_RESTRICT
);
1578 static struct token
*atomic_qualifier(struct token
*next
, struct decl_state
*ctx
)
1580 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_ATOMIC
);
1584 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1586 unsigned long mod
= thistype
->modifiers
;
1589 apply_qualifier(&pos
, ctype
, mod
);
1592 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1593 (struct ptr_list
**)&ctype
->contexts
);
1596 if (thistype
->alignment
> ctype
->alignment
)
1597 ctype
->alignment
= thistype
->alignment
;
1601 ctype
->as
= thistype
->as
;
1604 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1607 if (what
& (Set_S
| Set_T
))
1609 if (what
& Set_Char
)
1611 else if (what
& Set_Double
)
1613 else if (what
& Set_Float
)
1615 else if (what
& Set_Signed
)
1617 else if (what
& Set_Unsigned
)
1619 else if (what
& Set_Short
)
1621 else if (what
& Set_Long
)
1625 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1626 old
, show_ident(new));
1630 sparse_error(pos
, "two or more data types in declaration specifiers");
1633 static struct symbol
* const int_types
[] =
1634 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1635 static struct symbol
* const signed_types
[] =
1636 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1638 static struct symbol
* const unsigned_types
[] =
1639 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1641 static struct symbol
* const real_types
[] =
1642 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1643 static struct symbol
* const * const types
[] = {
1644 [CInt
] = int_types
+ 2,
1645 [CSInt
] = signed_types
+ 2,
1646 [CUInt
] = unsigned_types
+ 2,
1647 [CReal
] = real_types
+ 1,
1650 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1652 return types
[want_unsigned
? CUInt
: CInt
][size
];
1655 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1657 while (token_type(t
) == TOKEN_IDENT
) {
1658 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1661 if (s
->type
!= SYM_KEYWORD
)
1663 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1666 if (s
->op
->declarator
)
1667 t
= s
->op
->declarator(t
, ctx
);
1672 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1678 while (token_type(token
) == TOKEN_IDENT
) {
1679 struct symbol
*s
= lookup_symbol(token
->ident
,
1680 NS_TYPEDEF
| NS_SYMBOL
);
1681 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1683 if (s
->type
!= SYM_KEYWORD
) {
1686 seen
|= Set_S
| Set_T
;
1687 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1688 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1689 token
= token
->next
;
1692 if (s
->op
->type
& KW_SPECIFIER
) {
1693 if (seen
& s
->op
->test
) {
1694 specifier_conflict(token
->pos
,
1700 class += s
->op
->class;
1701 if (s
->op
->set
& Set_Int128
)
1703 else if (s
->op
->set
& Set_Char
)
1705 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1707 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1708 if (class == CReal
) {
1709 specifier_conflict(token
->pos
,
1717 token
= token
->next
;
1718 if (s
->op
->declarator
) // Note: this eats attributes
1719 token
= s
->op
->declarator(token
, ctx
);
1720 if (s
->op
->type
& KW_EXACT
) {
1721 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1722 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1726 if (!(seen
& Set_S
)) { /* not set explicitly? */
1727 struct symbol
*base
= &incomplete_ctype
;
1729 base
= types
[class][rank
];
1730 ctx
->ctype
.base_type
= base
;
1733 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1734 struct symbol
*type
;
1735 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1736 if (!is_int_type(ctx
->ctype
.base_type
)) {
1737 sparse_error(token
->pos
, "invalid modifier");
1740 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1741 *type
= *ctx
->ctype
.base_type
;
1742 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1743 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1744 type
->type
= SYM_RESTRICT
;
1745 ctx
->ctype
.base_type
= type
;
1746 create_fouled(type
);
1751 static struct token
*abstract_array_static_declarator(struct token
*token
, int *has_static
)
1753 while (token
->ident
== &static_ident
) {
1755 sparse_error(token
->pos
, "duplicate array static declarator");
1758 token
= token
->next
;
1764 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1766 struct expression
*expr
= NULL
;
1769 token
= abstract_array_static_declarator(token
, &has_static
);
1771 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, &__restrict___ident
, NULL
))
1772 token
= abstract_array_static_declarator(token
->next
, &has_static
);
1773 token
= parse_expression(token
, &expr
);
1774 sym
->array_size
= expr
;
1778 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1779 static struct token
*identifier_list(struct token
*, struct symbol
*);
1780 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1782 static struct token
*skip_attribute(struct token
*token
)
1784 token
= token
->next
;
1785 if (match_op(token
, '(')) {
1787 token
= token
->next
;
1788 while (depth
&& !eof_token(token
)) {
1789 if (token_type(token
) == TOKEN_SPECIAL
) {
1790 if (token
->special
== '(')
1792 else if (token
->special
== ')')
1795 token
= token
->next
;
1801 static struct token
*skip_attributes(struct token
*token
)
1803 struct symbol
*keyword
;
1805 if (token_type(token
) != TOKEN_IDENT
)
1807 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1808 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1810 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1812 token
= expect(token
->next
, '(', "after attribute");
1813 token
= expect(token
, '(', "after attribute");
1815 if (eof_token(token
))
1817 if (match_op(token
, ';'))
1819 if (token_type(token
) != TOKEN_IDENT
)
1821 token
= skip_attribute(token
);
1822 if (!match_op(token
, ','))
1824 token
= token
->next
;
1826 token
= expect(token
, ')', "after attribute");
1827 token
= expect(token
, ')', "after attribute");
1832 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1834 struct symbol
*keyword
;
1836 if (token_type(token
) != TOKEN_IDENT
)
1838 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1839 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1841 if (!(keyword
->op
->type
& keywords
))
1843 token
= keyword
->op
->declarator(token
->next
, ctx
);
1844 keywords
&= KW_ATTRIBUTE
;
1849 static int is_nested(struct token
*token
, struct token
**p
,
1850 int prefer_abstract
)
1853 * This can be either a parameter list or a grouping.
1854 * For the direct (non-abstract) case, we know if must be
1855 * a parameter list if we already saw the identifier.
1856 * For the abstract case, we know if must be a parameter
1857 * list if it is empty or starts with a type.
1859 struct token
*next
= token
->next
;
1861 *p
= next
= skip_attributes(next
);
1863 if (token_type(next
) == TOKEN_IDENT
) {
1864 if (lookup_type(next
))
1865 return !prefer_abstract
;
1869 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1876 Empty
, K_R
, Proto
, Bad_Func
,
1879 static enum kind
which_func(struct token
*token
,
1881 int prefer_abstract
)
1883 struct token
*next
= token
->next
;
1885 if (token_type(next
) == TOKEN_IDENT
) {
1886 if (lookup_type(next
))
1888 /* identifier list not in definition; complain */
1889 if (prefer_abstract
)
1891 "identifier list not in definition");
1895 if (token_type(next
) != TOKEN_SPECIAL
)
1898 if (next
->special
== ')') {
1899 /* don't complain about those */
1900 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1902 if (Wstrict_prototypes
)
1904 "non-ANSI function declaration of function '%s'",
1909 if (next
->special
== SPECIAL_ELLIPSIS
) {
1911 "variadic functions must have one named argument");
1918 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1920 struct ctype
*ctype
= &ctx
->ctype
;
1922 struct ident
**p
= ctx
->ident
;
1924 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1925 *ctx
->ident
= token
->ident
;
1926 token
= token
->next
;
1927 } else if (match_op(token
, '(') &&
1928 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1929 struct symbol
*base_type
= ctype
->base_type
;
1930 if (token
->next
!= next
)
1931 next
= handle_attributes(token
->next
, ctx
,
1933 token
= declarator(next
, ctx
);
1934 token
= expect(token
, ')', "in nested declarator");
1935 while (ctype
->base_type
!= base_type
)
1936 ctype
= &ctype
->base_type
->ctype
;
1940 if (match_op(token
, '(')) {
1941 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1943 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1944 ctype
->modifiers
|= ctx
->f_modifiers
;
1945 token
= token
->next
;
1947 token
= identifier_list(token
, fn
);
1948 else if (kind
== Proto
)
1949 token
= parameter_type_list(token
, fn
);
1950 token
= expect(token
, ')', "in function declarator");
1951 fn
->endpos
= token
->pos
;
1955 while (match_op(token
, '[')) {
1956 struct symbol
*array
;
1957 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1958 token
= abstract_array_declarator(token
->next
, array
);
1959 token
= expect(token
, ']', "in abstract_array_declarator");
1960 array
->endpos
= token
->pos
;
1961 ctype
= &array
->ctype
;
1966 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1968 while (match_op(token
,'*')) {
1969 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1970 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1971 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1972 ptr
->ctype
.as
= ctx
->ctype
.as
;
1973 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1974 ctx
->ctype
.modifiers
= 0;
1975 ctx
->ctype
.base_type
= ptr
;
1976 ctx
->ctype
.as
= NULL
;
1977 ctx
->ctype
.contexts
= NULL
;
1978 ctx
->ctype
.alignment
= 0;
1980 token
= handle_qualifiers(token
->next
, ctx
);
1981 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1986 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1988 token
= pointer(token
, ctx
);
1989 return direct_declarator(token
, ctx
);
1992 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1994 struct ctype
*ctype
= &ctx
->ctype
;
1995 struct expression
*expr
;
1996 struct symbol
*bitfield
;
1999 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
2000 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
2001 show_typename(ctype
->base_type
));
2002 // Parse this to recover gracefully.
2003 return conditional_expression(token
->next
, &expr
);
2006 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
2007 token
= conditional_expression(token
->next
, &expr
);
2008 width
= const_expression_value(expr
);
2009 bitfield
->bit_size
= width
;
2011 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
2012 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
2013 show_ident(*ctx
->ident
), width
);
2015 } else if (*ctx
->ident
) {
2016 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
2017 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
2018 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
2019 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
2020 // Valid values are either {-1;0} or {0}, depending on integer
2021 // representation. The latter makes for very efficient code...
2022 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
2024 if (Wdefault_bitfield_sign
&&
2025 bitfield_type
->type
!= SYM_ENUM
&&
2026 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
2028 // The sign of bitfields is unspecified by default.
2029 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
2032 bitfield
->bit_size
= width
;
2033 bitfield
->endpos
= token
->pos
;
2034 bitfield
->ident
= *ctx
->ident
;
2038 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
2040 struct decl_state ctx
= {.prefer_abstract
= 0};
2044 token
= declaration_specifiers(token
, &ctx
);
2045 mod
= decl_modifiers(&ctx
);
2048 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2049 ctx
.ident
= &decl
->ident
;
2051 token
= declarator(token
, &ctx
);
2052 if (match_op(token
, ':'))
2053 token
= handle_bitfield(token
, &ctx
);
2055 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2056 apply_modifiers(token
->pos
, &ctx
);
2058 decl
->ctype
= ctx
.ctype
;
2059 decl
->ctype
.modifiers
|= mod
;
2060 decl
->endpos
= token
->pos
;
2061 add_symbol(list
, decl
);
2062 if (!match_op(token
, ','))
2064 token
= token
->next
;
2070 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
2072 while (!match_op(token
, '}')) {
2073 if (match_ident(token
, &_Static_assert_ident
)) {
2074 token
= parse_static_assert(token
, NULL
);
2077 if (!match_op(token
, ';'))
2078 token
= declaration_list(token
, list
);
2079 if (!match_op(token
, ';')) {
2080 sparse_error(token
->pos
, "expected ; at end of declaration");
2083 token
= token
->next
;
2088 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
2090 struct decl_state ctx
= {.prefer_abstract
= 1};
2092 token
= declaration_specifiers(token
, &ctx
);
2093 ctx
.ident
= &sym
->ident
;
2094 token
= declarator(token
, &ctx
);
2095 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2096 apply_modifiers(token
->pos
, &ctx
);
2097 sym
->ctype
= ctx
.ctype
;
2098 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
2099 sym
->endpos
= token
->pos
;
2100 sym
->forced_arg
= ctx
.storage_class
== SForced
;
2104 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
2106 struct decl_state ctx
= {.prefer_abstract
= 1};
2108 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2110 token
= declaration_specifiers(token
, &ctx
);
2111 token
= declarator(token
, &ctx
);
2112 apply_modifiers(token
->pos
, &ctx
);
2113 sym
->ctype
= ctx
.ctype
;
2114 sym
->endpos
= token
->pos
;
2115 class = ctx
.storage_class
;
2118 if (class == SForced
) {
2124 warning(sym
->pos
, "storage class in typename (%s %s)",
2125 storage_class
[class], show_typename(sym
));
2129 static struct token
*parse_underscore_Pragma(struct token
*token
)
2134 if (!match_op(next
, '('))
2137 if (next
->pos
.type
!= TOKEN_STRING
)
2140 if (!match_op(next
, ')'))
2145 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
2147 if (match_ident(token
, &_Pragma_ident
))
2148 return parse_underscore_Pragma(token
);
2150 token
= parse_expression(token
, tree
);
2151 return expect(token
, ';', "at end of statement");
2154 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2155 struct asm_operand_list
**inout
)
2157 /* Allow empty operands */
2158 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2161 struct asm_operand
*op
= __alloc_asm_operand(0);
2162 if (match_op(token
->next
, '[') &&
2163 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2164 match_op(token
->next
->next
->next
, ']')) {
2165 op
->name
= token
->next
->next
->ident
;
2166 token
= token
->next
->next
->next
;
2168 token
= token
->next
;
2169 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2170 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2171 add_ptr_list(inout
, op
);
2172 } while (match_op(token
, ','));
2176 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2177 struct expression_list
**clobbers
)
2179 struct expression
*expr
;
2182 token
= primary_expression(token
->next
, &expr
);
2184 add_expression(clobbers
, expr
);
2185 } while (match_op(token
, ','));
2189 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2190 struct symbol_list
**labels
)
2192 struct symbol
*label
;
2195 token
= token
->next
; /* skip ':' and ',' */
2196 if (token_type(token
) != TOKEN_IDENT
)
2198 label
= label_symbol(token
, 1);
2199 add_symbol(labels
, label
);
2200 token
= token
->next
;
2201 } while (match_op(token
, ','));
2205 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2207 unsigned long mods
= 0;
2209 token
= token
->next
;
2210 stmt
->type
= STMT_ASM
;
2211 while (token_type(token
) == TOKEN_IDENT
) {
2212 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2213 if (s
&& s
->op
&& s
->op
->asm_modifier
)
2214 s
->op
->asm_modifier(token
, &mods
);
2215 else if (token
->ident
== &goto_ident
)
2216 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2217 token
= token
->next
;
2219 token
= expect(token
, '(', "after asm");
2220 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2221 if (match_op(token
, ':'))
2222 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2223 if (match_op(token
, ':'))
2224 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2225 if (match_op(token
, ':'))
2226 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2227 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2228 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2229 token
= expect(token
, ')', "after asm");
2230 return expect(token
, ';', "at end of asm-statement");
2233 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
2235 struct expression
*expr
;
2236 token
= expect(token
, '(', "after asm");
2237 token
= string_expression(token
, &expr
, "inline asm");
2238 token
= expect(token
, ')', "after asm");
2242 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2244 struct expression
*cond
= NULL
, *message
= NULL
;
2246 token
= expect(token
->next
, '(', "after _Static_assert");
2247 token
= constant_expression(token
, &cond
);
2249 sparse_error(token
->pos
, "Expected constant expression");
2250 token
= expect(token
, ',', "after conditional expression in _Static_assert");
2251 token
= string_expression(token
, &message
, "_Static_assert()");
2254 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2256 token
= expect(token
, ';', "after _Static_assert()");
2258 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
)
2259 sparse_error(cond
->pos
, "static assertion failed: %s",
2260 show_string(message
->string
));
2264 /* Make a statement out of an expression */
2265 static struct statement
*make_statement(struct expression
*expr
)
2267 struct statement
*stmt
;
2271 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2272 stmt
->expression
= expr
;
2277 * All iterators have two symbols associated with them:
2278 * the "continue" and "break" symbols, which are targets
2279 * for continue and break statements respectively.
2281 * They are in a special name-space, but they follow
2282 * all the normal visibility rules, so nested iterators
2283 * automatically work right.
2285 static void start_iterator(struct statement
*stmt
)
2287 struct symbol
*cont
, *brk
;
2289 start_block_scope(stmt
->pos
);
2290 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2291 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2292 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2293 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2295 stmt
->type
= STMT_ITERATOR
;
2296 stmt
->iterator_break
= brk
;
2297 stmt
->iterator_continue
= cont
;
2298 fn_local_symbol(brk
);
2299 fn_local_symbol(cont
);
2302 static void end_iterator(struct statement
*stmt
)
2307 static struct statement
*start_function(struct symbol
*sym
)
2310 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2312 start_function_scope(sym
->pos
);
2313 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2314 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2315 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2316 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2317 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2319 fn_local_symbol(ret
);
2321 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2327 static void end_function(struct symbol
*sym
)
2330 end_function_scope();
2334 * A "switch()" statement, like an iterator, has a
2335 * the "break" symbol associated with it. It works
2336 * exactly like the iterator break - it's the target
2337 * for any break-statements in scope, and means that
2338 * "break" handling doesn't even need to know whether
2339 * it's breaking out of an iterator or a switch.
2341 * In addition, the "case" symbol is a marker for the
2342 * case/default statements to find the switch statement
2343 * that they are associated with.
2345 static void start_switch(struct statement
*stmt
)
2347 struct symbol
*brk
, *switch_case
;
2349 start_block_scope(stmt
->pos
);
2350 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2351 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2353 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2354 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2355 switch_case
->stmt
= stmt
;
2357 stmt
->type
= STMT_SWITCH
;
2358 stmt
->switch_break
= brk
;
2359 stmt
->switch_case
= switch_case
;
2361 fn_local_symbol(brk
);
2362 fn_local_symbol(switch_case
);
2365 static void end_switch(struct statement
*stmt
)
2367 if (!stmt
->switch_case
->symbol_list
)
2368 warning(stmt
->pos
, "switch with no cases");
2372 static void add_case_statement(struct statement
*stmt
)
2374 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2378 sparse_error(stmt
->pos
, "not in switch scope");
2379 stmt
->type
= STMT_NONE
;
2382 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2383 add_symbol(&target
->symbol_list
, sym
);
2385 stmt
->case_label
= sym
;
2386 fn_local_symbol(sym
);
2389 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2391 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2394 error_die(token
->pos
, "internal error: return without a function target");
2395 stmt
->type
= STMT_RETURN
;
2396 stmt
->ret_target
= target
;
2397 return expression_statement(token
->next
, &stmt
->ret_value
);
2400 static void validate_for_loop_decl(struct symbol
*sym
)
2402 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2404 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2405 const char *name
= show_ident(sym
->ident
);
2406 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2407 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2411 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2413 struct symbol_list
*syms
;
2414 struct expression
*e1
, *e2
, *e3
;
2415 struct statement
*iterator
;
2417 start_iterator(stmt
);
2418 token
= expect(token
->next
, '(', "after 'for'");
2422 /* C99 variable declaration? */
2423 if (lookup_type(token
)) {
2424 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2426 token
= parse_expression(token
, &e1
);
2427 token
= expect(token
, ';', "in 'for'");
2429 token
= parse_expression(token
, &e2
);
2430 token
= expect(token
, ';', "in 'for'");
2431 token
= parse_expression(token
, &e3
);
2432 token
= expect(token
, ')', "in 'for'");
2433 token
= statement(token
, &iterator
);
2435 stmt
->iterator_syms
= syms
;
2436 stmt
->iterator_pre_statement
= make_statement(e1
);
2437 stmt
->iterator_pre_condition
= e2
;
2438 stmt
->iterator_post_statement
= make_statement(e3
);
2439 stmt
->iterator_post_condition
= NULL
;
2440 stmt
->iterator_statement
= iterator
;
2446 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2448 struct expression
*expr
;
2449 struct statement
*iterator
;
2451 start_iterator(stmt
);
2452 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2453 token
= statement(token
, &iterator
);
2455 stmt
->iterator_pre_condition
= expr
;
2456 stmt
->iterator_post_condition
= NULL
;
2457 stmt
->iterator_statement
= iterator
;
2463 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2465 struct expression
*expr
;
2466 struct statement
*iterator
;
2468 start_iterator(stmt
);
2469 token
= statement(token
->next
, &iterator
);
2470 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2471 token
= token
->next
;
2473 sparse_error(token
->pos
, "expected 'while' after 'do'");
2474 token
= parens_expression(token
, &expr
, "after 'do-while'");
2476 stmt
->iterator_post_condition
= expr
;
2477 stmt
->iterator_statement
= iterator
;
2480 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2481 warning(iterator
->pos
, "do-while statement is not a compound statement");
2483 return expect(token
, ';', "after statement");
2486 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2488 stmt
->type
= STMT_IF
;
2489 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2490 token
= statement(token
, &stmt
->if_true
);
2491 if (token_type(token
) != TOKEN_IDENT
)
2493 if (token
->ident
!= &else_ident
)
2495 return statement(token
->next
, &stmt
->if_false
);
2498 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2500 stmt
->type
= STMT_CASE
;
2501 token
= expect(token
, ':', "after default/case");
2502 add_case_statement(stmt
);
2503 return statement(token
, &stmt
->case_statement
);
2506 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2508 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2509 if (match_op(token
, SPECIAL_ELLIPSIS
))
2510 token
= parse_expression(token
->next
, &stmt
->case_to
);
2511 return case_statement(token
, stmt
);
2514 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2516 return case_statement(token
->next
, stmt
);
2519 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2521 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2522 stmt
->type
= STMT_GOTO
;
2523 stmt
->goto_label
= target
;
2525 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2526 return expect(token
->next
, ';', "at end of statement");
2529 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2531 stmt
->type
= STMT_SWITCH
;
2533 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2534 token
= statement(token
, &stmt
->switch_statement
);
2539 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2541 const char *id
= show_ident(ident
);
2542 sparse_error(use
, "label '%s' used outside statement expression", id
);
2543 info(def
, " label '%s' defined here", id
);
2544 current_fn
->bogus_linear
= 1;
2547 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2549 struct statement
*def
= label
->stmt
;
2552 if (!is_in_scope(def
->label_scope
, label_scope
))
2553 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2554 } else if (!label
->label_scope
) {
2555 label
->label_scope
= label_scope
;
2556 label
->label_pos
= use_pos
;
2560 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2562 stmt
->type
= STMT_GOTO
;
2563 token
= token
->next
;
2564 if (match_op(token
, '*')) {
2565 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2566 add_statement(&function_computed_goto_list
, stmt
);
2567 } else if (token_type(token
) == TOKEN_IDENT
) {
2568 struct symbol
*label
= label_symbol(token
, 1);
2569 stmt
->goto_label
= label
;
2570 check_label_usage(label
, stmt
->pos
);
2571 token
= token
->next
;
2573 sparse_error(token
->pos
, "Expected identifier or goto expression");
2575 return expect(token
, ';', "at end of statement");
2578 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2580 stmt
->type
= STMT_CONTEXT
;
2581 token
= token
->next
;
2582 token
= expect(token
, '(', "after __context__ statement");
2583 token
= assignment_expression(token
, &stmt
->expression
);
2584 if (!stmt
->expression
)
2585 unexpected(token
, "expression expected after '('");
2586 if (match_op(token
, ',')) {
2587 token
= token
->next
;
2588 stmt
->context
= stmt
->expression
;
2589 token
= assignment_expression(token
, &stmt
->expression
);
2590 if (!stmt
->expression
)
2591 unexpected(token
, "expression expected after ','");
2593 token
= expect(token
, ')', "at end of __context__ statement");
2594 return expect(token
, ';', "at end of statement");
2597 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2599 stmt
->type
= STMT_RANGE
;
2600 token
= token
->next
;
2601 token
= expect(token
, '(', "after __range__ statement");
2602 token
= assignment_expression(token
, &stmt
->range_expression
);
2603 token
= expect(token
, ',', "after range expression");
2604 token
= assignment_expression(token
, &stmt
->range_low
);
2605 token
= expect(token
, ',', "after low range");
2606 token
= assignment_expression(token
, &stmt
->range_high
);
2607 token
= expect(token
, ')', "after range statement");
2608 return expect(token
, ';', "after range statement");
2611 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2613 struct decl_state ctx
= { };
2615 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2616 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2620 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2622 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2625 if (token_type(token
) == TOKEN_IDENT
) {
2626 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2627 if (s
&& s
->op
->statement
)
2628 return s
->op
->statement(token
, stmt
);
2630 if (match_op(token
->next
, ':')) {
2631 struct symbol
*s
= label_symbol(token
, 0);
2632 token
= handle_label_attributes(token
->next
->next
, s
);
2634 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2635 // skip the label to avoid multiple definitions
2636 return statement(token
, tree
);
2638 stmt
->type
= STMT_LABEL
;
2639 stmt
->label_identifier
= s
;
2640 stmt
->label_scope
= label_scope
;
2641 if (s
->label_scope
) {
2642 if (!is_in_scope(label_scope
, s
->label_scope
))
2643 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2646 return statement(token
, &stmt
->label_statement
);
2650 if (match_op(token
, '{')) {
2651 token
= compound_statement(token
->next
, stmt
);
2652 return expect(token
, '}', "at end of compound statement");
2655 stmt
->type
= STMT_EXPRESSION
;
2656 return expression_statement(token
, &stmt
->expression
);
2659 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2660 static struct token
*label_statement(struct token
*token
)
2662 while (token_type(token
) == TOKEN_IDENT
) {
2663 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2664 /* it's block-scope, but we want label namespace */
2665 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2666 fn_local_symbol(sym
);
2667 token
= token
->next
;
2668 if (!match_op(token
, ','))
2670 token
= token
->next
;
2672 return expect(token
, ';', "at end of label declaration");
2675 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2677 int seen_statement
= 0;
2678 while (token_type(token
) == TOKEN_IDENT
&&
2679 token
->ident
== &__label___ident
)
2680 token
= label_statement(token
->next
);
2682 struct statement
* stmt
;
2683 if (eof_token(token
))
2685 if (match_op(token
, '}'))
2687 if (match_ident(token
, &_Static_assert_ident
)) {
2688 token
= parse_static_assert(token
, NULL
);
2691 if (lookup_type(token
)) {
2692 if (seen_statement
) {
2693 warning(token
->pos
, "mixing declarations and code");
2696 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2697 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2699 seen_statement
= Wdeclarationafterstatement
;
2700 token
= statement(token
, &stmt
);
2702 add_statement(list
, stmt
);
2707 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2709 struct symbol_list
**list
= &fn
->arguments
;
2711 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2712 sym
->ident
= token
->ident
;
2713 token
= token
->next
;
2714 sym
->endpos
= token
->pos
;
2715 sym
->ctype
.base_type
= &incomplete_ctype
;
2716 add_symbol(list
, sym
);
2717 if (!match_op(token
, ',') ||
2718 token_type(token
->next
) != TOKEN_IDENT
||
2719 lookup_type(token
->next
))
2721 token
= token
->next
;
2726 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2728 struct symbol_list
**list
= &fn
->arguments
;
2733 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2735 token
= token
->next
;
2739 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2740 token
= parameter_declaration(token
, sym
);
2741 if (sym
->ctype
.base_type
== &void_ctype
) {
2742 /* Special case: (void) */
2743 if (!*list
&& !sym
->ident
)
2745 warning(token
->pos
, "void parameter");
2747 add_symbol(list
, sym
);
2748 if (!match_op(token
, ','))
2750 token
= token
->next
;
2755 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2757 stmt
->type
= STMT_COMPOUND
;
2758 start_block_scope(token
->pos
);
2759 token
= statement_list(token
, &stmt
->stmts
);
2764 static struct expression
*identifier_expression(struct token
*token
)
2766 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2767 expr
->expr_ident
= token
->ident
;
2771 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2773 int idx_from
, idx_to
;
2774 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2776 idx_from
= const_expression_value(from
);
2779 idx_to
= const_expression_value(to
);
2780 if (idx_to
< idx_from
|| idx_from
< 0)
2781 warning(from
->pos
, "nonsense array initializer index range");
2783 expr
->idx_from
= idx_from
;
2784 expr
->idx_to
= idx_to
;
2788 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2790 int expect_equal
= 0;
2791 struct token
*next
= token
->next
;
2792 struct expression
**tail
= ep
;
2797 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2798 struct expression
*expr
= identifier_expression(token
);
2799 if (Wold_initializer
)
2800 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2801 token
= initializer(&expr
->ident_expression
, next
->next
);
2802 if (expr
->ident_expression
)
2807 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2808 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2809 struct expression
*expr
= identifier_expression(next
);
2811 tail
= &expr
->ident_expression
;
2814 } else if (match_op(token
, '[')) {
2815 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2816 token
= constant_expression(token
->next
, &from
);
2818 sparse_error(token
->pos
, "Expected constant expression");
2821 if (match_op(token
, SPECIAL_ELLIPSIS
))
2822 token
= constant_expression(token
->next
, &to
);
2823 expr
= index_expression(from
, to
);
2825 tail
= &expr
->idx_expression
;
2826 token
= expect(token
, ']', "at end of initializer index");
2833 if (nested
&& !expect_equal
) {
2834 if (!match_op(token
, '='))
2835 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2840 token
= expect(token
, '=', "at end of initializer index");
2842 token
= initializer(tail
, token
);
2848 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2850 struct expression
*expr
;
2853 token
= single_initializer(&expr
, token
);
2856 add_expression(list
, expr
);
2857 if (!match_op(token
, ','))
2859 token
= token
->next
;
2864 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2866 if (match_op(token
, '{')) {
2867 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2869 if (!Wuniversal_initializer
) {
2870 struct token
*next
= token
->next
;
2871 // '{ 0 }' is equivalent to '{ }' except for some
2872 // warnings, like using 0 to initialize a null-pointer.
2873 if (match_token_zero(next
)) {
2874 if (match_op(next
->next
, '}'))
2875 expr
->zero_init
= 1;
2879 token
= initializer_list(&expr
->expr_list
, token
->next
);
2880 return expect(token
, '}', "at end of initializer");
2882 return assignment_expression(token
, tree
);
2885 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2888 sparse_error(sym
->pos
, "no identifier for function argument");
2891 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2894 static int is_syscall(struct symbol
*sym
)
2900 macro
= get_macro_name(sym
->pos
);
2902 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2903 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2906 name
= sym
->ident
->name
;
2908 if (name
&& strncmp(name
, "sys_", 4) ==0)
2911 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2918 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2919 struct symbol_list
**list
)
2921 struct symbol_list
**old_symbol_list
;
2922 struct symbol
*base_type
= decl
->ctype
.base_type
;
2923 struct statement
*stmt
, **p
;
2924 struct symbol
*prev
;
2927 old_symbol_list
= function_symbol_list
;
2928 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2929 function_symbol_list
= &decl
->inline_symbol_list
;
2930 p
= &base_type
->inline_stmt
;
2932 function_symbol_list
= &decl
->symbol_list
;
2933 p
= &base_type
->stmt
;
2935 function_computed_target_list
= NULL
;
2936 function_computed_goto_list
= NULL
;
2938 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2939 if (Wexternal_function_has_definition
)
2940 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2942 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2943 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2945 stmt
= start_function(decl
);
2948 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2949 declare_argument(arg
, base_type
);
2950 } END_FOR_EACH_PTR(arg
);
2952 token
= statement_list(token
->next
, &stmt
->stmts
);
2955 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2956 add_symbol(list
, decl
);
2957 else if (is_syscall(decl
)) {
2958 add_symbol(list
, decl
);
2960 printf("parse.c decl: %s\n", decl->ident->name);
2961 char *macro = get_macro_name(decl->pos);
2962 printf("decl macro: %s\n", macro);
2965 check_declaration(decl
);
2966 decl
->definition
= decl
;
2967 prev
= decl
->same_symbol
;
2968 if (prev
&& prev
->definition
) {
2969 warning(decl
->pos
, "multiple definitions for function '%s'",
2970 show_ident(decl
->ident
));
2971 info(prev
->definition
->pos
, " the previous one is here");
2974 rebind_scope(prev
, decl
->scope
);
2975 prev
->definition
= decl
;
2976 prev
= prev
->same_symbol
;
2979 function_symbol_list
= old_symbol_list
;
2980 if (function_computed_goto_list
) {
2981 if (!function_computed_target_list
)
2982 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2984 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2985 stmt
->target_list
= function_computed_target_list
;
2986 } END_FOR_EACH_PTR(stmt
);
2989 return expect(token
, '}', "at end of function");
2992 static void promote_k_r_types(struct symbol
*arg
)
2994 struct symbol
*base
= arg
->ctype
.base_type
;
2995 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2996 arg
->ctype
.base_type
= &int_ctype
;
3000 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
3002 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
3005 FOR_EACH_PTR(real_args
, arg
) {
3006 struct symbol
*type
;
3008 /* This is quadratic in the number of arguments. We _really_ don't care */
3009 FOR_EACH_PTR(argtypes
, type
) {
3010 if (type
->ident
== arg
->ident
)
3012 } END_FOR_EACH_PTR(type
);
3013 if (Wimplicit_int
) {
3014 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'",
3015 show_ident(arg
->ident
));
3017 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
3018 type
->ident
= arg
->ident
;
3019 type
->ctype
.base_type
= &int_ctype
;
3022 /* "char" and "short" promote to "int" */
3023 promote_k_r_types(type
);
3025 arg
->ctype
= type
->ctype
;
3026 } END_FOR_EACH_PTR(arg
);
3028 FOR_EACH_PTR(argtypes
, arg
) {
3030 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
3031 } END_FOR_EACH_PTR(arg
);
3035 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
3036 struct symbol_list
**list
)
3038 struct symbol_list
*args
= NULL
;
3040 if (Wold_style_definition
)
3041 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
3044 token
= declaration_list(token
, &args
);
3045 if (!match_op(token
, ';')) {
3046 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
3049 token
= token
->next
;
3050 } while (lookup_type(token
));
3052 apply_k_r_types(args
, decl
);
3054 if (!match_op(token
, '{')) {
3055 sparse_error(token
->pos
, "expected function body");
3058 return parse_function_body(token
, decl
, list
);
3061 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
3063 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
3064 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
3065 struct statement
*stmt
;
3067 anon
->ctype
.base_type
= fn
;
3068 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
3071 token
= parse_asm_statement(token
, stmt
);
3073 // FIXME: add_symbol(list, anon);
3077 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
3078 validate_decl_t validate_decl
)
3080 struct ident
*ident
= NULL
;
3081 struct symbol
*decl
;
3082 struct decl_state ctx
= { .ident
= &ident
};
3084 struct symbol
*base_type
;
3088 if (match_ident(token
, &_Pragma_ident
))
3089 return parse_underscore_Pragma(token
);
3091 /* Top-level inline asm or static assertion? */
3092 if (token_type(token
) == TOKEN_IDENT
) {
3093 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
3094 if (s
&& s
->op
->toplevel
)
3095 return s
->op
->toplevel(token
, list
);
3098 /* Parse declaration-specifiers, if any */
3099 token
= declaration_specifiers(token
, &ctx
);
3100 mod
= decl_modifiers(&ctx
);
3101 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3102 /* Just a type declaration? */
3103 if (match_op(token
, ';')) {
3104 apply_modifiers(token
->pos
, &ctx
);
3109 token
= declarator(token
, &ctx
);
3110 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
3111 apply_modifiers(token
->pos
, &ctx
);
3113 decl
->ctype
= ctx
.ctype
;
3114 decl
->ctype
.modifiers
|= mod
;
3115 decl
->endpos
= token
->pos
;
3117 /* Just a type declaration? */
3119 warning(token
->pos
, "missing identifier in declaration");
3120 return expect(token
, ';', "at the end of type declaration");
3123 /* type define declaration? */
3124 is_typedef
= ctx
.storage_class
== STypedef
;
3126 /* Typedefs don't have meaningful storage */
3128 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3130 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3132 base_type
= decl
->ctype
.base_type
;
3135 if (base_type
&& !base_type
->ident
) {
3136 switch (base_type
->type
) {
3141 base_type
->ident
= ident
;
3147 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3148 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3149 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3150 show_ident(decl
->ident
));
3151 base_type
->ctype
.base_type
= &int_ctype
;
3153 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3154 warning(decl
->pos
, "'%s()' has implicit return type",
3155 show_ident(decl
->ident
));
3156 base_type
->ctype
.base_type
= &int_ctype
;
3158 /* apply attributes placed after the declarator */
3159 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3161 /* K&R argument declaration? */
3162 if (lookup_type(token
))
3163 return parse_k_r_arguments(token
, decl
, list
);
3164 if (match_op(token
, '{'))
3165 return parse_function_body(token
, decl
, list
);
3167 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3168 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3169 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3170 sparse_error(token
->pos
, "void declaration");
3172 if (base_type
== &incomplete_ctype
) {
3173 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3174 decl
->ctype
.base_type
= &int_ctype
;;
3178 if (!is_typedef
&& match_op(token
, '=')) {
3179 token
= initializer(&decl
->initializer
, token
->next
);
3183 validate_decl(decl
);
3185 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3186 warning(decl
->pos
, "symbol with external linkage has initializer");
3187 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3190 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3191 add_symbol(list
, decl
);
3192 fn_local_symbol(decl
);
3195 check_declaration(decl
);
3196 if (decl
->same_symbol
) {
3197 decl
->definition
= decl
->same_symbol
->definition
;
3198 decl
->op
= decl
->same_symbol
->op
;
3200 // TODO: handle -std=c89 --pedantic
3201 check_duplicates(decl
);
3206 const char *msg
= NULL
;
3207 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3208 msg
= "on non-identifier";
3209 else if (match_op(token
, ','))
3210 msg
= "on declaration list";
3211 else if (!decl
->initializer
)
3212 msg
= "without initializer";
3213 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3214 decl
->initializer
->symbol
== decl
)
3215 msg
= "on self-init var";
3217 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3218 decl
->ctype
.base_type
= &bad_ctype
;
3222 if (!match_op(token
, ','))
3225 token
= token
->next
;
3227 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3229 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
3230 token
= declarator(token
, &ctx
);
3231 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
3232 apply_modifiers(token
->pos
, &ctx
);
3233 decl
->ctype
= ctx
.ctype
;
3234 decl
->ctype
.modifiers
|= mod
;
3235 decl
->endpos
= token
->pos
;
3237 sparse_error(token
->pos
, "expected identifier name in type definition");
3242 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3244 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3246 /* Function declarations are automatically extern unless specifically static */
3247 base_type
= decl
->ctype
.base_type
;
3248 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3249 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3250 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3253 return expect(token
, ';', "at end of declaration");