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
);
54 typedef struct token
*declarator_t(struct token
*, struct symbol
*, struct decl_state
*);
56 struct_specifier
, union_specifier
, enum_specifier
,
57 attribute_specifier
, typeof_specifier
, typeof_unqual_specifier
,
58 storage_specifier
, thread_specifier
;
59 static declarator_t generic_qualifier
;
60 static declarator_t autotype_specifier
;
62 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
63 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
64 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
65 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
66 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
67 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
68 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
69 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
70 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
71 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
72 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
73 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
74 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
75 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
76 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
);
77 static struct token
*ignore_seg_gs(struct token
*token
, struct symbol_list
**unused
);
79 typedef struct token
*attr_t(struct token
*, struct symbol
*,
83 attribute_packed
, attribute_aligned
, attribute_modifier
,
86 attribute_address_space
, attribute_context
,
88 attribute_designated_init
,
89 attribute_transparent_union
, ignore_attribute
,
90 attribute_mode
, attribute_force
;
92 typedef struct symbol
*to_mode_t(struct symbol
*);
95 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
;
96 static to_mode_t to_pointer_mode
, to_word_mode
;
111 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
115 CInt
= 0, CSInt
, CUInt
, CReal
,
118 static void asm_modifier(struct token
*token
, unsigned long *mods
, unsigned long mod
)
121 warning(token
->pos
, "duplicated asm modifier");
125 static struct symbol_op typedef_op
= {
127 .declarator
= storage_specifier
,
130 static struct symbol_op inline_op
= {
132 .declarator
= generic_qualifier
,
133 .asm_modifier
= asm_modifier
,
136 static struct symbol_op noreturn_op
= {
138 .declarator
= generic_qualifier
,
141 static declarator_t alignas_specifier
;
142 static struct symbol_op alignas_op
= {
144 .declarator
= alignas_specifier
,
147 static struct symbol_op auto_op
= {
149 .declarator
= storage_specifier
,
152 static struct symbol_op register_op
= {
154 .declarator
= storage_specifier
,
157 static struct symbol_op static_op
= {
158 .type
= KW_MODIFIER
|KW_STATIC
,
159 .declarator
= storage_specifier
,
162 static struct symbol_op extern_op
= {
164 .declarator
= storage_specifier
,
167 static struct symbol_op thread_op
= {
169 .declarator
= thread_specifier
,
172 static struct symbol_op const_op
= {
173 .type
= KW_QUALIFIER
,
174 .declarator
= generic_qualifier
,
177 static struct symbol_op volatile_op
= {
178 .type
= KW_QUALIFIER
,
179 .declarator
= generic_qualifier
,
180 .asm_modifier
= asm_modifier
,
183 static struct symbol_op restrict_op
= {
184 .type
= KW_QUALIFIER
,
185 .declarator
= generic_qualifier
,
188 static struct symbol_op atomic_op
= {
189 .type
= KW_QUALIFIER
,
190 .declarator
= generic_qualifier
,
193 static struct symbol_op typeof_op
= {
194 .type
= KW_SPECIFIER
,
195 .declarator
= typeof_specifier
,
200 static struct symbol_op typeof_unqual_op
= {
201 .type
= KW_SPECIFIER
,
202 .declarator
= typeof_unqual_specifier
,
207 static struct symbol_op autotype_op
= {
208 .type
= KW_SPECIFIER
,
209 .declarator
= autotype_specifier
,
214 static struct symbol_op attribute_op
= {
215 .type
= KW_ATTRIBUTE
,
216 .declarator
= attribute_specifier
,
219 static struct symbol_op struct_op
= {
220 .type
= KW_SPECIFIER
,
221 .declarator
= struct_specifier
,
226 static struct symbol_op union_op
= {
227 .type
= KW_SPECIFIER
,
228 .declarator
= union_specifier
,
233 static struct symbol_op enum_op
= {
234 .type
= KW_SPECIFIER
,
235 .declarator
= enum_specifier
,
240 static struct symbol_op spec_op
= {
241 .type
= KW_SPECIFIER
| KW_EXACT
,
246 static struct symbol_op char_op
= {
247 .type
= KW_SPECIFIER
,
248 .test
= Set_T
|Set_Long
|Set_Short
,
249 .set
= Set_T
|Set_Char
,
253 static struct symbol_op int_op
= {
254 .type
= KW_SPECIFIER
,
256 .set
= Set_T
|Set_Int
,
259 static struct symbol_op double_op
= {
260 .type
= KW_SPECIFIER
,
261 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
262 .set
= Set_T
|Set_Double
,
266 static struct symbol_op float_op
= {
267 .type
= KW_SPECIFIER
,
268 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
269 .set
= Set_T
|Set_Float
,
273 static struct symbol_op short_op
= {
274 .type
= KW_SPECIFIER
,
275 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
279 static struct symbol_op signed_op
= {
280 .type
= KW_SPECIFIER
,
281 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
286 static struct symbol_op unsigned_op
= {
287 .type
= KW_SPECIFIER
,
288 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
293 static struct symbol_op long_op
= {
294 .type
= KW_SPECIFIER
,
295 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
299 static struct symbol_op int128_op
= {
300 .type
= KW_SPECIFIER
,
301 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
302 .set
= Set_T
|Set_Int128
|Set_Vlong
,
306 static struct symbol_op if_op
= {
307 .statement
= parse_if_statement
,
310 static struct symbol_op return_op
= {
311 .statement
= parse_return_statement
,
314 static struct symbol_op loop_iter_op
= {
315 .statement
= parse_loop_iterator
,
318 static struct symbol_op default_op
= {
319 .statement
= parse_default_statement
,
322 static struct symbol_op case_op
= {
323 .statement
= parse_case_statement
,
326 static struct symbol_op switch_op
= {
327 .statement
= parse_switch_statement
,
330 static struct symbol_op for_op
= {
331 .statement
= parse_for_statement
,
334 static struct symbol_op while_op
= {
335 .statement
= parse_while_statement
,
338 static struct symbol_op do_op
= {
339 .statement
= parse_do_statement
,
342 static struct symbol_op goto_op
= {
343 .statement
= parse_goto_statement
,
346 static struct symbol_op __context___op
= {
347 .statement
= parse_context_statement
,
348 .attribute
= attribute_context
,
351 static struct symbol_op range_op
= {
352 .statement
= parse_range_statement
,
355 static struct symbol_op asm_op
= {
357 .statement
= parse_asm_statement
,
358 .toplevel
= toplevel_asm_declaration
,
361 static struct symbol_op static_assert_op
= {
362 .toplevel
= parse_static_assert
,
365 static struct symbol_op seg_gs
= {
366 .toplevel
= ignore_seg_gs
,
369 static struct symbol_op packed_op
= {
370 .attribute
= attribute_packed
,
373 static struct symbol_op aligned_op
= {
374 .attribute
= attribute_aligned
,
377 static struct symbol_op cleanup_op
= {
378 .attribute
= attribute_cleanup
,
381 static struct symbol_op attr_mod_op
= {
382 .attribute
= attribute_modifier
,
385 static struct symbol_op attr_fun_op
= {
386 .attribute
= attribute_function
,
389 static struct symbol_op attr_bitwise_op
= {
390 .attribute
= attribute_bitwise
,
393 static struct symbol_op attr_force_op
= {
394 .attribute
= attribute_force
,
397 static struct symbol_op address_space_op
= {
398 .attribute
= attribute_address_space
,
401 static struct symbol_op mode_op
= {
402 .attribute
= attribute_mode
,
405 static struct symbol_op context_op
= {
406 .attribute
= attribute_context
,
409 static struct symbol_op designated_init_op
= {
410 .attribute
= attribute_designated_init
,
413 static struct symbol_op transparent_union_op
= {
414 .attribute
= attribute_transparent_union
,
417 static struct symbol_op ignore_attr_op
= {
418 .attribute
= ignore_attribute
,
421 static struct symbol_op mode_QI_op
= {
423 .to_mode
= to_QI_mode
426 static struct symbol_op mode_HI_op
= {
428 .to_mode
= to_HI_mode
431 static struct symbol_op mode_SI_op
= {
433 .to_mode
= to_SI_mode
436 static struct symbol_op mode_DI_op
= {
438 .to_mode
= to_DI_mode
441 static struct symbol_op mode_TI_op
= {
443 .to_mode
= to_TI_mode
446 static struct symbol_op mode_pointer_op
= {
448 .to_mode
= to_pointer_mode
451 static struct symbol_op mode_word_op
= {
453 .to_mode
= to_word_mode
457 * Define the keyword and their effects.
458 * The entries in the 'typedef' and put in NS_TYPEDEF and
459 * are automatically set as reserved keyword while the ones
460 * in the 'keyword' table are just put in NS_KEYWORD.
462 * The entries are added via the 3 macros:
463 * N() for entries with "name" only,
464 * D() for entries with "name" & "__name__",
465 * A() for entries with "name", "__name" & "__name__",
466 * U() for entries with "__name" & "__name__".
468 static struct init_keyword
{
470 struct symbol_op
*op
;
474 #define N(I, O,...) { I, O,##__VA_ARGS__ }
475 #define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
476 N("__" I "__",O,##__VA_ARGS__)
477 #define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
478 N("__" I,O,##__VA_ARGS__), \
479 N("__" I "__",O,##__VA_ARGS__)
480 #define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
481 N("__" I "__",O,##__VA_ARGS__)
482 /* Storage classes */
483 N("auto", &auto_op
, .mods
= MOD_AUTO
),
484 N("register", ®ister_op
, .mods
= MOD_REGISTER
),
485 N("static", &static_op
, .mods
= MOD_STATIC
),
486 N("extern", &extern_op
, .mods
= MOD_EXTERN
),
487 N("__thread", &thread_op
),
488 N("_Thread_local", &thread_op
),
490 A("inline", &inline_op
, .mods
= MOD_INLINE
),
493 N("typedef", &typedef_op
, .mods
= MOD_USERTYPE
),
494 A("typeof", &typeof_op
),
495 A("typeof_unqual", &typeof_unqual_op
),
496 N("__auto_type", &autotype_op
),
498 /* Type qualifiers */
499 A("const", &const_op
, .mods
= MOD_CONST
),
500 A("volatile", &volatile_op
, .mods
= MOD_VOLATILE
),
501 A("restrict", &restrict_op
, .mods
= MOD_RESTRICT
),
503 N("_Atomic", &atomic_op
, .mods
= MOD_ATOMIC
),
504 N("_Noreturn", &noreturn_op
, .mods
= MOD_NORETURN
),
505 N("_Alignas", &alignas_op
),
507 U("attribute", &attribute_op
),
509 /* Type specifiers */
510 N("struct", &struct_op
),
511 N("union", &union_op
),
514 N("void", &spec_op
, .type
= &void_ctype
),
516 N("short", &short_op
),
519 N("float", &float_op
),
520 N("double", &double_op
),
521 A("signed", &signed_op
),
522 N("unsigned", &unsigned_op
),
523 N("__int128", &int128_op
),
524 N("_Bool", &spec_op
, .type
= &bool_ctype
),
526 /* Predeclared types */
527 N("__builtin_va_list", &spec_op
, .type
= &ptr_ctype
),
528 N("__builtin_ms_va_list",&spec_op
, .type
= &ptr_ctype
),
529 N("__int128_t", &spec_op
, .type
= &sint128_ctype
),
530 N("__uint128_t", &spec_op
, .type
= &uint128_ctype
),
531 N("_Float32", &spec_op
, .type
= &float32_ctype
),
532 N("_Float32x", &spec_op
, .type
= &float32x_ctype
),
533 N("_Float64", &spec_op
, .type
= &float64_ctype
),
534 N("_Float64x", &spec_op
, .type
= &float64x_ctype
),
535 N("_Float128", &spec_op
, .type
= &float128_ctype
),
536 N("__float128", &spec_op
, .type
= &float128_ctype
),
538 N("__seg_gs", &seg_gs
),
543 N("return", &return_op
),
544 N("break", &loop_iter_op
),
545 N("continue", &loop_iter_op
),
546 N("default", &default_op
),
548 N("switch", &switch_op
),
550 N("while", &while_op
),
554 N("context", &context_op
),
555 N("__context__", &__context___op
),
556 N("__range__", &range_op
),
557 N("_Static_assert", &static_assert_op
),
560 D("packed", &packed_op
),
561 D("aligned", &aligned_op
),
562 D("cleanup", &cleanup_op
),
563 D("nocast", &attr_mod_op
, .mods
= MOD_NOCAST
),
564 D("noderef", &attr_mod_op
, .mods
= MOD_NODEREF
),
565 D("safe", &attr_mod_op
, .mods
= MOD_SAFE
),
566 D("unused", &attr_mod_op
, .mods
= MOD_UNUSED
),
567 D("externally_visible", &attr_mod_op
, .mods
= MOD_EXT_VISIBLE
),
568 D("force", &attr_force_op
),
569 D("bitwise", &attr_bitwise_op
, .mods
= MOD_BITWISE
),
570 D("address_space", &address_space_op
),
571 D("designated_init", &designated_init_op
),
572 D("transparent_union", &transparent_union_op
),
573 D("noreturn", &attr_fun_op
, .mods
= MOD_NORETURN
),
574 D("pure", &attr_fun_op
, .mods
= MOD_PURE
),
575 A("const", &attr_fun_op
, .mods
= MOD_PURE
),
576 D("gnu_inline", &attr_fun_op
, .mods
= MOD_GNU_INLINE
),
580 D("QI", &mode_QI_op
),
581 D("HI", &mode_HI_op
),
582 D("SI", &mode_SI_op
),
583 D("DI", &mode_DI_op
),
584 D("TI", &mode_TI_op
),
585 D("byte", &mode_QI_op
),
586 D("pointer", &mode_pointer_op
),
587 D("word", &mode_word_op
),
591 static const char *ignored_attributes
[] = {
593 #define GCC_ATTR(x) \
595 STRINGIFY(__##x##__),
597 #include "gcc-attr-list.h"
609 static void init_keyword(int stream
, struct init_keyword
*kw
, enum namespace ns
)
611 struct symbol
*sym
= create_symbol(stream
, kw
->name
, SYM_KEYWORD
, ns
);
612 sym
->ident
->keyword
= 1;
613 sym
->ident
->reserved
|= (ns
== NS_TYPEDEF
);
614 sym
->ctype
.modifiers
= kw
->mods
;
615 sym
->ctype
.base_type
= kw
->type
;
619 void init_parser(int stream
)
623 for (i
= 0; i
< ARRAY_SIZE(typedefs
); i
++)
624 init_keyword(stream
, &typedefs
[i
], NS_TYPEDEF
);
625 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
626 init_keyword(stream
, &keywords
[i
], NS_KEYWORD
);
628 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
629 const char * name
= ignored_attributes
[i
];
630 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
633 sym
->ident
->keyword
= 1;
634 sym
->op
= &ignore_attr_op
;
640 static struct token
*skip_to(struct token
*token
, int op
)
642 while (!match_op(token
, op
) && !eof_token(token
))
647 static struct token bad_token
= { .pos
.type
= TOKEN_BAD
};
648 struct token
*expect(struct token
*token
, int op
, const char *where
)
650 if (!match_op(token
, op
)) {
651 if (token
!= &bad_token
) {
652 bad_token
.next
= token
;
653 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
654 sparse_error(token
->pos
, "got %s", show_token(token
));
657 return skip_to(token
, op
);
664 // issue an error message on new parsing errors
665 // @token: the current token
666 // @errmsg: the error message
667 // If the current token is from a previous error, an error message
668 // has already been issued, so nothing more is done.
669 // Otherwise, @errmsg is displayed followed by the current token.
670 static void unexpected(struct token
*token
, const char *errmsg
)
672 if (token
== &bad_token
)
674 sparse_error(token
->pos
, "%s", errmsg
);
675 sparse_error(token
->pos
, "got %s", show_token(token
));
678 // Add a symbol to the list of function-local symbols
679 static void fn_local_symbol(struct symbol
*sym
)
681 if (function_symbol_list
)
682 add_symbol(function_symbol_list
, sym
);
685 struct statement
*alloc_statement(struct position pos
, int type
)
687 struct statement
*stmt
= __alloc_statement(0);
693 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
695 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
);
697 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
699 struct symbol
*ctype
;
702 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
704 sparse_error(pos
, "don't know how to apply mode to %s",
705 show_typename(ctx
->ctype
.base_type
));
707 ctx
->ctype
.base_type
= ctype
;
711 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
713 struct symbol
*sym
= alloc_symbol(pos
, type
);
715 sym
->ctype
.base_type
= ctype
->base_type
;
716 sym
->ctype
.modifiers
= ctype
->modifiers
;
718 ctype
->base_type
= sym
;
719 ctype
->modifiers
= 0;
724 * NOTE! NS_LABEL is not just a different namespace,
725 * it also ends up using function scope instead of the
726 * regular symbol scope.
728 struct symbol
*label_symbol(struct token
*token
, int used
)
730 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
732 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
733 bind_symbol(sym
, token
->ident
, NS_LABEL
);
736 fn_local_symbol(sym
);
741 static struct token
*struct_union_enum_specifier(enum type type
,
742 struct token
*token
, struct decl_state
*ctx
,
743 struct token
*(*parse
)(struct token
*, struct symbol
*))
745 struct decl_state attr
= { };
747 struct position
*repos
;
749 token
= handle_attributes(token
, &attr
);
750 if (token_type(token
) == TOKEN_IDENT
) {
751 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
753 (is_outer_scope(sym
->scope
) &&
754 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
755 // Either a new symbol, or else an out-of-scope
756 // symbol being redefined.
757 sym
= alloc_symbol(token
->pos
, type
);
758 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
760 if (sym
->type
!= type
)
761 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
762 ctx
->ctype
.base_type
= sym
;
765 if (!match_op(token
, '{'))
768 // The following test is actually wrong for empty
769 // structs, but (1) they are not C99, (2) gcc does
770 // the same thing, and (3) it's easier.
771 if (sym
->symbol_list
)
772 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
775 // Mark the structure as needing re-examination
777 } else if (match_op(token
, '{')) {
778 // private struct/union/enum type
779 sym
= alloc_symbol(token
->pos
, type
);
780 set_current_scope(sym
); // used by dissect
781 ctx
->ctype
.base_type
= sym
;
783 sparse_error(token
->pos
, "expected declaration");
784 ctx
->ctype
.base_type
= &bad_ctype
;
788 token
= parse(token
->next
, sym
);
789 token
= expect(token
, '}', "at end of specifier");
790 attr
.ctype
.base_type
= sym
;
791 token
= handle_attributes(token
, &attr
);
792 apply_ctype(token
->pos
, &sym
->ctype
, &attr
.ctype
);
793 sym
->packed
= attr
.packed
;
795 sym
->endpos
= token
->pos
;
800 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
802 struct symbol
*field
, *last
= NULL
;
804 res
= struct_declaration_list(token
, &sym
->symbol_list
);
805 FOR_EACH_PTR(sym
->symbol_list
, field
) {
807 struct symbol
*base
= field
->ctype
.base_type
;
808 if (base
&& base
->type
== SYM_BITFIELD
)
812 last
->next_subobject
= field
;
814 } END_FOR_EACH_PTR(field
);
818 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
820 return struct_declaration_list(token
, &sym
->symbol_list
);
823 static struct token
*struct_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
825 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
828 static struct token
*union_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
830 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
836 // This allow to use a shift amount as big (or bigger)
837 // than the width of the value to be shifted, in which case
838 // the result is, of course, 0.
839 static unsigned long long rshift(unsigned long long val
, unsigned int n
)
841 if (n
>= (sizeof(val
) * 8))
848 unsigned long long pos
;
851 static void update_range(struct range
*range
, unsigned long long uval
, struct symbol
*vtype
)
853 long long sval
= uval
;
855 if (is_signed_type(vtype
) && (sval
< 0)) {
856 if (sval
< range
->neg
)
859 if (uval
> range
->pos
)
864 static int type_is_ok(struct symbol
*type
, struct range range
)
866 int shift
= type
->bit_size
;
867 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
871 if (rshift(range
.pos
, shift
))
877 if (rshift(~range
.neg
, shift
))
882 static struct range
type_range(struct symbol
*type
)
885 unsigned int size
= type
->bit_size
;
886 unsigned long long max
;
889 if (is_signed_type(type
)) {
890 min
= sign_bit(size
);
894 max
= bits_mask(size
);
902 static int val_in_range(struct range
*range
, long long sval
, struct symbol
*vtype
)
904 unsigned long long uval
= sval
;
906 if (is_signed_type(vtype
) && (sval
< 0))
907 return range
->neg
<= sval
;
909 return uval
<= range
->pos
;
912 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
914 struct range irange
= type_range(&int_ctype
);
917 FOR_EACH_PTR(list
, sym
) {
918 struct expression
*expr
= sym
->initializer
;
919 struct symbol
*ctype
;
921 if (expr
->type
!= EXPR_VALUE
)
924 val
= get_expression_value(expr
);
925 if (is_int_type(ctype
) && val_in_range(&irange
, val
, ctype
)) {
926 expr
->ctype
= &int_ctype
;
929 cast_value(expr
, base_type
, expr
);
930 } END_FOR_EACH_PTR(sym
);
933 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
935 unsigned long long lastval
= 0;
936 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
937 struct range range
= { };
940 parent
->examined
= 1;
941 parent
->ctype
.base_type
= &int_ctype
;
942 while (token_type(token
) == TOKEN_IDENT
) {
943 struct expression
*expr
= NULL
;
944 struct token
*next
= token
->next
;
945 struct decl_state ctx
= { };
948 // FIXME: only 'deprecated' should be accepted
949 next
= handle_attributes(next
, &ctx
);
951 if (match_op(next
, '=')) {
952 next
= constant_expression(next
->next
, &expr
);
953 lastval
= get_expression_value(expr
);
955 if (expr
&& expr
->ctype
)
959 } else if (is_int_type(ctype
)) {
962 error_die(token
->pos
, "can't increment the last enum member");
966 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
967 expr
->value
= lastval
;
971 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
972 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
973 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
974 sym
->initializer
= expr
;
975 sym
->enum_member
= 1;
976 sym
->ctype
.base_type
= parent
;
977 add_ptr_list(&parent
->symbol_list
, sym
);
979 if (base_type
!= &bad_ctype
) {
980 if (ctype
->type
== SYM_NODE
)
981 ctype
= ctype
->ctype
.base_type
;
982 if (ctype
->type
== SYM_ENUM
) {
986 ctype
= ctype
->ctype
.base_type
;
990 * - if all enums are of the same type, then
991 * the base_type is that type (two first
993 * - if enums are of different types, they
994 * all have to be integer types, and the
995 * base type is at least "int_ctype".
996 * - otherwise the base_type is "bad_ctype".
998 if (!base_type
|| ctype
== &bad_ctype
) {
1000 } else if (ctype
== base_type
) {
1002 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
1003 base_type
= &int_ctype
;
1004 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
1005 if (!mix_bitwise
++) {
1006 warning(expr
->pos
, "mixed bitwiseness");
1008 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
1009 sparse_error(expr
->pos
, "incompatible restricted type");
1010 info(expr
->pos
, " expected: %s", show_typename(base_type
));
1011 info(expr
->pos
, " got: %s", show_typename(ctype
));
1012 base_type
= &bad_ctype
;
1013 } else if (base_type
!= &bad_ctype
) {
1014 sparse_error(token
->pos
, "bad enum definition");
1015 base_type
= &bad_ctype
;
1017 parent
->ctype
.base_type
= base_type
;
1019 if (is_int_type(base_type
)) {
1020 update_range(&range
, lastval
, ctype
);
1024 sym
->endpos
= token
->pos
;
1026 if (!match_op(token
, ','))
1028 token
= token
->next
;
1031 sparse_error(token
->pos
, "empty enum definition");
1032 base_type
= &bad_ctype
;
1034 else if (!is_int_type(base_type
))
1036 else if (type_is_ok(&uint_ctype
, range
))
1037 base_type
= &uint_ctype
;
1038 else if (type_is_ok(&int_ctype
, range
))
1039 base_type
= &int_ctype
;
1040 else if (type_is_ok(&ulong_ctype
, range
))
1041 base_type
= &ulong_ctype
;
1042 else if (type_is_ok(&long_ctype
, range
))
1043 base_type
= &long_ctype
;
1044 else if (type_is_ok(&ullong_ctype
, range
))
1045 base_type
= &ullong_ctype
;
1046 else if (type_is_ok(&llong_ctype
, range
))
1047 base_type
= &llong_ctype
;
1049 base_type
= &bad_ctype
;
1050 parent
->ctype
.base_type
= base_type
;
1051 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1052 parent
->examined
= 0;
1056 cast_enum_list(parent
->symbol_list
, base_type
);
1061 static struct token
*enum_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1063 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1064 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1066 if (!ctype
->base_type
)
1067 ctype
->base_type
= &incomplete_ctype
;
1072 static struct token
*typeof_specifier_helper(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
, bool qual
)
1075 if (!match_op(token
, '(')) {
1076 sparse_error(token
->pos
, "expected '(' after typeof");
1079 if (lookup_type(token
->next
)) {
1081 token
= typename(token
->next
, &sym
, NULL
);
1082 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1083 apply_ctype(token
->pos
, &ctx
->ctype
, &sym
->ctype
);
1085 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, qual
? SYM_TYPEOF
: SYM_TYPEOF_UNQUAL
);
1086 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1088 typeof_sym
->endpos
= token
->pos
;
1089 if (!typeof_sym
->initializer
) {
1090 sparse_error(token
->pos
, "expected expression after the '(' token");
1091 typeof_sym
= &bad_ctype
;
1093 ctx
->ctype
.base_type
= typeof_sym
;
1095 return expect(token
, ')', "after typeof");
1098 static struct token
*typeof_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1100 return typeof_specifier_helper(token
, sym
, ctx
, true);
1103 static struct token
*typeof_unqual_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1105 return typeof_specifier_helper(token
, sym
, ctx
, false);
1108 static struct token
*autotype_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1110 ctx
->ctype
.base_type
= &autotype_ctype
;
1115 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1117 struct expression
*expr
= NULL
;
1118 if (match_op(token
, '('))
1119 token
= parens_expression(token
, &expr
, "in attribute");
1123 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1125 if (!ctx
->ctype
.alignment
) {
1126 ctx
->ctype
.alignment
= 1;
1132 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1134 int alignment
= max_alignment
;
1135 struct expression
*expr
= NULL
;
1137 if (match_op(token
, '(')) {
1138 token
= parens_expression(token
, &expr
, "in attribute");
1140 alignment
= const_expression_value(expr
);
1142 if (alignment
& (alignment
-1)) {
1143 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1145 } else if (alignment
> ctx
->ctype
.alignment
)
1146 ctx
->ctype
.alignment
= alignment
;
1150 static struct token
*attribute_cleanup(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1152 struct expression
*expr
= NULL
;
1154 if (match_op(token
, '(')) {
1155 token
= token
->next
;
1156 if (match_op(token
, ')'))
1157 sparse_error(token
->pos
, "an argument is expected for attribute 'cleanup'");
1158 else if (token_type(token
) != TOKEN_IDENT
)
1159 sparse_error(token
->pos
, "argument is not an identifier");
1160 token
= primary_expression(token
, &expr
);
1161 if (expr
&& expr
->type
== EXPR_SYMBOL
)
1162 ctx
->cleanup
= expr
;
1163 return expect(token
, ')', "after attribute's argument'");
1166 sparse_error(token
->pos
, "an argument is expected for attribute 'cleanup'");
1170 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1172 if (*mods
& mod
& ~MOD_DUP_OK
)
1173 warning(*pos
, "duplicate %s", modifier_name(mod
));
1177 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1179 apply_mod(pos
, &ctx
->modifiers
, qual
);
1182 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1184 apply_mod(&token
->pos
, &ctx
->ctype
.modifiers
, attr
->ctype
.modifiers
);
1188 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1190 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1194 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1197 attribute_modifier(token
, attr
, ctx
);
1201 static struct ident
*numerical_address_space(int asn
)
1207 sprintf(buff
, "<asn:%d>", asn
);
1208 return built_in_ident(buff
);
1211 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1213 struct expression
*expr
= NULL
;
1214 struct ident
*as
= NULL
;
1217 token
= expect(token
, '(', "after address_space attribute");
1218 switch (token_type(token
)) {
1220 next
= primary_expression(token
, &expr
);
1221 if (expr
->type
!= EXPR_VALUE
)
1223 as
= numerical_address_space(expr
->value
);
1233 warning(token
->pos
, "invalid address space name");
1236 if (Waddress_space
&& as
) {
1238 sparse_error(token
->pos
,
1239 "multiple address spaces given: %s & %s",
1240 show_as(ctx
->ctype
.as
), show_as(as
));
1243 token
= expect(next
, ')', "after address_space attribute");
1247 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1249 if (ctype
->ctype
.base_type
!= &int_type
)
1251 if (ctype
== &char_ctype
)
1253 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1257 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1259 if (ctype
->ctype
.base_type
!= &int_type
)
1261 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1265 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1267 if (ctype
->ctype
.base_type
!= &int_type
)
1269 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1273 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1275 if (ctype
->ctype
.base_type
!= &int_type
)
1277 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1281 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1283 if (ctype
->ctype
.base_type
!= &int_type
)
1285 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1289 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1291 if (ctype
->ctype
.base_type
!= &int_type
)
1293 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1297 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1299 if (ctype
->ctype
.base_type
!= &int_type
)
1301 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1305 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1307 token
= expect(token
, '(', "after mode attribute");
1308 if (token_type(token
) == TOKEN_IDENT
) {
1309 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1310 if (mode
&& mode
->op
->type
& KW_MODE
)
1311 ctx
->mode
= mode
->op
;
1313 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1314 token
= token
->next
;
1316 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1317 token
= expect(token
, ')', "after mode attribute");
1321 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1323 struct context
*context
= alloc_context();
1324 struct expression
*args
[3];
1327 token
= expect(token
, '(', "after context attribute");
1328 token
= conditional_expression(token
, &args
[0]);
1329 token
= expect(token
, ',', "after context 1st argument");
1330 token
= conditional_expression(token
, &args
[1]);
1331 if (match_op(token
, ',')) {
1332 token
= token
->next
;
1333 token
= conditional_expression(token
, &args
[2]);
1334 token
= expect(token
, ')', "after context 3rd argument");
1335 context
->context
= args
[0];
1338 token
= expect(token
, ')', "after context 2nd argument");
1340 context
->in
= get_expression_value(args
[idx
++]);
1341 context
->out
= get_expression_value(args
[idx
++]);
1342 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1346 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1348 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1349 ctx
->ctype
.base_type
->designated_init
= 1;
1351 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1355 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1357 if (Wtransparent_union
)
1358 warning(token
->pos
, "attribute __transparent_union__");
1360 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1361 ctx
->ctype
.base_type
->transparent_union
= 1;
1363 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1367 static struct token
*recover_unknown_attribute(struct token
*token
)
1369 struct expression
*expr
= NULL
;
1371 if (Wunknown_attribute
)
1372 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1373 token
= token
->next
;
1374 if (match_op(token
, '('))
1375 token
= parens_expression(token
, &expr
, "in attribute");
1379 static struct token
*attribute_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1381 token
= expect(token
, '(', "after attribute");
1382 token
= expect(token
, '(', "after attribute");
1384 while (token_type(token
) == TOKEN_IDENT
) {
1385 struct symbol
*attr
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1386 if (attr
&& attr
->op
->attribute
)
1387 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1389 token
= recover_unknown_attribute(token
);
1391 if (!match_op(token
, ','))
1393 token
= token
->next
;
1396 token
= expect(token
, ')', "after attribute");
1397 token
= expect(token
, ')', "after attribute");
1401 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1403 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1404 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1405 return ctx
->storage_class
| mods
;
1408 static struct token
*storage_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1410 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1411 unsigned long class = sym
->ctype
.modifiers
;
1412 const char *storage
= modifier_name(class);
1414 /* __thread can be used alone, or with extern or static */
1415 if (is_tls
&& (class & ~(MOD_STATIC
|MOD_EXTERN
)))
1416 sparse_error(next
->pos
, "__thread cannot be used with '%s'", storage
);
1417 else if (!ctx
->storage_class
)
1418 ctx
->storage_class
= class;
1419 else if (ctx
->storage_class
== class)
1420 sparse_error(next
->pos
, "duplicate %s", storage
);
1422 sparse_error(next
->pos
, "multiple storage classes");
1426 static struct token
*thread_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1428 /* This GCC extension can be used alone, or with extern or static */
1429 if (!(ctx
->storage_class
& ~(MOD_STATIC
|MOD_EXTERN
))) {
1430 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1432 sparse_error(next
->pos
, "__thread cannot be used with '%s'",
1433 modifier_name(ctx
->storage_class
));
1439 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1445 static struct token
*alignas_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1449 if (!match_op(token
, '(')) {
1450 sparse_error(token
->pos
, "expected '(' after _Alignas");
1453 if (lookup_type(token
->next
)) {
1454 struct symbol
*sym
= NULL
;
1455 token
= typename(token
->next
, &sym
, NULL
);
1456 sym
= examine_symbol_type(sym
);
1457 alignment
= sym
->ctype
.alignment
;
1458 token
= expect(token
, ')', "after _Alignas(...");
1460 struct expression
*expr
= NULL
;
1461 token
= parens_expression(token
, &expr
, "after _Alignas");
1464 alignment
= const_expression_value(expr
);
1467 if (alignment
< 0) {
1468 warning(token
->pos
, "non-positive alignment");
1471 if (alignment
& (alignment
-1)) {
1472 warning(token
->pos
, "non-power-of-2 alignment");
1475 if (alignment
> ctx
->ctype
.alignment
)
1476 ctx
->ctype
.alignment
= alignment
;
1480 static struct token
*generic_qualifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1482 apply_qualifier(&next
->pos
, &ctx
->ctype
, sym
->ctype
.modifiers
);
1486 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
)
1488 unsigned long mod
= src
->modifiers
;
1491 apply_qualifier(&pos
, dst
, mod
);
1494 concat_ptr_list((struct ptr_list
*)src
->contexts
,
1495 (struct ptr_list
**)&dst
->contexts
);
1498 if (src
->alignment
> dst
->alignment
)
1499 dst
->alignment
= src
->alignment
;
1506 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1509 if (what
& (Set_S
| Set_T
))
1511 if (what
& Set_Char
)
1513 else if (what
& Set_Double
)
1515 else if (what
& Set_Float
)
1517 else if (what
& Set_Signed
)
1519 else if (what
& Set_Unsigned
)
1521 else if (what
& Set_Short
)
1523 else if (what
& Set_Long
)
1527 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1528 old
, show_ident(new));
1532 sparse_error(pos
, "two or more data types in declaration specifiers");
1535 static struct symbol
* const int_types
[] =
1536 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1537 static struct symbol
* const signed_types
[] =
1538 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1540 static struct symbol
* const unsigned_types
[] =
1541 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1543 static struct symbol
* const real_types
[] =
1544 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1545 static struct symbol
* const * const types
[] = {
1546 [CInt
] = int_types
+ 2,
1547 [CSInt
] = signed_types
+ 2,
1548 [CUInt
] = unsigned_types
+ 2,
1549 [CReal
] = real_types
+ 1,
1552 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1554 return types
[want_unsigned
? CUInt
: CInt
][size
];
1557 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1559 while (token_type(t
) == TOKEN_IDENT
) {
1560 struct symbol
*s
= lookup_keyword(t
->ident
, NS_TYPEDEF
);
1563 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1566 if (s
->op
->declarator
)
1567 t
= s
->op
->declarator(t
, s
, ctx
);
1572 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1578 while (token_type(token
) == TOKEN_IDENT
) {
1579 struct symbol
*s
= lookup_symbol(token
->ident
,
1580 NS_TYPEDEF
| NS_SYMBOL
);
1581 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1583 if (s
->type
!= SYM_KEYWORD
) {
1586 seen
|= Set_S
| Set_T
;
1587 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1588 apply_ctype(token
->pos
, &ctx
->ctype
, &s
->ctype
);
1589 token
= token
->next
;
1592 if (s
->op
->type
& KW_SPECIFIER
) {
1593 if (seen
& s
->op
->test
) {
1594 specifier_conflict(token
->pos
,
1600 class += s
->op
->class;
1601 if (s
->op
->set
& Set_Int128
)
1603 else if (s
->op
->set
& Set_Char
)
1605 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1607 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1608 if (class == CReal
) {
1609 specifier_conflict(token
->pos
,
1617 token
= token
->next
;
1618 if (s
->op
->declarator
) // Note: this eats attributes
1619 token
= s
->op
->declarator(token
, s
, ctx
);
1620 if (s
->op
->type
& KW_EXACT
) {
1621 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1622 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1626 if (!(seen
& Set_S
)) { /* not set explicitly? */
1627 struct symbol
*base
= &incomplete_ctype
;
1629 base
= types
[class][rank
];
1630 ctx
->ctype
.base_type
= base
;
1633 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1634 struct symbol
*type
;
1635 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1636 if (!is_int_type(ctx
->ctype
.base_type
)) {
1637 sparse_error(token
->pos
, "invalid modifier");
1640 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1641 *type
= *ctx
->ctype
.base_type
;
1642 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1643 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1644 type
->type
= SYM_RESTRICT
;
1645 ctx
->ctype
.base_type
= type
;
1646 create_fouled(type
);
1651 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1653 struct expression
*expr
= NULL
;
1656 while (token_type(token
) == TOKEN_IDENT
) {
1657 struct symbol
*sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1658 if (!sym
|| !(sym
->op
->type
& (KW_STATIC
|KW_QUALIFIER
)))
1660 if (has_static
&& (sym
->op
->type
& KW_STATIC
))
1661 sparse_error(token
->pos
, "duplicate array static declarator");
1662 has_static
|= (sym
->op
->type
& KW_STATIC
);
1663 token
= token
->next
;
1665 if (match_op(token
, '*') && match_op(token
->next
, ']')) {
1666 // FIXME: '[*]' is treated like '[]'
1667 token
= token
->next
;
1669 token
= assignment_expression(token
, &expr
);
1671 sym
->array_size
= expr
;
1675 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1676 static struct token
*identifier_list(struct token
*, struct symbol
*);
1677 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1679 static struct token
*handle_asm_name(struct token
*token
, struct decl_state
*ctx
)
1681 struct expression
*expr
;
1682 struct symbol
*keyword
;
1684 if (token_type(token
) != TOKEN_IDENT
)
1686 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1689 if (!(keyword
->op
->type
& KW_ASM
))
1692 token
= token
->next
;
1693 token
= expect(token
, '(', "after asm");
1694 token
= string_expression(token
, &expr
, "asm name");
1695 token
= expect(token
, ')', "after asm");
1700 // test if @token is '__attribute__' (or one of its variant)
1701 static bool match_attribute(struct token
*token
)
1705 if (token_type(token
) != TOKEN_IDENT
)
1707 sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1708 if (!sym
|| !sym
->op
)
1710 return sym
->op
->type
& KW_ATTRIBUTE
;
1713 static struct token
*skip_attribute(struct token
*token
)
1715 token
= token
->next
;
1716 if (match_op(token
, '(')) {
1718 token
= token
->next
;
1719 while (depth
&& !eof_token(token
)) {
1720 if (token_type(token
) == TOKEN_SPECIAL
) {
1721 if (token
->special
== '(')
1723 else if (token
->special
== ')')
1726 token
= token
->next
;
1732 static struct token
*skip_attributes(struct token
*token
)
1734 while (match_attribute(token
)) {
1735 token
= expect(token
->next
, '(', "after attribute");
1736 token
= expect(token
, '(', "after attribute");
1737 while (token_type(token
) == TOKEN_IDENT
) {
1738 token
= skip_attribute(token
);
1739 if (!match_op(token
, ','))
1741 token
= token
->next
;
1743 token
= expect(token
, ')', "after attribute");
1744 token
= expect(token
, ')', "after attribute");
1749 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
)
1751 while (match_attribute(token
))
1752 token
= attribute_specifier(token
->next
, NULL
, ctx
);
1756 static int is_nested(struct token
*token
, struct token
**p
,
1757 int prefer_abstract
)
1760 * This can be either a parameter list or a grouping.
1761 * For the direct (non-abstract) case, we know if must be
1762 * a parameter list if we already saw the identifier.
1763 * For the abstract case, we know if must be a parameter
1764 * list if it is empty or starts with a type.
1766 struct token
*next
= token
->next
;
1768 *p
= next
= skip_attributes(next
);
1770 if (token_type(next
) == TOKEN_IDENT
) {
1771 if (lookup_type(next
))
1772 return !prefer_abstract
;
1776 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1783 Empty
, K_R
, Proto
, Bad_Func
,
1786 static enum kind
which_func(struct token
*token
,
1788 int prefer_abstract
)
1790 struct token
*next
= token
->next
;
1792 if (token_type(next
) == TOKEN_IDENT
) {
1793 if (lookup_type(next
))
1795 /* identifier list not in definition; complain */
1796 if (prefer_abstract
)
1798 "identifier list not in definition");
1802 if (token_type(next
) != TOKEN_SPECIAL
)
1805 if (next
->special
== ')') {
1806 /* don't complain about those */
1807 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1809 if (Wstrict_prototypes
)
1811 "non-ANSI function declaration of function '%s'",
1816 if (next
->special
== SPECIAL_ELLIPSIS
) {
1818 "variadic functions must have one named argument");
1825 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1827 struct ctype
*ctype
= &ctx
->ctype
;
1829 struct ident
**p
= ctx
->ident
;
1831 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1832 *ctx
->ident
= token
->ident
;
1833 token
= token
->next
;
1834 } else if (match_op(token
, '(') &&
1835 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1836 struct symbol
*base_type
= ctype
->base_type
;
1837 if (token
->next
!= next
)
1838 next
= handle_attributes(token
->next
, ctx
);
1839 token
= declarator(next
, ctx
);
1840 token
= expect(token
, ')', "in nested declarator");
1841 while (ctype
->base_type
!= base_type
)
1842 ctype
= &ctype
->base_type
->ctype
;
1846 if (match_op(token
, '(')) {
1847 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1849 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1850 ctype
->modifiers
|= ctx
->f_modifiers
;
1851 token
= token
->next
;
1853 token
= identifier_list(token
, fn
);
1854 else if (kind
== Proto
)
1855 token
= parameter_type_list(token
, fn
);
1856 token
= expect(token
, ')', "in function declarator");
1857 fn
->endpos
= token
->pos
;
1861 while (match_op(token
, '[')) {
1862 struct symbol
*array
;
1863 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1864 token
= abstract_array_declarator(token
->next
, array
);
1865 token
= expect(token
, ']', "in abstract_array_declarator");
1866 array
->endpos
= token
->pos
;
1867 ctype
= &array
->ctype
;
1872 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1874 while (match_op(token
,'*')) {
1875 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1876 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1877 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1878 ptr
->ctype
.as
= ctx
->ctype
.as
;
1879 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1880 ctx
->ctype
.modifiers
= 0;
1881 ctx
->ctype
.base_type
= ptr
;
1882 ctx
->ctype
.as
= NULL
;
1883 ctx
->ctype
.contexts
= NULL
;
1884 ctx
->ctype
.alignment
= 0;
1886 token
= handle_qualifiers(token
->next
, ctx
);
1887 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1892 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1894 token
= pointer(token
, ctx
);
1895 return direct_declarator(token
, ctx
);
1898 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1900 struct ctype
*ctype
= &ctx
->ctype
;
1901 struct expression
*expr
;
1902 struct symbol
*bitfield
;
1905 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1906 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1907 show_typename(ctype
->base_type
));
1908 // Parse this to recover gracefully.
1909 return conditional_expression(token
->next
, &expr
);
1912 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1913 token
= conditional_expression(token
->next
, &expr
);
1914 width
= const_expression_value(expr
);
1915 bitfield
->bit_size
= width
;
1917 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
1918 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
1919 show_ident(*ctx
->ident
), width
);
1921 } else if (*ctx
->ident
) {
1922 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1923 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1924 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1925 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1926 // Valid values are either {-1;0} or {0}, depending on integer
1927 // representation. The latter makes for very efficient code...
1928 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1930 if (Wdefault_bitfield_sign
&&
1931 bitfield_type
->type
!= SYM_ENUM
&&
1932 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1934 // The sign of bitfields is unspecified by default.
1935 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1938 bitfield
->bit_size
= width
;
1939 bitfield
->endpos
= token
->pos
;
1940 bitfield
->ident
= *ctx
->ident
;
1944 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1946 struct decl_state ctx
= {.prefer_abstract
= 0};
1950 token
= declaration_specifiers(token
, &ctx
);
1951 mod
= decl_modifiers(&ctx
);
1954 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1956 ctx
.ident
= &decl
->ident
;
1958 token
= declarator(token
, &ctx
);
1959 if (match_op(token
, ':'))
1960 token
= handle_bitfield(token
, &ctx
);
1962 token
= handle_attributes(token
, &ctx
);
1963 apply_modifiers(token
->pos
, &ctx
);
1965 decl
->ctype
= ctx
.ctype
;
1966 decl
->ctype
.modifiers
|= mod
;
1967 decl
->cleanup
= ctx
.cleanup
;
1968 decl
->endpos
= token
->pos
;
1969 add_symbol(list
, decl
);
1970 if (!match_op(token
, ','))
1972 token
= token
->next
;
1978 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1980 while (!match_op(token
, '}')) {
1981 if (match_ident(token
, &_Static_assert_ident
)) {
1982 token
= parse_static_assert(token
, NULL
);
1985 if (!match_op(token
, ';'))
1986 token
= declaration_list(token
, list
);
1987 if (!match_op(token
, ';')) {
1988 sparse_error(token
->pos
, "expected ; at end of declaration");
1991 token
= token
->next
;
1996 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1998 struct decl_state ctx
= {.prefer_abstract
= 1};
2000 token
= declaration_specifiers(token
, &ctx
);
2001 ctx
.ident
= &sym
->ident
;
2002 token
= declarator(token
, &ctx
);
2003 token
= handle_attributes(token
, &ctx
);
2004 apply_modifiers(token
->pos
, &ctx
);
2005 sym
->ctype
= ctx
.ctype
;
2006 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
2007 sym
->endpos
= token
->pos
;
2008 sym
->forced_arg
= ctx
.forced
;
2012 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
2014 struct decl_state ctx
= {.prefer_abstract
= 1};
2015 unsigned long class;
2016 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2018 token
= declaration_specifiers(token
, &ctx
);
2019 token
= declarator(token
, &ctx
);
2020 apply_modifiers(token
->pos
, &ctx
);
2021 sym
->ctype
= ctx
.ctype
;
2022 sym
->cleanup
= ctx
.cleanup
;
2023 sym
->endpos
= token
->pos
;
2024 class = ctx
.storage_class
;
2026 *forced
= ctx
.forced
;
2028 warning(sym
->pos
, "storage class in typename (%s%s)",
2029 modifier_string(class), show_typename(sym
));
2033 static struct token
*parse_underscore_Pragma(struct token
*token
)
2038 if (!match_op(next
, '('))
2041 if (next
->pos
.type
!= TOKEN_STRING
)
2044 if (!match_op(next
, ')'))
2049 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
2051 if (match_ident(token
, &_Pragma_ident
))
2052 return parse_underscore_Pragma(token
);
2054 token
= parse_expression(token
, tree
);
2055 return expect(token
, ';', "at end of statement");
2058 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2059 struct asm_operand_list
**inout
)
2061 /* Allow empty operands */
2062 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2065 struct asm_operand
*op
= __alloc_asm_operand(0);
2066 if (match_op(token
->next
, '[') &&
2067 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2068 match_op(token
->next
->next
->next
, ']')) {
2069 op
->name
= token
->next
->next
->ident
;
2070 token
= token
->next
->next
->next
;
2072 token
= token
->next
;
2073 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2074 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2075 add_ptr_list(inout
, op
);
2076 } while (match_op(token
, ','));
2080 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2081 struct expression_list
**clobbers
)
2083 struct expression
*expr
;
2086 token
= primary_expression(token
->next
, &expr
);
2088 add_expression(clobbers
, expr
);
2089 } while (match_op(token
, ','));
2093 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2094 struct symbol_list
**labels
)
2096 struct symbol
*label
;
2099 token
= token
->next
; /* skip ':' and ',' */
2100 if (token_type(token
) != TOKEN_IDENT
)
2102 label
= label_symbol(token
, 1);
2103 add_symbol(labels
, label
);
2104 token
= token
->next
;
2105 } while (match_op(token
, ','));
2109 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2111 unsigned long mods
= 0;
2113 token
= token
->next
;
2114 stmt
->type
= STMT_ASM
;
2115 while (token_type(token
) == TOKEN_IDENT
) {
2116 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2117 if (s
&& s
->op
->asm_modifier
)
2118 s
->op
->asm_modifier(token
, &mods
, s
->ctype
.modifiers
);
2119 else if (token
->ident
== &goto_ident
)
2120 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2121 token
= token
->next
;
2123 token
= expect(token
, '(', "after asm");
2124 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2125 if (match_op(token
, ':'))
2126 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2127 if (match_op(token
, ':'))
2128 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2129 if (match_op(token
, ':'))
2130 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2131 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2132 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2133 token
= expect(token
, ')', "after asm");
2134 return expect(token
, ';', "at end of asm-statement");
2137 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2139 struct expression
*cond
= NULL
, *message
= NULL
;
2141 token
= expect(token
->next
, '(', "after _Static_assert");
2142 token
= constant_expression(token
, &cond
);
2144 sparse_error(token
->pos
, "Expected constant expression");
2145 if (match_op(token
, ',')) {
2146 token
= token
->next
;
2147 token
= string_expression(token
, &message
, "_Static_assert()");
2151 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2152 token
= expect(token
, ';', "after _Static_assert()");
2154 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
) {
2155 const char *sep
= "", *msg
= "";
2159 msg
= show_string(message
->string
);
2161 sparse_error(cond
->pos
, "static assertion failed%s%s", sep
, msg
);
2167 static struct token
*ignore_seg_gs(struct token
*token
, struct symbol_list
**unused
)
2172 /* Make a statement out of an expression */
2173 static struct statement
*make_statement(struct expression
*expr
)
2175 struct statement
*stmt
;
2179 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2180 stmt
->expression
= expr
;
2185 * All iterators have two symbols associated with them:
2186 * the "continue" and "break" symbols, which are targets
2187 * for continue and break statements respectively.
2189 * They are in a special name-space, but they follow
2190 * all the normal visibility rules, so nested iterators
2191 * automatically work right.
2193 static void start_iterator(struct statement
*stmt
)
2195 struct symbol
*cont
, *brk
;
2197 start_block_scope(stmt
->pos
);
2198 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2199 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2200 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2201 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2203 stmt
->type
= STMT_ITERATOR
;
2204 stmt
->iterator_break
= brk
;
2205 stmt
->iterator_continue
= cont
;
2206 fn_local_symbol(brk
);
2207 fn_local_symbol(cont
);
2210 static void end_iterator(struct statement
*stmt
)
2215 static struct statement
*start_function(struct symbol
*sym
)
2218 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2220 start_function_scope(sym
->pos
);
2221 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2222 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2223 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2224 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2225 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2227 fn_local_symbol(ret
);
2229 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2235 static void end_function(struct symbol
*sym
)
2238 end_function_scope();
2242 * A "switch()" statement, like an iterator, has a
2243 * the "break" symbol associated with it. It works
2244 * exactly like the iterator break - it's the target
2245 * for any break-statements in scope, and means that
2246 * "break" handling doesn't even need to know whether
2247 * it's breaking out of an iterator or a switch.
2249 * In addition, the "case" symbol is a marker for the
2250 * case/default statements to find the switch statement
2251 * that they are associated with.
2253 static void start_switch(struct statement
*stmt
)
2255 struct symbol
*brk
, *switch_case
;
2257 start_block_scope(stmt
->pos
);
2258 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2259 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2261 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2262 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2263 switch_case
->stmt
= stmt
;
2265 stmt
->type
= STMT_SWITCH
;
2266 stmt
->switch_break
= brk
;
2267 stmt
->switch_case
= switch_case
;
2269 fn_local_symbol(brk
);
2270 fn_local_symbol(switch_case
);
2273 static void end_switch(struct statement
*stmt
)
2275 if (!stmt
->switch_case
->symbol_list
)
2276 warning(stmt
->pos
, "switch with no cases");
2280 static void add_case_statement(struct statement
*stmt
)
2282 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2286 sparse_error(stmt
->pos
, "not in switch scope");
2287 stmt
->type
= STMT_NONE
;
2290 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2291 add_symbol(&target
->symbol_list
, sym
);
2293 stmt
->case_label
= sym
;
2294 fn_local_symbol(sym
);
2297 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2299 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2302 error_die(token
->pos
, "internal error: return without a function target");
2303 stmt
->type
= STMT_RETURN
;
2304 stmt
->ret_target
= target
;
2305 return expression_statement(token
->next
, &stmt
->ret_value
);
2308 static void validate_for_loop_decl(struct symbol
*sym
)
2310 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2312 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2313 const char *name
= show_ident(sym
->ident
);
2314 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2315 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2319 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2321 struct symbol_list
*syms
;
2322 struct expression
*e1
, *e2
, *e3
;
2323 struct statement
*iterator
;
2325 start_iterator(stmt
);
2326 token
= expect(token
->next
, '(', "after 'for'");
2330 /* C99 variable declaration? */
2331 if (lookup_type(token
)) {
2332 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2334 token
= parse_expression(token
, &e1
);
2335 token
= expect(token
, ';', "in 'for'");
2337 token
= parse_expression(token
, &e2
);
2338 token
= expect(token
, ';', "in 'for'");
2339 token
= parse_expression(token
, &e3
);
2340 token
= expect(token
, ')', "in 'for'");
2341 token
= statement(token
, &iterator
);
2343 stmt
->iterator_syms
= syms
;
2344 stmt
->iterator_pre_statement
= make_statement(e1
);
2345 stmt
->iterator_pre_condition
= e2
;
2346 stmt
->iterator_post_statement
= make_statement(e3
);
2347 stmt
->iterator_post_condition
= NULL
;
2348 stmt
->iterator_statement
= iterator
;
2354 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2356 struct expression
*expr
;
2357 struct statement
*iterator
;
2359 start_iterator(stmt
);
2360 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2361 token
= statement(token
, &iterator
);
2363 stmt
->iterator_pre_condition
= expr
;
2364 stmt
->iterator_post_condition
= NULL
;
2365 stmt
->iterator_statement
= iterator
;
2371 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2373 struct expression
*expr
;
2374 struct statement
*iterator
;
2376 start_iterator(stmt
);
2377 token
= statement(token
->next
, &iterator
);
2378 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2379 token
= token
->next
;
2381 sparse_error(token
->pos
, "expected 'while' after 'do'");
2382 token
= parens_expression(token
, &expr
, "after 'do-while'");
2384 stmt
->iterator_post_condition
= expr
;
2385 stmt
->iterator_statement
= iterator
;
2388 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2389 warning(iterator
->pos
, "do-while statement is not a compound statement");
2391 return expect(token
, ';', "after statement");
2394 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2396 stmt
->type
= STMT_IF
;
2397 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2398 token
= statement(token
, &stmt
->if_true
);
2399 if (token_type(token
) != TOKEN_IDENT
)
2401 if (token
->ident
!= &else_ident
)
2403 return statement(token
->next
, &stmt
->if_false
);
2406 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2408 stmt
->type
= STMT_CASE
;
2409 token
= expect(token
, ':', "after default/case");
2410 add_case_statement(stmt
);
2411 if (match_op(token
, '}')) {
2412 warning(token
->pos
, "statement expected after case label");
2413 stmt
->case_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2416 return statement(token
, &stmt
->case_statement
);
2419 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2421 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2422 if (match_op(token
, SPECIAL_ELLIPSIS
))
2423 token
= parse_expression(token
->next
, &stmt
->case_to
);
2424 return case_statement(token
, stmt
);
2427 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2429 return case_statement(token
->next
, stmt
);
2432 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2434 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2435 stmt
->type
= STMT_GOTO
;
2436 stmt
->goto_label
= target
;
2438 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2439 return expect(token
->next
, ';', "at end of statement");
2442 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2444 stmt
->type
= STMT_SWITCH
;
2446 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2447 token
= statement(token
, &stmt
->switch_statement
);
2452 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2454 const char *id
= show_ident(ident
);
2455 sparse_error(use
, "label '%s' used outside statement expression", id
);
2456 info(def
, " label '%s' defined here", id
);
2457 current_fn
->bogus_linear
= 1;
2460 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2462 struct statement
*def
= label
->stmt
;
2465 if (!is_in_scope(def
->label_scope
, label_scope
))
2466 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2467 } else if (!label
->label_scope
) {
2468 label
->label_scope
= label_scope
;
2469 label
->label_pos
= use_pos
;
2473 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2475 stmt
->type
= STMT_GOTO
;
2476 token
= token
->next
;
2477 if (match_op(token
, '*')) {
2478 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2479 add_statement(&function_computed_goto_list
, stmt
);
2480 } else if (token_type(token
) == TOKEN_IDENT
) {
2481 struct symbol
*label
= label_symbol(token
, 1);
2482 stmt
->goto_label
= label
;
2483 check_label_usage(label
, stmt
->pos
);
2484 token
= token
->next
;
2486 sparse_error(token
->pos
, "Expected identifier or goto expression");
2488 return expect(token
, ';', "at end of statement");
2491 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2493 stmt
->type
= STMT_CONTEXT
;
2494 token
= token
->next
;
2495 token
= expect(token
, '(', "after __context__ statement");
2496 token
= assignment_expression(token
, &stmt
->expression
);
2497 if (!stmt
->expression
)
2498 unexpected(token
, "expression expected after '('");
2499 if (match_op(token
, ',')) {
2500 token
= token
->next
;
2501 stmt
->context
= stmt
->expression
;
2502 token
= assignment_expression(token
, &stmt
->expression
);
2503 if (!stmt
->expression
)
2504 unexpected(token
, "expression expected after ','");
2506 token
= expect(token
, ')', "at end of __context__ statement");
2507 return expect(token
, ';', "at end of statement");
2510 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2512 stmt
->type
= STMT_RANGE
;
2513 token
= token
->next
;
2514 token
= expect(token
, '(', "after __range__ statement");
2515 token
= assignment_expression(token
, &stmt
->range_expression
);
2516 token
= expect(token
, ',', "after range expression");
2517 token
= assignment_expression(token
, &stmt
->range_low
);
2518 token
= expect(token
, ',', "after low range");
2519 token
= assignment_expression(token
, &stmt
->range_high
);
2520 token
= expect(token
, ')', "after range statement");
2521 return expect(token
, ';', "after range statement");
2524 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2526 struct decl_state ctx
= { };
2528 token
= handle_attributes(token
, &ctx
);
2529 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2533 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2535 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2538 if (token_type(token
) == TOKEN_IDENT
) {
2539 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2540 if (s
&& s
->op
->statement
)
2541 return s
->op
->statement(token
, stmt
);
2543 if (match_op(token
->next
, ':')) {
2544 struct symbol
*s
= label_symbol(token
, 0);
2545 token
= handle_label_attributes(token
->next
->next
, s
);
2547 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2548 // skip the label to avoid multiple definitions
2549 return statement(token
, tree
);
2551 stmt
->type
= STMT_LABEL
;
2552 stmt
->label_identifier
= s
;
2553 stmt
->label_scope
= label_scope
;
2554 if (s
->label_scope
) {
2555 if (!is_in_scope(label_scope
, s
->label_scope
))
2556 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2559 if (match_op(token
, '}')) {
2560 warning(token
->pos
, "statement expected after label");
2561 stmt
->label_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2564 return statement(token
, &stmt
->label_statement
);
2568 if (match_op(token
, '{')) {
2569 token
= compound_statement(token
->next
, stmt
);
2570 return expect(token
, '}', "at end of compound statement");
2573 stmt
->type
= STMT_EXPRESSION
;
2574 return expression_statement(token
, &stmt
->expression
);
2577 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2578 static struct token
*label_statement(struct token
*token
)
2580 while (token_type(token
) == TOKEN_IDENT
) {
2581 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2582 /* it's block-scope, but we want label namespace */
2583 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2584 fn_local_symbol(sym
);
2585 token
= token
->next
;
2586 if (!match_op(token
, ','))
2588 token
= token
->next
;
2590 return expect(token
, ';', "at end of label declaration");
2593 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2595 int seen_statement
= 0;
2596 while (token_type(token
) == TOKEN_IDENT
&&
2597 token
->ident
== &__label___ident
)
2598 token
= label_statement(token
->next
);
2600 struct statement
* stmt
;
2601 if (eof_token(token
))
2603 if (match_op(token
, '}'))
2605 if (match_ident(token
, &_Static_assert_ident
)) {
2606 token
= parse_static_assert(token
, NULL
);
2609 if (lookup_type(token
)) {
2610 if (seen_statement
) {
2611 warning(token
->pos
, "mixing declarations and code");
2614 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2615 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2617 seen_statement
= Wdeclarationafterstatement
;
2618 token
= statement(token
, &stmt
);
2620 add_statement(list
, stmt
);
2625 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2627 struct symbol_list
**list
= &fn
->arguments
;
2629 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2630 sym
->ident
= token
->ident
;
2631 token
= token
->next
;
2632 sym
->endpos
= token
->pos
;
2633 sym
->ctype
.base_type
= &incomplete_ctype
;
2634 add_symbol(list
, sym
);
2635 if (!match_op(token
, ',') ||
2636 token_type(token
->next
) != TOKEN_IDENT
||
2637 lookup_type(token
->next
))
2639 token
= token
->next
;
2644 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2646 struct symbol_list
**list
= &fn
->arguments
;
2651 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2653 token
= token
->next
;
2657 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2658 token
= parameter_declaration(token
, sym
);
2659 if (sym
->ctype
.base_type
== &void_ctype
) {
2660 /* Special case: (void) */
2661 if (!*list
&& !sym
->ident
)
2663 warning(token
->pos
, "void parameter");
2665 add_symbol(list
, sym
);
2666 if (!match_op(token
, ','))
2668 token
= token
->next
;
2673 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2675 stmt
->type
= STMT_COMPOUND
;
2676 start_block_scope(token
->pos
);
2677 token
= statement_list(token
, &stmt
->stmts
);
2682 static struct expression
*identifier_expression(struct token
*token
)
2684 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2685 expr
->expr_ident
= token
->ident
;
2689 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2691 int idx_from
, idx_to
;
2692 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2694 idx_from
= const_expression_value(from
);
2697 idx_to
= const_expression_value(to
);
2698 if (idx_to
< idx_from
|| idx_from
< 0)
2699 warning(from
->pos
, "nonsense array initializer index range");
2701 expr
->idx_from
= idx_from
;
2702 expr
->idx_to
= idx_to
;
2706 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2708 int expect_equal
= 0;
2709 struct token
*next
= token
->next
;
2710 struct expression
**tail
= ep
;
2715 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2716 struct expression
*expr
= identifier_expression(token
);
2717 if (Wold_initializer
)
2718 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2719 token
= initializer(&expr
->ident_expression
, next
->next
);
2720 if (expr
->ident_expression
)
2725 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2726 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2727 struct expression
*expr
= identifier_expression(next
);
2729 tail
= &expr
->ident_expression
;
2732 } else if (match_op(token
, '[')) {
2733 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2734 token
= constant_expression(token
->next
, &from
);
2736 sparse_error(token
->pos
, "Expected constant expression");
2739 if (match_op(token
, SPECIAL_ELLIPSIS
))
2740 token
= constant_expression(token
->next
, &to
);
2741 expr
= index_expression(from
, to
);
2743 tail
= &expr
->idx_expression
;
2744 token
= expect(token
, ']', "at end of initializer index");
2751 if (nested
&& !expect_equal
) {
2752 if (!match_op(token
, '='))
2753 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2758 token
= expect(token
, '=', "at end of initializer index");
2760 token
= initializer(tail
, token
);
2766 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2768 struct expression
*expr
;
2771 token
= single_initializer(&expr
, token
);
2774 add_expression(list
, expr
);
2775 if (!match_op(token
, ','))
2777 token
= token
->next
;
2782 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2784 if (match_op(token
, '{')) {
2785 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2787 if (!Wuniversal_initializer
) {
2788 struct token
*next
= token
->next
;
2789 // '{ 0 }' is equivalent to '{ }' except for some
2790 // warnings, like using 0 to initialize a null-pointer.
2791 if (match_token_zero(next
)) {
2792 if (match_op(next
->next
, '}'))
2793 expr
->zero_init
= 1;
2797 token
= initializer_list(&expr
->expr_list
, token
->next
);
2798 return expect(token
, '}', "at end of initializer");
2800 return assignment_expression(token
, tree
);
2803 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2806 sparse_error(sym
->pos
, "no identifier for function argument");
2809 if (sym
->ctype
.base_type
== &incomplete_ctype
) {
2810 sym
->ctype
.base_type
= &int_ctype
;
2812 if (Wimplicit_int
) {
2813 sparse_error(sym
->pos
, "missing type declaration for parameter '%s'",
2814 show_ident(sym
->ident
));
2817 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2820 static int is_syscall(struct symbol
*sym
)
2826 macro
= get_macro_name(sym
->pos
);
2828 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2829 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2832 name
= sym
->ident
->name
;
2834 if (name
&& strncmp(name
, "sys_", 4) ==0)
2837 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2844 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2845 struct symbol_list
**list
)
2847 struct symbol_list
**old_symbol_list
;
2848 struct symbol
*base_type
= decl
->ctype
.base_type
;
2849 struct statement
*stmt
, **p
;
2850 struct symbol
*prev
;
2853 old_symbol_list
= function_symbol_list
;
2854 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2855 function_symbol_list
= &decl
->inline_symbol_list
;
2856 p
= &base_type
->inline_stmt
;
2858 function_symbol_list
= &decl
->symbol_list
;
2859 p
= &base_type
->stmt
;
2861 function_computed_target_list
= NULL
;
2862 function_computed_goto_list
= NULL
;
2864 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2865 if (Wexternal_function_has_definition
)
2866 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2868 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2869 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2871 stmt
= start_function(decl
);
2874 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2875 declare_argument(arg
, base_type
);
2876 } END_FOR_EACH_PTR(arg
);
2878 token
= statement_list(token
->next
, &stmt
->stmts
);
2881 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2882 add_symbol(list
, decl
);
2883 else if (is_syscall(decl
)) {
2884 add_symbol(list
, decl
);
2886 printf("parse.c decl: %s\n", decl->ident->name);
2887 char *macro = get_macro_name(decl->pos);
2888 printf("decl macro: %s\n", macro);
2891 check_declaration(decl
);
2892 decl
->definition
= decl
;
2893 prev
= decl
->same_symbol
;
2894 if (prev
&& prev
->definition
) {
2895 warning(decl
->pos
, "multiple definitions for function '%s'",
2896 show_ident(decl
->ident
));
2897 info(prev
->definition
->pos
, " the previous one is here");
2900 rebind_scope(prev
, decl
->scope
);
2901 prev
->definition
= decl
;
2902 prev
= prev
->same_symbol
;
2905 function_symbol_list
= old_symbol_list
;
2906 if (function_computed_goto_list
) {
2907 if (!function_computed_target_list
)
2908 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2910 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2911 stmt
->target_list
= function_computed_target_list
;
2912 } END_FOR_EACH_PTR(stmt
);
2915 return expect(token
, '}', "at end of function");
2918 static void promote_k_r_types(struct symbol
*arg
)
2920 struct symbol
*base
= arg
->ctype
.base_type
;
2921 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2922 arg
->ctype
.base_type
= &int_ctype
;
2926 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2928 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2931 FOR_EACH_PTR(real_args
, arg
) {
2932 struct symbol
*type
;
2934 /* This is quadratic in the number of arguments. We _really_ don't care */
2935 FOR_EACH_PTR(argtypes
, type
) {
2936 if (type
->ident
== arg
->ident
)
2938 } END_FOR_EACH_PTR(type
);
2939 if (Wimplicit_int
) {
2940 warning(arg
->pos
, "missing type declaration for parameter '%s'",
2941 show_ident(arg
->ident
));
2943 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
2944 type
->ident
= arg
->ident
;
2945 type
->ctype
.base_type
= &int_ctype
;
2948 /* "char" and "short" promote to "int" */
2949 promote_k_r_types(type
);
2951 arg
->ctype
= type
->ctype
;
2952 } END_FOR_EACH_PTR(arg
);
2954 FOR_EACH_PTR(argtypes
, arg
) {
2956 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2957 } END_FOR_EACH_PTR(arg
);
2961 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2962 struct symbol_list
**list
)
2964 struct symbol_list
*args
= NULL
;
2966 if (Wold_style_definition
)
2967 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2970 token
= declaration_list(token
, &args
);
2971 if (!match_op(token
, ';')) {
2972 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2975 token
= token
->next
;
2976 } while (lookup_type(token
));
2978 apply_k_r_types(args
, decl
);
2980 if (!match_op(token
, '{')) {
2981 sparse_error(token
->pos
, "expected function body");
2984 return parse_function_body(token
, decl
, list
);
2987 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2989 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2990 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2991 struct statement
*stmt
;
2993 anon
->ctype
.base_type
= fn
;
2994 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2997 token
= parse_asm_statement(token
, stmt
);
2999 // FIXME: add_symbol(list, anon);
3003 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
3004 validate_decl_t validate_decl
)
3006 struct ident
*ident
= NULL
;
3007 struct symbol
*decl
;
3008 struct decl_state ctx
= { .ident
= &ident
};
3010 struct symbol
*base_type
;
3014 if (match_ident(token
, &_Pragma_ident
))
3015 return parse_underscore_Pragma(token
);
3017 /* Top-level inline asm or static assertion? */
3018 if (token_type(token
) == TOKEN_IDENT
) {
3019 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
3020 if (s
&& s
->op
->toplevel
)
3021 return s
->op
->toplevel(token
, list
);
3024 /* Parse declaration-specifiers, if any */
3025 token
= declaration_specifiers(token
, &ctx
);
3026 mod
= decl_modifiers(&ctx
);
3027 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3028 /* Just a type declaration? */
3029 if (match_op(token
, ';')) {
3030 apply_modifiers(token
->pos
, &ctx
);
3035 token
= declarator(token
, &ctx
);
3036 token
= handle_asm_name(token
, &ctx
);
3037 token
= handle_attributes(token
, &ctx
);
3038 apply_modifiers(token
->pos
, &ctx
);
3040 decl
->ctype
= ctx
.ctype
;
3041 decl
->ctype
.modifiers
|= mod
;
3042 decl
->cleanup
= ctx
.cleanup
;
3043 decl
->endpos
= token
->pos
;
3045 /* Just a type declaration? */
3047 warning(token
->pos
, "missing identifier in declaration");
3048 return expect(token
, ';', "at the end of type declaration");
3051 /* type define declaration? */
3052 is_typedef
= ctx
.storage_class
== MOD_USERTYPE
;
3054 /* Typedefs don't have meaningful storage */
3056 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3058 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3060 base_type
= decl
->ctype
.base_type
;
3063 if (base_type
&& !base_type
->ident
) {
3064 switch (base_type
->type
) {
3069 base_type
->ident
= ident
;
3075 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3076 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3077 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3078 show_ident(decl
->ident
));
3079 base_type
->ctype
.base_type
= &int_ctype
;
3081 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3082 warning(decl
->pos
, "'%s()' has implicit return type",
3083 show_ident(decl
->ident
));
3084 base_type
->ctype
.base_type
= &int_ctype
;
3086 /* apply attributes placed after the declarator */
3087 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3089 /* K&R argument declaration? */
3090 if (lookup_type(token
))
3091 return parse_k_r_arguments(token
, decl
, list
);
3092 if (match_op(token
, '{'))
3093 return parse_function_body(token
, decl
, list
);
3095 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3096 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3097 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3098 sparse_error(token
->pos
, "void declaration");
3100 if (base_type
== &incomplete_ctype
) {
3101 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3102 decl
->ctype
.base_type
= &int_ctype
;;
3106 if (!is_typedef
&& match_op(token
, '=')) {
3107 struct token
*next
= token
->next
;
3108 token
= initializer(&decl
->initializer
, next
);
3110 sparse_error(token
->pos
, "expression expected before '%s'", show_token(token
));
3114 validate_decl(decl
);
3116 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3117 warning(decl
->pos
, "symbol with external linkage has initializer");
3118 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3121 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3122 add_symbol(list
, decl
);
3123 fn_local_symbol(decl
);
3126 check_declaration(decl
);
3127 if (decl
->same_symbol
) {
3128 decl
->definition
= decl
->same_symbol
->definition
;
3129 decl
->op
= decl
->same_symbol
->op
;
3131 // TODO: handle -std=c89 --pedantic
3132 check_duplicates(decl
);
3137 const char *msg
= NULL
;
3138 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3139 msg
= "on non-identifier";
3140 else if (match_op(token
, ','))
3141 msg
= "on declaration list";
3142 else if (!decl
->initializer
)
3143 msg
= "without initializer";
3144 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3145 decl
->initializer
->symbol
== decl
)
3146 msg
= "on self-init var";
3148 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3149 decl
->ctype
.base_type
= &bad_ctype
;
3153 if (!match_op(token
, ','))
3156 token
= token
->next
;
3158 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3161 token
= handle_attributes(token
, &ctx
);
3162 token
= declarator(token
, &ctx
);
3163 token
= handle_asm_name(token
, &ctx
);
3164 token
= handle_attributes(token
, &ctx
);
3165 apply_modifiers(token
->pos
, &ctx
);
3166 decl
->ctype
= ctx
.ctype
;
3167 decl
->ctype
.modifiers
|= mod
;
3168 decl
->cleanup
= ctx
.cleanup
;
3169 decl
->endpos
= token
->pos
;
3171 sparse_error(token
->pos
, "expected identifier name in type definition");
3176 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3178 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3180 /* Function declarations are automatically extern unless specifically static */
3181 base_type
= decl
->ctype
.base_type
;
3182 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3183 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3184 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3187 return expect(token
, ';', "at end of declaration");