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_modifier
,
85 attribute_address_space
, attribute_context
,
86 attribute_designated_init
,
87 attribute_transparent_union
, ignore_attribute
,
88 attribute_mode
, attribute_force
;
90 typedef struct symbol
*to_mode_t(struct symbol
*);
93 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
;
94 static to_mode_t to_pointer_mode
, to_word_mode
;
109 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
113 CInt
= 0, CSInt
, CUInt
, CReal
,
116 static void asm_modifier(struct token
*token
, unsigned long *mods
, unsigned long mod
)
119 warning(token
->pos
, "duplicated asm modifier");
123 static struct symbol_op typedef_op
= {
125 .declarator
= storage_specifier
,
128 static struct symbol_op inline_op
= {
130 .declarator
= generic_qualifier
,
131 .asm_modifier
= asm_modifier
,
134 static struct symbol_op noreturn_op
= {
136 .declarator
= generic_qualifier
,
139 static declarator_t alignas_specifier
;
140 static struct symbol_op alignas_op
= {
142 .declarator
= alignas_specifier
,
145 static struct symbol_op auto_op
= {
147 .declarator
= storage_specifier
,
150 static struct symbol_op register_op
= {
152 .declarator
= storage_specifier
,
155 static struct symbol_op static_op
= {
156 .type
= KW_MODIFIER
|KW_STATIC
,
157 .declarator
= storage_specifier
,
160 static struct symbol_op extern_op
= {
162 .declarator
= storage_specifier
,
165 static struct symbol_op thread_op
= {
167 .declarator
= thread_specifier
,
170 static struct symbol_op const_op
= {
171 .type
= KW_QUALIFIER
,
172 .declarator
= generic_qualifier
,
175 static struct symbol_op volatile_op
= {
176 .type
= KW_QUALIFIER
,
177 .declarator
= generic_qualifier
,
178 .asm_modifier
= asm_modifier
,
181 static struct symbol_op restrict_op
= {
182 .type
= KW_QUALIFIER
,
183 .declarator
= generic_qualifier
,
186 static struct symbol_op atomic_op
= {
187 .type
= KW_QUALIFIER
,
188 .declarator
= generic_qualifier
,
191 static struct symbol_op typeof_op
= {
192 .type
= KW_SPECIFIER
,
193 .declarator
= typeof_specifier
,
198 static struct symbol_op autotype_op
= {
199 .type
= KW_SPECIFIER
,
200 .declarator
= autotype_specifier
,
205 static struct symbol_op attribute_op
= {
206 .type
= KW_ATTRIBUTE
,
207 .declarator
= attribute_specifier
,
210 static struct symbol_op struct_op
= {
211 .type
= KW_SPECIFIER
,
212 .declarator
= struct_specifier
,
217 static struct symbol_op union_op
= {
218 .type
= KW_SPECIFIER
,
219 .declarator
= union_specifier
,
224 static struct symbol_op enum_op
= {
225 .type
= KW_SPECIFIER
,
226 .declarator
= enum_specifier
,
231 static struct symbol_op spec_op
= {
232 .type
= KW_SPECIFIER
| KW_EXACT
,
237 static struct symbol_op char_op
= {
238 .type
= KW_SPECIFIER
,
239 .test
= Set_T
|Set_Long
|Set_Short
,
240 .set
= Set_T
|Set_Char
,
244 static struct symbol_op int_op
= {
245 .type
= KW_SPECIFIER
,
247 .set
= Set_T
|Set_Int
,
250 static struct symbol_op double_op
= {
251 .type
= KW_SPECIFIER
,
252 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
253 .set
= Set_T
|Set_Double
,
257 static struct symbol_op float_op
= {
258 .type
= KW_SPECIFIER
,
259 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
260 .set
= Set_T
|Set_Float
,
264 static struct symbol_op short_op
= {
265 .type
= KW_SPECIFIER
,
266 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
270 static struct symbol_op signed_op
= {
271 .type
= KW_SPECIFIER
,
272 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
277 static struct symbol_op unsigned_op
= {
278 .type
= KW_SPECIFIER
,
279 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
284 static struct symbol_op long_op
= {
285 .type
= KW_SPECIFIER
,
286 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
290 static struct symbol_op int128_op
= {
291 .type
= KW_SPECIFIER
,
292 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
293 .set
= Set_T
|Set_Int128
|Set_Vlong
,
297 static struct symbol_op if_op
= {
298 .statement
= parse_if_statement
,
301 static struct symbol_op return_op
= {
302 .statement
= parse_return_statement
,
305 static struct symbol_op loop_iter_op
= {
306 .statement
= parse_loop_iterator
,
309 static struct symbol_op default_op
= {
310 .statement
= parse_default_statement
,
313 static struct symbol_op case_op
= {
314 .statement
= parse_case_statement
,
317 static struct symbol_op switch_op
= {
318 .statement
= parse_switch_statement
,
321 static struct symbol_op for_op
= {
322 .statement
= parse_for_statement
,
325 static struct symbol_op while_op
= {
326 .statement
= parse_while_statement
,
329 static struct symbol_op do_op
= {
330 .statement
= parse_do_statement
,
333 static struct symbol_op goto_op
= {
334 .statement
= parse_goto_statement
,
337 static struct symbol_op __context___op
= {
338 .statement
= parse_context_statement
,
339 .attribute
= attribute_context
,
342 static struct symbol_op range_op
= {
343 .statement
= parse_range_statement
,
346 static struct symbol_op asm_op
= {
348 .statement
= parse_asm_statement
,
349 .toplevel
= toplevel_asm_declaration
,
352 static struct symbol_op static_assert_op
= {
353 .toplevel
= parse_static_assert
,
356 static struct symbol_op packed_op
= {
357 .attribute
= attribute_packed
,
360 static struct symbol_op aligned_op
= {
361 .attribute
= attribute_aligned
,
364 static struct symbol_op attr_mod_op
= {
365 .attribute
= attribute_modifier
,
368 static struct symbol_op attr_fun_op
= {
369 .attribute
= attribute_function
,
372 static struct symbol_op attr_bitwise_op
= {
373 .attribute
= attribute_bitwise
,
376 static struct symbol_op attr_force_op
= {
377 .attribute
= attribute_force
,
380 static struct symbol_op address_space_op
= {
381 .attribute
= attribute_address_space
,
384 static struct symbol_op mode_op
= {
385 .attribute
= attribute_mode
,
388 static struct symbol_op context_op
= {
389 .attribute
= attribute_context
,
392 static struct symbol_op designated_init_op
= {
393 .attribute
= attribute_designated_init
,
396 static struct symbol_op transparent_union_op
= {
397 .attribute
= attribute_transparent_union
,
400 static struct symbol_op ignore_attr_op
= {
401 .attribute
= ignore_attribute
,
404 static struct symbol_op mode_QI_op
= {
406 .to_mode
= to_QI_mode
409 static struct symbol_op mode_HI_op
= {
411 .to_mode
= to_HI_mode
414 static struct symbol_op mode_SI_op
= {
416 .to_mode
= to_SI_mode
419 static struct symbol_op mode_DI_op
= {
421 .to_mode
= to_DI_mode
424 static struct symbol_op mode_TI_op
= {
426 .to_mode
= to_TI_mode
429 static struct symbol_op mode_pointer_op
= {
431 .to_mode
= to_pointer_mode
434 static struct symbol_op mode_word_op
= {
436 .to_mode
= to_word_mode
440 * Define the keyword and their effects.
441 * The entries in the 'typedef' and put in NS_TYPEDEF and
442 * are automatically set as reserved keyword while the ones
443 * in the 'keyword' table are just put in NS_KEYWORD.
445 * The entries are added via the 3 macros:
446 * N() for entries with "name" only,
447 * D() for entries with "name" & "__name__",
448 * A() for entries with "name", "__name" & "__name__",
449 * U() for entries with "__name" & "__name__".
451 static struct init_keyword
{
453 struct symbol_op
*op
;
457 #define N(I, O,...) { I, O,##__VA_ARGS__ }
458 #define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
459 N("__" I "__",O,##__VA_ARGS__)
460 #define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
461 N("__" I,O,##__VA_ARGS__), \
462 N("__" I "__",O,##__VA_ARGS__)
463 #define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
464 N("__" I "__",O,##__VA_ARGS__)
465 /* Storage classes */
466 N("auto", &auto_op
, .mods
= MOD_AUTO
),
467 N("register", ®ister_op
, .mods
= MOD_REGISTER
),
468 N("static", &static_op
, .mods
= MOD_STATIC
),
469 N("extern", &extern_op
, .mods
= MOD_EXTERN
),
470 N("__thread", &thread_op
),
471 N("_Thread_local", &thread_op
),
473 A("inline", &inline_op
, .mods
= MOD_INLINE
),
476 N("typedef", &typedef_op
, .mods
= MOD_USERTYPE
),
477 A("typeof", &typeof_op
),
478 N("__auto_type", &autotype_op
),
480 /* Type qualifiers */
481 A("const", &const_op
, .mods
= MOD_CONST
),
482 A("volatile", &volatile_op
, .mods
= MOD_VOLATILE
),
483 A("restrict", &restrict_op
, .mods
= MOD_RESTRICT
),
485 N("_Atomic", &atomic_op
, .mods
= MOD_ATOMIC
),
486 N("_Noreturn", &noreturn_op
, .mods
= MOD_NORETURN
),
487 N("_Alignas", &alignas_op
),
489 U("attribute", &attribute_op
),
491 /* Type specifiers */
492 N("struct", &struct_op
),
493 N("union", &union_op
),
496 N("void", &spec_op
, .type
= &void_ctype
),
498 N("short", &short_op
),
501 N("float", &float_op
),
502 N("double", &double_op
),
503 A("signed", &signed_op
),
504 N("unsigned", &unsigned_op
),
505 N("__int128", &int128_op
),
506 N("_Bool", &spec_op
, .type
= &bool_ctype
),
508 /* Predeclared types */
509 N("__builtin_va_list", &spec_op
, .type
= &ptr_ctype
),
510 N("__builtin_ms_va_list",&spec_op
, .type
= &ptr_ctype
),
511 N("__int128_t", &spec_op
, .type
= &sint128_ctype
),
512 N("__uint128_t", &spec_op
, .type
= &uint128_ctype
),
513 N("_Float32", &spec_op
, .type
= &float32_ctype
),
514 N("_Float32x", &spec_op
, .type
= &float32x_ctype
),
515 N("_Float64", &spec_op
, .type
= &float64_ctype
),
516 N("_Float64x", &spec_op
, .type
= &float64x_ctype
),
517 N("_Float128", &spec_op
, .type
= &float128_ctype
),
521 N("return", &return_op
),
522 N("break", &loop_iter_op
),
523 N("continue", &loop_iter_op
),
524 N("default", &default_op
),
526 N("switch", &switch_op
),
528 N("while", &while_op
),
532 N("context", &context_op
),
533 N("__context__", &__context___op
),
534 N("__range__", &range_op
),
535 N("_Static_assert", &static_assert_op
),
538 D("packed", &packed_op
),
539 D("aligned", &aligned_op
),
540 D("nocast", &attr_mod_op
, .mods
= MOD_NOCAST
),
541 D("noderef", &attr_mod_op
, .mods
= MOD_NODEREF
),
542 D("safe", &attr_mod_op
, .mods
= MOD_SAFE
),
543 D("unused", &attr_mod_op
, .mods
= MOD_UNUSED
),
544 D("externally_visible", &attr_mod_op
, .mods
= MOD_EXT_VISIBLE
),
545 D("force", &attr_force_op
),
546 D("bitwise", &attr_bitwise_op
, .mods
= MOD_BITWISE
),
547 D("address_space", &address_space_op
),
548 D("designated_init", &designated_init_op
),
549 D("transparent_union", &transparent_union_op
),
550 D("noreturn", &attr_fun_op
, .mods
= MOD_NORETURN
),
551 D("pure", &attr_fun_op
, .mods
= MOD_PURE
),
552 A("const", &attr_fun_op
, .mods
= MOD_PURE
),
553 D("gnu_inline", &attr_fun_op
, .mods
= MOD_GNU_INLINE
),
557 D("QI", &mode_QI_op
),
558 D("HI", &mode_HI_op
),
559 D("SI", &mode_SI_op
),
560 D("DI", &mode_DI_op
),
561 D("TI", &mode_TI_op
),
562 D("byte", &mode_QI_op
),
563 D("pointer", &mode_pointer_op
),
564 D("word", &mode_word_op
),
568 static const char *ignored_attributes
[] = {
570 #define GCC_ATTR(x) \
572 STRINGIFY(__##x##__),
574 #include "gcc-attr-list.h"
586 static void init_keyword(int stream
, struct init_keyword
*kw
, enum namespace ns
)
588 struct symbol
*sym
= create_symbol(stream
, kw
->name
, SYM_KEYWORD
, ns
);
589 sym
->ident
->keyword
= 1;
590 sym
->ident
->reserved
|= (ns
== NS_TYPEDEF
);
591 sym
->ctype
.modifiers
= kw
->mods
;
592 sym
->ctype
.base_type
= kw
->type
;
596 void init_parser(int stream
)
600 for (i
= 0; i
< ARRAY_SIZE(typedefs
); i
++)
601 init_keyword(stream
, &typedefs
[i
], NS_TYPEDEF
);
602 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
603 init_keyword(stream
, &keywords
[i
], NS_KEYWORD
);
605 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
606 const char * name
= ignored_attributes
[i
];
607 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
610 sym
->ident
->keyword
= 1;
611 sym
->op
= &ignore_attr_op
;
617 static struct token
*skip_to(struct token
*token
, int op
)
619 while (!match_op(token
, op
) && !eof_token(token
))
624 static struct token bad_token
= { .pos
.type
= TOKEN_BAD
};
625 struct token
*expect(struct token
*token
, int op
, const char *where
)
627 if (!match_op(token
, op
)) {
628 if (token
!= &bad_token
) {
629 bad_token
.next
= token
;
630 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
631 sparse_error(token
->pos
, "got %s", show_token(token
));
634 return skip_to(token
, op
);
641 // issue an error message on new parsing errors
642 // @token: the current token
643 // @errmsg: the error message
644 // If the current token is from a previous error, an error message
645 // has already been issued, so nothing more is done.
646 // Otherwise, @errmsg is displayed followed by the current token.
647 static void unexpected(struct token
*token
, const char *errmsg
)
649 if (token
== &bad_token
)
651 sparse_error(token
->pos
, "%s", errmsg
);
652 sparse_error(token
->pos
, "got %s", show_token(token
));
655 // Add a symbol to the list of function-local symbols
656 static void fn_local_symbol(struct symbol
*sym
)
658 if (function_symbol_list
)
659 add_symbol(function_symbol_list
, sym
);
662 struct statement
*alloc_statement(struct position pos
, int type
)
664 struct statement
*stmt
= __alloc_statement(0);
670 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
672 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
674 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
676 struct symbol
*ctype
;
679 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
681 sparse_error(pos
, "don't know how to apply mode to %s",
682 show_typename(ctx
->ctype
.base_type
));
684 ctx
->ctype
.base_type
= ctype
;
688 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
690 struct symbol
*sym
= alloc_symbol(pos
, type
);
692 sym
->ctype
.base_type
= ctype
->base_type
;
693 sym
->ctype
.modifiers
= ctype
->modifiers
;
695 ctype
->base_type
= sym
;
696 ctype
->modifiers
= 0;
701 * NOTE! NS_LABEL is not just a different namespace,
702 * it also ends up using function scope instead of the
703 * regular symbol scope.
705 struct symbol
*label_symbol(struct token
*token
, int used
)
707 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
709 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
710 bind_symbol(sym
, token
->ident
, NS_LABEL
);
713 fn_local_symbol(sym
);
718 static struct token
*struct_union_enum_specifier(enum type type
,
719 struct token
*token
, struct decl_state
*ctx
,
720 struct token
*(*parse
)(struct token
*, struct symbol
*))
723 struct position
*repos
;
725 token
= handle_attributes(token
, ctx
);
726 if (token_type(token
) == TOKEN_IDENT
) {
727 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
729 (is_outer_scope(sym
->scope
) &&
730 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
731 // Either a new symbol, or else an out-of-scope
732 // symbol being redefined.
733 sym
= alloc_symbol(token
->pos
, type
);
734 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
736 if (sym
->type
!= type
)
737 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
738 ctx
->ctype
.base_type
= sym
;
741 if (match_op(token
, '{')) {
742 struct decl_state attr
= { .ctype
.base_type
= sym
, };
744 // The following test is actually wrong for empty
745 // structs, but (1) they are not C99, (2) gcc does
746 // the same thing, and (3) it's easier.
747 if (sym
->symbol_list
)
748 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
750 token
= parse(token
->next
, sym
);
751 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
753 token
= handle_attributes(token
, &attr
);
754 apply_ctype(token
->pos
, &attr
.ctype
, &sym
->ctype
);
756 // Mark the structure as needing re-examination
758 sym
->endpos
= token
->pos
;
763 // private struct/union/enum type
764 if (!match_op(token
, '{')) {
765 sparse_error(token
->pos
, "expected declaration");
766 ctx
->ctype
.base_type
= &bad_ctype
;
770 sym
= alloc_symbol(token
->pos
, type
);
771 set_current_scope(sym
); // used by dissect
772 token
= parse(token
->next
, sym
);
773 ctx
->ctype
.base_type
= sym
;
774 token
= expect(token
, '}', "at end of specifier");
775 sym
->endpos
= token
->pos
;
780 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
782 struct symbol
*field
, *last
= NULL
;
784 res
= struct_declaration_list(token
, &sym
->symbol_list
);
785 FOR_EACH_PTR(sym
->symbol_list
, field
) {
787 struct symbol
*base
= field
->ctype
.base_type
;
788 if (base
&& base
->type
== SYM_BITFIELD
)
792 last
->next_subobject
= field
;
794 } END_FOR_EACH_PTR(field
);
798 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
800 return struct_declaration_list(token
, &sym
->symbol_list
);
803 static struct token
*struct_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
805 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
808 static struct token
*union_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
810 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
816 // This allow to use a shift amount as big (or bigger)
817 // than the width of the value to be shifted, in which case
818 // the result is, of course, 0.
819 static unsigned long long rshift(unsigned long long val
, unsigned int n
)
821 if (n
>= (sizeof(val
) * 8))
828 unsigned long long pos
;
831 static void update_range(struct range
*range
, unsigned long long uval
, struct symbol
*vtype
)
833 long long sval
= uval
;
835 if (is_signed_type(vtype
) && (sval
< 0)) {
836 if (sval
< range
->neg
)
839 if (uval
> range
->pos
)
844 static int type_is_ok(struct symbol
*type
, struct range range
)
846 int shift
= type
->bit_size
;
847 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
851 if (rshift(range
.pos
, shift
))
857 if (rshift(~range
.neg
, shift
))
862 static struct range
type_range(struct symbol
*type
)
865 unsigned int size
= type
->bit_size
;
866 unsigned long long max
;
869 if (is_signed_type(type
)) {
870 min
= sign_bit(size
);
874 max
= bits_mask(size
);
882 static int val_in_range(struct range
*range
, long long sval
, struct symbol
*vtype
)
884 unsigned long long uval
= sval
;
886 if (is_signed_type(vtype
) && (sval
< 0))
887 return range
->neg
<= sval
;
889 return uval
<= range
->pos
;
892 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
894 struct range irange
= type_range(&int_ctype
);
897 FOR_EACH_PTR(list
, sym
) {
898 struct expression
*expr
= sym
->initializer
;
899 struct symbol
*ctype
;
901 if (expr
->type
!= EXPR_VALUE
)
904 val
= get_expression_value(expr
);
905 if (is_int_type(ctype
) && val_in_range(&irange
, val
, ctype
)) {
906 expr
->ctype
= &int_ctype
;
909 cast_value(expr
, base_type
, expr
, ctype
);
910 expr
->ctype
= base_type
;
911 } END_FOR_EACH_PTR(sym
);
914 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
916 unsigned long long lastval
= 0;
917 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
918 struct range range
= { };
921 parent
->examined
= 1;
922 parent
->ctype
.base_type
= &int_ctype
;
923 while (token_type(token
) == TOKEN_IDENT
) {
924 struct expression
*expr
= NULL
;
925 struct token
*next
= token
->next
;
926 struct decl_state ctx
= { };
929 // FIXME: only 'deprecated' should be accepted
930 next
= handle_attributes(next
, &ctx
);
932 if (match_op(next
, '=')) {
933 next
= constant_expression(next
->next
, &expr
);
934 lastval
= get_expression_value(expr
);
936 if (expr
&& expr
->ctype
)
940 } else if (is_int_type(ctype
)) {
943 error_die(token
->pos
, "can't increment the last enum member");
947 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
948 expr
->value
= lastval
;
952 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
953 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
954 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
955 sym
->initializer
= expr
;
956 sym
->enum_member
= 1;
957 sym
->ctype
.base_type
= parent
;
958 add_ptr_list(&parent
->symbol_list
, sym
);
960 if (base_type
!= &bad_ctype
) {
961 if (ctype
->type
== SYM_NODE
)
962 ctype
= ctype
->ctype
.base_type
;
963 if (ctype
->type
== SYM_ENUM
) {
967 ctype
= ctype
->ctype
.base_type
;
971 * - if all enums are of the same type, then
972 * the base_type is that type (two first
974 * - if enums are of different types, they
975 * all have to be integer types, and the
976 * base type is at least "int_ctype".
977 * - otherwise the base_type is "bad_ctype".
979 if (!base_type
|| ctype
== &bad_ctype
) {
981 } else if (ctype
== base_type
) {
983 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
984 base_type
= &int_ctype
;
985 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
986 if (!mix_bitwise
++) {
987 warning(expr
->pos
, "mixed bitwiseness");
989 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
990 sparse_error(expr
->pos
, "incompatible restricted type");
991 info(expr
->pos
, " expected: %s", show_typename(base_type
));
992 info(expr
->pos
, " got: %s", show_typename(ctype
));
993 base_type
= &bad_ctype
;
994 } else if (base_type
!= &bad_ctype
) {
995 sparse_error(token
->pos
, "bad enum definition");
996 base_type
= &bad_ctype
;
998 parent
->ctype
.base_type
= base_type
;
1000 if (is_int_type(base_type
)) {
1001 update_range(&range
, lastval
, ctype
);
1005 sym
->endpos
= token
->pos
;
1007 if (!match_op(token
, ','))
1009 token
= token
->next
;
1012 sparse_error(token
->pos
, "empty enum definition");
1013 base_type
= &bad_ctype
;
1015 else if (!is_int_type(base_type
))
1017 else if (type_is_ok(&uint_ctype
, range
))
1018 base_type
= &uint_ctype
;
1019 else if (type_is_ok(&int_ctype
, range
))
1020 base_type
= &int_ctype
;
1021 else if (type_is_ok(&ulong_ctype
, range
))
1022 base_type
= &ulong_ctype
;
1023 else if (type_is_ok(&long_ctype
, range
))
1024 base_type
= &long_ctype
;
1025 else if (type_is_ok(&ullong_ctype
, range
))
1026 base_type
= &ullong_ctype
;
1027 else if (type_is_ok(&llong_ctype
, range
))
1028 base_type
= &llong_ctype
;
1030 base_type
= &bad_ctype
;
1031 parent
->ctype
.base_type
= base_type
;
1032 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1033 parent
->examined
= 0;
1037 cast_enum_list(parent
->symbol_list
, base_type
);
1042 static struct token
*enum_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1044 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1045 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1047 if (!ctype
->base_type
)
1048 ctype
->base_type
= &incomplete_ctype
;
1053 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*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
, &sym
->ctype
, &ctx
->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;
1103 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1105 int alignment
= max_alignment
;
1106 struct expression
*expr
= NULL
;
1108 if (match_op(token
, '(')) {
1109 token
= parens_expression(token
, &expr
, "in attribute");
1111 alignment
= const_expression_value(expr
);
1113 if (alignment
& (alignment
-1)) {
1114 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1116 } else if (alignment
> ctx
->ctype
.alignment
)
1117 ctx
->ctype
.alignment
= alignment
;
1121 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1123 if (*mods
& mod
& ~MOD_DUP_OK
)
1124 warning(*pos
, "duplicate %s", modifier_name(mod
));
1128 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1130 apply_mod(pos
, &ctx
->modifiers
, qual
);
1133 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1135 apply_mod(&token
->pos
, &ctx
->ctype
.modifiers
, attr
->ctype
.modifiers
);
1139 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1141 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1145 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1148 attribute_modifier(token
, attr
, ctx
);
1152 static struct ident
*numerical_address_space(int asn
)
1158 sprintf(buff
, "<asn:%d>", asn
);
1159 return built_in_ident(buff
);
1162 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1164 struct expression
*expr
= NULL
;
1165 struct ident
*as
= NULL
;
1168 token
= expect(token
, '(', "after address_space attribute");
1169 switch (token_type(token
)) {
1171 next
= primary_expression(token
, &expr
);
1172 if (expr
->type
!= EXPR_VALUE
)
1174 as
= numerical_address_space(expr
->value
);
1184 warning(token
->pos
, "invalid address space name");
1187 if (Waddress_space
&& as
) {
1189 sparse_error(token
->pos
,
1190 "multiple address spaces given: %s & %s",
1191 show_as(ctx
->ctype
.as
), show_as(as
));
1194 token
= expect(next
, ')', "after address_space attribute");
1198 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1200 if (ctype
->ctype
.base_type
!= &int_type
)
1202 if (ctype
== &char_ctype
)
1204 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1208 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1210 if (ctype
->ctype
.base_type
!= &int_type
)
1212 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1216 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1218 if (ctype
->ctype
.base_type
!= &int_type
)
1220 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1224 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1226 if (ctype
->ctype
.base_type
!= &int_type
)
1228 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1232 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1234 if (ctype
->ctype
.base_type
!= &int_type
)
1236 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1240 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1242 if (ctype
->ctype
.base_type
!= &int_type
)
1244 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1248 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1250 if (ctype
->ctype
.base_type
!= &int_type
)
1252 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1256 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1258 token
= expect(token
, '(', "after mode attribute");
1259 if (token_type(token
) == TOKEN_IDENT
) {
1260 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1261 if (mode
&& mode
->op
->type
& KW_MODE
)
1262 ctx
->mode
= mode
->op
;
1264 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1265 token
= token
->next
;
1267 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1268 token
= expect(token
, ')', "after mode attribute");
1272 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1274 struct context
*context
= alloc_context();
1275 struct expression
*args
[3];
1278 token
= expect(token
, '(', "after context attribute");
1279 token
= conditional_expression(token
, &args
[0]);
1280 token
= expect(token
, ',', "after context 1st argument");
1281 token
= conditional_expression(token
, &args
[1]);
1282 if (match_op(token
, ',')) {
1283 token
= token
->next
;
1284 token
= conditional_expression(token
, &args
[2]);
1285 token
= expect(token
, ')', "after context 3rd argument");
1286 context
->context
= args
[0];
1289 token
= expect(token
, ')', "after context 2nd argument");
1291 context
->in
= get_expression_value(args
[idx
++]);
1292 context
->out
= get_expression_value(args
[idx
++]);
1293 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1297 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1299 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1300 ctx
->ctype
.base_type
->designated_init
= 1;
1302 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1306 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1308 if (Wtransparent_union
)
1309 warning(token
->pos
, "attribute __transparent_union__");
1311 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1312 ctx
->ctype
.base_type
->transparent_union
= 1;
1314 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1318 static struct token
*recover_unknown_attribute(struct token
*token
)
1320 struct expression
*expr
= NULL
;
1322 if (Wunknown_attribute
)
1323 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1324 token
= token
->next
;
1325 if (match_op(token
, '('))
1326 token
= parens_expression(token
, &expr
, "in attribute");
1330 static struct token
*attribute_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1332 token
= expect(token
, '(', "after attribute");
1333 token
= expect(token
, '(', "after attribute");
1335 while (token_type(token
) == TOKEN_IDENT
) {
1336 struct symbol
*attr
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1337 if (attr
&& attr
->op
->attribute
)
1338 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1340 token
= recover_unknown_attribute(token
);
1342 if (!match_op(token
, ','))
1344 token
= token
->next
;
1347 token
= expect(token
, ')', "after attribute");
1348 token
= expect(token
, ')', "after attribute");
1352 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1354 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1355 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1356 return ctx
->storage_class
| mods
;
1359 static struct token
*storage_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1361 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1362 unsigned long class = sym
->ctype
.modifiers
;
1363 const char *storage
= modifier_name(class);
1365 /* __thread can be used alone, or with extern or static */
1366 if (is_tls
&& (class & ~(MOD_STATIC
|MOD_EXTERN
)))
1367 sparse_error(next
->pos
, "__thread cannot be used with '%s'", storage
);
1368 else if (!ctx
->storage_class
)
1369 ctx
->storage_class
= class;
1370 else if (ctx
->storage_class
== class)
1371 sparse_error(next
->pos
, "duplicate %s", storage
);
1373 sparse_error(next
->pos
, "multiple storage classes");
1377 static struct token
*thread_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1379 /* This GCC extension can be used alone, or with extern or static */
1380 if (!(ctx
->storage_class
& ~(MOD_STATIC
|MOD_EXTERN
))) {
1381 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1383 sparse_error(next
->pos
, "__thread cannot be used with '%s'",
1384 modifier_name(ctx
->storage_class
));
1390 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1396 static struct token
*alignas_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1400 if (!match_op(token
, '(')) {
1401 sparse_error(token
->pos
, "expected '(' after _Alignas");
1404 if (lookup_type(token
->next
)) {
1405 struct symbol
*sym
= NULL
;
1406 token
= typename(token
->next
, &sym
, NULL
);
1407 sym
= examine_symbol_type(sym
);
1408 alignment
= sym
->ctype
.alignment
;
1409 token
= expect(token
, ')', "after _Alignas(...");
1411 struct expression
*expr
= NULL
;
1412 token
= parens_expression(token
, &expr
, "after _Alignas");
1415 alignment
= const_expression_value(expr
);
1418 if (alignment
< 0) {
1419 warning(token
->pos
, "non-positive alignment");
1422 if (alignment
& (alignment
-1)) {
1423 warning(token
->pos
, "non-power-of-2 alignment");
1426 if (alignment
> ctx
->ctype
.alignment
)
1427 ctx
->ctype
.alignment
= alignment
;
1431 static struct token
*generic_qualifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1433 apply_qualifier(&next
->pos
, &ctx
->ctype
, sym
->ctype
.modifiers
);
1437 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1439 unsigned long mod
= thistype
->modifiers
;
1442 apply_qualifier(&pos
, ctype
, mod
);
1445 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1446 (struct ptr_list
**)&ctype
->contexts
);
1449 if (thistype
->alignment
> ctype
->alignment
)
1450 ctype
->alignment
= thistype
->alignment
;
1454 ctype
->as
= thistype
->as
;
1457 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1460 if (what
& (Set_S
| Set_T
))
1462 if (what
& Set_Char
)
1464 else if (what
& Set_Double
)
1466 else if (what
& Set_Float
)
1468 else if (what
& Set_Signed
)
1470 else if (what
& Set_Unsigned
)
1472 else if (what
& Set_Short
)
1474 else if (what
& Set_Long
)
1478 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1479 old
, show_ident(new));
1483 sparse_error(pos
, "two or more data types in declaration specifiers");
1486 static struct symbol
* const int_types
[] =
1487 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1488 static struct symbol
* const signed_types
[] =
1489 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1491 static struct symbol
* const unsigned_types
[] =
1492 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1494 static struct symbol
* const real_types
[] =
1495 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1496 static struct symbol
* const * const types
[] = {
1497 [CInt
] = int_types
+ 2,
1498 [CSInt
] = signed_types
+ 2,
1499 [CUInt
] = unsigned_types
+ 2,
1500 [CReal
] = real_types
+ 1,
1503 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1505 return types
[want_unsigned
? CUInt
: CInt
][size
];
1508 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1510 while (token_type(t
) == TOKEN_IDENT
) {
1511 struct symbol
*s
= lookup_keyword(t
->ident
, NS_TYPEDEF
);
1514 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1517 if (s
->op
->declarator
)
1518 t
= s
->op
->declarator(t
, s
, ctx
);
1523 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1529 while (token_type(token
) == TOKEN_IDENT
) {
1530 struct symbol
*s
= lookup_symbol(token
->ident
,
1531 NS_TYPEDEF
| NS_SYMBOL
);
1532 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1534 if (s
->type
!= SYM_KEYWORD
) {
1537 seen
|= Set_S
| Set_T
;
1538 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1539 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1540 token
= token
->next
;
1543 if (s
->op
->type
& KW_SPECIFIER
) {
1544 if (seen
& s
->op
->test
) {
1545 specifier_conflict(token
->pos
,
1551 class += s
->op
->class;
1552 if (s
->op
->set
& Set_Int128
)
1554 else if (s
->op
->set
& Set_Char
)
1556 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1558 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1559 if (class == CReal
) {
1560 specifier_conflict(token
->pos
,
1568 token
= token
->next
;
1569 if (s
->op
->declarator
) // Note: this eats attributes
1570 token
= s
->op
->declarator(token
, s
, ctx
);
1571 if (s
->op
->type
& KW_EXACT
) {
1572 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1573 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1577 if (!(seen
& Set_S
)) { /* not set explicitly? */
1578 struct symbol
*base
= &incomplete_ctype
;
1580 base
= types
[class][rank
];
1581 ctx
->ctype
.base_type
= base
;
1584 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1585 struct symbol
*type
;
1586 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1587 if (!is_int_type(ctx
->ctype
.base_type
)) {
1588 sparse_error(token
->pos
, "invalid modifier");
1591 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1592 *type
= *ctx
->ctype
.base_type
;
1593 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1594 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1595 type
->type
= SYM_RESTRICT
;
1596 ctx
->ctype
.base_type
= type
;
1597 create_fouled(type
);
1602 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1604 struct expression
*expr
= NULL
;
1607 while (token_type(token
) == TOKEN_IDENT
) {
1608 struct symbol
*sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1609 if (!sym
|| !(sym
->op
->type
& (KW_STATIC
|KW_QUALIFIER
)))
1611 if (has_static
&& (sym
->op
->type
& KW_STATIC
))
1612 sparse_error(token
->pos
, "duplicate array static declarator");
1613 has_static
|= (sym
->op
->type
& KW_STATIC
);
1614 token
= token
->next
;
1616 if (match_op(token
, '*') && match_op(token
->next
, ']')) {
1617 // FIXME: '[*]' is treated like '[]'
1618 token
= token
->next
;
1620 token
= assignment_expression(token
, &expr
);
1622 sym
->array_size
= expr
;
1626 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1627 static struct token
*identifier_list(struct token
*, struct symbol
*);
1628 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1630 static struct token
*handle_asm_name(struct token
*token
, struct decl_state
*ctx
)
1632 struct expression
*expr
;
1633 struct symbol
*keyword
;
1635 if (token_type(token
) != TOKEN_IDENT
)
1637 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1640 if (!(keyword
->op
->type
& KW_ASM
))
1643 token
= token
->next
;
1644 token
= expect(token
, '(', "after asm");
1645 token
= string_expression(token
, &expr
, "asm name");
1646 token
= expect(token
, ')', "after asm");
1651 // test if @token is '__attribute__' (or one of its variant)
1652 static bool match_attribute(struct token
*token
)
1656 if (token_type(token
) != TOKEN_IDENT
)
1658 sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1661 return sym
->op
->type
& KW_ATTRIBUTE
;
1664 static struct token
*skip_attribute(struct token
*token
)
1666 token
= token
->next
;
1667 if (match_op(token
, '(')) {
1669 token
= token
->next
;
1670 while (depth
&& !eof_token(token
)) {
1671 if (token_type(token
) == TOKEN_SPECIAL
) {
1672 if (token
->special
== '(')
1674 else if (token
->special
== ')')
1677 token
= token
->next
;
1683 static struct token
*skip_attributes(struct token
*token
)
1685 while (match_attribute(token
)) {
1686 token
= expect(token
->next
, '(', "after attribute");
1687 token
= expect(token
, '(', "after attribute");
1688 while (token_type(token
) == TOKEN_IDENT
) {
1689 token
= skip_attribute(token
);
1690 if (!match_op(token
, ','))
1692 token
= token
->next
;
1694 token
= expect(token
, ')', "after attribute");
1695 token
= expect(token
, ')', "after attribute");
1700 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
)
1702 while (match_attribute(token
))
1703 token
= attribute_specifier(token
->next
, NULL
, ctx
);
1707 static int is_nested(struct token
*token
, struct token
**p
,
1708 int prefer_abstract
)
1711 * This can be either a parameter list or a grouping.
1712 * For the direct (non-abstract) case, we know if must be
1713 * a parameter list if we already saw the identifier.
1714 * For the abstract case, we know if must be a parameter
1715 * list if it is empty or starts with a type.
1717 struct token
*next
= token
->next
;
1719 *p
= next
= skip_attributes(next
);
1721 if (token_type(next
) == TOKEN_IDENT
) {
1722 if (lookup_type(next
))
1723 return !prefer_abstract
;
1727 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1734 Empty
, K_R
, Proto
, Bad_Func
,
1737 static enum kind
which_func(struct token
*token
,
1739 int prefer_abstract
)
1741 struct token
*next
= token
->next
;
1743 if (token_type(next
) == TOKEN_IDENT
) {
1744 if (lookup_type(next
))
1746 /* identifier list not in definition; complain */
1747 if (prefer_abstract
)
1749 "identifier list not in definition");
1753 if (token_type(next
) != TOKEN_SPECIAL
)
1756 if (next
->special
== ')') {
1757 /* don't complain about those */
1758 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1760 if (Wstrict_prototypes
)
1762 "non-ANSI function declaration of function '%s'",
1767 if (next
->special
== SPECIAL_ELLIPSIS
) {
1769 "variadic functions must have one named argument");
1776 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1778 struct ctype
*ctype
= &ctx
->ctype
;
1780 struct ident
**p
= ctx
->ident
;
1782 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1783 *ctx
->ident
= token
->ident
;
1784 token
= token
->next
;
1785 } else if (match_op(token
, '(') &&
1786 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1787 struct symbol
*base_type
= ctype
->base_type
;
1788 if (token
->next
!= next
)
1789 next
= handle_attributes(token
->next
, ctx
);
1790 token
= declarator(next
, ctx
);
1791 token
= expect(token
, ')', "in nested declarator");
1792 while (ctype
->base_type
!= base_type
)
1793 ctype
= &ctype
->base_type
->ctype
;
1797 if (match_op(token
, '(')) {
1798 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1800 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1801 ctype
->modifiers
|= ctx
->f_modifiers
;
1802 token
= token
->next
;
1804 token
= identifier_list(token
, fn
);
1805 else if (kind
== Proto
)
1806 token
= parameter_type_list(token
, fn
);
1807 token
= expect(token
, ')', "in function declarator");
1808 fn
->endpos
= token
->pos
;
1812 while (match_op(token
, '[')) {
1813 struct symbol
*array
;
1814 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1815 token
= abstract_array_declarator(token
->next
, array
);
1816 token
= expect(token
, ']', "in abstract_array_declarator");
1817 array
->endpos
= token
->pos
;
1818 ctype
= &array
->ctype
;
1823 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1825 while (match_op(token
,'*')) {
1826 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1827 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1828 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1829 ptr
->ctype
.as
= ctx
->ctype
.as
;
1830 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1831 ctx
->ctype
.modifiers
= 0;
1832 ctx
->ctype
.base_type
= ptr
;
1833 ctx
->ctype
.as
= NULL
;
1834 ctx
->ctype
.contexts
= NULL
;
1835 ctx
->ctype
.alignment
= 0;
1837 token
= handle_qualifiers(token
->next
, ctx
);
1838 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1843 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1845 token
= pointer(token
, ctx
);
1846 return direct_declarator(token
, ctx
);
1849 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1851 struct ctype
*ctype
= &ctx
->ctype
;
1852 struct expression
*expr
;
1853 struct symbol
*bitfield
;
1856 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1857 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1858 show_typename(ctype
->base_type
));
1859 // Parse this to recover gracefully.
1860 return conditional_expression(token
->next
, &expr
);
1863 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1864 token
= conditional_expression(token
->next
, &expr
);
1865 width
= const_expression_value(expr
);
1866 bitfield
->bit_size
= width
;
1868 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
1869 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
1870 show_ident(*ctx
->ident
), width
);
1872 } else if (*ctx
->ident
) {
1873 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1874 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1875 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1876 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1877 // Valid values are either {-1;0} or {0}, depending on integer
1878 // representation. The latter makes for very efficient code...
1879 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1881 if (Wdefault_bitfield_sign
&&
1882 bitfield_type
->type
!= SYM_ENUM
&&
1883 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1885 // The sign of bitfields is unspecified by default.
1886 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1889 bitfield
->bit_size
= width
;
1890 bitfield
->endpos
= token
->pos
;
1891 bitfield
->ident
= *ctx
->ident
;
1895 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1897 struct decl_state ctx
= {.prefer_abstract
= 0};
1901 token
= declaration_specifiers(token
, &ctx
);
1902 mod
= decl_modifiers(&ctx
);
1905 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1906 ctx
.ident
= &decl
->ident
;
1908 token
= declarator(token
, &ctx
);
1909 if (match_op(token
, ':'))
1910 token
= handle_bitfield(token
, &ctx
);
1912 token
= handle_attributes(token
, &ctx
);
1913 apply_modifiers(token
->pos
, &ctx
);
1915 decl
->ctype
= ctx
.ctype
;
1916 decl
->ctype
.modifiers
|= mod
;
1917 decl
->endpos
= token
->pos
;
1918 add_symbol(list
, decl
);
1919 if (!match_op(token
, ','))
1921 token
= token
->next
;
1927 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1929 while (!match_op(token
, '}')) {
1930 if (match_ident(token
, &_Static_assert_ident
)) {
1931 token
= parse_static_assert(token
, NULL
);
1934 if (!match_op(token
, ';'))
1935 token
= declaration_list(token
, list
);
1936 if (!match_op(token
, ';')) {
1937 sparse_error(token
->pos
, "expected ; at end of declaration");
1940 token
= token
->next
;
1945 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1947 struct decl_state ctx
= {.prefer_abstract
= 1};
1949 token
= declaration_specifiers(token
, &ctx
);
1950 ctx
.ident
= &sym
->ident
;
1951 token
= declarator(token
, &ctx
);
1952 token
= handle_attributes(token
, &ctx
);
1953 apply_modifiers(token
->pos
, &ctx
);
1954 sym
->ctype
= ctx
.ctype
;
1955 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
1956 sym
->endpos
= token
->pos
;
1957 sym
->forced_arg
= ctx
.forced
;
1961 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1963 struct decl_state ctx
= {.prefer_abstract
= 1};
1964 unsigned long class;
1965 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1967 token
= declaration_specifiers(token
, &ctx
);
1968 token
= declarator(token
, &ctx
);
1969 apply_modifiers(token
->pos
, &ctx
);
1970 sym
->ctype
= ctx
.ctype
;
1971 sym
->endpos
= token
->pos
;
1972 class = ctx
.storage_class
;
1974 *forced
= ctx
.forced
;
1976 warning(sym
->pos
, "storage class in typename (%s%s)",
1977 modifier_string(class), show_typename(sym
));
1981 static struct token
*parse_underscore_Pragma(struct token
*token
)
1986 if (!match_op(next
, '('))
1989 if (next
->pos
.type
!= TOKEN_STRING
)
1992 if (!match_op(next
, ')'))
1997 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1999 if (match_ident(token
, &_Pragma_ident
))
2000 return parse_underscore_Pragma(token
);
2002 token
= parse_expression(token
, tree
);
2003 return expect(token
, ';', "at end of statement");
2006 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2007 struct asm_operand_list
**inout
)
2009 /* Allow empty operands */
2010 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2013 struct asm_operand
*op
= __alloc_asm_operand(0);
2014 if (match_op(token
->next
, '[') &&
2015 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2016 match_op(token
->next
->next
->next
, ']')) {
2017 op
->name
= token
->next
->next
->ident
;
2018 token
= token
->next
->next
->next
;
2020 token
= token
->next
;
2021 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2022 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2023 add_ptr_list(inout
, op
);
2024 } while (match_op(token
, ','));
2028 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2029 struct expression_list
**clobbers
)
2031 struct expression
*expr
;
2034 token
= primary_expression(token
->next
, &expr
);
2036 add_expression(clobbers
, expr
);
2037 } while (match_op(token
, ','));
2041 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2042 struct symbol_list
**labels
)
2044 struct symbol
*label
;
2047 token
= token
->next
; /* skip ':' and ',' */
2048 if (token_type(token
) != TOKEN_IDENT
)
2050 label
= label_symbol(token
, 1);
2051 add_symbol(labels
, label
);
2052 token
= token
->next
;
2053 } while (match_op(token
, ','));
2057 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2059 unsigned long mods
= 0;
2061 token
= token
->next
;
2062 stmt
->type
= STMT_ASM
;
2063 while (token_type(token
) == TOKEN_IDENT
) {
2064 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2065 if (s
&& s
->op
->asm_modifier
)
2066 s
->op
->asm_modifier(token
, &mods
, s
->ctype
.modifiers
);
2067 else if (token
->ident
== &goto_ident
)
2068 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2069 token
= token
->next
;
2071 token
= expect(token
, '(', "after asm");
2072 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2073 if (match_op(token
, ':'))
2074 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2075 if (match_op(token
, ':'))
2076 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2077 if (match_op(token
, ':'))
2078 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2079 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2080 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2081 token
= expect(token
, ')', "after asm");
2082 return expect(token
, ';', "at end of asm-statement");
2085 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2087 struct expression
*cond
= NULL
, *message
= NULL
;
2089 token
= expect(token
->next
, '(', "after _Static_assert");
2090 token
= constant_expression(token
, &cond
);
2092 sparse_error(token
->pos
, "Expected constant expression");
2093 if (match_op(token
, ',')) {
2094 token
= token
->next
;
2095 token
= string_expression(token
, &message
, "_Static_assert()");
2099 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2100 token
= expect(token
, ';', "after _Static_assert()");
2102 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
) {
2103 const char *sep
= "", *msg
= "";
2107 msg
= show_string(message
->string
);
2109 sparse_error(cond
->pos
, "static assertion failed%s%s", sep
, msg
);
2115 /* Make a statement out of an expression */
2116 static struct statement
*make_statement(struct expression
*expr
)
2118 struct statement
*stmt
;
2122 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2123 stmt
->expression
= expr
;
2128 * All iterators have two symbols associated with them:
2129 * the "continue" and "break" symbols, which are targets
2130 * for continue and break statements respectively.
2132 * They are in a special name-space, but they follow
2133 * all the normal visibility rules, so nested iterators
2134 * automatically work right.
2136 static void start_iterator(struct statement
*stmt
)
2138 struct symbol
*cont
, *brk
;
2140 start_block_scope(stmt
->pos
);
2141 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2142 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2143 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2144 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2146 stmt
->type
= STMT_ITERATOR
;
2147 stmt
->iterator_break
= brk
;
2148 stmt
->iterator_continue
= cont
;
2149 fn_local_symbol(brk
);
2150 fn_local_symbol(cont
);
2153 static void end_iterator(struct statement
*stmt
)
2158 static struct statement
*start_function(struct symbol
*sym
)
2161 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2163 start_function_scope(sym
->pos
);
2164 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2165 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2166 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2167 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2168 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2170 fn_local_symbol(ret
);
2172 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2178 static void end_function(struct symbol
*sym
)
2181 end_function_scope();
2185 * A "switch()" statement, like an iterator, has a
2186 * the "break" symbol associated with it. It works
2187 * exactly like the iterator break - it's the target
2188 * for any break-statements in scope, and means that
2189 * "break" handling doesn't even need to know whether
2190 * it's breaking out of an iterator or a switch.
2192 * In addition, the "case" symbol is a marker for the
2193 * case/default statements to find the switch statement
2194 * that they are associated with.
2196 static void start_switch(struct statement
*stmt
)
2198 struct symbol
*brk
, *switch_case
;
2200 start_block_scope(stmt
->pos
);
2201 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2202 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2204 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2205 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2206 switch_case
->stmt
= stmt
;
2208 stmt
->type
= STMT_SWITCH
;
2209 stmt
->switch_break
= brk
;
2210 stmt
->switch_case
= switch_case
;
2212 fn_local_symbol(brk
);
2213 fn_local_symbol(switch_case
);
2216 static void end_switch(struct statement
*stmt
)
2218 if (!stmt
->switch_case
->symbol_list
)
2219 warning(stmt
->pos
, "switch with no cases");
2223 static void add_case_statement(struct statement
*stmt
)
2225 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2229 sparse_error(stmt
->pos
, "not in switch scope");
2230 stmt
->type
= STMT_NONE
;
2233 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2234 add_symbol(&target
->symbol_list
, sym
);
2236 stmt
->case_label
= sym
;
2237 fn_local_symbol(sym
);
2240 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2242 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2245 error_die(token
->pos
, "internal error: return without a function target");
2246 stmt
->type
= STMT_RETURN
;
2247 stmt
->ret_target
= target
;
2248 return expression_statement(token
->next
, &stmt
->ret_value
);
2251 static void validate_for_loop_decl(struct symbol
*sym
)
2253 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2255 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2256 const char *name
= show_ident(sym
->ident
);
2257 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2258 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2262 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2264 struct symbol_list
*syms
;
2265 struct expression
*e1
, *e2
, *e3
;
2266 struct statement
*iterator
;
2268 start_iterator(stmt
);
2269 token
= expect(token
->next
, '(', "after 'for'");
2273 /* C99 variable declaration? */
2274 if (lookup_type(token
)) {
2275 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2277 token
= parse_expression(token
, &e1
);
2278 token
= expect(token
, ';', "in 'for'");
2280 token
= parse_expression(token
, &e2
);
2281 token
= expect(token
, ';', "in 'for'");
2282 token
= parse_expression(token
, &e3
);
2283 token
= expect(token
, ')', "in 'for'");
2284 token
= statement(token
, &iterator
);
2286 stmt
->iterator_syms
= syms
;
2287 stmt
->iterator_pre_statement
= make_statement(e1
);
2288 stmt
->iterator_pre_condition
= e2
;
2289 stmt
->iterator_post_statement
= make_statement(e3
);
2290 stmt
->iterator_post_condition
= NULL
;
2291 stmt
->iterator_statement
= iterator
;
2297 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2299 struct expression
*expr
;
2300 struct statement
*iterator
;
2302 start_iterator(stmt
);
2303 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2304 token
= statement(token
, &iterator
);
2306 stmt
->iterator_pre_condition
= expr
;
2307 stmt
->iterator_post_condition
= NULL
;
2308 stmt
->iterator_statement
= iterator
;
2314 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2316 struct expression
*expr
;
2317 struct statement
*iterator
;
2319 start_iterator(stmt
);
2320 token
= statement(token
->next
, &iterator
);
2321 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2322 token
= token
->next
;
2324 sparse_error(token
->pos
, "expected 'while' after 'do'");
2325 token
= parens_expression(token
, &expr
, "after 'do-while'");
2327 stmt
->iterator_post_condition
= expr
;
2328 stmt
->iterator_statement
= iterator
;
2331 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2332 warning(iterator
->pos
, "do-while statement is not a compound statement");
2334 return expect(token
, ';', "after statement");
2337 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2339 stmt
->type
= STMT_IF
;
2340 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2341 token
= statement(token
, &stmt
->if_true
);
2342 if (token_type(token
) != TOKEN_IDENT
)
2344 if (token
->ident
!= &else_ident
)
2346 return statement(token
->next
, &stmt
->if_false
);
2349 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2351 stmt
->type
= STMT_CASE
;
2352 token
= expect(token
, ':', "after default/case");
2353 add_case_statement(stmt
);
2354 return statement(token
, &stmt
->case_statement
);
2357 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2359 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2360 if (match_op(token
, SPECIAL_ELLIPSIS
))
2361 token
= parse_expression(token
->next
, &stmt
->case_to
);
2362 return case_statement(token
, stmt
);
2365 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2367 return case_statement(token
->next
, stmt
);
2370 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2372 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2373 stmt
->type
= STMT_GOTO
;
2374 stmt
->goto_label
= target
;
2376 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2377 return expect(token
->next
, ';', "at end of statement");
2380 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2382 stmt
->type
= STMT_SWITCH
;
2384 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2385 token
= statement(token
, &stmt
->switch_statement
);
2390 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2392 const char *id
= show_ident(ident
);
2393 sparse_error(use
, "label '%s' used outside statement expression", id
);
2394 info(def
, " label '%s' defined here", id
);
2395 current_fn
->bogus_linear
= 1;
2398 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2400 struct statement
*def
= label
->stmt
;
2403 if (!is_in_scope(def
->label_scope
, label_scope
))
2404 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2405 } else if (!label
->label_scope
) {
2406 label
->label_scope
= label_scope
;
2407 label
->label_pos
= use_pos
;
2411 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2413 stmt
->type
= STMT_GOTO
;
2414 token
= token
->next
;
2415 if (match_op(token
, '*')) {
2416 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2417 add_statement(&function_computed_goto_list
, stmt
);
2418 } else if (token_type(token
) == TOKEN_IDENT
) {
2419 struct symbol
*label
= label_symbol(token
, 1);
2420 stmt
->goto_label
= label
;
2421 check_label_usage(label
, stmt
->pos
);
2422 token
= token
->next
;
2424 sparse_error(token
->pos
, "Expected identifier or goto expression");
2426 return expect(token
, ';', "at end of statement");
2429 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2431 stmt
->type
= STMT_CONTEXT
;
2432 token
= token
->next
;
2433 token
= expect(token
, '(', "after __context__ statement");
2434 token
= assignment_expression(token
, &stmt
->expression
);
2435 if (!stmt
->expression
)
2436 unexpected(token
, "expression expected after '('");
2437 if (match_op(token
, ',')) {
2438 token
= token
->next
;
2439 stmt
->context
= stmt
->expression
;
2440 token
= assignment_expression(token
, &stmt
->expression
);
2441 if (!stmt
->expression
)
2442 unexpected(token
, "expression expected after ','");
2444 token
= expect(token
, ')', "at end of __context__ statement");
2445 return expect(token
, ';', "at end of statement");
2448 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2450 stmt
->type
= STMT_RANGE
;
2451 token
= token
->next
;
2452 token
= expect(token
, '(', "after __range__ statement");
2453 token
= assignment_expression(token
, &stmt
->range_expression
);
2454 token
= expect(token
, ',', "after range expression");
2455 token
= assignment_expression(token
, &stmt
->range_low
);
2456 token
= expect(token
, ',', "after low range");
2457 token
= assignment_expression(token
, &stmt
->range_high
);
2458 token
= expect(token
, ')', "after range statement");
2459 return expect(token
, ';', "after range statement");
2462 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2464 struct decl_state ctx
= { };
2466 token
= handle_attributes(token
, &ctx
);
2467 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2471 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2473 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2476 if (token_type(token
) == TOKEN_IDENT
) {
2477 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2478 if (s
&& s
->op
->statement
)
2479 return s
->op
->statement(token
, stmt
);
2481 if (match_op(token
->next
, ':')) {
2482 struct symbol
*s
= label_symbol(token
, 0);
2483 token
= handle_label_attributes(token
->next
->next
, s
);
2485 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2486 // skip the label to avoid multiple definitions
2487 return statement(token
, tree
);
2489 stmt
->type
= STMT_LABEL
;
2490 stmt
->label_identifier
= s
;
2491 stmt
->label_scope
= label_scope
;
2492 if (s
->label_scope
) {
2493 if (!is_in_scope(label_scope
, s
->label_scope
))
2494 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2497 return statement(token
, &stmt
->label_statement
);
2501 if (match_op(token
, '{')) {
2502 token
= compound_statement(token
->next
, stmt
);
2503 return expect(token
, '}', "at end of compound statement");
2506 stmt
->type
= STMT_EXPRESSION
;
2507 return expression_statement(token
, &stmt
->expression
);
2510 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2511 static struct token
*label_statement(struct token
*token
)
2513 while (token_type(token
) == TOKEN_IDENT
) {
2514 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2515 /* it's block-scope, but we want label namespace */
2516 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2517 fn_local_symbol(sym
);
2518 token
= token
->next
;
2519 if (!match_op(token
, ','))
2521 token
= token
->next
;
2523 return expect(token
, ';', "at end of label declaration");
2526 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2528 int seen_statement
= 0;
2529 while (token_type(token
) == TOKEN_IDENT
&&
2530 token
->ident
== &__label___ident
)
2531 token
= label_statement(token
->next
);
2533 struct statement
* stmt
;
2534 if (eof_token(token
))
2536 if (match_op(token
, '}'))
2538 if (match_ident(token
, &_Static_assert_ident
)) {
2539 token
= parse_static_assert(token
, NULL
);
2542 if (lookup_type(token
)) {
2543 if (seen_statement
) {
2544 warning(token
->pos
, "mixing declarations and code");
2547 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2548 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2550 seen_statement
= Wdeclarationafterstatement
;
2551 token
= statement(token
, &stmt
);
2553 add_statement(list
, stmt
);
2558 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2560 struct symbol_list
**list
= &fn
->arguments
;
2562 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2563 sym
->ident
= token
->ident
;
2564 token
= token
->next
;
2565 sym
->endpos
= token
->pos
;
2566 sym
->ctype
.base_type
= &incomplete_ctype
;
2567 add_symbol(list
, sym
);
2568 if (!match_op(token
, ',') ||
2569 token_type(token
->next
) != TOKEN_IDENT
||
2570 lookup_type(token
->next
))
2572 token
= token
->next
;
2577 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2579 struct symbol_list
**list
= &fn
->arguments
;
2584 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2586 token
= token
->next
;
2590 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2591 token
= parameter_declaration(token
, sym
);
2592 if (sym
->ctype
.base_type
== &void_ctype
) {
2593 /* Special case: (void) */
2594 if (!*list
&& !sym
->ident
)
2596 warning(token
->pos
, "void parameter");
2598 add_symbol(list
, sym
);
2599 if (!match_op(token
, ','))
2601 token
= token
->next
;
2606 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2608 stmt
->type
= STMT_COMPOUND
;
2609 start_block_scope(token
->pos
);
2610 token
= statement_list(token
, &stmt
->stmts
);
2615 static struct expression
*identifier_expression(struct token
*token
)
2617 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2618 expr
->expr_ident
= token
->ident
;
2622 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2624 int idx_from
, idx_to
;
2625 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2627 idx_from
= const_expression_value(from
);
2630 idx_to
= const_expression_value(to
);
2631 if (idx_to
< idx_from
|| idx_from
< 0)
2632 warning(from
->pos
, "nonsense array initializer index range");
2634 expr
->idx_from
= idx_from
;
2635 expr
->idx_to
= idx_to
;
2639 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2641 int expect_equal
= 0;
2642 struct token
*next
= token
->next
;
2643 struct expression
**tail
= ep
;
2648 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2649 struct expression
*expr
= identifier_expression(token
);
2650 if (Wold_initializer
)
2651 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2652 token
= initializer(&expr
->ident_expression
, next
->next
);
2653 if (expr
->ident_expression
)
2658 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2659 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2660 struct expression
*expr
= identifier_expression(next
);
2662 tail
= &expr
->ident_expression
;
2665 } else if (match_op(token
, '[')) {
2666 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2667 token
= constant_expression(token
->next
, &from
);
2669 sparse_error(token
->pos
, "Expected constant expression");
2672 if (match_op(token
, SPECIAL_ELLIPSIS
))
2673 token
= constant_expression(token
->next
, &to
);
2674 expr
= index_expression(from
, to
);
2676 tail
= &expr
->idx_expression
;
2677 token
= expect(token
, ']', "at end of initializer index");
2684 if (nested
&& !expect_equal
) {
2685 if (!match_op(token
, '='))
2686 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2691 token
= expect(token
, '=', "at end of initializer index");
2693 token
= initializer(tail
, token
);
2699 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2701 struct expression
*expr
;
2704 token
= single_initializer(&expr
, token
);
2707 add_expression(list
, expr
);
2708 if (!match_op(token
, ','))
2710 token
= token
->next
;
2715 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2717 if (match_op(token
, '{')) {
2718 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2720 if (!Wuniversal_initializer
) {
2721 struct token
*next
= token
->next
;
2722 // '{ 0 }' is equivalent to '{ }' except for some
2723 // warnings, like using 0 to initialize a null-pointer.
2724 if (match_token_zero(next
)) {
2725 if (match_op(next
->next
, '}'))
2726 expr
->zero_init
= 1;
2730 token
= initializer_list(&expr
->expr_list
, token
->next
);
2731 return expect(token
, '}', "at end of initializer");
2733 return assignment_expression(token
, tree
);
2736 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2739 sparse_error(sym
->pos
, "no identifier for function argument");
2742 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2745 static int is_syscall(struct symbol
*sym
)
2751 macro
= get_macro_name(sym
->pos
);
2753 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2754 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2757 name
= sym
->ident
->name
;
2759 if (name
&& strncmp(name
, "sys_", 4) ==0)
2762 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2769 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2770 struct symbol_list
**list
)
2772 struct symbol_list
**old_symbol_list
;
2773 struct symbol
*base_type
= decl
->ctype
.base_type
;
2774 struct statement
*stmt
, **p
;
2775 struct symbol
*prev
;
2778 old_symbol_list
= function_symbol_list
;
2779 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2780 function_symbol_list
= &decl
->inline_symbol_list
;
2781 p
= &base_type
->inline_stmt
;
2783 function_symbol_list
= &decl
->symbol_list
;
2784 p
= &base_type
->stmt
;
2786 function_computed_target_list
= NULL
;
2787 function_computed_goto_list
= NULL
;
2789 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2790 if (Wexternal_function_has_definition
)
2791 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2793 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2794 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2796 stmt
= start_function(decl
);
2799 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2800 declare_argument(arg
, base_type
);
2801 } END_FOR_EACH_PTR(arg
);
2803 token
= statement_list(token
->next
, &stmt
->stmts
);
2806 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2807 add_symbol(list
, decl
);
2808 else if (is_syscall(decl
)) {
2809 add_symbol(list
, decl
);
2811 printf("parse.c decl: %s\n", decl->ident->name);
2812 char *macro = get_macro_name(decl->pos);
2813 printf("decl macro: %s\n", macro);
2816 check_declaration(decl
);
2817 decl
->definition
= decl
;
2818 prev
= decl
->same_symbol
;
2819 if (prev
&& prev
->definition
) {
2820 warning(decl
->pos
, "multiple definitions for function '%s'",
2821 show_ident(decl
->ident
));
2822 info(prev
->definition
->pos
, " the previous one is here");
2825 rebind_scope(prev
, decl
->scope
);
2826 prev
->definition
= decl
;
2827 prev
= prev
->same_symbol
;
2830 function_symbol_list
= old_symbol_list
;
2831 if (function_computed_goto_list
) {
2832 if (!function_computed_target_list
)
2833 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2835 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2836 stmt
->target_list
= function_computed_target_list
;
2837 } END_FOR_EACH_PTR(stmt
);
2840 return expect(token
, '}', "at end of function");
2843 static void promote_k_r_types(struct symbol
*arg
)
2845 struct symbol
*base
= arg
->ctype
.base_type
;
2846 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2847 arg
->ctype
.base_type
= &int_ctype
;
2851 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2853 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2856 FOR_EACH_PTR(real_args
, arg
) {
2857 struct symbol
*type
;
2859 /* This is quadratic in the number of arguments. We _really_ don't care */
2860 FOR_EACH_PTR(argtypes
, type
) {
2861 if (type
->ident
== arg
->ident
)
2863 } END_FOR_EACH_PTR(type
);
2864 if (Wimplicit_int
) {
2865 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'",
2866 show_ident(arg
->ident
));
2868 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
2869 type
->ident
= arg
->ident
;
2870 type
->ctype
.base_type
= &int_ctype
;
2873 /* "char" and "short" promote to "int" */
2874 promote_k_r_types(type
);
2876 arg
->ctype
= type
->ctype
;
2877 } END_FOR_EACH_PTR(arg
);
2879 FOR_EACH_PTR(argtypes
, arg
) {
2881 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2882 } END_FOR_EACH_PTR(arg
);
2886 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2887 struct symbol_list
**list
)
2889 struct symbol_list
*args
= NULL
;
2891 if (Wold_style_definition
)
2892 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2895 token
= declaration_list(token
, &args
);
2896 if (!match_op(token
, ';')) {
2897 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2900 token
= token
->next
;
2901 } while (lookup_type(token
));
2903 apply_k_r_types(args
, decl
);
2905 if (!match_op(token
, '{')) {
2906 sparse_error(token
->pos
, "expected function body");
2909 return parse_function_body(token
, decl
, list
);
2912 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2914 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2915 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2916 struct statement
*stmt
;
2918 anon
->ctype
.base_type
= fn
;
2919 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2922 token
= parse_asm_statement(token
, stmt
);
2924 // FIXME: add_symbol(list, anon);
2928 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2929 validate_decl_t validate_decl
)
2931 struct ident
*ident
= NULL
;
2932 struct symbol
*decl
;
2933 struct decl_state ctx
= { .ident
= &ident
};
2935 struct symbol
*base_type
;
2939 if (match_ident(token
, &_Pragma_ident
))
2940 return parse_underscore_Pragma(token
);
2942 /* Top-level inline asm or static assertion? */
2943 if (token_type(token
) == TOKEN_IDENT
) {
2944 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2945 if (s
&& s
->op
->toplevel
)
2946 return s
->op
->toplevel(token
, list
);
2949 /* Parse declaration-specifiers, if any */
2950 token
= declaration_specifiers(token
, &ctx
);
2951 mod
= decl_modifiers(&ctx
);
2952 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2953 /* Just a type declaration? */
2954 if (match_op(token
, ';')) {
2955 apply_modifiers(token
->pos
, &ctx
);
2960 token
= declarator(token
, &ctx
);
2961 token
= handle_asm_name(token
, &ctx
);
2962 token
= handle_attributes(token
, &ctx
);
2963 apply_modifiers(token
->pos
, &ctx
);
2965 decl
->ctype
= ctx
.ctype
;
2966 decl
->ctype
.modifiers
|= mod
;
2967 decl
->endpos
= token
->pos
;
2969 /* Just a type declaration? */
2971 warning(token
->pos
, "missing identifier in declaration");
2972 return expect(token
, ';', "at the end of type declaration");
2975 /* type define declaration? */
2976 is_typedef
= ctx
.storage_class
== MOD_USERTYPE
;
2978 /* Typedefs don't have meaningful storage */
2980 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2982 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2984 base_type
= decl
->ctype
.base_type
;
2987 if (base_type
&& !base_type
->ident
) {
2988 switch (base_type
->type
) {
2993 base_type
->ident
= ident
;
2999 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3000 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3001 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3002 show_ident(decl
->ident
));
3003 base_type
->ctype
.base_type
= &int_ctype
;
3005 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3006 warning(decl
->pos
, "'%s()' has implicit return type",
3007 show_ident(decl
->ident
));
3008 base_type
->ctype
.base_type
= &int_ctype
;
3010 /* apply attributes placed after the declarator */
3011 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3013 /* K&R argument declaration? */
3014 if (lookup_type(token
))
3015 return parse_k_r_arguments(token
, decl
, list
);
3016 if (match_op(token
, '{'))
3017 return parse_function_body(token
, decl
, list
);
3019 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3020 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3021 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3022 sparse_error(token
->pos
, "void declaration");
3024 if (base_type
== &incomplete_ctype
) {
3025 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3026 decl
->ctype
.base_type
= &int_ctype
;;
3030 if (!is_typedef
&& match_op(token
, '=')) {
3031 struct token
*next
= token
->next
;
3032 token
= initializer(&decl
->initializer
, next
);
3034 sparse_error(token
->pos
, "expression expected before '%s'", show_token(token
));
3038 validate_decl(decl
);
3040 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3041 warning(decl
->pos
, "symbol with external linkage has initializer");
3042 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3045 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3046 add_symbol(list
, decl
);
3047 fn_local_symbol(decl
);
3050 check_declaration(decl
);
3051 if (decl
->same_symbol
) {
3052 decl
->definition
= decl
->same_symbol
->definition
;
3053 decl
->op
= decl
->same_symbol
->op
;
3055 // TODO: handle -std=c89 --pedantic
3056 check_duplicates(decl
);
3061 const char *msg
= NULL
;
3062 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3063 msg
= "on non-identifier";
3064 else if (match_op(token
, ','))
3065 msg
= "on declaration list";
3066 else if (!decl
->initializer
)
3067 msg
= "without initializer";
3068 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3069 decl
->initializer
->symbol
== decl
)
3070 msg
= "on self-init var";
3072 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3073 decl
->ctype
.base_type
= &bad_ctype
;
3077 if (!match_op(token
, ','))
3080 token
= token
->next
;
3082 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3084 token
= handle_attributes(token
, &ctx
);
3085 token
= declarator(token
, &ctx
);
3086 token
= handle_asm_name(token
, &ctx
);
3087 token
= handle_attributes(token
, &ctx
);
3088 apply_modifiers(token
->pos
, &ctx
);
3089 decl
->ctype
= ctx
.ctype
;
3090 decl
->ctype
.modifiers
|= mod
;
3091 decl
->endpos
= token
->pos
;
3093 sparse_error(token
->pos
, "expected identifier name in type definition");
3098 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3100 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3102 /* Function declarations are automatically extern unless specifically static */
3103 base_type
= decl
->ctype
.base_type
;
3104 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3105 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3106 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3109 return expect(token
, ';', "at end of declaration");