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
*dst
, struct ctype
*src
);
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
*))
722 struct decl_state attr
= { };
724 struct position
*repos
;
726 token
= handle_attributes(token
, &attr
);
727 if (token_type(token
) == TOKEN_IDENT
) {
728 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
730 (is_outer_scope(sym
->scope
) &&
731 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
732 // Either a new symbol, or else an out-of-scope
733 // symbol being redefined.
734 sym
= alloc_symbol(token
->pos
, type
);
735 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
737 if (sym
->type
!= type
)
738 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
739 ctx
->ctype
.base_type
= sym
;
742 if (!match_op(token
, '{'))
745 // The following test is actually wrong for empty
746 // structs, but (1) they are not C99, (2) gcc does
747 // the same thing, and (3) it's easier.
748 if (sym
->symbol_list
)
749 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
752 // Mark the structure as needing re-examination
754 } else if (match_op(token
, '{')) {
755 // private struct/union/enum type
756 sym
= alloc_symbol(token
->pos
, type
);
757 set_current_scope(sym
); // used by dissect
758 ctx
->ctype
.base_type
= sym
;
760 sparse_error(token
->pos
, "expected declaration");
761 ctx
->ctype
.base_type
= &bad_ctype
;
765 token
= parse(token
->next
, sym
);
766 token
= expect(token
, '}', "at end of specifier");
767 attr
.ctype
.base_type
= sym
;
768 token
= handle_attributes(token
, &attr
);
769 apply_ctype(token
->pos
, &sym
->ctype
, &attr
.ctype
);
770 sym
->packed
= attr
.packed
;
772 sym
->endpos
= token
->pos
;
777 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
779 struct symbol
*field
, *last
= NULL
;
781 res
= struct_declaration_list(token
, &sym
->symbol_list
);
782 FOR_EACH_PTR(sym
->symbol_list
, field
) {
784 struct symbol
*base
= field
->ctype
.base_type
;
785 if (base
&& base
->type
== SYM_BITFIELD
)
789 last
->next_subobject
= field
;
791 } END_FOR_EACH_PTR(field
);
795 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
797 return struct_declaration_list(token
, &sym
->symbol_list
);
800 static struct token
*struct_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
802 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
805 static struct token
*union_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
807 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
813 // This allow to use a shift amount as big (or bigger)
814 // than the width of the value to be shifted, in which case
815 // the result is, of course, 0.
816 static unsigned long long rshift(unsigned long long val
, unsigned int n
)
818 if (n
>= (sizeof(val
) * 8))
825 unsigned long long pos
;
828 static void update_range(struct range
*range
, unsigned long long uval
, struct symbol
*vtype
)
830 long long sval
= uval
;
832 if (is_signed_type(vtype
) && (sval
< 0)) {
833 if (sval
< range
->neg
)
836 if (uval
> range
->pos
)
841 static int type_is_ok(struct symbol
*type
, struct range range
)
843 int shift
= type
->bit_size
;
844 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
848 if (rshift(range
.pos
, shift
))
854 if (rshift(~range
.neg
, shift
))
859 static struct range
type_range(struct symbol
*type
)
862 unsigned int size
= type
->bit_size
;
863 unsigned long long max
;
866 if (is_signed_type(type
)) {
867 min
= sign_bit(size
);
871 max
= bits_mask(size
);
879 static int val_in_range(struct range
*range
, long long sval
, struct symbol
*vtype
)
881 unsigned long long uval
= sval
;
883 if (is_signed_type(vtype
) && (sval
< 0))
884 return range
->neg
<= sval
;
886 return uval
<= range
->pos
;
889 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
891 struct range irange
= type_range(&int_ctype
);
894 FOR_EACH_PTR(list
, sym
) {
895 struct expression
*expr
= sym
->initializer
;
896 struct symbol
*ctype
;
898 if (expr
->type
!= EXPR_VALUE
)
901 val
= get_expression_value(expr
);
902 if (is_int_type(ctype
) && val_in_range(&irange
, val
, ctype
)) {
903 expr
->ctype
= &int_ctype
;
906 cast_value(expr
, base_type
, expr
, ctype
);
907 expr
->ctype
= base_type
;
908 } END_FOR_EACH_PTR(sym
);
911 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
913 unsigned long long lastval
= 0;
914 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
915 struct range range
= { };
918 parent
->examined
= 1;
919 parent
->ctype
.base_type
= &int_ctype
;
920 while (token_type(token
) == TOKEN_IDENT
) {
921 struct expression
*expr
= NULL
;
922 struct token
*next
= token
->next
;
923 struct decl_state ctx
= { };
926 // FIXME: only 'deprecated' should be accepted
927 next
= handle_attributes(next
, &ctx
);
929 if (match_op(next
, '=')) {
930 next
= constant_expression(next
->next
, &expr
);
931 lastval
= get_expression_value(expr
);
933 if (expr
&& expr
->ctype
)
937 } else if (is_int_type(ctype
)) {
940 error_die(token
->pos
, "can't increment the last enum member");
944 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
945 expr
->value
= lastval
;
949 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
950 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
951 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
952 sym
->initializer
= expr
;
953 sym
->enum_member
= 1;
954 sym
->ctype
.base_type
= parent
;
955 add_ptr_list(&parent
->symbol_list
, sym
);
957 if (base_type
!= &bad_ctype
) {
958 if (ctype
->type
== SYM_NODE
)
959 ctype
= ctype
->ctype
.base_type
;
960 if (ctype
->type
== SYM_ENUM
) {
964 ctype
= ctype
->ctype
.base_type
;
968 * - if all enums are of the same type, then
969 * the base_type is that type (two first
971 * - if enums are of different types, they
972 * all have to be integer types, and the
973 * base type is at least "int_ctype".
974 * - otherwise the base_type is "bad_ctype".
976 if (!base_type
|| ctype
== &bad_ctype
) {
978 } else if (ctype
== base_type
) {
980 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
981 base_type
= &int_ctype
;
982 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
983 if (!mix_bitwise
++) {
984 warning(expr
->pos
, "mixed bitwiseness");
986 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
987 sparse_error(expr
->pos
, "incompatible restricted type");
988 info(expr
->pos
, " expected: %s", show_typename(base_type
));
989 info(expr
->pos
, " got: %s", show_typename(ctype
));
990 base_type
= &bad_ctype
;
991 } else if (base_type
!= &bad_ctype
) {
992 sparse_error(token
->pos
, "bad enum definition");
993 base_type
= &bad_ctype
;
995 parent
->ctype
.base_type
= base_type
;
997 if (is_int_type(base_type
)) {
998 update_range(&range
, lastval
, ctype
);
1002 sym
->endpos
= token
->pos
;
1004 if (!match_op(token
, ','))
1006 token
= token
->next
;
1009 sparse_error(token
->pos
, "empty enum definition");
1010 base_type
= &bad_ctype
;
1012 else if (!is_int_type(base_type
))
1014 else if (type_is_ok(&uint_ctype
, range
))
1015 base_type
= &uint_ctype
;
1016 else if (type_is_ok(&int_ctype
, range
))
1017 base_type
= &int_ctype
;
1018 else if (type_is_ok(&ulong_ctype
, range
))
1019 base_type
= &ulong_ctype
;
1020 else if (type_is_ok(&long_ctype
, range
))
1021 base_type
= &long_ctype
;
1022 else if (type_is_ok(&ullong_ctype
, range
))
1023 base_type
= &ullong_ctype
;
1024 else if (type_is_ok(&llong_ctype
, range
))
1025 base_type
= &llong_ctype
;
1027 base_type
= &bad_ctype
;
1028 parent
->ctype
.base_type
= base_type
;
1029 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1030 parent
->examined
= 0;
1034 cast_enum_list(parent
->symbol_list
, base_type
);
1039 static struct token
*enum_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1041 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1042 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1044 if (!ctype
->base_type
)
1045 ctype
->base_type
= &incomplete_ctype
;
1050 static struct token
*typeof_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1053 if (!match_op(token
, '(')) {
1054 sparse_error(token
->pos
, "expected '(' after typeof");
1057 if (lookup_type(token
->next
)) {
1059 token
= typename(token
->next
, &sym
, NULL
);
1060 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1061 apply_ctype(token
->pos
, &ctx
->ctype
, &sym
->ctype
);
1063 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1064 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1066 typeof_sym
->endpos
= token
->pos
;
1067 if (!typeof_sym
->initializer
) {
1068 sparse_error(token
->pos
, "expected expression after the '(' token");
1069 typeof_sym
= &bad_ctype
;
1071 ctx
->ctype
.base_type
= typeof_sym
;
1073 return expect(token
, ')', "after typeof");
1076 static struct token
*autotype_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1078 ctx
->ctype
.base_type
= &autotype_ctype
;
1083 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1085 struct expression
*expr
= NULL
;
1086 if (match_op(token
, '('))
1087 token
= parens_expression(token
, &expr
, "in attribute");
1091 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1093 if (!ctx
->ctype
.alignment
) {
1094 ctx
->ctype
.alignment
= 1;
1100 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1102 int alignment
= max_alignment
;
1103 struct expression
*expr
= NULL
;
1105 if (match_op(token
, '(')) {
1106 token
= parens_expression(token
, &expr
, "in attribute");
1108 alignment
= const_expression_value(expr
);
1110 if (alignment
& (alignment
-1)) {
1111 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1113 } else if (alignment
> ctx
->ctype
.alignment
)
1114 ctx
->ctype
.alignment
= alignment
;
1118 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1120 if (*mods
& mod
& ~MOD_DUP_OK
)
1121 warning(*pos
, "duplicate %s", modifier_name(mod
));
1125 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1127 apply_mod(pos
, &ctx
->modifiers
, qual
);
1130 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1132 apply_mod(&token
->pos
, &ctx
->ctype
.modifiers
, attr
->ctype
.modifiers
);
1136 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1138 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1142 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1145 attribute_modifier(token
, attr
, ctx
);
1149 static struct ident
*numerical_address_space(int asn
)
1155 sprintf(buff
, "<asn:%d>", asn
);
1156 return built_in_ident(buff
);
1159 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1161 struct expression
*expr
= NULL
;
1162 struct ident
*as
= NULL
;
1165 token
= expect(token
, '(', "after address_space attribute");
1166 switch (token_type(token
)) {
1168 next
= primary_expression(token
, &expr
);
1169 if (expr
->type
!= EXPR_VALUE
)
1171 as
= numerical_address_space(expr
->value
);
1181 warning(token
->pos
, "invalid address space name");
1184 if (Waddress_space
&& as
) {
1186 sparse_error(token
->pos
,
1187 "multiple address spaces given: %s & %s",
1188 show_as(ctx
->ctype
.as
), show_as(as
));
1191 token
= expect(next
, ')', "after address_space attribute");
1195 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1197 if (ctype
->ctype
.base_type
!= &int_type
)
1199 if (ctype
== &char_ctype
)
1201 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1205 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1207 if (ctype
->ctype
.base_type
!= &int_type
)
1209 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1213 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1215 if (ctype
->ctype
.base_type
!= &int_type
)
1217 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1221 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1223 if (ctype
->ctype
.base_type
!= &int_type
)
1225 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1229 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1231 if (ctype
->ctype
.base_type
!= &int_type
)
1233 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1237 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1239 if (ctype
->ctype
.base_type
!= &int_type
)
1241 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1245 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1247 if (ctype
->ctype
.base_type
!= &int_type
)
1249 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1253 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1255 token
= expect(token
, '(', "after mode attribute");
1256 if (token_type(token
) == TOKEN_IDENT
) {
1257 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1258 if (mode
&& mode
->op
->type
& KW_MODE
)
1259 ctx
->mode
= mode
->op
;
1261 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1262 token
= token
->next
;
1264 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1265 token
= expect(token
, ')', "after mode attribute");
1269 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1271 struct context
*context
= alloc_context();
1272 struct expression
*args
[3];
1275 token
= expect(token
, '(', "after context attribute");
1276 token
= conditional_expression(token
, &args
[0]);
1277 token
= expect(token
, ',', "after context 1st argument");
1278 token
= conditional_expression(token
, &args
[1]);
1279 if (match_op(token
, ',')) {
1280 token
= token
->next
;
1281 token
= conditional_expression(token
, &args
[2]);
1282 token
= expect(token
, ')', "after context 3rd argument");
1283 context
->context
= args
[0];
1286 token
= expect(token
, ')', "after context 2nd argument");
1288 context
->in
= get_expression_value(args
[idx
++]);
1289 context
->out
= get_expression_value(args
[idx
++]);
1290 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1294 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1296 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1297 ctx
->ctype
.base_type
->designated_init
= 1;
1299 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1303 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1305 if (Wtransparent_union
)
1306 warning(token
->pos
, "attribute __transparent_union__");
1308 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1309 ctx
->ctype
.base_type
->transparent_union
= 1;
1311 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1315 static struct token
*recover_unknown_attribute(struct token
*token
)
1317 struct expression
*expr
= NULL
;
1319 if (Wunknown_attribute
)
1320 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1321 token
= token
->next
;
1322 if (match_op(token
, '('))
1323 token
= parens_expression(token
, &expr
, "in attribute");
1327 static struct token
*attribute_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1329 token
= expect(token
, '(', "after attribute");
1330 token
= expect(token
, '(', "after attribute");
1332 while (token_type(token
) == TOKEN_IDENT
) {
1333 struct symbol
*attr
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1334 if (attr
&& attr
->op
->attribute
)
1335 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1337 token
= recover_unknown_attribute(token
);
1339 if (!match_op(token
, ','))
1341 token
= token
->next
;
1344 token
= expect(token
, ')', "after attribute");
1345 token
= expect(token
, ')', "after attribute");
1349 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1351 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1352 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1353 return ctx
->storage_class
| mods
;
1356 static struct token
*storage_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1358 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1359 unsigned long class = sym
->ctype
.modifiers
;
1360 const char *storage
= modifier_name(class);
1362 /* __thread can be used alone, or with extern or static */
1363 if (is_tls
&& (class & ~(MOD_STATIC
|MOD_EXTERN
)))
1364 sparse_error(next
->pos
, "__thread cannot be used with '%s'", storage
);
1365 else if (!ctx
->storage_class
)
1366 ctx
->storage_class
= class;
1367 else if (ctx
->storage_class
== class)
1368 sparse_error(next
->pos
, "duplicate %s", storage
);
1370 sparse_error(next
->pos
, "multiple storage classes");
1374 static struct token
*thread_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1376 /* This GCC extension can be used alone, or with extern or static */
1377 if (!(ctx
->storage_class
& ~(MOD_STATIC
|MOD_EXTERN
))) {
1378 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1380 sparse_error(next
->pos
, "__thread cannot be used with '%s'",
1381 modifier_name(ctx
->storage_class
));
1387 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1393 static struct token
*alignas_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1397 if (!match_op(token
, '(')) {
1398 sparse_error(token
->pos
, "expected '(' after _Alignas");
1401 if (lookup_type(token
->next
)) {
1402 struct symbol
*sym
= NULL
;
1403 token
= typename(token
->next
, &sym
, NULL
);
1404 sym
= examine_symbol_type(sym
);
1405 alignment
= sym
->ctype
.alignment
;
1406 token
= expect(token
, ')', "after _Alignas(...");
1408 struct expression
*expr
= NULL
;
1409 token
= parens_expression(token
, &expr
, "after _Alignas");
1412 alignment
= const_expression_value(expr
);
1415 if (alignment
< 0) {
1416 warning(token
->pos
, "non-positive alignment");
1419 if (alignment
& (alignment
-1)) {
1420 warning(token
->pos
, "non-power-of-2 alignment");
1423 if (alignment
> ctx
->ctype
.alignment
)
1424 ctx
->ctype
.alignment
= alignment
;
1428 static struct token
*generic_qualifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1430 apply_qualifier(&next
->pos
, &ctx
->ctype
, sym
->ctype
.modifiers
);
1434 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
)
1436 unsigned long mod
= src
->modifiers
;
1439 apply_qualifier(&pos
, dst
, mod
);
1442 concat_ptr_list((struct ptr_list
*)src
->contexts
,
1443 (struct ptr_list
**)&dst
->contexts
);
1446 if (src
->alignment
> dst
->alignment
)
1447 dst
->alignment
= src
->alignment
;
1454 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1457 if (what
& (Set_S
| Set_T
))
1459 if (what
& Set_Char
)
1461 else if (what
& Set_Double
)
1463 else if (what
& Set_Float
)
1465 else if (what
& Set_Signed
)
1467 else if (what
& Set_Unsigned
)
1469 else if (what
& Set_Short
)
1471 else if (what
& Set_Long
)
1475 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1476 old
, show_ident(new));
1480 sparse_error(pos
, "two or more data types in declaration specifiers");
1483 static struct symbol
* const int_types
[] =
1484 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1485 static struct symbol
* const signed_types
[] =
1486 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1488 static struct symbol
* const unsigned_types
[] =
1489 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1491 static struct symbol
* const real_types
[] =
1492 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1493 static struct symbol
* const * const types
[] = {
1494 [CInt
] = int_types
+ 2,
1495 [CSInt
] = signed_types
+ 2,
1496 [CUInt
] = unsigned_types
+ 2,
1497 [CReal
] = real_types
+ 1,
1500 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1502 return types
[want_unsigned
? CUInt
: CInt
][size
];
1505 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1507 while (token_type(t
) == TOKEN_IDENT
) {
1508 struct symbol
*s
= lookup_keyword(t
->ident
, NS_TYPEDEF
);
1511 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1514 if (s
->op
->declarator
)
1515 t
= s
->op
->declarator(t
, s
, ctx
);
1520 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1526 while (token_type(token
) == TOKEN_IDENT
) {
1527 struct symbol
*s
= lookup_symbol(token
->ident
,
1528 NS_TYPEDEF
| NS_SYMBOL
);
1529 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1531 if (s
->type
!= SYM_KEYWORD
) {
1534 seen
|= Set_S
| Set_T
;
1535 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1536 apply_ctype(token
->pos
, &ctx
->ctype
, &s
->ctype
);
1537 token
= token
->next
;
1540 if (s
->op
->type
& KW_SPECIFIER
) {
1541 if (seen
& s
->op
->test
) {
1542 specifier_conflict(token
->pos
,
1548 class += s
->op
->class;
1549 if (s
->op
->set
& Set_Int128
)
1551 else if (s
->op
->set
& Set_Char
)
1553 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1555 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1556 if (class == CReal
) {
1557 specifier_conflict(token
->pos
,
1565 token
= token
->next
;
1566 if (s
->op
->declarator
) // Note: this eats attributes
1567 token
= s
->op
->declarator(token
, s
, ctx
);
1568 if (s
->op
->type
& KW_EXACT
) {
1569 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1570 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1574 if (!(seen
& Set_S
)) { /* not set explicitly? */
1575 struct symbol
*base
= &incomplete_ctype
;
1577 base
= types
[class][rank
];
1578 ctx
->ctype
.base_type
= base
;
1581 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1582 struct symbol
*type
;
1583 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1584 if (!is_int_type(ctx
->ctype
.base_type
)) {
1585 sparse_error(token
->pos
, "invalid modifier");
1588 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1589 *type
= *ctx
->ctype
.base_type
;
1590 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1591 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1592 type
->type
= SYM_RESTRICT
;
1593 ctx
->ctype
.base_type
= type
;
1594 create_fouled(type
);
1599 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1601 struct expression
*expr
= NULL
;
1604 while (token_type(token
) == TOKEN_IDENT
) {
1605 struct symbol
*sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1606 if (!sym
|| !(sym
->op
->type
& (KW_STATIC
|KW_QUALIFIER
)))
1608 if (has_static
&& (sym
->op
->type
& KW_STATIC
))
1609 sparse_error(token
->pos
, "duplicate array static declarator");
1610 has_static
|= (sym
->op
->type
& KW_STATIC
);
1611 token
= token
->next
;
1613 if (match_op(token
, '*') && match_op(token
->next
, ']')) {
1614 // FIXME: '[*]' is treated like '[]'
1615 token
= token
->next
;
1617 token
= assignment_expression(token
, &expr
);
1619 sym
->array_size
= expr
;
1623 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1624 static struct token
*identifier_list(struct token
*, struct symbol
*);
1625 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1627 static struct token
*handle_asm_name(struct token
*token
, struct decl_state
*ctx
)
1629 struct expression
*expr
;
1630 struct symbol
*keyword
;
1632 if (token_type(token
) != TOKEN_IDENT
)
1634 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1637 if (!(keyword
->op
->type
& KW_ASM
))
1640 token
= token
->next
;
1641 token
= expect(token
, '(', "after asm");
1642 token
= string_expression(token
, &expr
, "asm name");
1643 token
= expect(token
, ')', "after asm");
1648 // test if @token is '__attribute__' (or one of its variant)
1649 static bool match_attribute(struct token
*token
)
1653 if (token_type(token
) != TOKEN_IDENT
)
1655 sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1658 return sym
->op
->type
& KW_ATTRIBUTE
;
1661 static struct token
*skip_attribute(struct token
*token
)
1663 token
= token
->next
;
1664 if (match_op(token
, '(')) {
1666 token
= token
->next
;
1667 while (depth
&& !eof_token(token
)) {
1668 if (token_type(token
) == TOKEN_SPECIAL
) {
1669 if (token
->special
== '(')
1671 else if (token
->special
== ')')
1674 token
= token
->next
;
1680 static struct token
*skip_attributes(struct token
*token
)
1682 while (match_attribute(token
)) {
1683 token
= expect(token
->next
, '(', "after attribute");
1684 token
= expect(token
, '(', "after attribute");
1685 while (token_type(token
) == TOKEN_IDENT
) {
1686 token
= skip_attribute(token
);
1687 if (!match_op(token
, ','))
1689 token
= token
->next
;
1691 token
= expect(token
, ')', "after attribute");
1692 token
= expect(token
, ')', "after attribute");
1697 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
)
1699 while (match_attribute(token
))
1700 token
= attribute_specifier(token
->next
, NULL
, ctx
);
1704 static int is_nested(struct token
*token
, struct token
**p
,
1705 int prefer_abstract
)
1708 * This can be either a parameter list or a grouping.
1709 * For the direct (non-abstract) case, we know if must be
1710 * a parameter list if we already saw the identifier.
1711 * For the abstract case, we know if must be a parameter
1712 * list if it is empty or starts with a type.
1714 struct token
*next
= token
->next
;
1716 *p
= next
= skip_attributes(next
);
1718 if (token_type(next
) == TOKEN_IDENT
) {
1719 if (lookup_type(next
))
1720 return !prefer_abstract
;
1724 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1731 Empty
, K_R
, Proto
, Bad_Func
,
1734 static enum kind
which_func(struct token
*token
,
1736 int prefer_abstract
)
1738 struct token
*next
= token
->next
;
1740 if (token_type(next
) == TOKEN_IDENT
) {
1741 if (lookup_type(next
))
1743 /* identifier list not in definition; complain */
1744 if (prefer_abstract
)
1746 "identifier list not in definition");
1750 if (token_type(next
) != TOKEN_SPECIAL
)
1753 if (next
->special
== ')') {
1754 /* don't complain about those */
1755 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1757 if (Wstrict_prototypes
)
1759 "non-ANSI function declaration of function '%s'",
1764 if (next
->special
== SPECIAL_ELLIPSIS
) {
1766 "variadic functions must have one named argument");
1773 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1775 struct ctype
*ctype
= &ctx
->ctype
;
1777 struct ident
**p
= ctx
->ident
;
1779 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1780 *ctx
->ident
= token
->ident
;
1781 token
= token
->next
;
1782 } else if (match_op(token
, '(') &&
1783 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1784 struct symbol
*base_type
= ctype
->base_type
;
1785 if (token
->next
!= next
)
1786 next
= handle_attributes(token
->next
, ctx
);
1787 token
= declarator(next
, ctx
);
1788 token
= expect(token
, ')', "in nested declarator");
1789 while (ctype
->base_type
!= base_type
)
1790 ctype
= &ctype
->base_type
->ctype
;
1794 if (match_op(token
, '(')) {
1795 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1797 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1798 ctype
->modifiers
|= ctx
->f_modifiers
;
1799 token
= token
->next
;
1801 token
= identifier_list(token
, fn
);
1802 else if (kind
== Proto
)
1803 token
= parameter_type_list(token
, fn
);
1804 token
= expect(token
, ')', "in function declarator");
1805 fn
->endpos
= token
->pos
;
1809 while (match_op(token
, '[')) {
1810 struct symbol
*array
;
1811 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1812 token
= abstract_array_declarator(token
->next
, array
);
1813 token
= expect(token
, ']', "in abstract_array_declarator");
1814 array
->endpos
= token
->pos
;
1815 ctype
= &array
->ctype
;
1820 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1822 while (match_op(token
,'*')) {
1823 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1824 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1825 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1826 ptr
->ctype
.as
= ctx
->ctype
.as
;
1827 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1828 ctx
->ctype
.modifiers
= 0;
1829 ctx
->ctype
.base_type
= ptr
;
1830 ctx
->ctype
.as
= NULL
;
1831 ctx
->ctype
.contexts
= NULL
;
1832 ctx
->ctype
.alignment
= 0;
1834 token
= handle_qualifiers(token
->next
, ctx
);
1835 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1840 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1842 token
= pointer(token
, ctx
);
1843 return direct_declarator(token
, ctx
);
1846 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1848 struct ctype
*ctype
= &ctx
->ctype
;
1849 struct expression
*expr
;
1850 struct symbol
*bitfield
;
1853 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1854 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1855 show_typename(ctype
->base_type
));
1856 // Parse this to recover gracefully.
1857 return conditional_expression(token
->next
, &expr
);
1860 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1861 token
= conditional_expression(token
->next
, &expr
);
1862 width
= const_expression_value(expr
);
1863 bitfield
->bit_size
= width
;
1865 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
1866 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
1867 show_ident(*ctx
->ident
), width
);
1869 } else if (*ctx
->ident
) {
1870 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1871 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1872 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1873 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1874 // Valid values are either {-1;0} or {0}, depending on integer
1875 // representation. The latter makes for very efficient code...
1876 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1878 if (Wdefault_bitfield_sign
&&
1879 bitfield_type
->type
!= SYM_ENUM
&&
1880 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1882 // The sign of bitfields is unspecified by default.
1883 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1886 bitfield
->bit_size
= width
;
1887 bitfield
->endpos
= token
->pos
;
1888 bitfield
->ident
= *ctx
->ident
;
1892 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1894 struct decl_state ctx
= {.prefer_abstract
= 0};
1898 token
= declaration_specifiers(token
, &ctx
);
1899 mod
= decl_modifiers(&ctx
);
1902 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1903 ctx
.ident
= &decl
->ident
;
1905 token
= declarator(token
, &ctx
);
1906 if (match_op(token
, ':'))
1907 token
= handle_bitfield(token
, &ctx
);
1909 token
= handle_attributes(token
, &ctx
);
1910 apply_modifiers(token
->pos
, &ctx
);
1912 decl
->ctype
= ctx
.ctype
;
1913 decl
->ctype
.modifiers
|= mod
;
1914 decl
->endpos
= token
->pos
;
1915 add_symbol(list
, decl
);
1916 if (!match_op(token
, ','))
1918 token
= token
->next
;
1924 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1926 while (!match_op(token
, '}')) {
1927 if (match_ident(token
, &_Static_assert_ident
)) {
1928 token
= parse_static_assert(token
, NULL
);
1931 if (!match_op(token
, ';'))
1932 token
= declaration_list(token
, list
);
1933 if (!match_op(token
, ';')) {
1934 sparse_error(token
->pos
, "expected ; at end of declaration");
1937 token
= token
->next
;
1942 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1944 struct decl_state ctx
= {.prefer_abstract
= 1};
1946 token
= declaration_specifiers(token
, &ctx
);
1947 ctx
.ident
= &sym
->ident
;
1948 token
= declarator(token
, &ctx
);
1949 token
= handle_attributes(token
, &ctx
);
1950 apply_modifiers(token
->pos
, &ctx
);
1951 sym
->ctype
= ctx
.ctype
;
1952 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
1953 sym
->endpos
= token
->pos
;
1954 sym
->forced_arg
= ctx
.forced
;
1958 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1960 struct decl_state ctx
= {.prefer_abstract
= 1};
1961 unsigned long class;
1962 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1964 token
= declaration_specifiers(token
, &ctx
);
1965 token
= declarator(token
, &ctx
);
1966 apply_modifiers(token
->pos
, &ctx
);
1967 sym
->ctype
= ctx
.ctype
;
1968 sym
->endpos
= token
->pos
;
1969 class = ctx
.storage_class
;
1971 *forced
= ctx
.forced
;
1973 warning(sym
->pos
, "storage class in typename (%s%s)",
1974 modifier_string(class), show_typename(sym
));
1978 static struct token
*parse_underscore_Pragma(struct token
*token
)
1983 if (!match_op(next
, '('))
1986 if (next
->pos
.type
!= TOKEN_STRING
)
1989 if (!match_op(next
, ')'))
1994 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1996 if (match_ident(token
, &_Pragma_ident
))
1997 return parse_underscore_Pragma(token
);
1999 token
= parse_expression(token
, tree
);
2000 return expect(token
, ';', "at end of statement");
2003 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2004 struct asm_operand_list
**inout
)
2006 /* Allow empty operands */
2007 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2010 struct asm_operand
*op
= __alloc_asm_operand(0);
2011 if (match_op(token
->next
, '[') &&
2012 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2013 match_op(token
->next
->next
->next
, ']')) {
2014 op
->name
= token
->next
->next
->ident
;
2015 token
= token
->next
->next
->next
;
2017 token
= token
->next
;
2018 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2019 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2020 add_ptr_list(inout
, op
);
2021 } while (match_op(token
, ','));
2025 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2026 struct expression_list
**clobbers
)
2028 struct expression
*expr
;
2031 token
= primary_expression(token
->next
, &expr
);
2033 add_expression(clobbers
, expr
);
2034 } while (match_op(token
, ','));
2038 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2039 struct symbol_list
**labels
)
2041 struct symbol
*label
;
2044 token
= token
->next
; /* skip ':' and ',' */
2045 if (token_type(token
) != TOKEN_IDENT
)
2047 label
= label_symbol(token
, 1);
2048 add_symbol(labels
, label
);
2049 token
= token
->next
;
2050 } while (match_op(token
, ','));
2054 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2056 unsigned long mods
= 0;
2058 token
= token
->next
;
2059 stmt
->type
= STMT_ASM
;
2060 while (token_type(token
) == TOKEN_IDENT
) {
2061 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2062 if (s
&& s
->op
->asm_modifier
)
2063 s
->op
->asm_modifier(token
, &mods
, s
->ctype
.modifiers
);
2064 else if (token
->ident
== &goto_ident
)
2065 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2066 token
= token
->next
;
2068 token
= expect(token
, '(', "after asm");
2069 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2070 if (match_op(token
, ':'))
2071 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2072 if (match_op(token
, ':'))
2073 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2074 if (match_op(token
, ':'))
2075 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2076 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2077 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2078 token
= expect(token
, ')', "after asm");
2079 return expect(token
, ';', "at end of asm-statement");
2082 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2084 struct expression
*cond
= NULL
, *message
= NULL
;
2086 token
= expect(token
->next
, '(', "after _Static_assert");
2087 token
= constant_expression(token
, &cond
);
2089 sparse_error(token
->pos
, "Expected constant expression");
2090 if (match_op(token
, ',')) {
2091 token
= token
->next
;
2092 token
= string_expression(token
, &message
, "_Static_assert()");
2096 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2097 token
= expect(token
, ';', "after _Static_assert()");
2099 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
) {
2100 const char *sep
= "", *msg
= "";
2104 msg
= show_string(message
->string
);
2106 sparse_error(cond
->pos
, "static assertion failed%s%s", sep
, msg
);
2112 /* Make a statement out of an expression */
2113 static struct statement
*make_statement(struct expression
*expr
)
2115 struct statement
*stmt
;
2119 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2120 stmt
->expression
= expr
;
2125 * All iterators have two symbols associated with them:
2126 * the "continue" and "break" symbols, which are targets
2127 * for continue and break statements respectively.
2129 * They are in a special name-space, but they follow
2130 * all the normal visibility rules, so nested iterators
2131 * automatically work right.
2133 static void start_iterator(struct statement
*stmt
)
2135 struct symbol
*cont
, *brk
;
2137 start_block_scope(stmt
->pos
);
2138 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2139 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2140 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2141 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2143 stmt
->type
= STMT_ITERATOR
;
2144 stmt
->iterator_break
= brk
;
2145 stmt
->iterator_continue
= cont
;
2146 fn_local_symbol(brk
);
2147 fn_local_symbol(cont
);
2150 static void end_iterator(struct statement
*stmt
)
2155 static struct statement
*start_function(struct symbol
*sym
)
2158 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2160 start_function_scope(sym
->pos
);
2161 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2162 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2163 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2164 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2165 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2167 fn_local_symbol(ret
);
2169 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2175 static void end_function(struct symbol
*sym
)
2178 end_function_scope();
2182 * A "switch()" statement, like an iterator, has a
2183 * the "break" symbol associated with it. It works
2184 * exactly like the iterator break - it's the target
2185 * for any break-statements in scope, and means that
2186 * "break" handling doesn't even need to know whether
2187 * it's breaking out of an iterator or a switch.
2189 * In addition, the "case" symbol is a marker for the
2190 * case/default statements to find the switch statement
2191 * that they are associated with.
2193 static void start_switch(struct statement
*stmt
)
2195 struct symbol
*brk
, *switch_case
;
2197 start_block_scope(stmt
->pos
);
2198 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2199 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2201 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2202 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2203 switch_case
->stmt
= stmt
;
2205 stmt
->type
= STMT_SWITCH
;
2206 stmt
->switch_break
= brk
;
2207 stmt
->switch_case
= switch_case
;
2209 fn_local_symbol(brk
);
2210 fn_local_symbol(switch_case
);
2213 static void end_switch(struct statement
*stmt
)
2215 if (!stmt
->switch_case
->symbol_list
)
2216 warning(stmt
->pos
, "switch with no cases");
2220 static void add_case_statement(struct statement
*stmt
)
2222 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2226 sparse_error(stmt
->pos
, "not in switch scope");
2227 stmt
->type
= STMT_NONE
;
2230 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2231 add_symbol(&target
->symbol_list
, sym
);
2233 stmt
->case_label
= sym
;
2234 fn_local_symbol(sym
);
2237 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2239 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2242 error_die(token
->pos
, "internal error: return without a function target");
2243 stmt
->type
= STMT_RETURN
;
2244 stmt
->ret_target
= target
;
2245 return expression_statement(token
->next
, &stmt
->ret_value
);
2248 static void validate_for_loop_decl(struct symbol
*sym
)
2250 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2252 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2253 const char *name
= show_ident(sym
->ident
);
2254 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2255 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2259 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2261 struct symbol_list
*syms
;
2262 struct expression
*e1
, *e2
, *e3
;
2263 struct statement
*iterator
;
2265 start_iterator(stmt
);
2266 token
= expect(token
->next
, '(', "after 'for'");
2270 /* C99 variable declaration? */
2271 if (lookup_type(token
)) {
2272 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2274 token
= parse_expression(token
, &e1
);
2275 token
= expect(token
, ';', "in 'for'");
2277 token
= parse_expression(token
, &e2
);
2278 token
= expect(token
, ';', "in 'for'");
2279 token
= parse_expression(token
, &e3
);
2280 token
= expect(token
, ')', "in 'for'");
2281 token
= statement(token
, &iterator
);
2283 stmt
->iterator_syms
= syms
;
2284 stmt
->iterator_pre_statement
= make_statement(e1
);
2285 stmt
->iterator_pre_condition
= e2
;
2286 stmt
->iterator_post_statement
= make_statement(e3
);
2287 stmt
->iterator_post_condition
= NULL
;
2288 stmt
->iterator_statement
= iterator
;
2294 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2296 struct expression
*expr
;
2297 struct statement
*iterator
;
2299 start_iterator(stmt
);
2300 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2301 token
= statement(token
, &iterator
);
2303 stmt
->iterator_pre_condition
= expr
;
2304 stmt
->iterator_post_condition
= NULL
;
2305 stmt
->iterator_statement
= iterator
;
2311 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2313 struct expression
*expr
;
2314 struct statement
*iterator
;
2316 start_iterator(stmt
);
2317 token
= statement(token
->next
, &iterator
);
2318 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2319 token
= token
->next
;
2321 sparse_error(token
->pos
, "expected 'while' after 'do'");
2322 token
= parens_expression(token
, &expr
, "after 'do-while'");
2324 stmt
->iterator_post_condition
= expr
;
2325 stmt
->iterator_statement
= iterator
;
2328 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2329 warning(iterator
->pos
, "do-while statement is not a compound statement");
2331 return expect(token
, ';', "after statement");
2334 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2336 stmt
->type
= STMT_IF
;
2337 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2338 token
= statement(token
, &stmt
->if_true
);
2339 if (token_type(token
) != TOKEN_IDENT
)
2341 if (token
->ident
!= &else_ident
)
2343 return statement(token
->next
, &stmt
->if_false
);
2346 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2348 stmt
->type
= STMT_CASE
;
2349 token
= expect(token
, ':', "after default/case");
2350 add_case_statement(stmt
);
2351 return statement(token
, &stmt
->case_statement
);
2354 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2356 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2357 if (match_op(token
, SPECIAL_ELLIPSIS
))
2358 token
= parse_expression(token
->next
, &stmt
->case_to
);
2359 return case_statement(token
, stmt
);
2362 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2364 return case_statement(token
->next
, stmt
);
2367 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2369 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2370 stmt
->type
= STMT_GOTO
;
2371 stmt
->goto_label
= target
;
2373 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2374 return expect(token
->next
, ';', "at end of statement");
2377 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2379 stmt
->type
= STMT_SWITCH
;
2381 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2382 token
= statement(token
, &stmt
->switch_statement
);
2387 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2389 const char *id
= show_ident(ident
);
2390 sparse_error(use
, "label '%s' used outside statement expression", id
);
2391 info(def
, " label '%s' defined here", id
);
2392 current_fn
->bogus_linear
= 1;
2395 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2397 struct statement
*def
= label
->stmt
;
2400 if (!is_in_scope(def
->label_scope
, label_scope
))
2401 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2402 } else if (!label
->label_scope
) {
2403 label
->label_scope
= label_scope
;
2404 label
->label_pos
= use_pos
;
2408 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2410 stmt
->type
= STMT_GOTO
;
2411 token
= token
->next
;
2412 if (match_op(token
, '*')) {
2413 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2414 add_statement(&function_computed_goto_list
, stmt
);
2415 } else if (token_type(token
) == TOKEN_IDENT
) {
2416 struct symbol
*label
= label_symbol(token
, 1);
2417 stmt
->goto_label
= label
;
2418 check_label_usage(label
, stmt
->pos
);
2419 token
= token
->next
;
2421 sparse_error(token
->pos
, "Expected identifier or goto expression");
2423 return expect(token
, ';', "at end of statement");
2426 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2428 stmt
->type
= STMT_CONTEXT
;
2429 token
= token
->next
;
2430 token
= expect(token
, '(', "after __context__ statement");
2431 token
= assignment_expression(token
, &stmt
->expression
);
2432 if (!stmt
->expression
)
2433 unexpected(token
, "expression expected after '('");
2434 if (match_op(token
, ',')) {
2435 token
= token
->next
;
2436 stmt
->context
= stmt
->expression
;
2437 token
= assignment_expression(token
, &stmt
->expression
);
2438 if (!stmt
->expression
)
2439 unexpected(token
, "expression expected after ','");
2441 token
= expect(token
, ')', "at end of __context__ statement");
2442 return expect(token
, ';', "at end of statement");
2445 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2447 stmt
->type
= STMT_RANGE
;
2448 token
= token
->next
;
2449 token
= expect(token
, '(', "after __range__ statement");
2450 token
= assignment_expression(token
, &stmt
->range_expression
);
2451 token
= expect(token
, ',', "after range expression");
2452 token
= assignment_expression(token
, &stmt
->range_low
);
2453 token
= expect(token
, ',', "after low range");
2454 token
= assignment_expression(token
, &stmt
->range_high
);
2455 token
= expect(token
, ')', "after range statement");
2456 return expect(token
, ';', "after range statement");
2459 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2461 struct decl_state ctx
= { };
2463 token
= handle_attributes(token
, &ctx
);
2464 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2468 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2470 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2473 if (token_type(token
) == TOKEN_IDENT
) {
2474 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2475 if (s
&& s
->op
->statement
)
2476 return s
->op
->statement(token
, stmt
);
2478 if (match_op(token
->next
, ':')) {
2479 struct symbol
*s
= label_symbol(token
, 0);
2480 token
= handle_label_attributes(token
->next
->next
, s
);
2482 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2483 // skip the label to avoid multiple definitions
2484 return statement(token
, tree
);
2486 stmt
->type
= STMT_LABEL
;
2487 stmt
->label_identifier
= s
;
2488 stmt
->label_scope
= label_scope
;
2489 if (s
->label_scope
) {
2490 if (!is_in_scope(label_scope
, s
->label_scope
))
2491 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2494 if (match_op(token
, '}')) {
2495 warning(token
->pos
, "statement expected after label");
2496 stmt
->label_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2499 return statement(token
, &stmt
->label_statement
);
2503 if (match_op(token
, '{')) {
2504 token
= compound_statement(token
->next
, stmt
);
2505 return expect(token
, '}', "at end of compound statement");
2508 stmt
->type
= STMT_EXPRESSION
;
2509 return expression_statement(token
, &stmt
->expression
);
2512 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2513 static struct token
*label_statement(struct token
*token
)
2515 while (token_type(token
) == TOKEN_IDENT
) {
2516 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2517 /* it's block-scope, but we want label namespace */
2518 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2519 fn_local_symbol(sym
);
2520 token
= token
->next
;
2521 if (!match_op(token
, ','))
2523 token
= token
->next
;
2525 return expect(token
, ';', "at end of label declaration");
2528 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2530 int seen_statement
= 0;
2531 while (token_type(token
) == TOKEN_IDENT
&&
2532 token
->ident
== &__label___ident
)
2533 token
= label_statement(token
->next
);
2535 struct statement
* stmt
;
2536 if (eof_token(token
))
2538 if (match_op(token
, '}'))
2540 if (match_ident(token
, &_Static_assert_ident
)) {
2541 token
= parse_static_assert(token
, NULL
);
2544 if (lookup_type(token
)) {
2545 if (seen_statement
) {
2546 warning(token
->pos
, "mixing declarations and code");
2549 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2550 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2552 seen_statement
= Wdeclarationafterstatement
;
2553 token
= statement(token
, &stmt
);
2555 add_statement(list
, stmt
);
2560 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2562 struct symbol_list
**list
= &fn
->arguments
;
2564 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2565 sym
->ident
= token
->ident
;
2566 token
= token
->next
;
2567 sym
->endpos
= token
->pos
;
2568 sym
->ctype
.base_type
= &incomplete_ctype
;
2569 add_symbol(list
, sym
);
2570 if (!match_op(token
, ',') ||
2571 token_type(token
->next
) != TOKEN_IDENT
||
2572 lookup_type(token
->next
))
2574 token
= token
->next
;
2579 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2581 struct symbol_list
**list
= &fn
->arguments
;
2586 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2588 token
= token
->next
;
2592 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2593 token
= parameter_declaration(token
, sym
);
2594 if (sym
->ctype
.base_type
== &void_ctype
) {
2595 /* Special case: (void) */
2596 if (!*list
&& !sym
->ident
)
2598 warning(token
->pos
, "void parameter");
2600 add_symbol(list
, sym
);
2601 if (!match_op(token
, ','))
2603 token
= token
->next
;
2608 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2610 stmt
->type
= STMT_COMPOUND
;
2611 start_block_scope(token
->pos
);
2612 token
= statement_list(token
, &stmt
->stmts
);
2617 static struct expression
*identifier_expression(struct token
*token
)
2619 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2620 expr
->expr_ident
= token
->ident
;
2624 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2626 int idx_from
, idx_to
;
2627 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2629 idx_from
= const_expression_value(from
);
2632 idx_to
= const_expression_value(to
);
2633 if (idx_to
< idx_from
|| idx_from
< 0)
2634 warning(from
->pos
, "nonsense array initializer index range");
2636 expr
->idx_from
= idx_from
;
2637 expr
->idx_to
= idx_to
;
2641 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2643 int expect_equal
= 0;
2644 struct token
*next
= token
->next
;
2645 struct expression
**tail
= ep
;
2650 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2651 struct expression
*expr
= identifier_expression(token
);
2652 if (Wold_initializer
)
2653 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2654 token
= initializer(&expr
->ident_expression
, next
->next
);
2655 if (expr
->ident_expression
)
2660 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2661 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2662 struct expression
*expr
= identifier_expression(next
);
2664 tail
= &expr
->ident_expression
;
2667 } else if (match_op(token
, '[')) {
2668 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2669 token
= constant_expression(token
->next
, &from
);
2671 sparse_error(token
->pos
, "Expected constant expression");
2674 if (match_op(token
, SPECIAL_ELLIPSIS
))
2675 token
= constant_expression(token
->next
, &to
);
2676 expr
= index_expression(from
, to
);
2678 tail
= &expr
->idx_expression
;
2679 token
= expect(token
, ']', "at end of initializer index");
2686 if (nested
&& !expect_equal
) {
2687 if (!match_op(token
, '='))
2688 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2693 token
= expect(token
, '=', "at end of initializer index");
2695 token
= initializer(tail
, token
);
2701 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2703 struct expression
*expr
;
2706 token
= single_initializer(&expr
, token
);
2709 add_expression(list
, expr
);
2710 if (!match_op(token
, ','))
2712 token
= token
->next
;
2717 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2719 if (match_op(token
, '{')) {
2720 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2722 if (!Wuniversal_initializer
) {
2723 struct token
*next
= token
->next
;
2724 // '{ 0 }' is equivalent to '{ }' except for some
2725 // warnings, like using 0 to initialize a null-pointer.
2726 if (match_token_zero(next
)) {
2727 if (match_op(next
->next
, '}'))
2728 expr
->zero_init
= 1;
2732 token
= initializer_list(&expr
->expr_list
, token
->next
);
2733 return expect(token
, '}', "at end of initializer");
2735 return assignment_expression(token
, tree
);
2738 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2741 sparse_error(sym
->pos
, "no identifier for function argument");
2744 if (sym
->ctype
.base_type
== &incomplete_ctype
) {
2745 sym
->ctype
.base_type
= &int_ctype
;
2747 if (Wimplicit_int
) {
2748 sparse_error(sym
->pos
, "missing type declaration for parameter '%s'",
2749 show_ident(sym
->ident
));
2752 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2755 static int is_syscall(struct symbol
*sym
)
2761 macro
= get_macro_name(sym
->pos
);
2763 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2764 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2767 name
= sym
->ident
->name
;
2769 if (name
&& strncmp(name
, "sys_", 4) ==0)
2772 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2779 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2780 struct symbol_list
**list
)
2782 struct symbol_list
**old_symbol_list
;
2783 struct symbol
*base_type
= decl
->ctype
.base_type
;
2784 struct statement
*stmt
, **p
;
2785 struct symbol
*prev
;
2788 old_symbol_list
= function_symbol_list
;
2789 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2790 function_symbol_list
= &decl
->inline_symbol_list
;
2791 p
= &base_type
->inline_stmt
;
2793 function_symbol_list
= &decl
->symbol_list
;
2794 p
= &base_type
->stmt
;
2796 function_computed_target_list
= NULL
;
2797 function_computed_goto_list
= NULL
;
2799 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2800 if (Wexternal_function_has_definition
)
2801 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2803 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2804 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2806 stmt
= start_function(decl
);
2809 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2810 declare_argument(arg
, base_type
);
2811 } END_FOR_EACH_PTR(arg
);
2813 token
= statement_list(token
->next
, &stmt
->stmts
);
2816 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2817 add_symbol(list
, decl
);
2818 else if (is_syscall(decl
)) {
2819 add_symbol(list
, decl
);
2821 printf("parse.c decl: %s\n", decl->ident->name);
2822 char *macro = get_macro_name(decl->pos);
2823 printf("decl macro: %s\n", macro);
2826 check_declaration(decl
);
2827 decl
->definition
= decl
;
2828 prev
= decl
->same_symbol
;
2829 if (prev
&& prev
->definition
) {
2830 warning(decl
->pos
, "multiple definitions for function '%s'",
2831 show_ident(decl
->ident
));
2832 info(prev
->definition
->pos
, " the previous one is here");
2835 rebind_scope(prev
, decl
->scope
);
2836 prev
->definition
= decl
;
2837 prev
= prev
->same_symbol
;
2840 function_symbol_list
= old_symbol_list
;
2841 if (function_computed_goto_list
) {
2842 if (!function_computed_target_list
)
2843 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2845 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2846 stmt
->target_list
= function_computed_target_list
;
2847 } END_FOR_EACH_PTR(stmt
);
2850 return expect(token
, '}', "at end of function");
2853 static void promote_k_r_types(struct symbol
*arg
)
2855 struct symbol
*base
= arg
->ctype
.base_type
;
2856 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2857 arg
->ctype
.base_type
= &int_ctype
;
2861 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2863 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2866 FOR_EACH_PTR(real_args
, arg
) {
2867 struct symbol
*type
;
2869 /* This is quadratic in the number of arguments. We _really_ don't care */
2870 FOR_EACH_PTR(argtypes
, type
) {
2871 if (type
->ident
== arg
->ident
)
2873 } END_FOR_EACH_PTR(type
);
2874 if (Wimplicit_int
) {
2875 warning(arg
->pos
, "missing type declaration for parameter '%s'",
2876 show_ident(arg
->ident
));
2878 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
2879 type
->ident
= arg
->ident
;
2880 type
->ctype
.base_type
= &int_ctype
;
2883 /* "char" and "short" promote to "int" */
2884 promote_k_r_types(type
);
2886 arg
->ctype
= type
->ctype
;
2887 } END_FOR_EACH_PTR(arg
);
2889 FOR_EACH_PTR(argtypes
, arg
) {
2891 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2892 } END_FOR_EACH_PTR(arg
);
2896 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2897 struct symbol_list
**list
)
2899 struct symbol_list
*args
= NULL
;
2901 if (Wold_style_definition
)
2902 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2905 token
= declaration_list(token
, &args
);
2906 if (!match_op(token
, ';')) {
2907 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2910 token
= token
->next
;
2911 } while (lookup_type(token
));
2913 apply_k_r_types(args
, decl
);
2915 if (!match_op(token
, '{')) {
2916 sparse_error(token
->pos
, "expected function body");
2919 return parse_function_body(token
, decl
, list
);
2922 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2924 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2925 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2926 struct statement
*stmt
;
2928 anon
->ctype
.base_type
= fn
;
2929 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2932 token
= parse_asm_statement(token
, stmt
);
2934 // FIXME: add_symbol(list, anon);
2938 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2939 validate_decl_t validate_decl
)
2941 struct ident
*ident
= NULL
;
2942 struct symbol
*decl
;
2943 struct decl_state ctx
= { .ident
= &ident
};
2945 struct symbol
*base_type
;
2949 if (match_ident(token
, &_Pragma_ident
))
2950 return parse_underscore_Pragma(token
);
2952 /* Top-level inline asm or static assertion? */
2953 if (token_type(token
) == TOKEN_IDENT
) {
2954 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2955 if (s
&& s
->op
->toplevel
)
2956 return s
->op
->toplevel(token
, list
);
2959 /* Parse declaration-specifiers, if any */
2960 token
= declaration_specifiers(token
, &ctx
);
2961 mod
= decl_modifiers(&ctx
);
2962 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2963 /* Just a type declaration? */
2964 if (match_op(token
, ';')) {
2965 apply_modifiers(token
->pos
, &ctx
);
2970 token
= declarator(token
, &ctx
);
2971 token
= handle_asm_name(token
, &ctx
);
2972 token
= handle_attributes(token
, &ctx
);
2973 apply_modifiers(token
->pos
, &ctx
);
2975 decl
->ctype
= ctx
.ctype
;
2976 decl
->ctype
.modifiers
|= mod
;
2977 decl
->endpos
= token
->pos
;
2979 /* Just a type declaration? */
2981 warning(token
->pos
, "missing identifier in declaration");
2982 return expect(token
, ';', "at the end of type declaration");
2985 /* type define declaration? */
2986 is_typedef
= ctx
.storage_class
== MOD_USERTYPE
;
2988 /* Typedefs don't have meaningful storage */
2990 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2992 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2994 base_type
= decl
->ctype
.base_type
;
2997 if (base_type
&& !base_type
->ident
) {
2998 switch (base_type
->type
) {
3003 base_type
->ident
= ident
;
3009 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3010 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3011 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3012 show_ident(decl
->ident
));
3013 base_type
->ctype
.base_type
= &int_ctype
;
3015 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3016 warning(decl
->pos
, "'%s()' has implicit return type",
3017 show_ident(decl
->ident
));
3018 base_type
->ctype
.base_type
= &int_ctype
;
3020 /* apply attributes placed after the declarator */
3021 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3023 /* K&R argument declaration? */
3024 if (lookup_type(token
))
3025 return parse_k_r_arguments(token
, decl
, list
);
3026 if (match_op(token
, '{'))
3027 return parse_function_body(token
, decl
, list
);
3029 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3030 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3031 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3032 sparse_error(token
->pos
, "void declaration");
3034 if (base_type
== &incomplete_ctype
) {
3035 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3036 decl
->ctype
.base_type
= &int_ctype
;;
3040 if (!is_typedef
&& match_op(token
, '=')) {
3041 struct token
*next
= token
->next
;
3042 token
= initializer(&decl
->initializer
, next
);
3044 sparse_error(token
->pos
, "expression expected before '%s'", show_token(token
));
3048 validate_decl(decl
);
3050 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3051 warning(decl
->pos
, "symbol with external linkage has initializer");
3052 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3055 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3056 add_symbol(list
, decl
);
3057 fn_local_symbol(decl
);
3060 check_declaration(decl
);
3061 if (decl
->same_symbol
) {
3062 decl
->definition
= decl
->same_symbol
->definition
;
3063 decl
->op
= decl
->same_symbol
->op
;
3065 // TODO: handle -std=c89 --pedantic
3066 check_duplicates(decl
);
3071 const char *msg
= NULL
;
3072 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3073 msg
= "on non-identifier";
3074 else if (match_op(token
, ','))
3075 msg
= "on declaration list";
3076 else if (!decl
->initializer
)
3077 msg
= "without initializer";
3078 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3079 decl
->initializer
->symbol
== decl
)
3080 msg
= "on self-init var";
3082 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3083 decl
->ctype
.base_type
= &bad_ctype
;
3087 if (!match_op(token
, ','))
3090 token
= token
->next
;
3092 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3094 token
= handle_attributes(token
, &ctx
);
3095 token
= declarator(token
, &ctx
);
3096 token
= handle_asm_name(token
, &ctx
);
3097 token
= handle_attributes(token
, &ctx
);
3098 apply_modifiers(token
->pos
, &ctx
);
3099 decl
->ctype
= ctx
.ctype
;
3100 decl
->ctype
.modifiers
|= mod
;
3101 decl
->endpos
= token
->pos
;
3103 sparse_error(token
->pos
, "expected identifier name in type definition");
3108 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3110 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3112 /* Function declarations are automatically extern unless specifically static */
3113 base_type
= decl
->ctype
.base_type
;
3114 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3115 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3116 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3119 return expect(token
, ';', "at end of declaration");