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
);
77 static struct token
*ignore_seg_gs(struct token
*token
, struct symbol_list
**unused
);
79 typedef struct token
*attr_t(struct token
*, struct symbol
*,
83 attribute_packed
, attribute_aligned
, attribute_modifier
,
86 attribute_address_space
, attribute_context
,
88 attribute_designated_init
,
89 attribute_transparent_union
, ignore_attribute
,
90 attribute_mode
, attribute_force
;
92 typedef struct symbol
*to_mode_t(struct symbol
*);
95 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
;
96 static to_mode_t to_pointer_mode
, to_word_mode
;
111 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
115 CInt
= 0, CSInt
, CUInt
, CReal
,
118 static void asm_modifier(struct token
*token
, unsigned long *mods
, unsigned long mod
)
121 warning(token
->pos
, "duplicated asm modifier");
125 static struct symbol_op typedef_op
= {
127 .declarator
= storage_specifier
,
130 static struct symbol_op inline_op
= {
132 .declarator
= generic_qualifier
,
133 .asm_modifier
= asm_modifier
,
136 static struct symbol_op noreturn_op
= {
138 .declarator
= generic_qualifier
,
141 static declarator_t alignas_specifier
;
142 static struct symbol_op alignas_op
= {
144 .declarator
= alignas_specifier
,
147 static struct symbol_op auto_op
= {
149 .declarator
= storage_specifier
,
152 static struct symbol_op register_op
= {
154 .declarator
= storage_specifier
,
157 static struct symbol_op static_op
= {
158 .type
= KW_MODIFIER
|KW_STATIC
,
159 .declarator
= storage_specifier
,
162 static struct symbol_op extern_op
= {
164 .declarator
= storage_specifier
,
167 static struct symbol_op thread_op
= {
169 .declarator
= thread_specifier
,
172 static struct symbol_op const_op
= {
173 .type
= KW_QUALIFIER
,
174 .declarator
= generic_qualifier
,
177 static struct symbol_op volatile_op
= {
178 .type
= KW_QUALIFIER
,
179 .declarator
= generic_qualifier
,
180 .asm_modifier
= asm_modifier
,
183 static struct symbol_op restrict_op
= {
184 .type
= KW_QUALIFIER
,
185 .declarator
= generic_qualifier
,
188 static struct symbol_op atomic_op
= {
189 .type
= KW_QUALIFIER
,
190 .declarator
= generic_qualifier
,
193 static struct symbol_op typeof_op
= {
194 .type
= KW_SPECIFIER
,
195 .declarator
= typeof_specifier
,
200 static struct symbol_op autotype_op
= {
201 .type
= KW_SPECIFIER
,
202 .declarator
= autotype_specifier
,
207 static struct symbol_op attribute_op
= {
208 .type
= KW_ATTRIBUTE
,
209 .declarator
= attribute_specifier
,
212 static struct symbol_op struct_op
= {
213 .type
= KW_SPECIFIER
,
214 .declarator
= struct_specifier
,
219 static struct symbol_op union_op
= {
220 .type
= KW_SPECIFIER
,
221 .declarator
= union_specifier
,
226 static struct symbol_op enum_op
= {
227 .type
= KW_SPECIFIER
,
228 .declarator
= enum_specifier
,
233 static struct symbol_op spec_op
= {
234 .type
= KW_SPECIFIER
| KW_EXACT
,
239 static struct symbol_op char_op
= {
240 .type
= KW_SPECIFIER
,
241 .test
= Set_T
|Set_Long
|Set_Short
,
242 .set
= Set_T
|Set_Char
,
246 static struct symbol_op int_op
= {
247 .type
= KW_SPECIFIER
,
249 .set
= Set_T
|Set_Int
,
252 static struct symbol_op double_op
= {
253 .type
= KW_SPECIFIER
,
254 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
255 .set
= Set_T
|Set_Double
,
259 static struct symbol_op float_op
= {
260 .type
= KW_SPECIFIER
,
261 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
262 .set
= Set_T
|Set_Float
,
266 static struct symbol_op short_op
= {
267 .type
= KW_SPECIFIER
,
268 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
272 static struct symbol_op signed_op
= {
273 .type
= KW_SPECIFIER
,
274 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
279 static struct symbol_op unsigned_op
= {
280 .type
= KW_SPECIFIER
,
281 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
286 static struct symbol_op long_op
= {
287 .type
= KW_SPECIFIER
,
288 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
292 static struct symbol_op int128_op
= {
293 .type
= KW_SPECIFIER
,
294 .test
= Set_S
|Set_T
|Set_Char
|Set_Short
|Set_Int
|Set_Float
|Set_Double
|Set_Long
|Set_Vlong
|Set_Int128
,
295 .set
= Set_T
|Set_Int128
|Set_Vlong
,
299 static struct symbol_op if_op
= {
300 .statement
= parse_if_statement
,
303 static struct symbol_op return_op
= {
304 .statement
= parse_return_statement
,
307 static struct symbol_op loop_iter_op
= {
308 .statement
= parse_loop_iterator
,
311 static struct symbol_op default_op
= {
312 .statement
= parse_default_statement
,
315 static struct symbol_op case_op
= {
316 .statement
= parse_case_statement
,
319 static struct symbol_op switch_op
= {
320 .statement
= parse_switch_statement
,
323 static struct symbol_op for_op
= {
324 .statement
= parse_for_statement
,
327 static struct symbol_op while_op
= {
328 .statement
= parse_while_statement
,
331 static struct symbol_op do_op
= {
332 .statement
= parse_do_statement
,
335 static struct symbol_op goto_op
= {
336 .statement
= parse_goto_statement
,
339 static struct symbol_op __context___op
= {
340 .statement
= parse_context_statement
,
341 .attribute
= attribute_context
,
344 static struct symbol_op range_op
= {
345 .statement
= parse_range_statement
,
348 static struct symbol_op asm_op
= {
350 .statement
= parse_asm_statement
,
351 .toplevel
= toplevel_asm_declaration
,
354 static struct symbol_op static_assert_op
= {
355 .toplevel
= parse_static_assert
,
358 static struct symbol_op seg_gs
= {
359 .toplevel
= ignore_seg_gs
,
362 static struct symbol_op packed_op
= {
363 .attribute
= attribute_packed
,
366 static struct symbol_op aligned_op
= {
367 .attribute
= attribute_aligned
,
370 static struct symbol_op cleanup_op
= {
371 .attribute
= attribute_cleanup
,
374 static struct symbol_op attr_mod_op
= {
375 .attribute
= attribute_modifier
,
378 static struct symbol_op attr_fun_op
= {
379 .attribute
= attribute_function
,
382 static struct symbol_op attr_bitwise_op
= {
383 .attribute
= attribute_bitwise
,
386 static struct symbol_op attr_force_op
= {
387 .attribute
= attribute_force
,
390 static struct symbol_op address_space_op
= {
391 .attribute
= attribute_address_space
,
394 static struct symbol_op mode_op
= {
395 .attribute
= attribute_mode
,
398 static struct symbol_op context_op
= {
399 .attribute
= attribute_context
,
402 static struct symbol_op designated_init_op
= {
403 .attribute
= attribute_designated_init
,
406 static struct symbol_op transparent_union_op
= {
407 .attribute
= attribute_transparent_union
,
410 static struct symbol_op ignore_attr_op
= {
411 .attribute
= ignore_attribute
,
414 static struct symbol_op mode_QI_op
= {
416 .to_mode
= to_QI_mode
419 static struct symbol_op mode_HI_op
= {
421 .to_mode
= to_HI_mode
424 static struct symbol_op mode_SI_op
= {
426 .to_mode
= to_SI_mode
429 static struct symbol_op mode_DI_op
= {
431 .to_mode
= to_DI_mode
434 static struct symbol_op mode_TI_op
= {
436 .to_mode
= to_TI_mode
439 static struct symbol_op mode_pointer_op
= {
441 .to_mode
= to_pointer_mode
444 static struct symbol_op mode_word_op
= {
446 .to_mode
= to_word_mode
450 * Define the keyword and their effects.
451 * The entries in the 'typedef' and put in NS_TYPEDEF and
452 * are automatically set as reserved keyword while the ones
453 * in the 'keyword' table are just put in NS_KEYWORD.
455 * The entries are added via the 3 macros:
456 * N() for entries with "name" only,
457 * D() for entries with "name" & "__name__",
458 * A() for entries with "name", "__name" & "__name__",
459 * U() for entries with "__name" & "__name__".
461 static struct init_keyword
{
463 struct symbol_op
*op
;
467 #define N(I, O,...) { I, O,##__VA_ARGS__ }
468 #define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
469 N("__" I "__",O,##__VA_ARGS__)
470 #define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
471 N("__" I,O,##__VA_ARGS__), \
472 N("__" I "__",O,##__VA_ARGS__)
473 #define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
474 N("__" I "__",O,##__VA_ARGS__)
475 /* Storage classes */
476 N("auto", &auto_op
, .mods
= MOD_AUTO
),
477 N("register", ®ister_op
, .mods
= MOD_REGISTER
),
478 N("static", &static_op
, .mods
= MOD_STATIC
),
479 N("extern", &extern_op
, .mods
= MOD_EXTERN
),
480 N("__thread", &thread_op
),
481 N("_Thread_local", &thread_op
),
483 A("inline", &inline_op
, .mods
= MOD_INLINE
),
486 N("typedef", &typedef_op
, .mods
= MOD_USERTYPE
),
487 A("typeof", &typeof_op
),
488 N("__auto_type", &autotype_op
),
490 /* Type qualifiers */
491 A("const", &const_op
, .mods
= MOD_CONST
),
492 A("volatile", &volatile_op
, .mods
= MOD_VOLATILE
),
493 A("restrict", &restrict_op
, .mods
= MOD_RESTRICT
),
495 N("_Atomic", &atomic_op
, .mods
= MOD_ATOMIC
),
496 N("_Noreturn", &noreturn_op
, .mods
= MOD_NORETURN
),
497 N("_Alignas", &alignas_op
),
499 U("attribute", &attribute_op
),
501 /* Type specifiers */
502 N("struct", &struct_op
),
503 N("union", &union_op
),
506 N("void", &spec_op
, .type
= &void_ctype
),
508 N("short", &short_op
),
511 N("float", &float_op
),
512 N("double", &double_op
),
513 A("signed", &signed_op
),
514 N("unsigned", &unsigned_op
),
515 N("__int128", &int128_op
),
516 N("_Bool", &spec_op
, .type
= &bool_ctype
),
518 /* Predeclared types */
519 N("__builtin_va_list", &spec_op
, .type
= &ptr_ctype
),
520 N("__builtin_ms_va_list",&spec_op
, .type
= &ptr_ctype
),
521 N("__int128_t", &spec_op
, .type
= &sint128_ctype
),
522 N("__uint128_t", &spec_op
, .type
= &uint128_ctype
),
523 N("_Float32", &spec_op
, .type
= &float32_ctype
),
524 N("_Float32x", &spec_op
, .type
= &float32x_ctype
),
525 N("_Float64", &spec_op
, .type
= &float64_ctype
),
526 N("_Float64x", &spec_op
, .type
= &float64x_ctype
),
527 N("_Float128", &spec_op
, .type
= &float128_ctype
),
528 N("__float128", &spec_op
, .type
= &float128_ctype
),
530 N("__seg_gs", &seg_gs
),
535 N("return", &return_op
),
536 N("break", &loop_iter_op
),
537 N("continue", &loop_iter_op
),
538 N("default", &default_op
),
540 N("switch", &switch_op
),
542 N("while", &while_op
),
546 N("context", &context_op
),
547 N("__context__", &__context___op
),
548 N("__range__", &range_op
),
549 N("_Static_assert", &static_assert_op
),
552 D("packed", &packed_op
),
553 D("aligned", &aligned_op
),
554 D("cleanup", &cleanup_op
),
555 D("nocast", &attr_mod_op
, .mods
= MOD_NOCAST
),
556 D("noderef", &attr_mod_op
, .mods
= MOD_NODEREF
),
557 D("safe", &attr_mod_op
, .mods
= MOD_SAFE
),
558 D("unused", &attr_mod_op
, .mods
= MOD_UNUSED
),
559 D("externally_visible", &attr_mod_op
, .mods
= MOD_EXT_VISIBLE
),
560 D("force", &attr_force_op
),
561 D("bitwise", &attr_bitwise_op
, .mods
= MOD_BITWISE
),
562 D("address_space", &address_space_op
),
563 D("designated_init", &designated_init_op
),
564 D("transparent_union", &transparent_union_op
),
565 D("noreturn", &attr_fun_op
, .mods
= MOD_NORETURN
),
566 D("pure", &attr_fun_op
, .mods
= MOD_PURE
),
567 A("const", &attr_fun_op
, .mods
= MOD_PURE
),
568 D("gnu_inline", &attr_fun_op
, .mods
= MOD_GNU_INLINE
),
572 D("QI", &mode_QI_op
),
573 D("HI", &mode_HI_op
),
574 D("SI", &mode_SI_op
),
575 D("DI", &mode_DI_op
),
576 D("TI", &mode_TI_op
),
577 D("byte", &mode_QI_op
),
578 D("pointer", &mode_pointer_op
),
579 D("word", &mode_word_op
),
583 static const char *ignored_attributes
[] = {
585 #define GCC_ATTR(x) \
587 STRINGIFY(__##x##__),
589 #include "gcc-attr-list.h"
601 static void init_keyword(int stream
, struct init_keyword
*kw
, enum namespace ns
)
603 struct symbol
*sym
= create_symbol(stream
, kw
->name
, SYM_KEYWORD
, ns
);
604 sym
->ident
->keyword
= 1;
605 sym
->ident
->reserved
|= (ns
== NS_TYPEDEF
);
606 sym
->ctype
.modifiers
= kw
->mods
;
607 sym
->ctype
.base_type
= kw
->type
;
611 void init_parser(int stream
)
615 for (i
= 0; i
< ARRAY_SIZE(typedefs
); i
++)
616 init_keyword(stream
, &typedefs
[i
], NS_TYPEDEF
);
617 for (i
= 0; i
< ARRAY_SIZE(keywords
); i
++)
618 init_keyword(stream
, &keywords
[i
], NS_KEYWORD
);
620 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
621 const char * name
= ignored_attributes
[i
];
622 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
625 sym
->ident
->keyword
= 1;
626 sym
->op
= &ignore_attr_op
;
632 static struct token
*skip_to(struct token
*token
, int op
)
634 while (!match_op(token
, op
) && !eof_token(token
))
639 static struct token bad_token
= { .pos
.type
= TOKEN_BAD
};
640 struct token
*expect(struct token
*token
, int op
, const char *where
)
642 if (!match_op(token
, op
)) {
643 if (token
!= &bad_token
) {
644 bad_token
.next
= token
;
645 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
646 sparse_error(token
->pos
, "got %s", show_token(token
));
649 return skip_to(token
, op
);
656 // issue an error message on new parsing errors
657 // @token: the current token
658 // @errmsg: the error message
659 // If the current token is from a previous error, an error message
660 // has already been issued, so nothing more is done.
661 // Otherwise, @errmsg is displayed followed by the current token.
662 static void unexpected(struct token
*token
, const char *errmsg
)
664 if (token
== &bad_token
)
666 sparse_error(token
->pos
, "%s", errmsg
);
667 sparse_error(token
->pos
, "got %s", show_token(token
));
670 // Add a symbol to the list of function-local symbols
671 static void fn_local_symbol(struct symbol
*sym
)
673 if (function_symbol_list
)
674 add_symbol(function_symbol_list
, sym
);
677 struct statement
*alloc_statement(struct position pos
, int type
)
679 struct statement
*stmt
= __alloc_statement(0);
685 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
687 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
);
689 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
691 struct symbol
*ctype
;
694 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
696 sparse_error(pos
, "don't know how to apply mode to %s",
697 show_typename(ctx
->ctype
.base_type
));
699 ctx
->ctype
.base_type
= ctype
;
703 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
705 struct symbol
*sym
= alloc_symbol(pos
, type
);
707 sym
->ctype
.base_type
= ctype
->base_type
;
708 sym
->ctype
.modifiers
= ctype
->modifiers
;
710 ctype
->base_type
= sym
;
711 ctype
->modifiers
= 0;
716 * NOTE! NS_LABEL is not just a different namespace,
717 * it also ends up using function scope instead of the
718 * regular symbol scope.
720 struct symbol
*label_symbol(struct token
*token
, int used
)
722 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
724 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
725 bind_symbol(sym
, token
->ident
, NS_LABEL
);
728 fn_local_symbol(sym
);
733 static struct token
*struct_union_enum_specifier(enum type type
,
734 struct token
*token
, struct decl_state
*ctx
,
735 struct token
*(*parse
)(struct token
*, struct symbol
*))
737 struct decl_state attr
= { };
739 struct position
*repos
;
741 token
= handle_attributes(token
, &attr
);
742 if (token_type(token
) == TOKEN_IDENT
) {
743 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
745 (is_outer_scope(sym
->scope
) &&
746 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
747 // Either a new symbol, or else an out-of-scope
748 // symbol being redefined.
749 sym
= alloc_symbol(token
->pos
, type
);
750 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
752 if (sym
->type
!= type
)
753 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
754 ctx
->ctype
.base_type
= sym
;
757 if (!match_op(token
, '{'))
760 // The following test is actually wrong for empty
761 // structs, but (1) they are not C99, (2) gcc does
762 // the same thing, and (3) it's easier.
763 if (sym
->symbol_list
)
764 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
767 // Mark the structure as needing re-examination
769 } else if (match_op(token
, '{')) {
770 // private struct/union/enum type
771 sym
= alloc_symbol(token
->pos
, type
);
772 set_current_scope(sym
); // used by dissect
773 ctx
->ctype
.base_type
= sym
;
775 sparse_error(token
->pos
, "expected declaration");
776 ctx
->ctype
.base_type
= &bad_ctype
;
780 token
= parse(token
->next
, sym
);
781 token
= expect(token
, '}', "at end of specifier");
782 attr
.ctype
.base_type
= sym
;
783 token
= handle_attributes(token
, &attr
);
784 apply_ctype(token
->pos
, &sym
->ctype
, &attr
.ctype
);
785 sym
->packed
= attr
.packed
;
787 sym
->endpos
= token
->pos
;
792 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
794 struct symbol
*field
, *last
= NULL
;
796 res
= struct_declaration_list(token
, &sym
->symbol_list
);
797 FOR_EACH_PTR(sym
->symbol_list
, field
) {
799 struct symbol
*base
= field
->ctype
.base_type
;
800 if (base
&& base
->type
== SYM_BITFIELD
)
804 last
->next_subobject
= field
;
806 } END_FOR_EACH_PTR(field
);
810 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
812 return struct_declaration_list(token
, &sym
->symbol_list
);
815 static struct token
*struct_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
817 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
820 static struct token
*union_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
822 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
828 // This allow to use a shift amount as big (or bigger)
829 // than the width of the value to be shifted, in which case
830 // the result is, of course, 0.
831 static unsigned long long rshift(unsigned long long val
, unsigned int n
)
833 if (n
>= (sizeof(val
) * 8))
840 unsigned long long pos
;
843 static void update_range(struct range
*range
, unsigned long long uval
, struct symbol
*vtype
)
845 long long sval
= uval
;
847 if (is_signed_type(vtype
) && (sval
< 0)) {
848 if (sval
< range
->neg
)
851 if (uval
> range
->pos
)
856 static int type_is_ok(struct symbol
*type
, struct range range
)
858 int shift
= type
->bit_size
;
859 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
863 if (rshift(range
.pos
, shift
))
869 if (rshift(~range
.neg
, shift
))
874 static struct range
type_range(struct symbol
*type
)
877 unsigned int size
= type
->bit_size
;
878 unsigned long long max
;
881 if (is_signed_type(type
)) {
882 min
= sign_bit(size
);
886 max
= bits_mask(size
);
894 static int val_in_range(struct range
*range
, long long sval
, struct symbol
*vtype
)
896 unsigned long long uval
= sval
;
898 if (is_signed_type(vtype
) && (sval
< 0))
899 return range
->neg
<= sval
;
901 return uval
<= range
->pos
;
904 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
906 struct range irange
= type_range(&int_ctype
);
909 FOR_EACH_PTR(list
, sym
) {
910 struct expression
*expr
= sym
->initializer
;
911 struct symbol
*ctype
;
913 if (expr
->type
!= EXPR_VALUE
)
916 val
= get_expression_value(expr
);
917 if (is_int_type(ctype
) && val_in_range(&irange
, val
, ctype
)) {
918 expr
->ctype
= &int_ctype
;
921 cast_value(expr
, base_type
, expr
);
922 } END_FOR_EACH_PTR(sym
);
925 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
927 unsigned long long lastval
= 0;
928 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
929 struct range range
= { };
932 parent
->examined
= 1;
933 parent
->ctype
.base_type
= &int_ctype
;
934 while (token_type(token
) == TOKEN_IDENT
) {
935 struct expression
*expr
= NULL
;
936 struct token
*next
= token
->next
;
937 struct decl_state ctx
= { };
940 // FIXME: only 'deprecated' should be accepted
941 next
= handle_attributes(next
, &ctx
);
943 if (match_op(next
, '=')) {
944 next
= constant_expression(next
->next
, &expr
);
945 lastval
= get_expression_value(expr
);
947 if (expr
&& expr
->ctype
)
951 } else if (is_int_type(ctype
)) {
954 error_die(token
->pos
, "can't increment the last enum member");
958 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
959 expr
->value
= lastval
;
963 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
964 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
965 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
966 sym
->initializer
= expr
;
967 sym
->enum_member
= 1;
968 sym
->ctype
.base_type
= parent
;
969 add_ptr_list(&parent
->symbol_list
, sym
);
971 if (base_type
!= &bad_ctype
) {
972 if (ctype
->type
== SYM_NODE
)
973 ctype
= ctype
->ctype
.base_type
;
974 if (ctype
->type
== SYM_ENUM
) {
978 ctype
= ctype
->ctype
.base_type
;
982 * - if all enums are of the same type, then
983 * the base_type is that type (two first
985 * - if enums are of different types, they
986 * all have to be integer types, and the
987 * base type is at least "int_ctype".
988 * - otherwise the base_type is "bad_ctype".
990 if (!base_type
|| ctype
== &bad_ctype
) {
992 } else if (ctype
== base_type
) {
994 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
995 base_type
= &int_ctype
;
996 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
997 if (!mix_bitwise
++) {
998 warning(expr
->pos
, "mixed bitwiseness");
1000 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
1001 sparse_error(expr
->pos
, "incompatible restricted type");
1002 info(expr
->pos
, " expected: %s", show_typename(base_type
));
1003 info(expr
->pos
, " got: %s", show_typename(ctype
));
1004 base_type
= &bad_ctype
;
1005 } else if (base_type
!= &bad_ctype
) {
1006 sparse_error(token
->pos
, "bad enum definition");
1007 base_type
= &bad_ctype
;
1009 parent
->ctype
.base_type
= base_type
;
1011 if (is_int_type(base_type
)) {
1012 update_range(&range
, lastval
, ctype
);
1016 sym
->endpos
= token
->pos
;
1018 if (!match_op(token
, ','))
1020 token
= token
->next
;
1023 sparse_error(token
->pos
, "empty enum definition");
1024 base_type
= &bad_ctype
;
1026 else if (!is_int_type(base_type
))
1028 else if (type_is_ok(&uint_ctype
, range
))
1029 base_type
= &uint_ctype
;
1030 else if (type_is_ok(&int_ctype
, range
))
1031 base_type
= &int_ctype
;
1032 else if (type_is_ok(&ulong_ctype
, range
))
1033 base_type
= &ulong_ctype
;
1034 else if (type_is_ok(&long_ctype
, range
))
1035 base_type
= &long_ctype
;
1036 else if (type_is_ok(&ullong_ctype
, range
))
1037 base_type
= &ullong_ctype
;
1038 else if (type_is_ok(&llong_ctype
, range
))
1039 base_type
= &llong_ctype
;
1041 base_type
= &bad_ctype
;
1042 parent
->ctype
.base_type
= base_type
;
1043 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1044 parent
->examined
= 0;
1048 cast_enum_list(parent
->symbol_list
, base_type
);
1053 static struct token
*enum_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1055 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1056 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1058 if (!ctype
->base_type
)
1059 ctype
->base_type
= &incomplete_ctype
;
1064 static struct token
*typeof_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1067 if (!match_op(token
, '(')) {
1068 sparse_error(token
->pos
, "expected '(' after typeof");
1071 if (lookup_type(token
->next
)) {
1073 token
= typename(token
->next
, &sym
, NULL
);
1074 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1075 apply_ctype(token
->pos
, &ctx
->ctype
, &sym
->ctype
);
1077 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1078 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1080 typeof_sym
->endpos
= token
->pos
;
1081 if (!typeof_sym
->initializer
) {
1082 sparse_error(token
->pos
, "expected expression after the '(' token");
1083 typeof_sym
= &bad_ctype
;
1085 ctx
->ctype
.base_type
= typeof_sym
;
1087 return expect(token
, ')', "after typeof");
1090 static struct token
*autotype_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1092 ctx
->ctype
.base_type
= &autotype_ctype
;
1097 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1099 struct expression
*expr
= NULL
;
1100 if (match_op(token
, '('))
1101 token
= parens_expression(token
, &expr
, "in attribute");
1105 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1107 if (!ctx
->ctype
.alignment
) {
1108 ctx
->ctype
.alignment
= 1;
1114 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1116 int alignment
= max_alignment
;
1117 struct expression
*expr
= NULL
;
1119 if (match_op(token
, '(')) {
1120 token
= parens_expression(token
, &expr
, "in attribute");
1122 alignment
= const_expression_value(expr
);
1124 if (alignment
& (alignment
-1)) {
1125 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1127 } else if (alignment
> ctx
->ctype
.alignment
)
1128 ctx
->ctype
.alignment
= alignment
;
1132 static struct token
*attribute_cleanup(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1134 struct expression
*expr
= NULL
;
1136 if (match_op(token
, '(')) {
1137 token
= token
->next
;
1138 if (match_op(token
, ')'))
1139 sparse_error(token
->pos
, "an argument is expected for attribute 'cleanup'");
1140 else if (token_type(token
) != TOKEN_IDENT
)
1141 sparse_error(token
->pos
, "argument is not an identifier");
1142 token
= primary_expression(token
, &expr
);
1143 if (expr
&& expr
->type
== EXPR_SYMBOL
)
1144 ctx
->cleanup
= expr
;
1145 return expect(token
, ')', "after attribute's argument'");
1148 sparse_error(token
->pos
, "an argument is expected for attribute 'cleanup'");
1152 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1154 if (*mods
& mod
& ~MOD_DUP_OK
)
1155 warning(*pos
, "duplicate %s", modifier_name(mod
));
1159 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1161 apply_mod(pos
, &ctx
->modifiers
, qual
);
1164 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1166 apply_mod(&token
->pos
, &ctx
->ctype
.modifiers
, attr
->ctype
.modifiers
);
1170 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1172 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1176 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1179 attribute_modifier(token
, attr
, ctx
);
1183 static struct ident
*numerical_address_space(int asn
)
1189 sprintf(buff
, "<asn:%d>", asn
);
1190 return built_in_ident(buff
);
1193 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1195 struct expression
*expr
= NULL
;
1196 struct ident
*as
= NULL
;
1199 token
= expect(token
, '(', "after address_space attribute");
1200 switch (token_type(token
)) {
1202 next
= primary_expression(token
, &expr
);
1203 if (expr
->type
!= EXPR_VALUE
)
1205 as
= numerical_address_space(expr
->value
);
1215 warning(token
->pos
, "invalid address space name");
1218 if (Waddress_space
&& as
) {
1220 sparse_error(token
->pos
,
1221 "multiple address spaces given: %s & %s",
1222 show_as(ctx
->ctype
.as
), show_as(as
));
1225 token
= expect(next
, ')', "after address_space attribute");
1229 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1231 if (ctype
->ctype
.base_type
!= &int_type
)
1233 if (ctype
== &char_ctype
)
1235 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1239 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1241 if (ctype
->ctype
.base_type
!= &int_type
)
1243 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1247 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1249 if (ctype
->ctype
.base_type
!= &int_type
)
1251 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1255 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1257 if (ctype
->ctype
.base_type
!= &int_type
)
1259 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1263 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1265 if (ctype
->ctype
.base_type
!= &int_type
)
1267 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1271 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1273 if (ctype
->ctype
.base_type
!= &int_type
)
1275 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1279 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1281 if (ctype
->ctype
.base_type
!= &int_type
)
1283 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1287 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1289 token
= expect(token
, '(', "after mode attribute");
1290 if (token_type(token
) == TOKEN_IDENT
) {
1291 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1292 if (mode
&& mode
->op
->type
& KW_MODE
)
1293 ctx
->mode
= mode
->op
;
1295 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1296 token
= token
->next
;
1298 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1299 token
= expect(token
, ')', "after mode attribute");
1303 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1305 struct context
*context
= alloc_context();
1306 struct expression
*args
[3];
1309 token
= expect(token
, '(', "after context attribute");
1310 token
= conditional_expression(token
, &args
[0]);
1311 token
= expect(token
, ',', "after context 1st argument");
1312 token
= conditional_expression(token
, &args
[1]);
1313 if (match_op(token
, ',')) {
1314 token
= token
->next
;
1315 token
= conditional_expression(token
, &args
[2]);
1316 token
= expect(token
, ')', "after context 3rd argument");
1317 context
->context
= args
[0];
1320 token
= expect(token
, ')', "after context 2nd argument");
1322 context
->in
= get_expression_value(args
[idx
++]);
1323 context
->out
= get_expression_value(args
[idx
++]);
1324 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1328 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1330 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1331 ctx
->ctype
.base_type
->designated_init
= 1;
1333 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1337 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1339 if (Wtransparent_union
)
1340 warning(token
->pos
, "attribute __transparent_union__");
1342 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1343 ctx
->ctype
.base_type
->transparent_union
= 1;
1345 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1349 static struct token
*recover_unknown_attribute(struct token
*token
)
1351 struct expression
*expr
= NULL
;
1353 if (Wunknown_attribute
)
1354 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1355 token
= token
->next
;
1356 if (match_op(token
, '('))
1357 token
= parens_expression(token
, &expr
, "in attribute");
1361 static struct token
*attribute_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1363 token
= expect(token
, '(', "after attribute");
1364 token
= expect(token
, '(', "after attribute");
1366 while (token_type(token
) == TOKEN_IDENT
) {
1367 struct symbol
*attr
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1368 if (attr
&& attr
->op
->attribute
)
1369 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1371 token
= recover_unknown_attribute(token
);
1373 if (!match_op(token
, ','))
1375 token
= token
->next
;
1378 token
= expect(token
, ')', "after attribute");
1379 token
= expect(token
, ')', "after attribute");
1383 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1385 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1386 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1387 return ctx
->storage_class
| mods
;
1390 static struct token
*storage_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1392 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1393 unsigned long class = sym
->ctype
.modifiers
;
1394 const char *storage
= modifier_name(class);
1396 /* __thread can be used alone, or with extern or static */
1397 if (is_tls
&& (class & ~(MOD_STATIC
|MOD_EXTERN
)))
1398 sparse_error(next
->pos
, "__thread cannot be used with '%s'", storage
);
1399 else if (!ctx
->storage_class
)
1400 ctx
->storage_class
= class;
1401 else if (ctx
->storage_class
== class)
1402 sparse_error(next
->pos
, "duplicate %s", storage
);
1404 sparse_error(next
->pos
, "multiple storage classes");
1408 static struct token
*thread_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1410 /* This GCC extension can be used alone, or with extern or static */
1411 if (!(ctx
->storage_class
& ~(MOD_STATIC
|MOD_EXTERN
))) {
1412 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1414 sparse_error(next
->pos
, "__thread cannot be used with '%s'",
1415 modifier_name(ctx
->storage_class
));
1421 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1427 static struct token
*alignas_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1431 if (!match_op(token
, '(')) {
1432 sparse_error(token
->pos
, "expected '(' after _Alignas");
1435 if (lookup_type(token
->next
)) {
1436 struct symbol
*sym
= NULL
;
1437 token
= typename(token
->next
, &sym
, NULL
);
1438 sym
= examine_symbol_type(sym
);
1439 alignment
= sym
->ctype
.alignment
;
1440 token
= expect(token
, ')', "after _Alignas(...");
1442 struct expression
*expr
= NULL
;
1443 token
= parens_expression(token
, &expr
, "after _Alignas");
1446 alignment
= const_expression_value(expr
);
1449 if (alignment
< 0) {
1450 warning(token
->pos
, "non-positive alignment");
1453 if (alignment
& (alignment
-1)) {
1454 warning(token
->pos
, "non-power-of-2 alignment");
1457 if (alignment
> ctx
->ctype
.alignment
)
1458 ctx
->ctype
.alignment
= alignment
;
1462 static struct token
*generic_qualifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1464 apply_qualifier(&next
->pos
, &ctx
->ctype
, sym
->ctype
.modifiers
);
1468 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
)
1470 unsigned long mod
= src
->modifiers
;
1473 apply_qualifier(&pos
, dst
, mod
);
1476 concat_ptr_list((struct ptr_list
*)src
->contexts
,
1477 (struct ptr_list
**)&dst
->contexts
);
1480 if (src
->alignment
> dst
->alignment
)
1481 dst
->alignment
= src
->alignment
;
1488 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1491 if (what
& (Set_S
| Set_T
))
1493 if (what
& Set_Char
)
1495 else if (what
& Set_Double
)
1497 else if (what
& Set_Float
)
1499 else if (what
& Set_Signed
)
1501 else if (what
& Set_Unsigned
)
1503 else if (what
& Set_Short
)
1505 else if (what
& Set_Long
)
1509 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1510 old
, show_ident(new));
1514 sparse_error(pos
, "two or more data types in declaration specifiers");
1517 static struct symbol
* const int_types
[] =
1518 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1519 static struct symbol
* const signed_types
[] =
1520 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1522 static struct symbol
* const unsigned_types
[] =
1523 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1525 static struct symbol
* const real_types
[] =
1526 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1527 static struct symbol
* const * const types
[] = {
1528 [CInt
] = int_types
+ 2,
1529 [CSInt
] = signed_types
+ 2,
1530 [CUInt
] = unsigned_types
+ 2,
1531 [CReal
] = real_types
+ 1,
1534 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1536 return types
[want_unsigned
? CUInt
: CInt
][size
];
1539 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1541 while (token_type(t
) == TOKEN_IDENT
) {
1542 struct symbol
*s
= lookup_keyword(t
->ident
, NS_TYPEDEF
);
1545 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1548 if (s
->op
->declarator
)
1549 t
= s
->op
->declarator(t
, s
, ctx
);
1554 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1560 while (token_type(token
) == TOKEN_IDENT
) {
1561 struct symbol
*s
= lookup_symbol(token
->ident
,
1562 NS_TYPEDEF
| NS_SYMBOL
);
1563 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1565 if (s
->type
!= SYM_KEYWORD
) {
1568 seen
|= Set_S
| Set_T
;
1569 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1570 apply_ctype(token
->pos
, &ctx
->ctype
, &s
->ctype
);
1571 token
= token
->next
;
1574 if (s
->op
->type
& KW_SPECIFIER
) {
1575 if (seen
& s
->op
->test
) {
1576 specifier_conflict(token
->pos
,
1582 class += s
->op
->class;
1583 if (s
->op
->set
& Set_Int128
)
1585 else if (s
->op
->set
& Set_Char
)
1587 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1589 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1590 if (class == CReal
) {
1591 specifier_conflict(token
->pos
,
1599 token
= token
->next
;
1600 if (s
->op
->declarator
) // Note: this eats attributes
1601 token
= s
->op
->declarator(token
, s
, ctx
);
1602 if (s
->op
->type
& KW_EXACT
) {
1603 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1604 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1608 if (!(seen
& Set_S
)) { /* not set explicitly? */
1609 struct symbol
*base
= &incomplete_ctype
;
1611 base
= types
[class][rank
];
1612 ctx
->ctype
.base_type
= base
;
1615 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1616 struct symbol
*type
;
1617 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1618 if (!is_int_type(ctx
->ctype
.base_type
)) {
1619 sparse_error(token
->pos
, "invalid modifier");
1622 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1623 *type
= *ctx
->ctype
.base_type
;
1624 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1625 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1626 type
->type
= SYM_RESTRICT
;
1627 ctx
->ctype
.base_type
= type
;
1628 create_fouled(type
);
1633 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1635 struct expression
*expr
= NULL
;
1638 while (token_type(token
) == TOKEN_IDENT
) {
1639 struct symbol
*sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1640 if (!sym
|| !(sym
->op
->type
& (KW_STATIC
|KW_QUALIFIER
)))
1642 if (has_static
&& (sym
->op
->type
& KW_STATIC
))
1643 sparse_error(token
->pos
, "duplicate array static declarator");
1644 has_static
|= (sym
->op
->type
& KW_STATIC
);
1645 token
= token
->next
;
1647 if (match_op(token
, '*') && match_op(token
->next
, ']')) {
1648 // FIXME: '[*]' is treated like '[]'
1649 token
= token
->next
;
1651 token
= assignment_expression(token
, &expr
);
1653 sym
->array_size
= expr
;
1657 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1658 static struct token
*identifier_list(struct token
*, struct symbol
*);
1659 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1661 static struct token
*handle_asm_name(struct token
*token
, struct decl_state
*ctx
)
1663 struct expression
*expr
;
1664 struct symbol
*keyword
;
1666 if (token_type(token
) != TOKEN_IDENT
)
1668 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1671 if (!(keyword
->op
->type
& KW_ASM
))
1674 token
= token
->next
;
1675 token
= expect(token
, '(', "after asm");
1676 token
= string_expression(token
, &expr
, "asm name");
1677 token
= expect(token
, ')', "after asm");
1682 // test if @token is '__attribute__' (or one of its variant)
1683 static bool match_attribute(struct token
*token
)
1687 if (token_type(token
) != TOKEN_IDENT
)
1689 sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1690 if (!sym
|| !sym
->op
)
1692 return sym
->op
->type
& KW_ATTRIBUTE
;
1695 static struct token
*skip_attribute(struct token
*token
)
1697 token
= token
->next
;
1698 if (match_op(token
, '(')) {
1700 token
= token
->next
;
1701 while (depth
&& !eof_token(token
)) {
1702 if (token_type(token
) == TOKEN_SPECIAL
) {
1703 if (token
->special
== '(')
1705 else if (token
->special
== ')')
1708 token
= token
->next
;
1714 static struct token
*skip_attributes(struct token
*token
)
1716 while (match_attribute(token
)) {
1717 token
= expect(token
->next
, '(', "after attribute");
1718 token
= expect(token
, '(', "after attribute");
1719 while (token_type(token
) == TOKEN_IDENT
) {
1720 token
= skip_attribute(token
);
1721 if (!match_op(token
, ','))
1723 token
= token
->next
;
1725 token
= expect(token
, ')', "after attribute");
1726 token
= expect(token
, ')', "after attribute");
1731 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
)
1733 while (match_attribute(token
))
1734 token
= attribute_specifier(token
->next
, NULL
, ctx
);
1738 static int is_nested(struct token
*token
, struct token
**p
,
1739 int prefer_abstract
)
1742 * This can be either a parameter list or a grouping.
1743 * For the direct (non-abstract) case, we know if must be
1744 * a parameter list if we already saw the identifier.
1745 * For the abstract case, we know if must be a parameter
1746 * list if it is empty or starts with a type.
1748 struct token
*next
= token
->next
;
1750 *p
= next
= skip_attributes(next
);
1752 if (token_type(next
) == TOKEN_IDENT
) {
1753 if (lookup_type(next
))
1754 return !prefer_abstract
;
1758 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1765 Empty
, K_R
, Proto
, Bad_Func
,
1768 static enum kind
which_func(struct token
*token
,
1770 int prefer_abstract
)
1772 struct token
*next
= token
->next
;
1774 if (token_type(next
) == TOKEN_IDENT
) {
1775 if (lookup_type(next
))
1777 /* identifier list not in definition; complain */
1778 if (prefer_abstract
)
1780 "identifier list not in definition");
1784 if (token_type(next
) != TOKEN_SPECIAL
)
1787 if (next
->special
== ')') {
1788 /* don't complain about those */
1789 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1791 if (Wstrict_prototypes
)
1793 "non-ANSI function declaration of function '%s'",
1798 if (next
->special
== SPECIAL_ELLIPSIS
) {
1800 "variadic functions must have one named argument");
1807 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1809 struct ctype
*ctype
= &ctx
->ctype
;
1811 struct ident
**p
= ctx
->ident
;
1813 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1814 *ctx
->ident
= token
->ident
;
1815 token
= token
->next
;
1816 } else if (match_op(token
, '(') &&
1817 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1818 struct symbol
*base_type
= ctype
->base_type
;
1819 if (token
->next
!= next
)
1820 next
= handle_attributes(token
->next
, ctx
);
1821 token
= declarator(next
, ctx
);
1822 token
= expect(token
, ')', "in nested declarator");
1823 while (ctype
->base_type
!= base_type
)
1824 ctype
= &ctype
->base_type
->ctype
;
1828 if (match_op(token
, '(')) {
1829 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1831 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1832 ctype
->modifiers
|= ctx
->f_modifiers
;
1833 token
= token
->next
;
1835 token
= identifier_list(token
, fn
);
1836 else if (kind
== Proto
)
1837 token
= parameter_type_list(token
, fn
);
1838 token
= expect(token
, ')', "in function declarator");
1839 fn
->endpos
= token
->pos
;
1843 while (match_op(token
, '[')) {
1844 struct symbol
*array
;
1845 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1846 token
= abstract_array_declarator(token
->next
, array
);
1847 token
= expect(token
, ']', "in abstract_array_declarator");
1848 array
->endpos
= token
->pos
;
1849 ctype
= &array
->ctype
;
1854 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1856 while (match_op(token
,'*')) {
1857 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1858 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1859 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1860 ptr
->ctype
.as
= ctx
->ctype
.as
;
1861 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1862 ctx
->ctype
.modifiers
= 0;
1863 ctx
->ctype
.base_type
= ptr
;
1864 ctx
->ctype
.as
= NULL
;
1865 ctx
->ctype
.contexts
= NULL
;
1866 ctx
->ctype
.alignment
= 0;
1868 token
= handle_qualifiers(token
->next
, ctx
);
1869 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1874 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1876 token
= pointer(token
, ctx
);
1877 return direct_declarator(token
, ctx
);
1880 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1882 struct ctype
*ctype
= &ctx
->ctype
;
1883 struct expression
*expr
;
1884 struct symbol
*bitfield
;
1887 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1888 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1889 show_typename(ctype
->base_type
));
1890 // Parse this to recover gracefully.
1891 return conditional_expression(token
->next
, &expr
);
1894 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1895 token
= conditional_expression(token
->next
, &expr
);
1896 width
= const_expression_value(expr
);
1897 bitfield
->bit_size
= width
;
1899 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
1900 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
1901 show_ident(*ctx
->ident
), width
);
1903 } else if (*ctx
->ident
) {
1904 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1905 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1906 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1907 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1908 // Valid values are either {-1;0} or {0}, depending on integer
1909 // representation. The latter makes for very efficient code...
1910 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1912 if (Wdefault_bitfield_sign
&&
1913 bitfield_type
->type
!= SYM_ENUM
&&
1914 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1916 // The sign of bitfields is unspecified by default.
1917 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1920 bitfield
->bit_size
= width
;
1921 bitfield
->endpos
= token
->pos
;
1922 bitfield
->ident
= *ctx
->ident
;
1926 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1928 struct decl_state ctx
= {.prefer_abstract
= 0};
1932 token
= declaration_specifiers(token
, &ctx
);
1933 mod
= decl_modifiers(&ctx
);
1936 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1938 ctx
.ident
= &decl
->ident
;
1940 token
= declarator(token
, &ctx
);
1941 if (match_op(token
, ':'))
1942 token
= handle_bitfield(token
, &ctx
);
1944 token
= handle_attributes(token
, &ctx
);
1945 apply_modifiers(token
->pos
, &ctx
);
1947 decl
->ctype
= ctx
.ctype
;
1948 decl
->ctype
.modifiers
|= mod
;
1949 decl
->cleanup
= ctx
.cleanup
;
1950 decl
->endpos
= token
->pos
;
1951 add_symbol(list
, decl
);
1952 if (!match_op(token
, ','))
1954 token
= token
->next
;
1960 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1962 while (!match_op(token
, '}')) {
1963 if (match_ident(token
, &_Static_assert_ident
)) {
1964 token
= parse_static_assert(token
, NULL
);
1967 if (!match_op(token
, ';'))
1968 token
= declaration_list(token
, list
);
1969 if (!match_op(token
, ';')) {
1970 sparse_error(token
->pos
, "expected ; at end of declaration");
1973 token
= token
->next
;
1978 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1980 struct decl_state ctx
= {.prefer_abstract
= 1};
1982 token
= declaration_specifiers(token
, &ctx
);
1983 ctx
.ident
= &sym
->ident
;
1984 token
= declarator(token
, &ctx
);
1985 token
= handle_attributes(token
, &ctx
);
1986 apply_modifiers(token
->pos
, &ctx
);
1987 sym
->ctype
= ctx
.ctype
;
1988 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
1989 sym
->endpos
= token
->pos
;
1990 sym
->forced_arg
= ctx
.forced
;
1994 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1996 struct decl_state ctx
= {.prefer_abstract
= 1};
1997 unsigned long class;
1998 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2000 token
= declaration_specifiers(token
, &ctx
);
2001 token
= declarator(token
, &ctx
);
2002 apply_modifiers(token
->pos
, &ctx
);
2003 sym
->ctype
= ctx
.ctype
;
2004 sym
->cleanup
= ctx
.cleanup
;
2005 sym
->endpos
= token
->pos
;
2006 class = ctx
.storage_class
;
2008 *forced
= ctx
.forced
;
2010 warning(sym
->pos
, "storage class in typename (%s%s)",
2011 modifier_string(class), show_typename(sym
));
2015 static struct token
*parse_underscore_Pragma(struct token
*token
)
2020 if (!match_op(next
, '('))
2023 if (next
->pos
.type
!= TOKEN_STRING
)
2026 if (!match_op(next
, ')'))
2031 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
2033 if (match_ident(token
, &_Pragma_ident
))
2034 return parse_underscore_Pragma(token
);
2036 token
= parse_expression(token
, tree
);
2037 return expect(token
, ';', "at end of statement");
2040 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2041 struct asm_operand_list
**inout
)
2043 /* Allow empty operands */
2044 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2047 struct asm_operand
*op
= __alloc_asm_operand(0);
2048 if (match_op(token
->next
, '[') &&
2049 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2050 match_op(token
->next
->next
->next
, ']')) {
2051 op
->name
= token
->next
->next
->ident
;
2052 token
= token
->next
->next
->next
;
2054 token
= token
->next
;
2055 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2056 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2057 add_ptr_list(inout
, op
);
2058 } while (match_op(token
, ','));
2062 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2063 struct expression_list
**clobbers
)
2065 struct expression
*expr
;
2068 token
= primary_expression(token
->next
, &expr
);
2070 add_expression(clobbers
, expr
);
2071 } while (match_op(token
, ','));
2075 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2076 struct symbol_list
**labels
)
2078 struct symbol
*label
;
2081 token
= token
->next
; /* skip ':' and ',' */
2082 if (token_type(token
) != TOKEN_IDENT
)
2084 label
= label_symbol(token
, 1);
2085 add_symbol(labels
, label
);
2086 token
= token
->next
;
2087 } while (match_op(token
, ','));
2091 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2093 unsigned long mods
= 0;
2095 token
= token
->next
;
2096 stmt
->type
= STMT_ASM
;
2097 while (token_type(token
) == TOKEN_IDENT
) {
2098 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2099 if (s
&& s
->op
->asm_modifier
)
2100 s
->op
->asm_modifier(token
, &mods
, s
->ctype
.modifiers
);
2101 else if (token
->ident
== &goto_ident
)
2102 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2103 token
= token
->next
;
2105 token
= expect(token
, '(', "after asm");
2106 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2107 if (match_op(token
, ':'))
2108 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2109 if (match_op(token
, ':'))
2110 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2111 if (match_op(token
, ':'))
2112 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2113 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2114 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2115 token
= expect(token
, ')', "after asm");
2116 return expect(token
, ';', "at end of asm-statement");
2119 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2121 struct expression
*cond
= NULL
, *message
= NULL
;
2123 token
= expect(token
->next
, '(', "after _Static_assert");
2124 token
= constant_expression(token
, &cond
);
2126 sparse_error(token
->pos
, "Expected constant expression");
2127 if (match_op(token
, ',')) {
2128 token
= token
->next
;
2129 token
= string_expression(token
, &message
, "_Static_assert()");
2133 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2134 token
= expect(token
, ';', "after _Static_assert()");
2136 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
) {
2137 const char *sep
= "", *msg
= "";
2141 msg
= show_string(message
->string
);
2143 sparse_error(cond
->pos
, "static assertion failed%s%s", sep
, msg
);
2149 static struct token
*ignore_seg_gs(struct token
*token
, struct symbol_list
**unused
)
2154 /* Make a statement out of an expression */
2155 static struct statement
*make_statement(struct expression
*expr
)
2157 struct statement
*stmt
;
2161 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2162 stmt
->expression
= expr
;
2167 * All iterators have two symbols associated with them:
2168 * the "continue" and "break" symbols, which are targets
2169 * for continue and break statements respectively.
2171 * They are in a special name-space, but they follow
2172 * all the normal visibility rules, so nested iterators
2173 * automatically work right.
2175 static void start_iterator(struct statement
*stmt
)
2177 struct symbol
*cont
, *brk
;
2179 start_block_scope(stmt
->pos
);
2180 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2181 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2182 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2183 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2185 stmt
->type
= STMT_ITERATOR
;
2186 stmt
->iterator_break
= brk
;
2187 stmt
->iterator_continue
= cont
;
2188 fn_local_symbol(brk
);
2189 fn_local_symbol(cont
);
2192 static void end_iterator(struct statement
*stmt
)
2197 static struct statement
*start_function(struct symbol
*sym
)
2200 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2202 start_function_scope(sym
->pos
);
2203 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2204 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2205 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2206 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2207 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2209 fn_local_symbol(ret
);
2211 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2217 static void end_function(struct symbol
*sym
)
2220 end_function_scope();
2224 * A "switch()" statement, like an iterator, has a
2225 * the "break" symbol associated with it. It works
2226 * exactly like the iterator break - it's the target
2227 * for any break-statements in scope, and means that
2228 * "break" handling doesn't even need to know whether
2229 * it's breaking out of an iterator or a switch.
2231 * In addition, the "case" symbol is a marker for the
2232 * case/default statements to find the switch statement
2233 * that they are associated with.
2235 static void start_switch(struct statement
*stmt
)
2237 struct symbol
*brk
, *switch_case
;
2239 start_block_scope(stmt
->pos
);
2240 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2241 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2243 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2244 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2245 switch_case
->stmt
= stmt
;
2247 stmt
->type
= STMT_SWITCH
;
2248 stmt
->switch_break
= brk
;
2249 stmt
->switch_case
= switch_case
;
2251 fn_local_symbol(brk
);
2252 fn_local_symbol(switch_case
);
2255 static void end_switch(struct statement
*stmt
)
2257 if (!stmt
->switch_case
->symbol_list
)
2258 warning(stmt
->pos
, "switch with no cases");
2262 static void add_case_statement(struct statement
*stmt
)
2264 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2268 sparse_error(stmt
->pos
, "not in switch scope");
2269 stmt
->type
= STMT_NONE
;
2272 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2273 add_symbol(&target
->symbol_list
, sym
);
2275 stmt
->case_label
= sym
;
2276 fn_local_symbol(sym
);
2279 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2281 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2284 error_die(token
->pos
, "internal error: return without a function target");
2285 stmt
->type
= STMT_RETURN
;
2286 stmt
->ret_target
= target
;
2287 return expression_statement(token
->next
, &stmt
->ret_value
);
2290 static void validate_for_loop_decl(struct symbol
*sym
)
2292 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2294 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2295 const char *name
= show_ident(sym
->ident
);
2296 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2297 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2301 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2303 struct symbol_list
*syms
;
2304 struct expression
*e1
, *e2
, *e3
;
2305 struct statement
*iterator
;
2307 start_iterator(stmt
);
2308 token
= expect(token
->next
, '(', "after 'for'");
2312 /* C99 variable declaration? */
2313 if (lookup_type(token
)) {
2314 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2316 token
= parse_expression(token
, &e1
);
2317 token
= expect(token
, ';', "in 'for'");
2319 token
= parse_expression(token
, &e2
);
2320 token
= expect(token
, ';', "in 'for'");
2321 token
= parse_expression(token
, &e3
);
2322 token
= expect(token
, ')', "in 'for'");
2323 token
= statement(token
, &iterator
);
2325 stmt
->iterator_syms
= syms
;
2326 stmt
->iterator_pre_statement
= make_statement(e1
);
2327 stmt
->iterator_pre_condition
= e2
;
2328 stmt
->iterator_post_statement
= make_statement(e3
);
2329 stmt
->iterator_post_condition
= NULL
;
2330 stmt
->iterator_statement
= iterator
;
2336 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2338 struct expression
*expr
;
2339 struct statement
*iterator
;
2341 start_iterator(stmt
);
2342 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2343 token
= statement(token
, &iterator
);
2345 stmt
->iterator_pre_condition
= expr
;
2346 stmt
->iterator_post_condition
= NULL
;
2347 stmt
->iterator_statement
= iterator
;
2353 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2355 struct expression
*expr
;
2356 struct statement
*iterator
;
2358 start_iterator(stmt
);
2359 token
= statement(token
->next
, &iterator
);
2360 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2361 token
= token
->next
;
2363 sparse_error(token
->pos
, "expected 'while' after 'do'");
2364 token
= parens_expression(token
, &expr
, "after 'do-while'");
2366 stmt
->iterator_post_condition
= expr
;
2367 stmt
->iterator_statement
= iterator
;
2370 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2371 warning(iterator
->pos
, "do-while statement is not a compound statement");
2373 return expect(token
, ';', "after statement");
2376 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2378 stmt
->type
= STMT_IF
;
2379 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2380 token
= statement(token
, &stmt
->if_true
);
2381 if (token_type(token
) != TOKEN_IDENT
)
2383 if (token
->ident
!= &else_ident
)
2385 return statement(token
->next
, &stmt
->if_false
);
2388 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2390 stmt
->type
= STMT_CASE
;
2391 token
= expect(token
, ':', "after default/case");
2392 add_case_statement(stmt
);
2393 if (match_op(token
, '}')) {
2394 warning(token
->pos
, "statement expected after case label");
2395 stmt
->case_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2398 return statement(token
, &stmt
->case_statement
);
2401 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2403 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2404 if (match_op(token
, SPECIAL_ELLIPSIS
))
2405 token
= parse_expression(token
->next
, &stmt
->case_to
);
2406 return case_statement(token
, stmt
);
2409 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2411 return case_statement(token
->next
, stmt
);
2414 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2416 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2417 stmt
->type
= STMT_GOTO
;
2418 stmt
->goto_label
= target
;
2420 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2421 return expect(token
->next
, ';', "at end of statement");
2424 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2426 stmt
->type
= STMT_SWITCH
;
2428 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2429 token
= statement(token
, &stmt
->switch_statement
);
2434 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2436 const char *id
= show_ident(ident
);
2437 sparse_error(use
, "label '%s' used outside statement expression", id
);
2438 info(def
, " label '%s' defined here", id
);
2439 current_fn
->bogus_linear
= 1;
2442 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2444 struct statement
*def
= label
->stmt
;
2447 if (!is_in_scope(def
->label_scope
, label_scope
))
2448 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2449 } else if (!label
->label_scope
) {
2450 label
->label_scope
= label_scope
;
2451 label
->label_pos
= use_pos
;
2455 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2457 stmt
->type
= STMT_GOTO
;
2458 token
= token
->next
;
2459 if (match_op(token
, '*')) {
2460 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2461 add_statement(&function_computed_goto_list
, stmt
);
2462 } else if (token_type(token
) == TOKEN_IDENT
) {
2463 struct symbol
*label
= label_symbol(token
, 1);
2464 stmt
->goto_label
= label
;
2465 check_label_usage(label
, stmt
->pos
);
2466 token
= token
->next
;
2468 sparse_error(token
->pos
, "Expected identifier or goto expression");
2470 return expect(token
, ';', "at end of statement");
2473 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2475 stmt
->type
= STMT_CONTEXT
;
2476 token
= token
->next
;
2477 token
= expect(token
, '(', "after __context__ statement");
2478 token
= assignment_expression(token
, &stmt
->expression
);
2479 if (!stmt
->expression
)
2480 unexpected(token
, "expression expected after '('");
2481 if (match_op(token
, ',')) {
2482 token
= token
->next
;
2483 stmt
->context
= stmt
->expression
;
2484 token
= assignment_expression(token
, &stmt
->expression
);
2485 if (!stmt
->expression
)
2486 unexpected(token
, "expression expected after ','");
2488 token
= expect(token
, ')', "at end of __context__ statement");
2489 return expect(token
, ';', "at end of statement");
2492 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2494 stmt
->type
= STMT_RANGE
;
2495 token
= token
->next
;
2496 token
= expect(token
, '(', "after __range__ statement");
2497 token
= assignment_expression(token
, &stmt
->range_expression
);
2498 token
= expect(token
, ',', "after range expression");
2499 token
= assignment_expression(token
, &stmt
->range_low
);
2500 token
= expect(token
, ',', "after low range");
2501 token
= assignment_expression(token
, &stmt
->range_high
);
2502 token
= expect(token
, ')', "after range statement");
2503 return expect(token
, ';', "after range statement");
2506 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2508 struct decl_state ctx
= { };
2510 token
= handle_attributes(token
, &ctx
);
2511 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2515 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2517 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2520 if (token_type(token
) == TOKEN_IDENT
) {
2521 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2522 if (s
&& s
->op
->statement
)
2523 return s
->op
->statement(token
, stmt
);
2525 if (match_op(token
->next
, ':')) {
2526 struct symbol
*s
= label_symbol(token
, 0);
2527 token
= handle_label_attributes(token
->next
->next
, s
);
2529 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2530 // skip the label to avoid multiple definitions
2531 return statement(token
, tree
);
2533 stmt
->type
= STMT_LABEL
;
2534 stmt
->label_identifier
= s
;
2535 stmt
->label_scope
= label_scope
;
2536 if (s
->label_scope
) {
2537 if (!is_in_scope(label_scope
, s
->label_scope
))
2538 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2541 if (match_op(token
, '}')) {
2542 warning(token
->pos
, "statement expected after label");
2543 stmt
->label_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2546 return statement(token
, &stmt
->label_statement
);
2550 if (match_op(token
, '{')) {
2551 token
= compound_statement(token
->next
, stmt
);
2552 return expect(token
, '}', "at end of compound statement");
2555 stmt
->type
= STMT_EXPRESSION
;
2556 return expression_statement(token
, &stmt
->expression
);
2559 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2560 static struct token
*label_statement(struct token
*token
)
2562 while (token_type(token
) == TOKEN_IDENT
) {
2563 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2564 /* it's block-scope, but we want label namespace */
2565 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2566 fn_local_symbol(sym
);
2567 token
= token
->next
;
2568 if (!match_op(token
, ','))
2570 token
= token
->next
;
2572 return expect(token
, ';', "at end of label declaration");
2575 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2577 int seen_statement
= 0;
2578 while (token_type(token
) == TOKEN_IDENT
&&
2579 token
->ident
== &__label___ident
)
2580 token
= label_statement(token
->next
);
2582 struct statement
* stmt
;
2583 if (eof_token(token
))
2585 if (match_op(token
, '}'))
2587 if (match_ident(token
, &_Static_assert_ident
)) {
2588 token
= parse_static_assert(token
, NULL
);
2591 if (lookup_type(token
)) {
2592 if (seen_statement
) {
2593 warning(token
->pos
, "mixing declarations and code");
2596 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2597 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2599 seen_statement
= Wdeclarationafterstatement
;
2600 token
= statement(token
, &stmt
);
2602 add_statement(list
, stmt
);
2607 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2609 struct symbol_list
**list
= &fn
->arguments
;
2611 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2612 sym
->ident
= token
->ident
;
2613 token
= token
->next
;
2614 sym
->endpos
= token
->pos
;
2615 sym
->ctype
.base_type
= &incomplete_ctype
;
2616 add_symbol(list
, sym
);
2617 if (!match_op(token
, ',') ||
2618 token_type(token
->next
) != TOKEN_IDENT
||
2619 lookup_type(token
->next
))
2621 token
= token
->next
;
2626 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2628 struct symbol_list
**list
= &fn
->arguments
;
2633 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2635 token
= token
->next
;
2639 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2640 token
= parameter_declaration(token
, sym
);
2641 if (sym
->ctype
.base_type
== &void_ctype
) {
2642 /* Special case: (void) */
2643 if (!*list
&& !sym
->ident
)
2645 warning(token
->pos
, "void parameter");
2647 add_symbol(list
, sym
);
2648 if (!match_op(token
, ','))
2650 token
= token
->next
;
2655 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2657 stmt
->type
= STMT_COMPOUND
;
2658 start_block_scope(token
->pos
);
2659 token
= statement_list(token
, &stmt
->stmts
);
2664 static struct expression
*identifier_expression(struct token
*token
)
2666 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2667 expr
->expr_ident
= token
->ident
;
2671 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2673 int idx_from
, idx_to
;
2674 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2676 idx_from
= const_expression_value(from
);
2679 idx_to
= const_expression_value(to
);
2680 if (idx_to
< idx_from
|| idx_from
< 0)
2681 warning(from
->pos
, "nonsense array initializer index range");
2683 expr
->idx_from
= idx_from
;
2684 expr
->idx_to
= idx_to
;
2688 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2690 int expect_equal
= 0;
2691 struct token
*next
= token
->next
;
2692 struct expression
**tail
= ep
;
2697 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2698 struct expression
*expr
= identifier_expression(token
);
2699 if (Wold_initializer
)
2700 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2701 token
= initializer(&expr
->ident_expression
, next
->next
);
2702 if (expr
->ident_expression
)
2707 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2708 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2709 struct expression
*expr
= identifier_expression(next
);
2711 tail
= &expr
->ident_expression
;
2714 } else if (match_op(token
, '[')) {
2715 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2716 token
= constant_expression(token
->next
, &from
);
2718 sparse_error(token
->pos
, "Expected constant expression");
2721 if (match_op(token
, SPECIAL_ELLIPSIS
))
2722 token
= constant_expression(token
->next
, &to
);
2723 expr
= index_expression(from
, to
);
2725 tail
= &expr
->idx_expression
;
2726 token
= expect(token
, ']', "at end of initializer index");
2733 if (nested
&& !expect_equal
) {
2734 if (!match_op(token
, '='))
2735 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2740 token
= expect(token
, '=', "at end of initializer index");
2742 token
= initializer(tail
, token
);
2748 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2750 struct expression
*expr
;
2753 token
= single_initializer(&expr
, token
);
2756 add_expression(list
, expr
);
2757 if (!match_op(token
, ','))
2759 token
= token
->next
;
2764 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2766 if (match_op(token
, '{')) {
2767 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2769 if (!Wuniversal_initializer
) {
2770 struct token
*next
= token
->next
;
2771 // '{ 0 }' is equivalent to '{ }' except for some
2772 // warnings, like using 0 to initialize a null-pointer.
2773 if (match_token_zero(next
)) {
2774 if (match_op(next
->next
, '}'))
2775 expr
->zero_init
= 1;
2779 token
= initializer_list(&expr
->expr_list
, token
->next
);
2780 return expect(token
, '}', "at end of initializer");
2782 return assignment_expression(token
, tree
);
2785 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2788 sparse_error(sym
->pos
, "no identifier for function argument");
2791 if (sym
->ctype
.base_type
== &incomplete_ctype
) {
2792 sym
->ctype
.base_type
= &int_ctype
;
2794 if (Wimplicit_int
) {
2795 sparse_error(sym
->pos
, "missing type declaration for parameter '%s'",
2796 show_ident(sym
->ident
));
2799 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2802 static int is_syscall(struct symbol
*sym
)
2808 macro
= get_macro_name(sym
->pos
);
2810 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2811 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2814 name
= sym
->ident
->name
;
2816 if (name
&& strncmp(name
, "sys_", 4) ==0)
2819 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2826 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2827 struct symbol_list
**list
)
2829 struct symbol_list
**old_symbol_list
;
2830 struct symbol
*base_type
= decl
->ctype
.base_type
;
2831 struct statement
*stmt
, **p
;
2832 struct symbol
*prev
;
2835 old_symbol_list
= function_symbol_list
;
2836 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2837 function_symbol_list
= &decl
->inline_symbol_list
;
2838 p
= &base_type
->inline_stmt
;
2840 function_symbol_list
= &decl
->symbol_list
;
2841 p
= &base_type
->stmt
;
2843 function_computed_target_list
= NULL
;
2844 function_computed_goto_list
= NULL
;
2846 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2847 if (Wexternal_function_has_definition
)
2848 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2850 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2851 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2853 stmt
= start_function(decl
);
2856 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2857 declare_argument(arg
, base_type
);
2858 } END_FOR_EACH_PTR(arg
);
2860 token
= statement_list(token
->next
, &stmt
->stmts
);
2863 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2864 add_symbol(list
, decl
);
2865 else if (is_syscall(decl
)) {
2866 add_symbol(list
, decl
);
2868 printf("parse.c decl: %s\n", decl->ident->name);
2869 char *macro = get_macro_name(decl->pos);
2870 printf("decl macro: %s\n", macro);
2873 check_declaration(decl
);
2874 decl
->definition
= decl
;
2875 prev
= decl
->same_symbol
;
2876 if (prev
&& prev
->definition
) {
2877 warning(decl
->pos
, "multiple definitions for function '%s'",
2878 show_ident(decl
->ident
));
2879 info(prev
->definition
->pos
, " the previous one is here");
2882 rebind_scope(prev
, decl
->scope
);
2883 prev
->definition
= decl
;
2884 prev
= prev
->same_symbol
;
2887 function_symbol_list
= old_symbol_list
;
2888 if (function_computed_goto_list
) {
2889 if (!function_computed_target_list
)
2890 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2892 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2893 stmt
->target_list
= function_computed_target_list
;
2894 } END_FOR_EACH_PTR(stmt
);
2897 return expect(token
, '}', "at end of function");
2900 static void promote_k_r_types(struct symbol
*arg
)
2902 struct symbol
*base
= arg
->ctype
.base_type
;
2903 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2904 arg
->ctype
.base_type
= &int_ctype
;
2908 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2910 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2913 FOR_EACH_PTR(real_args
, arg
) {
2914 struct symbol
*type
;
2916 /* This is quadratic in the number of arguments. We _really_ don't care */
2917 FOR_EACH_PTR(argtypes
, type
) {
2918 if (type
->ident
== arg
->ident
)
2920 } END_FOR_EACH_PTR(type
);
2921 if (Wimplicit_int
) {
2922 warning(arg
->pos
, "missing type declaration for parameter '%s'",
2923 show_ident(arg
->ident
));
2925 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
2926 type
->ident
= arg
->ident
;
2927 type
->ctype
.base_type
= &int_ctype
;
2930 /* "char" and "short" promote to "int" */
2931 promote_k_r_types(type
);
2933 arg
->ctype
= type
->ctype
;
2934 } END_FOR_EACH_PTR(arg
);
2936 FOR_EACH_PTR(argtypes
, arg
) {
2938 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2939 } END_FOR_EACH_PTR(arg
);
2943 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2944 struct symbol_list
**list
)
2946 struct symbol_list
*args
= NULL
;
2948 if (Wold_style_definition
)
2949 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2952 token
= declaration_list(token
, &args
);
2953 if (!match_op(token
, ';')) {
2954 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2957 token
= token
->next
;
2958 } while (lookup_type(token
));
2960 apply_k_r_types(args
, decl
);
2962 if (!match_op(token
, '{')) {
2963 sparse_error(token
->pos
, "expected function body");
2966 return parse_function_body(token
, decl
, list
);
2969 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2971 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2972 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2973 struct statement
*stmt
;
2975 anon
->ctype
.base_type
= fn
;
2976 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2979 token
= parse_asm_statement(token
, stmt
);
2981 // FIXME: add_symbol(list, anon);
2985 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2986 validate_decl_t validate_decl
)
2988 struct ident
*ident
= NULL
;
2989 struct symbol
*decl
;
2990 struct decl_state ctx
= { .ident
= &ident
};
2992 struct symbol
*base_type
;
2996 if (match_ident(token
, &_Pragma_ident
))
2997 return parse_underscore_Pragma(token
);
2999 /* Top-level inline asm or static assertion? */
3000 if (token_type(token
) == TOKEN_IDENT
) {
3001 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
3002 if (s
&& s
->op
->toplevel
)
3003 return s
->op
->toplevel(token
, list
);
3006 /* Parse declaration-specifiers, if any */
3007 token
= declaration_specifiers(token
, &ctx
);
3008 mod
= decl_modifiers(&ctx
);
3009 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3010 /* Just a type declaration? */
3011 if (match_op(token
, ';')) {
3012 apply_modifiers(token
->pos
, &ctx
);
3017 token
= declarator(token
, &ctx
);
3018 token
= handle_asm_name(token
, &ctx
);
3019 token
= handle_attributes(token
, &ctx
);
3020 apply_modifiers(token
->pos
, &ctx
);
3022 decl
->ctype
= ctx
.ctype
;
3023 decl
->ctype
.modifiers
|= mod
;
3024 decl
->cleanup
= ctx
.cleanup
;
3025 decl
->endpos
= token
->pos
;
3027 /* Just a type declaration? */
3029 warning(token
->pos
, "missing identifier in declaration");
3030 return expect(token
, ';', "at the end of type declaration");
3033 /* type define declaration? */
3034 is_typedef
= ctx
.storage_class
== MOD_USERTYPE
;
3036 /* Typedefs don't have meaningful storage */
3038 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3040 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3042 base_type
= decl
->ctype
.base_type
;
3045 if (base_type
&& !base_type
->ident
) {
3046 switch (base_type
->type
) {
3051 base_type
->ident
= ident
;
3057 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3058 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3059 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3060 show_ident(decl
->ident
));
3061 base_type
->ctype
.base_type
= &int_ctype
;
3063 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3064 warning(decl
->pos
, "'%s()' has implicit return type",
3065 show_ident(decl
->ident
));
3066 base_type
->ctype
.base_type
= &int_ctype
;
3068 /* apply attributes placed after the declarator */
3069 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3071 /* K&R argument declaration? */
3072 if (lookup_type(token
))
3073 return parse_k_r_arguments(token
, decl
, list
);
3074 if (match_op(token
, '{'))
3075 return parse_function_body(token
, decl
, list
);
3077 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3078 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3079 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3080 sparse_error(token
->pos
, "void declaration");
3082 if (base_type
== &incomplete_ctype
) {
3083 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3084 decl
->ctype
.base_type
= &int_ctype
;;
3088 if (!is_typedef
&& match_op(token
, '=')) {
3089 struct token
*next
= token
->next
;
3090 token
= initializer(&decl
->initializer
, next
);
3092 sparse_error(token
->pos
, "expression expected before '%s'", show_token(token
));
3096 validate_decl(decl
);
3098 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3099 warning(decl
->pos
, "symbol with external linkage has initializer");
3100 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3103 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3104 add_symbol(list
, decl
);
3105 fn_local_symbol(decl
);
3108 check_declaration(decl
);
3109 if (decl
->same_symbol
) {
3110 decl
->definition
= decl
->same_symbol
->definition
;
3111 decl
->op
= decl
->same_symbol
->op
;
3113 // TODO: handle -std=c89 --pedantic
3114 check_duplicates(decl
);
3119 const char *msg
= NULL
;
3120 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3121 msg
= "on non-identifier";
3122 else if (match_op(token
, ','))
3123 msg
= "on declaration list";
3124 else if (!decl
->initializer
)
3125 msg
= "without initializer";
3126 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3127 decl
->initializer
->symbol
== decl
)
3128 msg
= "on self-init var";
3130 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3131 decl
->ctype
.base_type
= &bad_ctype
;
3135 if (!match_op(token
, ','))
3138 token
= token
->next
;
3140 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3143 token
= handle_attributes(token
, &ctx
);
3144 token
= declarator(token
, &ctx
);
3145 token
= handle_asm_name(token
, &ctx
);
3146 token
= handle_attributes(token
, &ctx
);
3147 apply_modifiers(token
->pos
, &ctx
);
3148 decl
->ctype
= ctx
.ctype
;
3149 decl
->ctype
.modifiers
|= mod
;
3150 decl
->cleanup
= ctx
.cleanup
;
3151 decl
->endpos
= token
->pos
;
3153 sparse_error(token
->pos
, "expected identifier name in type definition");
3158 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3160 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3162 /* Function declarations are automatically extern unless specifically static */
3163 base_type
= decl
->ctype
.base_type
;
3164 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3165 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3166 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3169 return expect(token
, ';', "at end of declaration");