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
,
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
);
78 typedef struct token
*attr_t(struct token
*, struct symbol
*,
82 attribute_packed
, attribute_aligned
, attribute_cleanup
,
86 attribute_address_space
, attribute_context
,
87 attribute_designated_init
,
88 attribute_transparent_union
, ignore_attribute
,
89 attribute_mode
, attribute_force
;
91 typedef struct symbol
*to_mode_t(struct symbol
*);
94 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
;
95 static to_mode_t to_pointer_mode
, to_word_mode
;
110 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
114 CInt
= 0, CSInt
, CUInt
, CReal
,
117 static void asm_modifier(struct token
*token
, unsigned long *mods
, unsigned long mod
)
120 warning(token
->pos
, "duplicated asm modifier");
124 static struct symbol_op typedef_op
= {
126 .declarator
= storage_specifier
,
129 static struct symbol_op inline_op
= {
131 .declarator
= generic_qualifier
,
132 .asm_modifier
= asm_modifier
,
135 static struct symbol_op noreturn_op
= {
137 .declarator
= generic_qualifier
,
140 static declarator_t alignas_specifier
;
141 static struct symbol_op alignas_op
= {
143 .declarator
= alignas_specifier
,
146 static struct symbol_op auto_op
= {
148 .declarator
= storage_specifier
,
151 static struct symbol_op register_op
= {
153 .declarator
= storage_specifier
,
156 static struct symbol_op static_op
= {
157 .type
= KW_MODIFIER
|KW_STATIC
,
158 .declarator
= storage_specifier
,
161 static struct symbol_op extern_op
= {
163 .declarator
= storage_specifier
,
166 static struct symbol_op thread_op
= {
168 .declarator
= thread_specifier
,
171 static struct symbol_op const_op
= {
172 .type
= KW_QUALIFIER
,
173 .declarator
= generic_qualifier
,
176 static struct symbol_op volatile_op
= {
177 .type
= KW_QUALIFIER
,
178 .declarator
= generic_qualifier
,
179 .asm_modifier
= asm_modifier
,
182 static struct symbol_op restrict_op
= {
183 .type
= KW_QUALIFIER
,
184 .declarator
= generic_qualifier
,
187 static struct symbol_op atomic_op
= {
188 .type
= KW_QUALIFIER
,
189 .declarator
= generic_qualifier
,
192 static struct symbol_op typeof_op
= {
193 .type
= KW_SPECIFIER
,
194 .declarator
= typeof_specifier
,
199 static struct symbol_op autotype_op
= {
200 .type
= KW_SPECIFIER
,
201 .declarator
= autotype_specifier
,
206 static struct symbol_op attribute_op
= {
207 .type
= KW_ATTRIBUTE
,
208 .declarator
= attribute_specifier
,
211 static struct symbol_op struct_op
= {
212 .type
= KW_SPECIFIER
,
213 .declarator
= struct_specifier
,
218 static struct symbol_op union_op
= {
219 .type
= KW_SPECIFIER
,
220 .declarator
= union_specifier
,
225 static struct symbol_op enum_op
= {
226 .type
= KW_SPECIFIER
,
227 .declarator
= enum_specifier
,
232 static struct symbol_op spec_op
= {
233 .type
= KW_SPECIFIER
| KW_EXACT
,
238 static struct symbol_op char_op
= {
239 .type
= KW_SPECIFIER
,
240 .test
= Set_T
|Set_Long
|Set_Short
,
241 .set
= Set_T
|Set_Char
,
245 static struct symbol_op int_op
= {
246 .type
= KW_SPECIFIER
,
248 .set
= Set_T
|Set_Int
,
251 static struct symbol_op double_op
= {
252 .type
= KW_SPECIFIER
,
253 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
254 .set
= Set_T
|Set_Double
,
258 static struct symbol_op float_op
= {
259 .type
= KW_SPECIFIER
,
260 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
261 .set
= Set_T
|Set_Float
,
265 static struct symbol_op short_op
= {
266 .type
= KW_SPECIFIER
,
267 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
271 static struct symbol_op signed_op
= {
272 .type
= KW_SPECIFIER
,
273 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
278 static struct symbol_op unsigned_op
= {
279 .type
= KW_SPECIFIER
,
280 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
285 static struct symbol_op long_op
= {
286 .type
= KW_SPECIFIER
,
287 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
291 static struct symbol_op int128_op
= {
292 .type
= KW_SPECIFIER
,
293 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
294 .set
= Set_T
|Set_Int128
|Set_Vlong
,
298 static struct symbol_op if_op
= {
299 .statement
= parse_if_statement
,
302 static struct symbol_op return_op
= {
303 .statement
= parse_return_statement
,
306 static struct symbol_op loop_iter_op
= {
307 .statement
= parse_loop_iterator
,
310 static struct symbol_op default_op
= {
311 .statement
= parse_default_statement
,
314 static struct symbol_op case_op
= {
315 .statement
= parse_case_statement
,
318 static struct symbol_op switch_op
= {
319 .statement
= parse_switch_statement
,
322 static struct symbol_op for_op
= {
323 .statement
= parse_for_statement
,
326 static struct symbol_op while_op
= {
327 .statement
= parse_while_statement
,
330 static struct symbol_op do_op
= {
331 .statement
= parse_do_statement
,
334 static struct symbol_op goto_op
= {
335 .statement
= parse_goto_statement
,
338 static struct symbol_op __context___op
= {
339 .statement
= parse_context_statement
,
340 .attribute
= attribute_context
,
343 static struct symbol_op range_op
= {
344 .statement
= parse_range_statement
,
347 static struct symbol_op asm_op
= {
349 .statement
= parse_asm_statement
,
350 .toplevel
= toplevel_asm_declaration
,
353 static struct symbol_op static_assert_op
= {
354 .toplevel
= parse_static_assert
,
357 static struct symbol_op packed_op
= {
358 .attribute
= attribute_packed
,
361 static struct symbol_op aligned_op
= {
362 .attribute
= attribute_aligned
,
365 static struct symbol_op cleanup_op
= {
366 .attribute
= attribute_cleanup
,
369 static struct symbol_op attr_mod_op
= {
370 .attribute
= attribute_modifier
,
373 static struct symbol_op attr_fun_op
= {
374 .attribute
= attribute_function
,
377 static struct symbol_op attr_bitwise_op
= {
378 .attribute
= attribute_bitwise
,
381 static struct symbol_op attr_force_op
= {
382 .attribute
= attribute_force
,
385 static struct symbol_op address_space_op
= {
386 .attribute
= attribute_address_space
,
389 static struct symbol_op mode_op
= {
390 .attribute
= attribute_mode
,
393 static struct symbol_op context_op
= {
394 .attribute
= attribute_context
,
397 static struct symbol_op designated_init_op
= {
398 .attribute
= attribute_designated_init
,
401 static struct symbol_op transparent_union_op
= {
402 .attribute
= attribute_transparent_union
,
405 static struct symbol_op ignore_attr_op
= {
406 .attribute
= ignore_attribute
,
409 static struct symbol_op mode_QI_op
= {
411 .to_mode
= to_QI_mode
414 static struct symbol_op mode_HI_op
= {
416 .to_mode
= to_HI_mode
419 static struct symbol_op mode_SI_op
= {
421 .to_mode
= to_SI_mode
424 static struct symbol_op mode_DI_op
= {
426 .to_mode
= to_DI_mode
429 static struct symbol_op mode_TI_op
= {
431 .to_mode
= to_TI_mode
434 static struct symbol_op mode_pointer_op
= {
436 .to_mode
= to_pointer_mode
439 static struct symbol_op mode_word_op
= {
441 .to_mode
= to_word_mode
445 * Define the keyword and their effects.
446 * The entries in the 'typedef' and put in NS_TYPEDEF and
447 * are automatically set as reserved keyword while the ones
448 * in the 'keyword' table are just put in NS_KEYWORD.
450 * The entries are added via the 3 macros:
451 * N() for entries with "name" only,
452 * D() for entries with "name" & "__name__",
453 * A() for entries with "name", "__name" & "__name__",
454 * U() for entries with "__name" & "__name__".
456 static struct init_keyword
{
458 struct symbol_op
*op
;
462 #define N(I, O,...) { I, O,##__VA_ARGS__ }
463 #define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
464 N("__" I "__",O,##__VA_ARGS__)
465 #define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
466 N("__" I,O,##__VA_ARGS__), \
467 N("__" I "__",O,##__VA_ARGS__)
468 #define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
469 N("__" I "__",O,##__VA_ARGS__)
470 /* Storage classes */
471 N("auto", &auto_op
, .mods
= MOD_AUTO
),
472 N("register", ®ister_op
, .mods
= MOD_REGISTER
),
473 N("static", &static_op
, .mods
= MOD_STATIC
),
474 N("extern", &extern_op
, .mods
= MOD_EXTERN
),
475 N("__thread", &thread_op
),
476 N("_Thread_local", &thread_op
),
478 A("inline", &inline_op
, .mods
= MOD_INLINE
),
481 N("typedef", &typedef_op
, .mods
= MOD_USERTYPE
),
482 A("typeof", &typeof_op
),
483 N("__auto_type", &autotype_op
),
485 /* Type qualifiers */
486 A("const", &const_op
, .mods
= MOD_CONST
),
487 A("volatile", &volatile_op
, .mods
= MOD_VOLATILE
),
488 A("restrict", &restrict_op
, .mods
= MOD_RESTRICT
),
490 N("_Atomic", &atomic_op
, .mods
= MOD_ATOMIC
),
491 N("_Noreturn", &noreturn_op
, .mods
= MOD_NORETURN
),
492 N("_Alignas", &alignas_op
),
494 U("attribute", &attribute_op
),
496 /* Type specifiers */
497 N("struct", &struct_op
),
498 N("union", &union_op
),
501 N("void", &spec_op
, .type
= &void_ctype
),
503 N("short", &short_op
),
506 N("float", &float_op
),
507 N("double", &double_op
),
508 A("signed", &signed_op
),
509 N("unsigned", &unsigned_op
),
510 N("__int128", &int128_op
),
511 N("_Bool", &spec_op
, .type
= &bool_ctype
),
513 /* Predeclared types */
514 N("__builtin_va_list", &spec_op
, .type
= &ptr_ctype
),
515 N("__builtin_ms_va_list",&spec_op
, .type
= &ptr_ctype
),
516 N("__int128_t", &spec_op
, .type
= &sint128_ctype
),
517 N("__uint128_t", &spec_op
, .type
= &uint128_ctype
),
518 N("_Float32", &spec_op
, .type
= &float32_ctype
),
519 N("_Float32x", &spec_op
, .type
= &float32x_ctype
),
520 N("_Float64", &spec_op
, .type
= &float64_ctype
),
521 N("_Float64x", &spec_op
, .type
= &float64x_ctype
),
522 N("_Float128", &spec_op
, .type
= &float128_ctype
),
526 N("return", &return_op
),
527 N("break", &loop_iter_op
),
528 N("continue", &loop_iter_op
),
529 N("default", &default_op
),
531 N("switch", &switch_op
),
533 N("while", &while_op
),
537 N("context", &context_op
),
538 N("__context__", &__context___op
),
539 N("__range__", &range_op
),
540 N("_Static_assert", &static_assert_op
),
543 D("packed", &packed_op
),
544 D("aligned", &aligned_op
),
545 D("__cleanup__", &cleanup_op
),
546 D("nocast", &attr_mod_op
, .mods
= MOD_NOCAST
),
547 D("noderef", &attr_mod_op
, .mods
= MOD_NODEREF
),
548 D("safe", &attr_mod_op
, .mods
= MOD_SAFE
),
549 D("unused", &attr_mod_op
, .mods
= MOD_UNUSED
),
550 D("externally_visible", &attr_mod_op
, .mods
= MOD_EXT_VISIBLE
),
551 D("force", &attr_force_op
),
552 D("bitwise", &attr_bitwise_op
, .mods
= MOD_BITWISE
),
553 D("address_space", &address_space_op
),
554 D("designated_init", &designated_init_op
),
555 D("transparent_union", &transparent_union_op
),
556 D("noreturn", &attr_fun_op
, .mods
= MOD_NORETURN
),
557 D("pure", &attr_fun_op
, .mods
= MOD_PURE
),
558 A("const", &attr_fun_op
, .mods
= MOD_PURE
),
559 D("gnu_inline", &attr_fun_op
, .mods
= MOD_GNU_INLINE
),
563 D("QI", &mode_QI_op
),
564 D("HI", &mode_HI_op
),
565 D("SI", &mode_SI_op
),
566 D("DI", &mode_DI_op
),
567 D("TI", &mode_TI_op
),
568 D("byte", &mode_QI_op
),
569 D("pointer", &mode_pointer_op
),
570 D("word", &mode_word_op
),
574 static const char *ignored_attributes
[] = {
576 #define GCC_ATTR(x) \
578 STRINGIFY(__##x##__),
580 #include "gcc-attr-list.h"
592 static void init_keyword(int stream
, struct init_keyword
*kw
, enum namespace ns
)
594 struct symbol
*sym
= create_symbol(stream
, kw
->name
, SYM_KEYWORD
, ns
);
595 sym
->ident
->keyword
= 1;
596 sym
->ident
->reserved
|= (ns
== NS_TYPEDEF
);
597 sym
->ctype
.modifiers
= kw
->mods
;
598 sym
->ctype
.base_type
= kw
->type
;
602 void init_parser(int stream
)
606 for (i
= 0; i
< ARRAY_SIZE(typedefs
); i
++)
607 init_keyword(stream
, &typedefs
[i
], NS_TYPEDEF
);
608 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
609 init_keyword(stream
, &keywords
[i
], NS_KEYWORD
);
611 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
612 const char * name
= ignored_attributes
[i
];
613 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
616 sym
->ident
->keyword
= 1;
617 sym
->op
= &ignore_attr_op
;
623 static struct token
*skip_to(struct token
*token
, int op
)
625 while (!match_op(token
, op
) && !eof_token(token
))
630 static struct token bad_token
= { .pos
.type
= TOKEN_BAD
};
631 struct token
*expect(struct token
*token
, int op
, const char *where
)
633 if (!match_op(token
, op
)) {
634 if (token
!= &bad_token
) {
635 bad_token
.next
= token
;
636 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
637 sparse_error(token
->pos
, "got %s", show_token(token
));
640 return skip_to(token
, op
);
647 // issue an error message on new parsing errors
648 // @token: the current token
649 // @errmsg: the error message
650 // If the current token is from a previous error, an error message
651 // has already been issued, so nothing more is done.
652 // Otherwise, @errmsg is displayed followed by the current token.
653 static void unexpected(struct token
*token
, const char *errmsg
)
655 if (token
== &bad_token
)
657 sparse_error(token
->pos
, "%s", errmsg
);
658 sparse_error(token
->pos
, "got %s", show_token(token
));
661 // Add a symbol to the list of function-local symbols
662 static void fn_local_symbol(struct symbol
*sym
)
664 if (function_symbol_list
)
665 add_symbol(function_symbol_list
, sym
);
668 struct statement
*alloc_statement(struct position pos
, int type
)
670 struct statement
*stmt
= __alloc_statement(0);
676 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
678 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
);
680 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
682 struct symbol
*ctype
;
685 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
687 sparse_error(pos
, "don't know how to apply mode to %s",
688 show_typename(ctx
->ctype
.base_type
));
690 ctx
->ctype
.base_type
= ctype
;
694 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
696 struct symbol
*sym
= alloc_symbol(pos
, type
);
698 sym
->ctype
.base_type
= ctype
->base_type
;
699 sym
->ctype
.modifiers
= ctype
->modifiers
;
701 ctype
->base_type
= sym
;
702 ctype
->modifiers
= 0;
707 * NOTE! NS_LABEL is not just a different namespace,
708 * it also ends up using function scope instead of the
709 * regular symbol scope.
711 struct symbol
*label_symbol(struct token
*token
, int used
)
713 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
715 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
716 bind_symbol(sym
, token
->ident
, NS_LABEL
);
719 fn_local_symbol(sym
);
724 static struct token
*struct_union_enum_specifier(enum type type
,
725 struct token
*token
, struct decl_state
*ctx
,
726 struct token
*(*parse
)(struct token
*, struct symbol
*))
728 struct decl_state attr
= { };
730 struct position
*repos
;
732 token
= handle_attributes(token
, &attr
);
733 if (token_type(token
) == TOKEN_IDENT
) {
734 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
736 (is_outer_scope(sym
->scope
) &&
737 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
738 // Either a new symbol, or else an out-of-scope
739 // symbol being redefined.
740 sym
= alloc_symbol(token
->pos
, type
);
741 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
743 if (sym
->type
!= type
)
744 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
745 ctx
->ctype
.base_type
= sym
;
748 if (!match_op(token
, '{'))
751 // The following test is actually wrong for empty
752 // structs, but (1) they are not C99, (2) gcc does
753 // the same thing, and (3) it's easier.
754 if (sym
->symbol_list
)
755 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
758 // Mark the structure as needing re-examination
760 } else if (match_op(token
, '{')) {
761 // private struct/union/enum type
762 sym
= alloc_symbol(token
->pos
, type
);
763 set_current_scope(sym
); // used by dissect
764 ctx
->ctype
.base_type
= sym
;
766 sparse_error(token
->pos
, "expected declaration");
767 ctx
->ctype
.base_type
= &bad_ctype
;
771 token
= parse(token
->next
, sym
);
772 token
= expect(token
, '}', "at end of specifier");
773 attr
.ctype
.base_type
= sym
;
774 token
= handle_attributes(token
, &attr
);
775 apply_ctype(token
->pos
, &sym
->ctype
, &attr
.ctype
);
776 sym
->packed
= attr
.packed
;
778 sym
->endpos
= token
->pos
;
783 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
785 struct symbol
*field
, *last
= NULL
;
787 res
= struct_declaration_list(token
, &sym
->symbol_list
);
788 FOR_EACH_PTR(sym
->symbol_list
, field
) {
790 struct symbol
*base
= field
->ctype
.base_type
;
791 if (base
&& base
->type
== SYM_BITFIELD
)
795 last
->next_subobject
= field
;
797 } END_FOR_EACH_PTR(field
);
801 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
803 return struct_declaration_list(token
, &sym
->symbol_list
);
806 static struct token
*struct_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
808 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
811 static struct token
*union_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
813 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
819 // This allow to use a shift amount as big (or bigger)
820 // than the width of the value to be shifted, in which case
821 // the result is, of course, 0.
822 static unsigned long long rshift(unsigned long long val
, unsigned int n
)
824 if (n
>= (sizeof(val
) * 8))
831 unsigned long long pos
;
834 static void update_range(struct range
*range
, unsigned long long uval
, struct symbol
*vtype
)
836 long long sval
= uval
;
838 if (is_signed_type(vtype
) && (sval
< 0)) {
839 if (sval
< range
->neg
)
842 if (uval
> range
->pos
)
847 static int type_is_ok(struct symbol
*type
, struct range range
)
849 int shift
= type
->bit_size
;
850 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
854 if (rshift(range
.pos
, shift
))
860 if (rshift(~range
.neg
, shift
))
865 static struct range
type_range(struct symbol
*type
)
868 unsigned int size
= type
->bit_size
;
869 unsigned long long max
;
872 if (is_signed_type(type
)) {
873 min
= sign_bit(size
);
877 max
= bits_mask(size
);
885 static int val_in_range(struct range
*range
, long long sval
, struct symbol
*vtype
)
887 unsigned long long uval
= sval
;
889 if (is_signed_type(vtype
) && (sval
< 0))
890 return range
->neg
<= sval
;
892 return uval
<= range
->pos
;
895 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
897 struct range irange
= type_range(&int_ctype
);
900 FOR_EACH_PTR(list
, sym
) {
901 struct expression
*expr
= sym
->initializer
;
902 struct symbol
*ctype
;
904 if (expr
->type
!= EXPR_VALUE
)
907 val
= get_expression_value(expr
);
908 if (is_int_type(ctype
) && val_in_range(&irange
, val
, ctype
)) {
909 expr
->ctype
= &int_ctype
;
912 cast_value(expr
, base_type
, expr
);
913 } END_FOR_EACH_PTR(sym
);
916 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
918 unsigned long long lastval
= 0;
919 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
920 struct range range
= { };
923 parent
->examined
= 1;
924 parent
->ctype
.base_type
= &int_ctype
;
925 while (token_type(token
) == TOKEN_IDENT
) {
926 struct expression
*expr
= NULL
;
927 struct token
*next
= token
->next
;
928 struct decl_state ctx
= { };
931 // FIXME: only 'deprecated' should be accepted
932 next
= handle_attributes(next
, &ctx
);
934 if (match_op(next
, '=')) {
935 next
= constant_expression(next
->next
, &expr
);
936 lastval
= get_expression_value(expr
);
938 if (expr
&& expr
->ctype
)
942 } else if (is_int_type(ctype
)) {
945 error_die(token
->pos
, "can't increment the last enum member");
949 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
950 expr
->value
= lastval
;
954 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
955 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
956 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
957 sym
->initializer
= expr
;
958 sym
->enum_member
= 1;
959 sym
->ctype
.base_type
= parent
;
960 add_ptr_list(&parent
->symbol_list
, sym
);
962 if (base_type
!= &bad_ctype
) {
963 if (ctype
->type
== SYM_NODE
)
964 ctype
= ctype
->ctype
.base_type
;
965 if (ctype
->type
== SYM_ENUM
) {
969 ctype
= ctype
->ctype
.base_type
;
973 * - if all enums are of the same type, then
974 * the base_type is that type (two first
976 * - if enums are of different types, they
977 * all have to be integer types, and the
978 * base type is at least "int_ctype".
979 * - otherwise the base_type is "bad_ctype".
981 if (!base_type
|| ctype
== &bad_ctype
) {
983 } else if (ctype
== base_type
) {
985 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
986 base_type
= &int_ctype
;
987 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
988 if (!mix_bitwise
++) {
989 warning(expr
->pos
, "mixed bitwiseness");
991 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
992 sparse_error(expr
->pos
, "incompatible restricted type");
993 info(expr
->pos
, " expected: %s", show_typename(base_type
));
994 info(expr
->pos
, " got: %s", show_typename(ctype
));
995 base_type
= &bad_ctype
;
996 } else if (base_type
!= &bad_ctype
) {
997 sparse_error(token
->pos
, "bad enum definition");
998 base_type
= &bad_ctype
;
1000 parent
->ctype
.base_type
= base_type
;
1002 if (is_int_type(base_type
)) {
1003 update_range(&range
, lastval
, ctype
);
1007 sym
->endpos
= token
->pos
;
1009 if (!match_op(token
, ','))
1011 token
= token
->next
;
1014 sparse_error(token
->pos
, "empty enum definition");
1015 base_type
= &bad_ctype
;
1017 else if (!is_int_type(base_type
))
1019 else if (type_is_ok(&uint_ctype
, range
))
1020 base_type
= &uint_ctype
;
1021 else if (type_is_ok(&int_ctype
, range
))
1022 base_type
= &int_ctype
;
1023 else if (type_is_ok(&ulong_ctype
, range
))
1024 base_type
= &ulong_ctype
;
1025 else if (type_is_ok(&long_ctype
, range
))
1026 base_type
= &long_ctype
;
1027 else if (type_is_ok(&ullong_ctype
, range
))
1028 base_type
= &ullong_ctype
;
1029 else if (type_is_ok(&llong_ctype
, range
))
1030 base_type
= &llong_ctype
;
1032 base_type
= &bad_ctype
;
1033 parent
->ctype
.base_type
= base_type
;
1034 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1035 parent
->examined
= 0;
1039 cast_enum_list(parent
->symbol_list
, base_type
);
1044 static struct token
*enum_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1046 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1047 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1049 if (!ctype
->base_type
)
1050 ctype
->base_type
= &incomplete_ctype
;
1055 static struct token
*typeof_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1058 if (!match_op(token
, '(')) {
1059 sparse_error(token
->pos
, "expected '(' after typeof");
1062 if (lookup_type(token
->next
)) {
1064 token
= typename(token
->next
, &sym
, NULL
);
1065 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1066 apply_ctype(token
->pos
, &ctx
->ctype
, &sym
->ctype
);
1068 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1069 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1071 typeof_sym
->endpos
= token
->pos
;
1072 if (!typeof_sym
->initializer
) {
1073 sparse_error(token
->pos
, "expected expression after the '(' token");
1074 typeof_sym
= &bad_ctype
;
1076 ctx
->ctype
.base_type
= typeof_sym
;
1078 return expect(token
, ')', "after typeof");
1081 static struct token
*autotype_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1083 ctx
->ctype
.base_type
= &autotype_ctype
;
1088 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1090 struct expression
*expr
= NULL
;
1091 if (match_op(token
, '('))
1092 token
= parens_expression(token
, &expr
, "in attribute");
1096 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1098 if (!ctx
->ctype
.alignment
) {
1099 ctx
->ctype
.alignment
= 1;
1105 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1107 int alignment
= max_alignment
;
1108 struct expression
*expr
= NULL
;
1110 if (match_op(token
, '(')) {
1111 token
= parens_expression(token
, &expr
, "in attribute");
1113 alignment
= const_expression_value(expr
);
1115 if (alignment
& (alignment
-1)) {
1116 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1118 } else if (alignment
> ctx
->ctype
.alignment
)
1119 ctx
->ctype
.alignment
= alignment
;
1123 static struct token
*attribute_cleanup(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1125 struct expression
*expr
= NULL
;
1127 if (match_op(token
, '(')) {
1128 token
= parens_expression(token
, &expr
, "in attribute");
1129 if (expr
&& expr
->type
== EXPR_SYMBOL
)
1130 ctx
->cleanup
= expr
;
1135 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1137 if (*mods
& mod
& ~MOD_DUP_OK
)
1138 warning(*pos
, "duplicate %s", modifier_name(mod
));
1142 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1144 apply_mod(pos
, &ctx
->modifiers
, qual
);
1147 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1149 apply_mod(&token
->pos
, &ctx
->ctype
.modifiers
, attr
->ctype
.modifiers
);
1153 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1155 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1159 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1162 attribute_modifier(token
, attr
, ctx
);
1166 static struct ident
*numerical_address_space(int asn
)
1172 sprintf(buff
, "<asn:%d>", asn
);
1173 return built_in_ident(buff
);
1176 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1178 struct expression
*expr
= NULL
;
1179 struct ident
*as
= NULL
;
1182 token
= expect(token
, '(', "after address_space attribute");
1183 switch (token_type(token
)) {
1185 next
= primary_expression(token
, &expr
);
1186 if (expr
->type
!= EXPR_VALUE
)
1188 as
= numerical_address_space(expr
->value
);
1198 warning(token
->pos
, "invalid address space name");
1201 if (Waddress_space
&& as
) {
1203 sparse_error(token
->pos
,
1204 "multiple address spaces given: %s & %s",
1205 show_as(ctx
->ctype
.as
), show_as(as
));
1208 token
= expect(next
, ')', "after address_space attribute");
1212 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1214 if (ctype
->ctype
.base_type
!= &int_type
)
1216 if (ctype
== &char_ctype
)
1218 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1222 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1224 if (ctype
->ctype
.base_type
!= &int_type
)
1226 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1230 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1232 if (ctype
->ctype
.base_type
!= &int_type
)
1234 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1238 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1240 if (ctype
->ctype
.base_type
!= &int_type
)
1242 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1246 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1248 if (ctype
->ctype
.base_type
!= &int_type
)
1250 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1254 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1256 if (ctype
->ctype
.base_type
!= &int_type
)
1258 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1262 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1264 if (ctype
->ctype
.base_type
!= &int_type
)
1266 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1270 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1272 token
= expect(token
, '(', "after mode attribute");
1273 if (token_type(token
) == TOKEN_IDENT
) {
1274 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1275 if (mode
&& mode
->op
->type
& KW_MODE
)
1276 ctx
->mode
= mode
->op
;
1278 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1279 token
= token
->next
;
1281 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1282 token
= expect(token
, ')', "after mode attribute");
1286 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1288 struct context
*context
= alloc_context();
1289 struct expression
*args
[3];
1292 token
= expect(token
, '(', "after context attribute");
1293 token
= conditional_expression(token
, &args
[0]);
1294 token
= expect(token
, ',', "after context 1st argument");
1295 token
= conditional_expression(token
, &args
[1]);
1296 if (match_op(token
, ',')) {
1297 token
= token
->next
;
1298 token
= conditional_expression(token
, &args
[2]);
1299 token
= expect(token
, ')', "after context 3rd argument");
1300 context
->context
= args
[0];
1303 token
= expect(token
, ')', "after context 2nd argument");
1305 context
->in
= get_expression_value(args
[idx
++]);
1306 context
->out
= get_expression_value(args
[idx
++]);
1307 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1311 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1313 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1314 ctx
->ctype
.base_type
->designated_init
= 1;
1316 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1320 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1322 if (Wtransparent_union
)
1323 warning(token
->pos
, "attribute __transparent_union__");
1325 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1326 ctx
->ctype
.base_type
->transparent_union
= 1;
1328 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1332 static struct token
*recover_unknown_attribute(struct token
*token
)
1334 struct expression
*expr
= NULL
;
1336 if (Wunknown_attribute
)
1337 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1338 token
= token
->next
;
1339 if (match_op(token
, '('))
1340 token
= parens_expression(token
, &expr
, "in attribute");
1344 static struct token
*attribute_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1346 token
= expect(token
, '(', "after attribute");
1347 token
= expect(token
, '(', "after attribute");
1349 while (token_type(token
) == TOKEN_IDENT
) {
1350 struct symbol
*attr
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1351 if (attr
&& attr
->op
->attribute
)
1352 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1354 token
= recover_unknown_attribute(token
);
1356 if (!match_op(token
, ','))
1358 token
= token
->next
;
1361 token
= expect(token
, ')', "after attribute");
1362 token
= expect(token
, ')', "after attribute");
1366 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1368 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1369 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1370 return ctx
->storage_class
| mods
;
1373 static struct token
*storage_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1375 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1376 unsigned long class = sym
->ctype
.modifiers
;
1377 const char *storage
= modifier_name(class);
1379 /* __thread can be used alone, or with extern or static */
1380 if (is_tls
&& (class & ~(MOD_STATIC
|MOD_EXTERN
)))
1381 sparse_error(next
->pos
, "__thread cannot be used with '%s'", storage
);
1382 else if (!ctx
->storage_class
)
1383 ctx
->storage_class
= class;
1384 else if (ctx
->storage_class
== class)
1385 sparse_error(next
->pos
, "duplicate %s", storage
);
1387 sparse_error(next
->pos
, "multiple storage classes");
1391 static struct token
*thread_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1393 /* This GCC extension can be used alone, or with extern or static */
1394 if (!(ctx
->storage_class
& ~(MOD_STATIC
|MOD_EXTERN
))) {
1395 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1397 sparse_error(next
->pos
, "__thread cannot be used with '%s'",
1398 modifier_name(ctx
->storage_class
));
1404 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1410 static struct token
*alignas_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1414 if (!match_op(token
, '(')) {
1415 sparse_error(token
->pos
, "expected '(' after _Alignas");
1418 if (lookup_type(token
->next
)) {
1419 struct symbol
*sym
= NULL
;
1420 token
= typename(token
->next
, &sym
, NULL
);
1421 sym
= examine_symbol_type(sym
);
1422 alignment
= sym
->ctype
.alignment
;
1423 token
= expect(token
, ')', "after _Alignas(...");
1425 struct expression
*expr
= NULL
;
1426 token
= parens_expression(token
, &expr
, "after _Alignas");
1429 alignment
= const_expression_value(expr
);
1432 if (alignment
< 0) {
1433 warning(token
->pos
, "non-positive alignment");
1436 if (alignment
& (alignment
-1)) {
1437 warning(token
->pos
, "non-power-of-2 alignment");
1440 if (alignment
> ctx
->ctype
.alignment
)
1441 ctx
->ctype
.alignment
= alignment
;
1445 static struct token
*generic_qualifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1447 apply_qualifier(&next
->pos
, &ctx
->ctype
, sym
->ctype
.modifiers
);
1451 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
)
1453 unsigned long mod
= src
->modifiers
;
1456 apply_qualifier(&pos
, dst
, mod
);
1459 concat_ptr_list((struct ptr_list
*)src
->contexts
,
1460 (struct ptr_list
**)&dst
->contexts
);
1463 if (src
->alignment
> dst
->alignment
)
1464 dst
->alignment
= src
->alignment
;
1471 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1474 if (what
& (Set_S
| Set_T
))
1476 if (what
& Set_Char
)
1478 else if (what
& Set_Double
)
1480 else if (what
& Set_Float
)
1482 else if (what
& Set_Signed
)
1484 else if (what
& Set_Unsigned
)
1486 else if (what
& Set_Short
)
1488 else if (what
& Set_Long
)
1492 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1493 old
, show_ident(new));
1497 sparse_error(pos
, "two or more data types in declaration specifiers");
1500 static struct symbol
* const int_types
[] =
1501 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1502 static struct symbol
* const signed_types
[] =
1503 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1505 static struct symbol
* const unsigned_types
[] =
1506 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1508 static struct symbol
* const real_types
[] =
1509 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1510 static struct symbol
* const * const types
[] = {
1511 [CInt
] = int_types
+ 2,
1512 [CSInt
] = signed_types
+ 2,
1513 [CUInt
] = unsigned_types
+ 2,
1514 [CReal
] = real_types
+ 1,
1517 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1519 return types
[want_unsigned
? CUInt
: CInt
][size
];
1522 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1524 while (token_type(t
) == TOKEN_IDENT
) {
1525 struct symbol
*s
= lookup_keyword(t
->ident
, NS_TYPEDEF
);
1528 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1531 if (s
->op
->declarator
)
1532 t
= s
->op
->declarator(t
, s
, ctx
);
1537 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1543 while (token_type(token
) == TOKEN_IDENT
) {
1544 struct symbol
*s
= lookup_symbol(token
->ident
,
1545 NS_TYPEDEF
| NS_SYMBOL
);
1546 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1548 if (s
->type
!= SYM_KEYWORD
) {
1551 seen
|= Set_S
| Set_T
;
1552 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1553 apply_ctype(token
->pos
, &ctx
->ctype
, &s
->ctype
);
1554 token
= token
->next
;
1557 if (s
->op
->type
& KW_SPECIFIER
) {
1558 if (seen
& s
->op
->test
) {
1559 specifier_conflict(token
->pos
,
1565 class += s
->op
->class;
1566 if (s
->op
->set
& Set_Int128
)
1568 else if (s
->op
->set
& Set_Char
)
1570 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1572 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1573 if (class == CReal
) {
1574 specifier_conflict(token
->pos
,
1582 token
= token
->next
;
1583 if (s
->op
->declarator
) // Note: this eats attributes
1584 token
= s
->op
->declarator(token
, s
, ctx
);
1585 if (s
->op
->type
& KW_EXACT
) {
1586 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1587 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1591 if (!(seen
& Set_S
)) { /* not set explicitly? */
1592 struct symbol
*base
= &incomplete_ctype
;
1594 base
= types
[class][rank
];
1595 ctx
->ctype
.base_type
= base
;
1598 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1599 struct symbol
*type
;
1600 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1601 if (!is_int_type(ctx
->ctype
.base_type
)) {
1602 sparse_error(token
->pos
, "invalid modifier");
1605 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1606 *type
= *ctx
->ctype
.base_type
;
1607 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1608 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1609 type
->type
= SYM_RESTRICT
;
1610 ctx
->ctype
.base_type
= type
;
1611 create_fouled(type
);
1616 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1618 struct expression
*expr
= NULL
;
1621 while (token_type(token
) == TOKEN_IDENT
) {
1622 struct symbol
*sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1623 if (!sym
|| !(sym
->op
->type
& (KW_STATIC
|KW_QUALIFIER
)))
1625 if (has_static
&& (sym
->op
->type
& KW_STATIC
))
1626 sparse_error(token
->pos
, "duplicate array static declarator");
1627 has_static
|= (sym
->op
->type
& KW_STATIC
);
1628 token
= token
->next
;
1630 if (match_op(token
, '*') && match_op(token
->next
, ']')) {
1631 // FIXME: '[*]' is treated like '[]'
1632 token
= token
->next
;
1634 token
= assignment_expression(token
, &expr
);
1636 sym
->array_size
= expr
;
1640 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1641 static struct token
*identifier_list(struct token
*, struct symbol
*);
1642 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1644 static struct token
*handle_asm_name(struct token
*token
, struct decl_state
*ctx
)
1646 struct expression
*expr
;
1647 struct symbol
*keyword
;
1649 if (token_type(token
) != TOKEN_IDENT
)
1651 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1654 if (!(keyword
->op
->type
& KW_ASM
))
1657 token
= token
->next
;
1658 token
= expect(token
, '(', "after asm");
1659 token
= string_expression(token
, &expr
, "asm name");
1660 token
= expect(token
, ')', "after asm");
1665 // test if @token is '__attribute__' (or one of its variant)
1666 static bool match_attribute(struct token
*token
)
1670 if (token_type(token
) != TOKEN_IDENT
)
1672 sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1673 if (!sym
|| !sym
->op
)
1675 return sym
->op
->type
& KW_ATTRIBUTE
;
1678 static struct token
*skip_attribute(struct token
*token
)
1680 token
= token
->next
;
1681 if (match_op(token
, '(')) {
1683 token
= token
->next
;
1684 while (depth
&& !eof_token(token
)) {
1685 if (token_type(token
) == TOKEN_SPECIAL
) {
1686 if (token
->special
== '(')
1688 else if (token
->special
== ')')
1691 token
= token
->next
;
1697 static struct token
*skip_attributes(struct token
*token
)
1699 while (match_attribute(token
)) {
1700 token
= expect(token
->next
, '(', "after attribute");
1701 token
= expect(token
, '(', "after attribute");
1702 while (token_type(token
) == TOKEN_IDENT
) {
1703 token
= skip_attribute(token
);
1704 if (!match_op(token
, ','))
1706 token
= token
->next
;
1708 token
= expect(token
, ')', "after attribute");
1709 token
= expect(token
, ')', "after attribute");
1714 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
)
1716 while (match_attribute(token
))
1717 token
= attribute_specifier(token
->next
, NULL
, ctx
);
1721 static int is_nested(struct token
*token
, struct token
**p
,
1722 int prefer_abstract
)
1725 * This can be either a parameter list or a grouping.
1726 * For the direct (non-abstract) case, we know if must be
1727 * a parameter list if we already saw the identifier.
1728 * For the abstract case, we know if must be a parameter
1729 * list if it is empty or starts with a type.
1731 struct token
*next
= token
->next
;
1733 *p
= next
= skip_attributes(next
);
1735 if (token_type(next
) == TOKEN_IDENT
) {
1736 if (lookup_type(next
))
1737 return !prefer_abstract
;
1741 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1748 Empty
, K_R
, Proto
, Bad_Func
,
1751 static enum kind
which_func(struct token
*token
,
1753 int prefer_abstract
)
1755 struct token
*next
= token
->next
;
1757 if (token_type(next
) == TOKEN_IDENT
) {
1758 if (lookup_type(next
))
1760 /* identifier list not in definition; complain */
1761 if (prefer_abstract
)
1763 "identifier list not in definition");
1767 if (token_type(next
) != TOKEN_SPECIAL
)
1770 if (next
->special
== ')') {
1771 /* don't complain about those */
1772 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1774 if (Wstrict_prototypes
)
1776 "non-ANSI function declaration of function '%s'",
1781 if (next
->special
== SPECIAL_ELLIPSIS
) {
1783 "variadic functions must have one named argument");
1790 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1792 struct ctype
*ctype
= &ctx
->ctype
;
1794 struct ident
**p
= ctx
->ident
;
1796 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1797 *ctx
->ident
= token
->ident
;
1798 token
= token
->next
;
1799 } else if (match_op(token
, '(') &&
1800 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1801 struct symbol
*base_type
= ctype
->base_type
;
1802 if (token
->next
!= next
)
1803 next
= handle_attributes(token
->next
, ctx
);
1804 token
= declarator(next
, ctx
);
1805 token
= expect(token
, ')', "in nested declarator");
1806 while (ctype
->base_type
!= base_type
)
1807 ctype
= &ctype
->base_type
->ctype
;
1811 if (match_op(token
, '(')) {
1812 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1814 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1815 ctype
->modifiers
|= ctx
->f_modifiers
;
1816 token
= token
->next
;
1818 token
= identifier_list(token
, fn
);
1819 else if (kind
== Proto
)
1820 token
= parameter_type_list(token
, fn
);
1821 token
= expect(token
, ')', "in function declarator");
1822 fn
->endpos
= token
->pos
;
1826 while (match_op(token
, '[')) {
1827 struct symbol
*array
;
1828 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1829 token
= abstract_array_declarator(token
->next
, array
);
1830 token
= expect(token
, ']', "in abstract_array_declarator");
1831 array
->endpos
= token
->pos
;
1832 ctype
= &array
->ctype
;
1837 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1839 while (match_op(token
,'*')) {
1840 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1841 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1842 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1843 ptr
->ctype
.as
= ctx
->ctype
.as
;
1844 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1845 ctx
->ctype
.modifiers
= 0;
1846 ctx
->ctype
.base_type
= ptr
;
1847 ctx
->ctype
.as
= NULL
;
1848 ctx
->ctype
.contexts
= NULL
;
1849 ctx
->ctype
.alignment
= 0;
1851 token
= handle_qualifiers(token
->next
, ctx
);
1852 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1857 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1859 token
= pointer(token
, ctx
);
1860 return direct_declarator(token
, ctx
);
1863 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1865 struct ctype
*ctype
= &ctx
->ctype
;
1866 struct expression
*expr
;
1867 struct symbol
*bitfield
;
1870 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1871 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1872 show_typename(ctype
->base_type
));
1873 // Parse this to recover gracefully.
1874 return conditional_expression(token
->next
, &expr
);
1877 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1878 token
= conditional_expression(token
->next
, &expr
);
1879 width
= const_expression_value(expr
);
1880 bitfield
->bit_size
= width
;
1882 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
1883 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
1884 show_ident(*ctx
->ident
), width
);
1886 } else if (*ctx
->ident
) {
1887 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1888 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1889 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1890 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1891 // Valid values are either {-1;0} or {0}, depending on integer
1892 // representation. The latter makes for very efficient code...
1893 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1895 if (Wdefault_bitfield_sign
&&
1896 bitfield_type
->type
!= SYM_ENUM
&&
1897 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1899 // The sign of bitfields is unspecified by default.
1900 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1903 bitfield
->bit_size
= width
;
1904 bitfield
->endpos
= token
->pos
;
1905 bitfield
->ident
= *ctx
->ident
;
1909 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1911 struct decl_state ctx
= {.prefer_abstract
= 0};
1915 token
= declaration_specifiers(token
, &ctx
);
1916 mod
= decl_modifiers(&ctx
);
1919 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1920 ctx
.ident
= &decl
->ident
;
1922 token
= declarator(token
, &ctx
);
1923 if (match_op(token
, ':'))
1924 token
= handle_bitfield(token
, &ctx
);
1926 token
= handle_attributes(token
, &ctx
);
1927 apply_modifiers(token
->pos
, &ctx
);
1929 decl
->ctype
= ctx
.ctype
;
1930 decl
->ctype
.modifiers
|= mod
;
1931 decl
->cleanup
= ctx
.cleanup
;
1932 decl
->endpos
= token
->pos
;
1933 add_symbol(list
, decl
);
1934 if (!match_op(token
, ','))
1936 token
= token
->next
;
1942 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1944 while (!match_op(token
, '}')) {
1945 if (match_ident(token
, &_Static_assert_ident
)) {
1946 token
= parse_static_assert(token
, NULL
);
1949 if (!match_op(token
, ';'))
1950 token
= declaration_list(token
, list
);
1951 if (!match_op(token
, ';')) {
1952 sparse_error(token
->pos
, "expected ; at end of declaration");
1955 token
= token
->next
;
1960 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1962 struct decl_state ctx
= {.prefer_abstract
= 1};
1964 token
= declaration_specifiers(token
, &ctx
);
1965 ctx
.ident
= &sym
->ident
;
1966 token
= declarator(token
, &ctx
);
1967 token
= handle_attributes(token
, &ctx
);
1968 apply_modifiers(token
->pos
, &ctx
);
1969 sym
->ctype
= ctx
.ctype
;
1970 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
1971 sym
->endpos
= token
->pos
;
1972 sym
->forced_arg
= ctx
.forced
;
1976 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1978 struct decl_state ctx
= {.prefer_abstract
= 1};
1979 unsigned long class;
1980 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1982 token
= declaration_specifiers(token
, &ctx
);
1983 token
= declarator(token
, &ctx
);
1984 apply_modifiers(token
->pos
, &ctx
);
1985 sym
->ctype
= ctx
.ctype
;
1986 sym
->cleanup
= ctx
.cleanup
;
1987 sym
->endpos
= token
->pos
;
1988 class = ctx
.storage_class
;
1990 *forced
= ctx
.forced
;
1992 warning(sym
->pos
, "storage class in typename (%s%s)",
1993 modifier_string(class), show_typename(sym
));
1997 static struct token
*parse_underscore_Pragma(struct token
*token
)
2002 if (!match_op(next
, '('))
2005 if (next
->pos
.type
!= TOKEN_STRING
)
2008 if (!match_op(next
, ')'))
2013 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
2015 if (match_ident(token
, &_Pragma_ident
))
2016 return parse_underscore_Pragma(token
);
2018 token
= parse_expression(token
, tree
);
2019 return expect(token
, ';', "at end of statement");
2022 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2023 struct asm_operand_list
**inout
)
2025 /* Allow empty operands */
2026 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2029 struct asm_operand
*op
= __alloc_asm_operand(0);
2030 if (match_op(token
->next
, '[') &&
2031 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2032 match_op(token
->next
->next
->next
, ']')) {
2033 op
->name
= token
->next
->next
->ident
;
2034 token
= token
->next
->next
->next
;
2036 token
= token
->next
;
2037 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2038 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2039 add_ptr_list(inout
, op
);
2040 } while (match_op(token
, ','));
2044 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2045 struct expression_list
**clobbers
)
2047 struct expression
*expr
;
2050 token
= primary_expression(token
->next
, &expr
);
2052 add_expression(clobbers
, expr
);
2053 } while (match_op(token
, ','));
2057 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2058 struct symbol_list
**labels
)
2060 struct symbol
*label
;
2063 token
= token
->next
; /* skip ':' and ',' */
2064 if (token_type(token
) != TOKEN_IDENT
)
2066 label
= label_symbol(token
, 1);
2067 add_symbol(labels
, label
);
2068 token
= token
->next
;
2069 } while (match_op(token
, ','));
2073 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2075 unsigned long mods
= 0;
2077 token
= token
->next
;
2078 stmt
->type
= STMT_ASM
;
2079 while (token_type(token
) == TOKEN_IDENT
) {
2080 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2081 if (s
&& s
->op
->asm_modifier
)
2082 s
->op
->asm_modifier(token
, &mods
, s
->ctype
.modifiers
);
2083 else if (token
->ident
== &goto_ident
)
2084 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2085 token
= token
->next
;
2087 token
= expect(token
, '(', "after asm");
2088 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2089 if (match_op(token
, ':'))
2090 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2091 if (match_op(token
, ':'))
2092 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2093 if (match_op(token
, ':'))
2094 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2095 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2096 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2097 token
= expect(token
, ')', "after asm");
2098 return expect(token
, ';', "at end of asm-statement");
2101 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2103 struct expression
*cond
= NULL
, *message
= NULL
;
2105 token
= expect(token
->next
, '(', "after _Static_assert");
2106 token
= constant_expression(token
, &cond
);
2108 sparse_error(token
->pos
, "Expected constant expression");
2109 if (match_op(token
, ',')) {
2110 token
= token
->next
;
2111 token
= string_expression(token
, &message
, "_Static_assert()");
2115 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2116 token
= expect(token
, ';', "after _Static_assert()");
2118 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
) {
2119 const char *sep
= "", *msg
= "";
2123 msg
= show_string(message
->string
);
2125 sparse_error(cond
->pos
, "static assertion failed%s%s", sep
, msg
);
2131 /* Make a statement out of an expression */
2132 static struct statement
*make_statement(struct expression
*expr
)
2134 struct statement
*stmt
;
2138 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2139 stmt
->expression
= expr
;
2144 * All iterators have two symbols associated with them:
2145 * the "continue" and "break" symbols, which are targets
2146 * for continue and break statements respectively.
2148 * They are in a special name-space, but they follow
2149 * all the normal visibility rules, so nested iterators
2150 * automatically work right.
2152 static void start_iterator(struct statement
*stmt
)
2154 struct symbol
*cont
, *brk
;
2156 start_block_scope(stmt
->pos
);
2157 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2158 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2159 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2160 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2162 stmt
->type
= STMT_ITERATOR
;
2163 stmt
->iterator_break
= brk
;
2164 stmt
->iterator_continue
= cont
;
2165 fn_local_symbol(brk
);
2166 fn_local_symbol(cont
);
2169 static void end_iterator(struct statement
*stmt
)
2174 static struct statement
*start_function(struct symbol
*sym
)
2177 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2179 start_function_scope(sym
->pos
);
2180 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2181 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2182 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2183 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2184 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2186 fn_local_symbol(ret
);
2188 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2194 static void end_function(struct symbol
*sym
)
2197 end_function_scope();
2201 * A "switch()" statement, like an iterator, has a
2202 * the "break" symbol associated with it. It works
2203 * exactly like the iterator break - it's the target
2204 * for any break-statements in scope, and means that
2205 * "break" handling doesn't even need to know whether
2206 * it's breaking out of an iterator or a switch.
2208 * In addition, the "case" symbol is a marker for the
2209 * case/default statements to find the switch statement
2210 * that they are associated with.
2212 static void start_switch(struct statement
*stmt
)
2214 struct symbol
*brk
, *switch_case
;
2216 start_block_scope(stmt
->pos
);
2217 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2218 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2220 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2221 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2222 switch_case
->stmt
= stmt
;
2224 stmt
->type
= STMT_SWITCH
;
2225 stmt
->switch_break
= brk
;
2226 stmt
->switch_case
= switch_case
;
2228 fn_local_symbol(brk
);
2229 fn_local_symbol(switch_case
);
2232 static void end_switch(struct statement
*stmt
)
2234 if (!stmt
->switch_case
->symbol_list
)
2235 warning(stmt
->pos
, "switch with no cases");
2239 static void add_case_statement(struct statement
*stmt
)
2241 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2245 sparse_error(stmt
->pos
, "not in switch scope");
2246 stmt
->type
= STMT_NONE
;
2249 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2250 add_symbol(&target
->symbol_list
, sym
);
2252 stmt
->case_label
= sym
;
2253 fn_local_symbol(sym
);
2256 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2258 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2261 error_die(token
->pos
, "internal error: return without a function target");
2262 stmt
->type
= STMT_RETURN
;
2263 stmt
->ret_target
= target
;
2264 return expression_statement(token
->next
, &stmt
->ret_value
);
2267 static void validate_for_loop_decl(struct symbol
*sym
)
2269 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2271 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2272 const char *name
= show_ident(sym
->ident
);
2273 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2274 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2278 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2280 struct symbol_list
*syms
;
2281 struct expression
*e1
, *e2
, *e3
;
2282 struct statement
*iterator
;
2284 start_iterator(stmt
);
2285 token
= expect(token
->next
, '(', "after 'for'");
2289 /* C99 variable declaration? */
2290 if (lookup_type(token
)) {
2291 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2293 token
= parse_expression(token
, &e1
);
2294 token
= expect(token
, ';', "in 'for'");
2296 token
= parse_expression(token
, &e2
);
2297 token
= expect(token
, ';', "in 'for'");
2298 token
= parse_expression(token
, &e3
);
2299 token
= expect(token
, ')', "in 'for'");
2300 token
= statement(token
, &iterator
);
2302 stmt
->iterator_syms
= syms
;
2303 stmt
->iterator_pre_statement
= make_statement(e1
);
2304 stmt
->iterator_pre_condition
= e2
;
2305 stmt
->iterator_post_statement
= make_statement(e3
);
2306 stmt
->iterator_post_condition
= NULL
;
2307 stmt
->iterator_statement
= iterator
;
2313 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2315 struct expression
*expr
;
2316 struct statement
*iterator
;
2318 start_iterator(stmt
);
2319 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2320 token
= statement(token
, &iterator
);
2322 stmt
->iterator_pre_condition
= expr
;
2323 stmt
->iterator_post_condition
= NULL
;
2324 stmt
->iterator_statement
= iterator
;
2330 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2332 struct expression
*expr
;
2333 struct statement
*iterator
;
2335 start_iterator(stmt
);
2336 token
= statement(token
->next
, &iterator
);
2337 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2338 token
= token
->next
;
2340 sparse_error(token
->pos
, "expected 'while' after 'do'");
2341 token
= parens_expression(token
, &expr
, "after 'do-while'");
2343 stmt
->iterator_post_condition
= expr
;
2344 stmt
->iterator_statement
= iterator
;
2347 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2348 warning(iterator
->pos
, "do-while statement is not a compound statement");
2350 return expect(token
, ';', "after statement");
2353 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2355 stmt
->type
= STMT_IF
;
2356 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2357 token
= statement(token
, &stmt
->if_true
);
2358 if (token_type(token
) != TOKEN_IDENT
)
2360 if (token
->ident
!= &else_ident
)
2362 return statement(token
->next
, &stmt
->if_false
);
2365 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2367 stmt
->type
= STMT_CASE
;
2368 token
= expect(token
, ':', "after default/case");
2369 add_case_statement(stmt
);
2370 if (match_op(token
, '}')) {
2371 warning(token
->pos
, "statement expected after case label");
2372 stmt
->case_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2375 return statement(token
, &stmt
->case_statement
);
2378 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2380 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2381 if (match_op(token
, SPECIAL_ELLIPSIS
))
2382 token
= parse_expression(token
->next
, &stmt
->case_to
);
2383 return case_statement(token
, stmt
);
2386 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2388 return case_statement(token
->next
, stmt
);
2391 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2393 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2394 stmt
->type
= STMT_GOTO
;
2395 stmt
->goto_label
= target
;
2397 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2398 return expect(token
->next
, ';', "at end of statement");
2401 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2403 stmt
->type
= STMT_SWITCH
;
2405 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2406 token
= statement(token
, &stmt
->switch_statement
);
2411 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2413 const char *id
= show_ident(ident
);
2414 sparse_error(use
, "label '%s' used outside statement expression", id
);
2415 info(def
, " label '%s' defined here", id
);
2416 current_fn
->bogus_linear
= 1;
2419 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2421 struct statement
*def
= label
->stmt
;
2424 if (!is_in_scope(def
->label_scope
, label_scope
))
2425 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2426 } else if (!label
->label_scope
) {
2427 label
->label_scope
= label_scope
;
2428 label
->label_pos
= use_pos
;
2432 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2434 stmt
->type
= STMT_GOTO
;
2435 token
= token
->next
;
2436 if (match_op(token
, '*')) {
2437 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2438 add_statement(&function_computed_goto_list
, stmt
);
2439 } else if (token_type(token
) == TOKEN_IDENT
) {
2440 struct symbol
*label
= label_symbol(token
, 1);
2441 stmt
->goto_label
= label
;
2442 check_label_usage(label
, stmt
->pos
);
2443 token
= token
->next
;
2445 sparse_error(token
->pos
, "Expected identifier or goto expression");
2447 return expect(token
, ';', "at end of statement");
2450 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2452 stmt
->type
= STMT_CONTEXT
;
2453 token
= token
->next
;
2454 token
= expect(token
, '(', "after __context__ statement");
2455 token
= assignment_expression(token
, &stmt
->expression
);
2456 if (!stmt
->expression
)
2457 unexpected(token
, "expression expected after '('");
2458 if (match_op(token
, ',')) {
2459 token
= token
->next
;
2460 stmt
->context
= stmt
->expression
;
2461 token
= assignment_expression(token
, &stmt
->expression
);
2462 if (!stmt
->expression
)
2463 unexpected(token
, "expression expected after ','");
2465 token
= expect(token
, ')', "at end of __context__ statement");
2466 return expect(token
, ';', "at end of statement");
2469 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2471 stmt
->type
= STMT_RANGE
;
2472 token
= token
->next
;
2473 token
= expect(token
, '(', "after __range__ statement");
2474 token
= assignment_expression(token
, &stmt
->range_expression
);
2475 token
= expect(token
, ',', "after range expression");
2476 token
= assignment_expression(token
, &stmt
->range_low
);
2477 token
= expect(token
, ',', "after low range");
2478 token
= assignment_expression(token
, &stmt
->range_high
);
2479 token
= expect(token
, ')', "after range statement");
2480 return expect(token
, ';', "after range statement");
2483 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2485 struct decl_state ctx
= { };
2487 token
= handle_attributes(token
, &ctx
);
2488 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2492 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2494 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2497 if (token_type(token
) == TOKEN_IDENT
) {
2498 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2499 if (s
&& s
->op
->statement
)
2500 return s
->op
->statement(token
, stmt
);
2502 if (match_op(token
->next
, ':')) {
2503 struct symbol
*s
= label_symbol(token
, 0);
2504 token
= handle_label_attributes(token
->next
->next
, s
);
2506 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2507 // skip the label to avoid multiple definitions
2508 return statement(token
, tree
);
2510 stmt
->type
= STMT_LABEL
;
2511 stmt
->label_identifier
= s
;
2512 stmt
->label_scope
= label_scope
;
2513 if (s
->label_scope
) {
2514 if (!is_in_scope(label_scope
, s
->label_scope
))
2515 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2518 if (match_op(token
, '}')) {
2519 warning(token
->pos
, "statement expected after label");
2520 stmt
->label_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2523 return statement(token
, &stmt
->label_statement
);
2527 if (match_op(token
, '{')) {
2528 token
= compound_statement(token
->next
, stmt
);
2529 return expect(token
, '}', "at end of compound statement");
2532 stmt
->type
= STMT_EXPRESSION
;
2533 return expression_statement(token
, &stmt
->expression
);
2536 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2537 static struct token
*label_statement(struct token
*token
)
2539 while (token_type(token
) == TOKEN_IDENT
) {
2540 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2541 /* it's block-scope, but we want label namespace */
2542 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2543 fn_local_symbol(sym
);
2544 token
= token
->next
;
2545 if (!match_op(token
, ','))
2547 token
= token
->next
;
2549 return expect(token
, ';', "at end of label declaration");
2552 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2554 int seen_statement
= 0;
2555 while (token_type(token
) == TOKEN_IDENT
&&
2556 token
->ident
== &__label___ident
)
2557 token
= label_statement(token
->next
);
2559 struct statement
* stmt
;
2560 if (eof_token(token
))
2562 if (match_op(token
, '}'))
2564 if (match_ident(token
, &_Static_assert_ident
)) {
2565 token
= parse_static_assert(token
, NULL
);
2568 if (lookup_type(token
)) {
2569 if (seen_statement
) {
2570 warning(token
->pos
, "mixing declarations and code");
2573 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2574 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2576 seen_statement
= Wdeclarationafterstatement
;
2577 token
= statement(token
, &stmt
);
2579 add_statement(list
, stmt
);
2584 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2586 struct symbol_list
**list
= &fn
->arguments
;
2588 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2589 sym
->ident
= token
->ident
;
2590 token
= token
->next
;
2591 sym
->endpos
= token
->pos
;
2592 sym
->ctype
.base_type
= &incomplete_ctype
;
2593 add_symbol(list
, sym
);
2594 if (!match_op(token
, ',') ||
2595 token_type(token
->next
) != TOKEN_IDENT
||
2596 lookup_type(token
->next
))
2598 token
= token
->next
;
2603 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2605 struct symbol_list
**list
= &fn
->arguments
;
2610 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2612 token
= token
->next
;
2616 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2617 token
= parameter_declaration(token
, sym
);
2618 if (sym
->ctype
.base_type
== &void_ctype
) {
2619 /* Special case: (void) */
2620 if (!*list
&& !sym
->ident
)
2622 warning(token
->pos
, "void parameter");
2624 add_symbol(list
, sym
);
2625 if (!match_op(token
, ','))
2627 token
= token
->next
;
2632 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2634 stmt
->type
= STMT_COMPOUND
;
2635 start_block_scope(token
->pos
);
2636 token
= statement_list(token
, &stmt
->stmts
);
2641 static struct expression
*identifier_expression(struct token
*token
)
2643 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2644 expr
->expr_ident
= token
->ident
;
2648 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2650 int idx_from
, idx_to
;
2651 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2653 idx_from
= const_expression_value(from
);
2656 idx_to
= const_expression_value(to
);
2657 if (idx_to
< idx_from
|| idx_from
< 0)
2658 warning(from
->pos
, "nonsense array initializer index range");
2660 expr
->idx_from
= idx_from
;
2661 expr
->idx_to
= idx_to
;
2665 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2667 int expect_equal
= 0;
2668 struct token
*next
= token
->next
;
2669 struct expression
**tail
= ep
;
2674 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2675 struct expression
*expr
= identifier_expression(token
);
2676 if (Wold_initializer
)
2677 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2678 token
= initializer(&expr
->ident_expression
, next
->next
);
2679 if (expr
->ident_expression
)
2684 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2685 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2686 struct expression
*expr
= identifier_expression(next
);
2688 tail
= &expr
->ident_expression
;
2691 } else if (match_op(token
, '[')) {
2692 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2693 token
= constant_expression(token
->next
, &from
);
2695 sparse_error(token
->pos
, "Expected constant expression");
2698 if (match_op(token
, SPECIAL_ELLIPSIS
))
2699 token
= constant_expression(token
->next
, &to
);
2700 expr
= index_expression(from
, to
);
2702 tail
= &expr
->idx_expression
;
2703 token
= expect(token
, ']', "at end of initializer index");
2710 if (nested
&& !expect_equal
) {
2711 if (!match_op(token
, '='))
2712 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2717 token
= expect(token
, '=', "at end of initializer index");
2719 token
= initializer(tail
, token
);
2725 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2727 struct expression
*expr
;
2730 token
= single_initializer(&expr
, token
);
2733 add_expression(list
, expr
);
2734 if (!match_op(token
, ','))
2736 token
= token
->next
;
2741 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2743 if (match_op(token
, '{')) {
2744 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2746 if (!Wuniversal_initializer
) {
2747 struct token
*next
= token
->next
;
2748 // '{ 0 }' is equivalent to '{ }' except for some
2749 // warnings, like using 0 to initialize a null-pointer.
2750 if (match_token_zero(next
)) {
2751 if (match_op(next
->next
, '}'))
2752 expr
->zero_init
= 1;
2756 token
= initializer_list(&expr
->expr_list
, token
->next
);
2757 return expect(token
, '}', "at end of initializer");
2759 return assignment_expression(token
, tree
);
2762 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2765 sparse_error(sym
->pos
, "no identifier for function argument");
2768 if (sym
->ctype
.base_type
== &incomplete_ctype
) {
2769 sym
->ctype
.base_type
= &int_ctype
;
2771 if (Wimplicit_int
) {
2772 sparse_error(sym
->pos
, "missing type declaration for parameter '%s'",
2773 show_ident(sym
->ident
));
2776 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2779 static int is_syscall(struct symbol
*sym
)
2785 macro
= get_macro_name(sym
->pos
);
2787 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2788 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2791 name
= sym
->ident
->name
;
2793 if (name
&& strncmp(name
, "sys_", 4) ==0)
2796 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2803 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2804 struct symbol_list
**list
)
2806 struct symbol_list
**old_symbol_list
;
2807 struct symbol
*base_type
= decl
->ctype
.base_type
;
2808 struct statement
*stmt
, **p
;
2809 struct symbol
*prev
;
2812 old_symbol_list
= function_symbol_list
;
2813 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2814 function_symbol_list
= &decl
->inline_symbol_list
;
2815 p
= &base_type
->inline_stmt
;
2817 function_symbol_list
= &decl
->symbol_list
;
2818 p
= &base_type
->stmt
;
2820 function_computed_target_list
= NULL
;
2821 function_computed_goto_list
= NULL
;
2823 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2824 if (Wexternal_function_has_definition
)
2825 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2827 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2828 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2830 stmt
= start_function(decl
);
2833 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2834 declare_argument(arg
, base_type
);
2835 } END_FOR_EACH_PTR(arg
);
2837 token
= statement_list(token
->next
, &stmt
->stmts
);
2840 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2841 add_symbol(list
, decl
);
2842 else if (is_syscall(decl
)) {
2843 add_symbol(list
, decl
);
2845 printf("parse.c decl: %s\n", decl->ident->name);
2846 char *macro = get_macro_name(decl->pos);
2847 printf("decl macro: %s\n", macro);
2850 check_declaration(decl
);
2851 decl
->definition
= decl
;
2852 prev
= decl
->same_symbol
;
2853 if (prev
&& prev
->definition
) {
2854 warning(decl
->pos
, "multiple definitions for function '%s'",
2855 show_ident(decl
->ident
));
2856 info(prev
->definition
->pos
, " the previous one is here");
2859 rebind_scope(prev
, decl
->scope
);
2860 prev
->definition
= decl
;
2861 prev
= prev
->same_symbol
;
2864 function_symbol_list
= old_symbol_list
;
2865 if (function_computed_goto_list
) {
2866 if (!function_computed_target_list
)
2867 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2869 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2870 stmt
->target_list
= function_computed_target_list
;
2871 } END_FOR_EACH_PTR(stmt
);
2874 return expect(token
, '}', "at end of function");
2877 static void promote_k_r_types(struct symbol
*arg
)
2879 struct symbol
*base
= arg
->ctype
.base_type
;
2880 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2881 arg
->ctype
.base_type
= &int_ctype
;
2885 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2887 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2890 FOR_EACH_PTR(real_args
, arg
) {
2891 struct symbol
*type
;
2893 /* This is quadratic in the number of arguments. We _really_ don't care */
2894 FOR_EACH_PTR(argtypes
, type
) {
2895 if (type
->ident
== arg
->ident
)
2897 } END_FOR_EACH_PTR(type
);
2898 if (Wimplicit_int
) {
2899 warning(arg
->pos
, "missing type declaration for parameter '%s'",
2900 show_ident(arg
->ident
));
2902 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
2903 type
->ident
= arg
->ident
;
2904 type
->ctype
.base_type
= &int_ctype
;
2907 /* "char" and "short" promote to "int" */
2908 promote_k_r_types(type
);
2910 arg
->ctype
= type
->ctype
;
2911 } END_FOR_EACH_PTR(arg
);
2913 FOR_EACH_PTR(argtypes
, arg
) {
2915 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2916 } END_FOR_EACH_PTR(arg
);
2920 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2921 struct symbol_list
**list
)
2923 struct symbol_list
*args
= NULL
;
2925 if (Wold_style_definition
)
2926 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2929 token
= declaration_list(token
, &args
);
2930 if (!match_op(token
, ';')) {
2931 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2934 token
= token
->next
;
2935 } while (lookup_type(token
));
2937 apply_k_r_types(args
, decl
);
2939 if (!match_op(token
, '{')) {
2940 sparse_error(token
->pos
, "expected function body");
2943 return parse_function_body(token
, decl
, list
);
2946 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2948 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2949 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2950 struct statement
*stmt
;
2952 anon
->ctype
.base_type
= fn
;
2953 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2956 token
= parse_asm_statement(token
, stmt
);
2958 // FIXME: add_symbol(list, anon);
2962 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2963 validate_decl_t validate_decl
)
2965 struct ident
*ident
= NULL
;
2966 struct symbol
*decl
;
2967 struct decl_state ctx
= { .ident
= &ident
};
2969 struct symbol
*base_type
;
2973 if (match_ident(token
, &_Pragma_ident
))
2974 return parse_underscore_Pragma(token
);
2976 /* Top-level inline asm or static assertion? */
2977 if (token_type(token
) == TOKEN_IDENT
) {
2978 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2979 if (s
&& s
->op
->toplevel
)
2980 return s
->op
->toplevel(token
, list
);
2983 /* Parse declaration-specifiers, if any */
2984 token
= declaration_specifiers(token
, &ctx
);
2985 mod
= decl_modifiers(&ctx
);
2986 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2987 /* Just a type declaration? */
2988 if (match_op(token
, ';')) {
2989 apply_modifiers(token
->pos
, &ctx
);
2994 token
= declarator(token
, &ctx
);
2995 token
= handle_asm_name(token
, &ctx
);
2996 token
= handle_attributes(token
, &ctx
);
2997 apply_modifiers(token
->pos
, &ctx
);
2999 decl
->ctype
= ctx
.ctype
;
3000 decl
->ctype
.modifiers
|= mod
;
3001 decl
->cleanup
= ctx
.cleanup
;
3002 decl
->endpos
= token
->pos
;
3004 /* Just a type declaration? */
3006 warning(token
->pos
, "missing identifier in declaration");
3007 return expect(token
, ';', "at the end of type declaration");
3010 /* type define declaration? */
3011 is_typedef
= ctx
.storage_class
== MOD_USERTYPE
;
3013 /* Typedefs don't have meaningful storage */
3015 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3017 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3019 base_type
= decl
->ctype
.base_type
;
3022 if (base_type
&& !base_type
->ident
) {
3023 switch (base_type
->type
) {
3028 base_type
->ident
= ident
;
3034 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3035 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3036 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3037 show_ident(decl
->ident
));
3038 base_type
->ctype
.base_type
= &int_ctype
;
3040 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3041 warning(decl
->pos
, "'%s()' has implicit return type",
3042 show_ident(decl
->ident
));
3043 base_type
->ctype
.base_type
= &int_ctype
;
3045 /* apply attributes placed after the declarator */
3046 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3048 /* K&R argument declaration? */
3049 if (lookup_type(token
))
3050 return parse_k_r_arguments(token
, decl
, list
);
3051 if (match_op(token
, '{'))
3052 return parse_function_body(token
, decl
, list
);
3054 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3055 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3056 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3057 sparse_error(token
->pos
, "void declaration");
3059 if (base_type
== &incomplete_ctype
) {
3060 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3061 decl
->ctype
.base_type
= &int_ctype
;;
3065 if (!is_typedef
&& match_op(token
, '=')) {
3066 struct token
*next
= token
->next
;
3067 token
= initializer(&decl
->initializer
, next
);
3069 sparse_error(token
->pos
, "expression expected before '%s'", show_token(token
));
3073 validate_decl(decl
);
3075 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3076 warning(decl
->pos
, "symbol with external linkage has initializer");
3077 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3080 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3081 add_symbol(list
, decl
);
3082 fn_local_symbol(decl
);
3085 check_declaration(decl
);
3086 if (decl
->same_symbol
) {
3087 decl
->definition
= decl
->same_symbol
->definition
;
3088 decl
->op
= decl
->same_symbol
->op
;
3090 // TODO: handle -std=c89 --pedantic
3091 check_duplicates(decl
);
3096 const char *msg
= NULL
;
3097 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3098 msg
= "on non-identifier";
3099 else if (match_op(token
, ','))
3100 msg
= "on declaration list";
3101 else if (!decl
->initializer
)
3102 msg
= "without initializer";
3103 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3104 decl
->initializer
->symbol
== decl
)
3105 msg
= "on self-init var";
3107 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3108 decl
->ctype
.base_type
= &bad_ctype
;
3112 if (!match_op(token
, ','))
3115 token
= token
->next
;
3117 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3119 token
= handle_attributes(token
, &ctx
);
3120 token
= declarator(token
, &ctx
);
3121 token
= handle_asm_name(token
, &ctx
);
3122 token
= handle_attributes(token
, &ctx
);
3123 apply_modifiers(token
->pos
, &ctx
);
3124 decl
->ctype
= ctx
.ctype
;
3125 decl
->ctype
.modifiers
|= mod
;
3126 decl
->cleanup
= ctx
.cleanup
;
3127 decl
->endpos
= token
->pos
;
3129 sparse_error(token
->pos
, "expected identifier name in type definition");
3134 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3136 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3138 /* Function declarations are automatically extern unless specifically static */
3139 base_type
= decl
->ctype
.base_type
;
3140 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3141 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3142 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3145 return expect(token
, ';', "at end of declaration");