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
);
907 } END_FOR_EACH_PTR(sym
);
910 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
912 unsigned long long lastval
= 0;
913 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
914 struct range range
= { };
917 parent
->examined
= 1;
918 parent
->ctype
.base_type
= &int_ctype
;
919 while (token_type(token
) == TOKEN_IDENT
) {
920 struct expression
*expr
= NULL
;
921 struct token
*next
= token
->next
;
922 struct decl_state ctx
= { };
925 // FIXME: only 'deprecated' should be accepted
926 next
= handle_attributes(next
, &ctx
);
928 if (match_op(next
, '=')) {
929 next
= constant_expression(next
->next
, &expr
);
930 lastval
= get_expression_value(expr
);
932 if (expr
&& expr
->ctype
)
936 } else if (is_int_type(ctype
)) {
939 error_die(token
->pos
, "can't increment the last enum member");
943 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
944 expr
->value
= lastval
;
948 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
949 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
950 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
951 sym
->initializer
= expr
;
952 sym
->enum_member
= 1;
953 sym
->ctype
.base_type
= parent
;
954 add_ptr_list(&parent
->symbol_list
, sym
);
956 if (base_type
!= &bad_ctype
) {
957 if (ctype
->type
== SYM_NODE
)
958 ctype
= ctype
->ctype
.base_type
;
959 if (ctype
->type
== SYM_ENUM
) {
963 ctype
= ctype
->ctype
.base_type
;
967 * - if all enums are of the same type, then
968 * the base_type is that type (two first
970 * - if enums are of different types, they
971 * all have to be integer types, and the
972 * base type is at least "int_ctype".
973 * - otherwise the base_type is "bad_ctype".
975 if (!base_type
|| ctype
== &bad_ctype
) {
977 } else if (ctype
== base_type
) {
979 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
980 base_type
= &int_ctype
;
981 } else if (is_restricted_type(base_type
) != is_restricted_type(ctype
)) {
982 if (!mix_bitwise
++) {
983 warning(expr
->pos
, "mixed bitwiseness");
985 } else if (is_restricted_type(base_type
) && base_type
!= ctype
) {
986 sparse_error(expr
->pos
, "incompatible restricted type");
987 info(expr
->pos
, " expected: %s", show_typename(base_type
));
988 info(expr
->pos
, " got: %s", show_typename(ctype
));
989 base_type
= &bad_ctype
;
990 } else if (base_type
!= &bad_ctype
) {
991 sparse_error(token
->pos
, "bad enum definition");
992 base_type
= &bad_ctype
;
994 parent
->ctype
.base_type
= base_type
;
996 if (is_int_type(base_type
)) {
997 update_range(&range
, lastval
, ctype
);
1001 sym
->endpos
= token
->pos
;
1003 if (!match_op(token
, ','))
1005 token
= token
->next
;
1008 sparse_error(token
->pos
, "empty enum definition");
1009 base_type
= &bad_ctype
;
1011 else if (!is_int_type(base_type
))
1013 else if (type_is_ok(&uint_ctype
, range
))
1014 base_type
= &uint_ctype
;
1015 else if (type_is_ok(&int_ctype
, range
))
1016 base_type
= &int_ctype
;
1017 else if (type_is_ok(&ulong_ctype
, range
))
1018 base_type
= &ulong_ctype
;
1019 else if (type_is_ok(&long_ctype
, range
))
1020 base_type
= &long_ctype
;
1021 else if (type_is_ok(&ullong_ctype
, range
))
1022 base_type
= &ullong_ctype
;
1023 else if (type_is_ok(&llong_ctype
, range
))
1024 base_type
= &llong_ctype
;
1026 base_type
= &bad_ctype
;
1027 parent
->ctype
.base_type
= base_type
;
1028 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1029 parent
->examined
= 0;
1033 cast_enum_list(parent
->symbol_list
, base_type
);
1038 static struct token
*enum_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1040 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
1041 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
1043 if (!ctype
->base_type
)
1044 ctype
->base_type
= &incomplete_ctype
;
1049 static struct token
*typeof_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1052 if (!match_op(token
, '(')) {
1053 sparse_error(token
->pos
, "expected '(' after typeof");
1056 if (lookup_type(token
->next
)) {
1058 token
= typename(token
->next
, &sym
, NULL
);
1059 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
1060 apply_ctype(token
->pos
, &ctx
->ctype
, &sym
->ctype
);
1062 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
1063 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
1065 typeof_sym
->endpos
= token
->pos
;
1066 if (!typeof_sym
->initializer
) {
1067 sparse_error(token
->pos
, "expected expression after the '(' token");
1068 typeof_sym
= &bad_ctype
;
1070 ctx
->ctype
.base_type
= typeof_sym
;
1072 return expect(token
, ')', "after typeof");
1075 static struct token
*autotype_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1077 ctx
->ctype
.base_type
= &autotype_ctype
;
1082 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1084 struct expression
*expr
= NULL
;
1085 if (match_op(token
, '('))
1086 token
= parens_expression(token
, &expr
, "in attribute");
1090 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1092 if (!ctx
->ctype
.alignment
) {
1093 ctx
->ctype
.alignment
= 1;
1099 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1101 int alignment
= max_alignment
;
1102 struct expression
*expr
= NULL
;
1104 if (match_op(token
, '(')) {
1105 token
= parens_expression(token
, &expr
, "in attribute");
1107 alignment
= const_expression_value(expr
);
1109 if (alignment
& (alignment
-1)) {
1110 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1112 } else if (alignment
> ctx
->ctype
.alignment
)
1113 ctx
->ctype
.alignment
= alignment
;
1117 static void apply_mod(struct position
*pos
, unsigned long *mods
, unsigned long mod
)
1119 if (*mods
& mod
& ~MOD_DUP_OK
)
1120 warning(*pos
, "duplicate %s", modifier_name(mod
));
1124 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1126 apply_mod(pos
, &ctx
->modifiers
, qual
);
1129 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1131 apply_mod(&token
->pos
, &ctx
->ctype
.modifiers
, attr
->ctype
.modifiers
);
1135 static struct token
*attribute_function(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1137 apply_mod(&token
->pos
, &ctx
->f_modifiers
, attr
->ctype
.modifiers
);
1141 static struct token
*attribute_bitwise(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1144 attribute_modifier(token
, attr
, ctx
);
1148 static struct ident
*numerical_address_space(int asn
)
1154 sprintf(buff
, "<asn:%d>", asn
);
1155 return built_in_ident(buff
);
1158 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1160 struct expression
*expr
= NULL
;
1161 struct ident
*as
= NULL
;
1164 token
= expect(token
, '(', "after address_space attribute");
1165 switch (token_type(token
)) {
1167 next
= primary_expression(token
, &expr
);
1168 if (expr
->type
!= EXPR_VALUE
)
1170 as
= numerical_address_space(expr
->value
);
1180 warning(token
->pos
, "invalid address space name");
1183 if (Waddress_space
&& as
) {
1185 sparse_error(token
->pos
,
1186 "multiple address spaces given: %s & %s",
1187 show_as(ctx
->ctype
.as
), show_as(as
));
1190 token
= expect(next
, ')', "after address_space attribute");
1194 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1196 if (ctype
->ctype
.base_type
!= &int_type
)
1198 if (ctype
== &char_ctype
)
1200 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1204 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1206 if (ctype
->ctype
.base_type
!= &int_type
)
1208 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1212 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1214 if (ctype
->ctype
.base_type
!= &int_type
)
1216 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1220 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1222 if (ctype
->ctype
.base_type
!= &int_type
)
1224 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1228 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1230 if (ctype
->ctype
.base_type
!= &int_type
)
1232 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint128_ctype
1236 static struct symbol
*to_pointer_mode(struct symbol
*ctype
)
1238 if (ctype
->ctype
.base_type
!= &int_type
)
1240 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? uintptr_ctype
1244 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1246 if (ctype
->ctype
.base_type
!= &int_type
)
1248 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1252 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1254 token
= expect(token
, '(', "after mode attribute");
1255 if (token_type(token
) == TOKEN_IDENT
) {
1256 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1257 if (mode
&& mode
->op
->type
& KW_MODE
)
1258 ctx
->mode
= mode
->op
;
1260 sparse_error(token
->pos
, "unknown mode attribute %s", show_ident(token
->ident
));
1261 token
= token
->next
;
1263 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1264 token
= expect(token
, ')', "after mode attribute");
1268 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1270 struct context
*context
= alloc_context();
1271 struct expression
*args
[3];
1274 token
= expect(token
, '(', "after context attribute");
1275 token
= conditional_expression(token
, &args
[0]);
1276 token
= expect(token
, ',', "after context 1st argument");
1277 token
= conditional_expression(token
, &args
[1]);
1278 if (match_op(token
, ',')) {
1279 token
= token
->next
;
1280 token
= conditional_expression(token
, &args
[2]);
1281 token
= expect(token
, ')', "after context 3rd argument");
1282 context
->context
= args
[0];
1285 token
= expect(token
, ')', "after context 2nd argument");
1287 context
->in
= get_expression_value(args
[idx
++]);
1288 context
->out
= get_expression_value(args
[idx
++]);
1289 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1293 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1295 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1296 ctx
->ctype
.base_type
->designated_init
= 1;
1298 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1302 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1304 if (Wtransparent_union
)
1305 warning(token
->pos
, "attribute __transparent_union__");
1307 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_UNION
)
1308 ctx
->ctype
.base_type
->transparent_union
= 1;
1310 warning(token
->pos
, "attribute __transparent_union__ applied to non-union type");
1314 static struct token
*recover_unknown_attribute(struct token
*token
)
1316 struct expression
*expr
= NULL
;
1318 if (Wunknown_attribute
)
1319 warning(token
->pos
, "unknown attribute '%s'", show_ident(token
->ident
));
1320 token
= token
->next
;
1321 if (match_op(token
, '('))
1322 token
= parens_expression(token
, &expr
, "in attribute");
1326 static struct token
*attribute_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1328 token
= expect(token
, '(', "after attribute");
1329 token
= expect(token
, '(', "after attribute");
1331 while (token_type(token
) == TOKEN_IDENT
) {
1332 struct symbol
*attr
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1333 if (attr
&& attr
->op
->attribute
)
1334 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1336 token
= recover_unknown_attribute(token
);
1338 if (!match_op(token
, ','))
1340 token
= token
->next
;
1343 token
= expect(token
, ')', "after attribute");
1344 token
= expect(token
, ')', "after attribute");
1348 static unsigned long decl_modifiers(struct decl_state
*ctx
)
1350 unsigned long mods
= ctx
->ctype
.modifiers
& MOD_DECLARE
;
1351 ctx
->ctype
.modifiers
&= ~MOD_DECLARE
;
1352 return ctx
->storage_class
| mods
;
1355 static struct token
*storage_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1357 int is_tls
= ctx
->ctype
.modifiers
& MOD_TLS
;
1358 unsigned long class = sym
->ctype
.modifiers
;
1359 const char *storage
= modifier_name(class);
1361 /* __thread can be used alone, or with extern or static */
1362 if (is_tls
&& (class & ~(MOD_STATIC
|MOD_EXTERN
)))
1363 sparse_error(next
->pos
, "__thread cannot be used with '%s'", storage
);
1364 else if (!ctx
->storage_class
)
1365 ctx
->storage_class
= class;
1366 else if (ctx
->storage_class
== class)
1367 sparse_error(next
->pos
, "duplicate %s", storage
);
1369 sparse_error(next
->pos
, "multiple storage classes");
1373 static struct token
*thread_specifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1375 /* This GCC extension can be used alone, or with extern or static */
1376 if (!(ctx
->storage_class
& ~(MOD_STATIC
|MOD_EXTERN
))) {
1377 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_TLS
);
1379 sparse_error(next
->pos
, "__thread cannot be used with '%s'",
1380 modifier_name(ctx
->storage_class
));
1386 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1392 static struct token
*alignas_specifier(struct token
*token
, struct symbol
*sym
, struct decl_state
*ctx
)
1396 if (!match_op(token
, '(')) {
1397 sparse_error(token
->pos
, "expected '(' after _Alignas");
1400 if (lookup_type(token
->next
)) {
1401 struct symbol
*sym
= NULL
;
1402 token
= typename(token
->next
, &sym
, NULL
);
1403 sym
= examine_symbol_type(sym
);
1404 alignment
= sym
->ctype
.alignment
;
1405 token
= expect(token
, ')', "after _Alignas(...");
1407 struct expression
*expr
= NULL
;
1408 token
= parens_expression(token
, &expr
, "after _Alignas");
1411 alignment
= const_expression_value(expr
);
1414 if (alignment
< 0) {
1415 warning(token
->pos
, "non-positive alignment");
1418 if (alignment
& (alignment
-1)) {
1419 warning(token
->pos
, "non-power-of-2 alignment");
1422 if (alignment
> ctx
->ctype
.alignment
)
1423 ctx
->ctype
.alignment
= alignment
;
1427 static struct token
*generic_qualifier(struct token
*next
, struct symbol
*sym
, struct decl_state
*ctx
)
1429 apply_qualifier(&next
->pos
, &ctx
->ctype
, sym
->ctype
.modifiers
);
1433 static void apply_ctype(struct position pos
, struct ctype
*dst
, struct ctype
*src
)
1435 unsigned long mod
= src
->modifiers
;
1438 apply_qualifier(&pos
, dst
, mod
);
1441 concat_ptr_list((struct ptr_list
*)src
->contexts
,
1442 (struct ptr_list
**)&dst
->contexts
);
1445 if (src
->alignment
> dst
->alignment
)
1446 dst
->alignment
= src
->alignment
;
1453 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1456 if (what
& (Set_S
| Set_T
))
1458 if (what
& Set_Char
)
1460 else if (what
& Set_Double
)
1462 else if (what
& Set_Float
)
1464 else if (what
& Set_Signed
)
1466 else if (what
& Set_Unsigned
)
1468 else if (what
& Set_Short
)
1470 else if (what
& Set_Long
)
1474 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1475 old
, show_ident(new));
1479 sparse_error(pos
, "two or more data types in declaration specifiers");
1482 static struct symbol
* const int_types
[] =
1483 {&char_ctype
, &short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
, &int128_ctype
};
1484 static struct symbol
* const signed_types
[] =
1485 {&schar_ctype
, &sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1487 static struct symbol
* const unsigned_types
[] =
1488 {&uchar_ctype
, &ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1490 static struct symbol
* const real_types
[] =
1491 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1492 static struct symbol
* const * const types
[] = {
1493 [CInt
] = int_types
+ 2,
1494 [CSInt
] = signed_types
+ 2,
1495 [CUInt
] = unsigned_types
+ 2,
1496 [CReal
] = real_types
+ 1,
1499 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1501 return types
[want_unsigned
? CUInt
: CInt
][size
];
1504 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1506 while (token_type(t
) == TOKEN_IDENT
) {
1507 struct symbol
*s
= lookup_keyword(t
->ident
, NS_TYPEDEF
);
1510 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1513 if (s
->op
->declarator
)
1514 t
= s
->op
->declarator(t
, s
, ctx
);
1519 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1525 while (token_type(token
) == TOKEN_IDENT
) {
1526 struct symbol
*s
= lookup_symbol(token
->ident
,
1527 NS_TYPEDEF
| NS_SYMBOL
);
1528 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1530 if (s
->type
!= SYM_KEYWORD
) {
1533 seen
|= Set_S
| Set_T
;
1534 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1535 apply_ctype(token
->pos
, &ctx
->ctype
, &s
->ctype
);
1536 token
= token
->next
;
1539 if (s
->op
->type
& KW_SPECIFIER
) {
1540 if (seen
& s
->op
->test
) {
1541 specifier_conflict(token
->pos
,
1547 class += s
->op
->class;
1548 if (s
->op
->set
& Set_Int128
)
1550 else if (s
->op
->set
& Set_Char
)
1552 if (s
->op
->set
& (Set_Short
|Set_Float
)) {
1554 } else if (s
->op
->set
& Set_Long
&& rank
++) {
1555 if (class == CReal
) {
1556 specifier_conflict(token
->pos
,
1564 token
= token
->next
;
1565 if (s
->op
->declarator
) // Note: this eats attributes
1566 token
= s
->op
->declarator(token
, s
, ctx
);
1567 if (s
->op
->type
& KW_EXACT
) {
1568 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1569 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1573 if (!(seen
& Set_S
)) { /* not set explicitly? */
1574 struct symbol
*base
= &incomplete_ctype
;
1576 base
= types
[class][rank
];
1577 ctx
->ctype
.base_type
= base
;
1580 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1581 struct symbol
*type
;
1582 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1583 if (!is_int_type(ctx
->ctype
.base_type
)) {
1584 sparse_error(token
->pos
, "invalid modifier");
1587 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1588 *type
= *ctx
->ctype
.base_type
;
1589 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1590 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1591 type
->type
= SYM_RESTRICT
;
1592 ctx
->ctype
.base_type
= type
;
1593 create_fouled(type
);
1598 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1600 struct expression
*expr
= NULL
;
1603 while (token_type(token
) == TOKEN_IDENT
) {
1604 struct symbol
*sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1605 if (!sym
|| !(sym
->op
->type
& (KW_STATIC
|KW_QUALIFIER
)))
1607 if (has_static
&& (sym
->op
->type
& KW_STATIC
))
1608 sparse_error(token
->pos
, "duplicate array static declarator");
1609 has_static
|= (sym
->op
->type
& KW_STATIC
);
1610 token
= token
->next
;
1612 if (match_op(token
, '*') && match_op(token
->next
, ']')) {
1613 // FIXME: '[*]' is treated like '[]'
1614 token
= token
->next
;
1616 token
= assignment_expression(token
, &expr
);
1618 sym
->array_size
= expr
;
1622 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1623 static struct token
*identifier_list(struct token
*, struct symbol
*);
1624 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1626 static struct token
*handle_asm_name(struct token
*token
, struct decl_state
*ctx
)
1628 struct expression
*expr
;
1629 struct symbol
*keyword
;
1631 if (token_type(token
) != TOKEN_IDENT
)
1633 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1636 if (!(keyword
->op
->type
& KW_ASM
))
1639 token
= token
->next
;
1640 token
= expect(token
, '(', "after asm");
1641 token
= string_expression(token
, &expr
, "asm name");
1642 token
= expect(token
, ')', "after asm");
1647 // test if @token is '__attribute__' (or one of its variant)
1648 static bool match_attribute(struct token
*token
)
1652 if (token_type(token
) != TOKEN_IDENT
)
1654 sym
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
1655 if (!sym
|| !sym
->op
)
1657 return sym
->op
->type
& KW_ATTRIBUTE
;
1660 static struct token
*skip_attribute(struct token
*token
)
1662 token
= token
->next
;
1663 if (match_op(token
, '(')) {
1665 token
= token
->next
;
1666 while (depth
&& !eof_token(token
)) {
1667 if (token_type(token
) == TOKEN_SPECIAL
) {
1668 if (token
->special
== '(')
1670 else if (token
->special
== ')')
1673 token
= token
->next
;
1679 static struct token
*skip_attributes(struct token
*token
)
1681 while (match_attribute(token
)) {
1682 token
= expect(token
->next
, '(', "after attribute");
1683 token
= expect(token
, '(', "after attribute");
1684 while (token_type(token
) == TOKEN_IDENT
) {
1685 token
= skip_attribute(token
);
1686 if (!match_op(token
, ','))
1688 token
= token
->next
;
1690 token
= expect(token
, ')', "after attribute");
1691 token
= expect(token
, ')', "after attribute");
1696 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
)
1698 while (match_attribute(token
))
1699 token
= attribute_specifier(token
->next
, NULL
, ctx
);
1703 static int is_nested(struct token
*token
, struct token
**p
,
1704 int prefer_abstract
)
1707 * This can be either a parameter list or a grouping.
1708 * For the direct (non-abstract) case, we know if must be
1709 * a parameter list if we already saw the identifier.
1710 * For the abstract case, we know if must be a parameter
1711 * list if it is empty or starts with a type.
1713 struct token
*next
= token
->next
;
1715 *p
= next
= skip_attributes(next
);
1717 if (token_type(next
) == TOKEN_IDENT
) {
1718 if (lookup_type(next
))
1719 return !prefer_abstract
;
1723 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1730 Empty
, K_R
, Proto
, Bad_Func
,
1733 static enum kind
which_func(struct token
*token
,
1735 int prefer_abstract
)
1737 struct token
*next
= token
->next
;
1739 if (token_type(next
) == TOKEN_IDENT
) {
1740 if (lookup_type(next
))
1742 /* identifier list not in definition; complain */
1743 if (prefer_abstract
)
1745 "identifier list not in definition");
1749 if (token_type(next
) != TOKEN_SPECIAL
)
1752 if (next
->special
== ')') {
1753 /* don't complain about those */
1754 if (!n
|| match_op(next
->next
, ';') || match_op(next
->next
, ','))
1756 if (Wstrict_prototypes
)
1758 "non-ANSI function declaration of function '%s'",
1763 if (next
->special
== SPECIAL_ELLIPSIS
) {
1765 "variadic functions must have one named argument");
1772 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1774 struct ctype
*ctype
= &ctx
->ctype
;
1776 struct ident
**p
= ctx
->ident
;
1778 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1779 *ctx
->ident
= token
->ident
;
1780 token
= token
->next
;
1781 } else if (match_op(token
, '(') &&
1782 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1783 struct symbol
*base_type
= ctype
->base_type
;
1784 if (token
->next
!= next
)
1785 next
= handle_attributes(token
->next
, ctx
);
1786 token
= declarator(next
, ctx
);
1787 token
= expect(token
, ')', "in nested declarator");
1788 while (ctype
->base_type
!= base_type
)
1789 ctype
= &ctype
->base_type
->ctype
;
1793 if (match_op(token
, '(')) {
1794 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1796 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1797 ctype
->modifiers
|= ctx
->f_modifiers
;
1798 token
= token
->next
;
1800 token
= identifier_list(token
, fn
);
1801 else if (kind
== Proto
)
1802 token
= parameter_type_list(token
, fn
);
1803 token
= expect(token
, ')', "in function declarator");
1804 fn
->endpos
= token
->pos
;
1808 while (match_op(token
, '[')) {
1809 struct symbol
*array
;
1810 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1811 token
= abstract_array_declarator(token
->next
, array
);
1812 token
= expect(token
, ']', "in abstract_array_declarator");
1813 array
->endpos
= token
->pos
;
1814 ctype
= &array
->ctype
;
1819 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1821 while (match_op(token
,'*')) {
1822 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1823 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1824 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1825 ptr
->ctype
.as
= ctx
->ctype
.as
;
1826 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1827 ctx
->ctype
.modifiers
= 0;
1828 ctx
->ctype
.base_type
= ptr
;
1829 ctx
->ctype
.as
= NULL
;
1830 ctx
->ctype
.contexts
= NULL
;
1831 ctx
->ctype
.alignment
= 0;
1833 token
= handle_qualifiers(token
->next
, ctx
);
1834 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1839 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1841 token
= pointer(token
, ctx
);
1842 return direct_declarator(token
, ctx
);
1845 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1847 struct ctype
*ctype
= &ctx
->ctype
;
1848 struct expression
*expr
;
1849 struct symbol
*bitfield
;
1852 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1853 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1854 show_typename(ctype
->base_type
));
1855 // Parse this to recover gracefully.
1856 return conditional_expression(token
->next
, &expr
);
1859 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1860 token
= conditional_expression(token
->next
, &expr
);
1861 width
= const_expression_value(expr
);
1862 bitfield
->bit_size
= width
;
1864 if (width
< 0 || width
> INT_MAX
|| (*ctx
->ident
&& width
== 0)) {
1865 sparse_error(token
->pos
, "bitfield '%s' has invalid width (%lld)",
1866 show_ident(*ctx
->ident
), width
);
1868 } else if (*ctx
->ident
) {
1869 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1870 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1871 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1872 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1873 // Valid values are either {-1;0} or {0}, depending on integer
1874 // representation. The latter makes for very efficient code...
1875 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1877 if (Wdefault_bitfield_sign
&&
1878 bitfield_type
->type
!= SYM_ENUM
&&
1879 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1881 // The sign of bitfields is unspecified by default.
1882 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1885 bitfield
->bit_size
= width
;
1886 bitfield
->endpos
= token
->pos
;
1887 bitfield
->ident
= *ctx
->ident
;
1891 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1893 struct decl_state ctx
= {.prefer_abstract
= 0};
1897 token
= declaration_specifiers(token
, &ctx
);
1898 mod
= decl_modifiers(&ctx
);
1901 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1902 ctx
.ident
= &decl
->ident
;
1904 token
= declarator(token
, &ctx
);
1905 if (match_op(token
, ':'))
1906 token
= handle_bitfield(token
, &ctx
);
1908 token
= handle_attributes(token
, &ctx
);
1909 apply_modifiers(token
->pos
, &ctx
);
1911 decl
->ctype
= ctx
.ctype
;
1912 decl
->ctype
.modifiers
|= mod
;
1913 decl
->endpos
= token
->pos
;
1914 add_symbol(list
, decl
);
1915 if (!match_op(token
, ','))
1917 token
= token
->next
;
1923 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1925 while (!match_op(token
, '}')) {
1926 if (match_ident(token
, &_Static_assert_ident
)) {
1927 token
= parse_static_assert(token
, NULL
);
1930 if (!match_op(token
, ';'))
1931 token
= declaration_list(token
, list
);
1932 if (!match_op(token
, ';')) {
1933 sparse_error(token
->pos
, "expected ; at end of declaration");
1936 token
= token
->next
;
1941 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1943 struct decl_state ctx
= {.prefer_abstract
= 1};
1945 token
= declaration_specifiers(token
, &ctx
);
1946 ctx
.ident
= &sym
->ident
;
1947 token
= declarator(token
, &ctx
);
1948 token
= handle_attributes(token
, &ctx
);
1949 apply_modifiers(token
->pos
, &ctx
);
1950 sym
->ctype
= ctx
.ctype
;
1951 sym
->ctype
.modifiers
|= decl_modifiers(&ctx
);
1952 sym
->endpos
= token
->pos
;
1953 sym
->forced_arg
= ctx
.forced
;
1957 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1959 struct decl_state ctx
= {.prefer_abstract
= 1};
1960 unsigned long class;
1961 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1963 token
= declaration_specifiers(token
, &ctx
);
1964 token
= declarator(token
, &ctx
);
1965 apply_modifiers(token
->pos
, &ctx
);
1966 sym
->ctype
= ctx
.ctype
;
1967 sym
->endpos
= token
->pos
;
1968 class = ctx
.storage_class
;
1970 *forced
= ctx
.forced
;
1972 warning(sym
->pos
, "storage class in typename (%s%s)",
1973 modifier_string(class), show_typename(sym
));
1977 static struct token
*parse_underscore_Pragma(struct token
*token
)
1982 if (!match_op(next
, '('))
1985 if (next
->pos
.type
!= TOKEN_STRING
)
1988 if (!match_op(next
, ')'))
1993 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1995 if (match_ident(token
, &_Pragma_ident
))
1996 return parse_underscore_Pragma(token
);
1998 token
= parse_expression(token
, tree
);
1999 return expect(token
, ';', "at end of statement");
2002 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
2003 struct asm_operand_list
**inout
)
2005 /* Allow empty operands */
2006 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
2009 struct asm_operand
*op
= __alloc_asm_operand(0);
2010 if (match_op(token
->next
, '[') &&
2011 token_type(token
->next
->next
) == TOKEN_IDENT
&&
2012 match_op(token
->next
->next
->next
, ']')) {
2013 op
->name
= token
->next
->next
->ident
;
2014 token
= token
->next
->next
->next
;
2016 token
= token
->next
;
2017 token
= string_expression(token
, &op
->constraint
, "asm constraint");
2018 token
= parens_expression(token
, &op
->expr
, "in asm parameter");
2019 add_ptr_list(inout
, op
);
2020 } while (match_op(token
, ','));
2024 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
2025 struct expression_list
**clobbers
)
2027 struct expression
*expr
;
2030 token
= primary_expression(token
->next
, &expr
);
2032 add_expression(clobbers
, expr
);
2033 } while (match_op(token
, ','));
2037 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
2038 struct symbol_list
**labels
)
2040 struct symbol
*label
;
2043 token
= token
->next
; /* skip ':' and ',' */
2044 if (token_type(token
) != TOKEN_IDENT
)
2046 label
= label_symbol(token
, 1);
2047 add_symbol(labels
, label
);
2048 token
= token
->next
;
2049 } while (match_op(token
, ','));
2053 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
2055 unsigned long mods
= 0;
2057 token
= token
->next
;
2058 stmt
->type
= STMT_ASM
;
2059 while (token_type(token
) == TOKEN_IDENT
) {
2060 struct symbol
*s
= lookup_keyword(token
->ident
, NS_TYPEDEF
);
2061 if (s
&& s
->op
->asm_modifier
)
2062 s
->op
->asm_modifier(token
, &mods
, s
->ctype
.modifiers
);
2063 else if (token
->ident
== &goto_ident
)
2064 asm_modifier(token
, &mods
, MOD_ASM_GOTO
);
2065 token
= token
->next
;
2067 token
= expect(token
, '(', "after asm");
2068 token
= string_expression(token
, &stmt
->asm_string
, "inline asm");
2069 if (match_op(token
, ':'))
2070 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
2071 if (match_op(token
, ':'))
2072 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
2073 if (match_op(token
, ':'))
2074 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
2075 if (match_op(token
, ':') && (mods
& MOD_ASM_GOTO
))
2076 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
2077 token
= expect(token
, ')', "after asm");
2078 return expect(token
, ';', "at end of asm-statement");
2081 static struct token
*parse_static_assert(struct token
*token
, struct symbol_list
**unused
)
2083 struct expression
*cond
= NULL
, *message
= NULL
;
2085 token
= expect(token
->next
, '(', "after _Static_assert");
2086 token
= constant_expression(token
, &cond
);
2088 sparse_error(token
->pos
, "Expected constant expression");
2089 if (match_op(token
, ',')) {
2090 token
= token
->next
;
2091 token
= string_expression(token
, &message
, "_Static_assert()");
2095 token
= expect(token
, ')', "after diagnostic message in _Static_assert");
2096 token
= expect(token
, ';', "after _Static_assert()");
2098 if (cond
&& !const_expression_value(cond
) && cond
->type
== EXPR_VALUE
) {
2099 const char *sep
= "", *msg
= "";
2103 msg
= show_string(message
->string
);
2105 sparse_error(cond
->pos
, "static assertion failed%s%s", sep
, msg
);
2111 /* Make a statement out of an expression */
2112 static struct statement
*make_statement(struct expression
*expr
)
2114 struct statement
*stmt
;
2118 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
2119 stmt
->expression
= expr
;
2124 * All iterators have two symbols associated with them:
2125 * the "continue" and "break" symbols, which are targets
2126 * for continue and break statements respectively.
2128 * They are in a special name-space, but they follow
2129 * all the normal visibility rules, so nested iterators
2130 * automatically work right.
2132 static void start_iterator(struct statement
*stmt
)
2134 struct symbol
*cont
, *brk
;
2136 start_block_scope(stmt
->pos
);
2137 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2138 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
2139 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2140 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2142 stmt
->type
= STMT_ITERATOR
;
2143 stmt
->iterator_break
= brk
;
2144 stmt
->iterator_continue
= cont
;
2145 fn_local_symbol(brk
);
2146 fn_local_symbol(cont
);
2149 static void end_iterator(struct statement
*stmt
)
2154 static struct statement
*start_function(struct symbol
*sym
)
2157 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2159 start_function_scope(sym
->pos
);
2160 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2161 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2162 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_QUALIFIER
| MOD_TLS
| MOD_ACCESS
| MOD_NOCAST
| MOD_NODEREF
);
2163 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2164 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2166 fn_local_symbol(ret
);
2168 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2174 static void end_function(struct symbol
*sym
)
2177 end_function_scope();
2181 * A "switch()" statement, like an iterator, has a
2182 * the "break" symbol associated with it. It works
2183 * exactly like the iterator break - it's the target
2184 * for any break-statements in scope, and means that
2185 * "break" handling doesn't even need to know whether
2186 * it's breaking out of an iterator or a switch.
2188 * In addition, the "case" symbol is a marker for the
2189 * case/default statements to find the switch statement
2190 * that they are associated with.
2192 static void start_switch(struct statement
*stmt
)
2194 struct symbol
*brk
, *switch_case
;
2196 start_block_scope(stmt
->pos
);
2197 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2198 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2200 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2201 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2202 switch_case
->stmt
= stmt
;
2204 stmt
->type
= STMT_SWITCH
;
2205 stmt
->switch_break
= brk
;
2206 stmt
->switch_case
= switch_case
;
2208 fn_local_symbol(brk
);
2209 fn_local_symbol(switch_case
);
2212 static void end_switch(struct statement
*stmt
)
2214 if (!stmt
->switch_case
->symbol_list
)
2215 warning(stmt
->pos
, "switch with no cases");
2219 static void add_case_statement(struct statement
*stmt
)
2221 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2225 sparse_error(stmt
->pos
, "not in switch scope");
2226 stmt
->type
= STMT_NONE
;
2229 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2230 add_symbol(&target
->symbol_list
, sym
);
2232 stmt
->case_label
= sym
;
2233 fn_local_symbol(sym
);
2236 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2238 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2241 error_die(token
->pos
, "internal error: return without a function target");
2242 stmt
->type
= STMT_RETURN
;
2243 stmt
->ret_target
= target
;
2244 return expression_statement(token
->next
, &stmt
->ret_value
);
2247 static void validate_for_loop_decl(struct symbol
*sym
)
2249 unsigned long storage
= sym
->ctype
.modifiers
& MOD_STORAGE
;
2251 if (storage
& ~(MOD_AUTO
| MOD_REGISTER
)) {
2252 const char *name
= show_ident(sym
->ident
);
2253 sparse_error(sym
->pos
, "non-local var '%s' in for-loop initializer", name
);
2254 sym
->ctype
.modifiers
&= ~MOD_STORAGE
;
2258 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2260 struct symbol_list
*syms
;
2261 struct expression
*e1
, *e2
, *e3
;
2262 struct statement
*iterator
;
2264 start_iterator(stmt
);
2265 token
= expect(token
->next
, '(', "after 'for'");
2269 /* C99 variable declaration? */
2270 if (lookup_type(token
)) {
2271 token
= external_declaration(token
, &syms
, validate_for_loop_decl
);
2273 token
= parse_expression(token
, &e1
);
2274 token
= expect(token
, ';', "in 'for'");
2276 token
= parse_expression(token
, &e2
);
2277 token
= expect(token
, ';', "in 'for'");
2278 token
= parse_expression(token
, &e3
);
2279 token
= expect(token
, ')', "in 'for'");
2280 token
= statement(token
, &iterator
);
2282 stmt
->iterator_syms
= syms
;
2283 stmt
->iterator_pre_statement
= make_statement(e1
);
2284 stmt
->iterator_pre_condition
= e2
;
2285 stmt
->iterator_post_statement
= make_statement(e3
);
2286 stmt
->iterator_post_condition
= NULL
;
2287 stmt
->iterator_statement
= iterator
;
2293 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2295 struct expression
*expr
;
2296 struct statement
*iterator
;
2298 start_iterator(stmt
);
2299 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2300 token
= statement(token
, &iterator
);
2302 stmt
->iterator_pre_condition
= expr
;
2303 stmt
->iterator_post_condition
= NULL
;
2304 stmt
->iterator_statement
= iterator
;
2310 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2312 struct expression
*expr
;
2313 struct statement
*iterator
;
2315 start_iterator(stmt
);
2316 token
= statement(token
->next
, &iterator
);
2317 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2318 token
= token
->next
;
2320 sparse_error(token
->pos
, "expected 'while' after 'do'");
2321 token
= parens_expression(token
, &expr
, "after 'do-while'");
2323 stmt
->iterator_post_condition
= expr
;
2324 stmt
->iterator_statement
= iterator
;
2327 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2328 warning(iterator
->pos
, "do-while statement is not a compound statement");
2330 return expect(token
, ';', "after statement");
2333 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2335 stmt
->type
= STMT_IF
;
2336 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2337 token
= statement(token
, &stmt
->if_true
);
2338 if (token_type(token
) != TOKEN_IDENT
)
2340 if (token
->ident
!= &else_ident
)
2342 return statement(token
->next
, &stmt
->if_false
);
2345 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2347 stmt
->type
= STMT_CASE
;
2348 token
= expect(token
, ':', "after default/case");
2349 add_case_statement(stmt
);
2350 if (match_op(token
, '}')) {
2351 warning(token
->pos
, "statement expected after case label");
2352 stmt
->case_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2355 return statement(token
, &stmt
->case_statement
);
2358 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2360 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2361 if (match_op(token
, SPECIAL_ELLIPSIS
))
2362 token
= parse_expression(token
->next
, &stmt
->case_to
);
2363 return case_statement(token
, stmt
);
2366 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2368 return case_statement(token
->next
, stmt
);
2371 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2373 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2374 stmt
->type
= STMT_GOTO
;
2375 stmt
->goto_label
= target
;
2377 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2378 return expect(token
->next
, ';', "at end of statement");
2381 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2383 stmt
->type
= STMT_SWITCH
;
2385 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2386 token
= statement(token
, &stmt
->switch_statement
);
2391 static void warn_label_usage(struct position def
, struct position use
, struct ident
*ident
)
2393 const char *id
= show_ident(ident
);
2394 sparse_error(use
, "label '%s' used outside statement expression", id
);
2395 info(def
, " label '%s' defined here", id
);
2396 current_fn
->bogus_linear
= 1;
2399 void check_label_usage(struct symbol
*label
, struct position use_pos
)
2401 struct statement
*def
= label
->stmt
;
2404 if (!is_in_scope(def
->label_scope
, label_scope
))
2405 warn_label_usage(def
->pos
, use_pos
, label
->ident
);
2406 } else if (!label
->label_scope
) {
2407 label
->label_scope
= label_scope
;
2408 label
->label_pos
= use_pos
;
2412 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2414 stmt
->type
= STMT_GOTO
;
2415 token
= token
->next
;
2416 if (match_op(token
, '*')) {
2417 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2418 add_statement(&function_computed_goto_list
, stmt
);
2419 } else if (token_type(token
) == TOKEN_IDENT
) {
2420 struct symbol
*label
= label_symbol(token
, 1);
2421 stmt
->goto_label
= label
;
2422 check_label_usage(label
, stmt
->pos
);
2423 token
= token
->next
;
2425 sparse_error(token
->pos
, "Expected identifier or goto expression");
2427 return expect(token
, ';', "at end of statement");
2430 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2432 stmt
->type
= STMT_CONTEXT
;
2433 token
= token
->next
;
2434 token
= expect(token
, '(', "after __context__ statement");
2435 token
= assignment_expression(token
, &stmt
->expression
);
2436 if (!stmt
->expression
)
2437 unexpected(token
, "expression expected after '('");
2438 if (match_op(token
, ',')) {
2439 token
= token
->next
;
2440 stmt
->context
= stmt
->expression
;
2441 token
= assignment_expression(token
, &stmt
->expression
);
2442 if (!stmt
->expression
)
2443 unexpected(token
, "expression expected after ','");
2445 token
= expect(token
, ')', "at end of __context__ statement");
2446 return expect(token
, ';', "at end of statement");
2449 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2451 stmt
->type
= STMT_RANGE
;
2452 token
= token
->next
;
2453 token
= expect(token
, '(', "after __range__ statement");
2454 token
= assignment_expression(token
, &stmt
->range_expression
);
2455 token
= expect(token
, ',', "after range expression");
2456 token
= assignment_expression(token
, &stmt
->range_low
);
2457 token
= expect(token
, ',', "after low range");
2458 token
= assignment_expression(token
, &stmt
->range_high
);
2459 token
= expect(token
, ')', "after range statement");
2460 return expect(token
, ';', "after range statement");
2463 static struct token
*handle_label_attributes(struct token
*token
, struct symbol
*label
)
2465 struct decl_state ctx
= { };
2467 token
= handle_attributes(token
, &ctx
);
2468 label
->label_modifiers
= ctx
.ctype
.modifiers
;
2472 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2474 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2477 if (token_type(token
) == TOKEN_IDENT
) {
2478 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2479 if (s
&& s
->op
->statement
)
2480 return s
->op
->statement(token
, stmt
);
2482 if (match_op(token
->next
, ':')) {
2483 struct symbol
*s
= label_symbol(token
, 0);
2484 token
= handle_label_attributes(token
->next
->next
, s
);
2486 sparse_error(stmt
->pos
, "label '%s' redefined", show_ident(s
->ident
));
2487 // skip the label to avoid multiple definitions
2488 return statement(token
, tree
);
2490 stmt
->type
= STMT_LABEL
;
2491 stmt
->label_identifier
= s
;
2492 stmt
->label_scope
= label_scope
;
2493 if (s
->label_scope
) {
2494 if (!is_in_scope(label_scope
, s
->label_scope
))
2495 warn_label_usage(stmt
->pos
, s
->label_pos
, s
->ident
);
2498 if (match_op(token
, '}')) {
2499 warning(token
->pos
, "statement expected after label");
2500 stmt
->label_statement
= alloc_statement(token
->pos
, STMT_NONE
);
2503 return statement(token
, &stmt
->label_statement
);
2507 if (match_op(token
, '{')) {
2508 token
= compound_statement(token
->next
, stmt
);
2509 return expect(token
, '}', "at end of compound statement");
2512 stmt
->type
= STMT_EXPRESSION
;
2513 return expression_statement(token
, &stmt
->expression
);
2516 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2517 static struct token
*label_statement(struct token
*token
)
2519 while (token_type(token
) == TOKEN_IDENT
) {
2520 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2521 /* it's block-scope, but we want label namespace */
2522 bind_symbol_with_scope(sym
, token
->ident
, NS_LABEL
, block_scope
);
2523 fn_local_symbol(sym
);
2524 token
= token
->next
;
2525 if (!match_op(token
, ','))
2527 token
= token
->next
;
2529 return expect(token
, ';', "at end of label declaration");
2532 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2534 int seen_statement
= 0;
2535 while (token_type(token
) == TOKEN_IDENT
&&
2536 token
->ident
== &__label___ident
)
2537 token
= label_statement(token
->next
);
2539 struct statement
* stmt
;
2540 if (eof_token(token
))
2542 if (match_op(token
, '}'))
2544 if (match_ident(token
, &_Static_assert_ident
)) {
2545 token
= parse_static_assert(token
, NULL
);
2548 if (lookup_type(token
)) {
2549 if (seen_statement
) {
2550 warning(token
->pos
, "mixing declarations and code");
2553 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2554 token
= external_declaration(token
, &stmt
->declaration
, NULL
);
2556 seen_statement
= Wdeclarationafterstatement
;
2557 token
= statement(token
, &stmt
);
2559 add_statement(list
, stmt
);
2564 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2566 struct symbol_list
**list
= &fn
->arguments
;
2568 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2569 sym
->ident
= token
->ident
;
2570 token
= token
->next
;
2571 sym
->endpos
= token
->pos
;
2572 sym
->ctype
.base_type
= &incomplete_ctype
;
2573 add_symbol(list
, sym
);
2574 if (!match_op(token
, ',') ||
2575 token_type(token
->next
) != TOKEN_IDENT
||
2576 lookup_type(token
->next
))
2578 token
= token
->next
;
2583 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2585 struct symbol_list
**list
= &fn
->arguments
;
2590 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2592 token
= token
->next
;
2596 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2597 token
= parameter_declaration(token
, sym
);
2598 if (sym
->ctype
.base_type
== &void_ctype
) {
2599 /* Special case: (void) */
2600 if (!*list
&& !sym
->ident
)
2602 warning(token
->pos
, "void parameter");
2604 add_symbol(list
, sym
);
2605 if (!match_op(token
, ','))
2607 token
= token
->next
;
2612 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2614 stmt
->type
= STMT_COMPOUND
;
2615 start_block_scope(token
->pos
);
2616 token
= statement_list(token
, &stmt
->stmts
);
2621 static struct expression
*identifier_expression(struct token
*token
)
2623 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2624 expr
->expr_ident
= token
->ident
;
2628 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2630 int idx_from
, idx_to
;
2631 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2633 idx_from
= const_expression_value(from
);
2636 idx_to
= const_expression_value(to
);
2637 if (idx_to
< idx_from
|| idx_from
< 0)
2638 warning(from
->pos
, "nonsense array initializer index range");
2640 expr
->idx_from
= idx_from
;
2641 expr
->idx_to
= idx_to
;
2645 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2647 int expect_equal
= 0;
2648 struct token
*next
= token
->next
;
2649 struct expression
**tail
= ep
;
2654 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2655 struct expression
*expr
= identifier_expression(token
);
2656 if (Wold_initializer
)
2657 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2658 token
= initializer(&expr
->ident_expression
, next
->next
);
2659 if (expr
->ident_expression
)
2664 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2665 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2666 struct expression
*expr
= identifier_expression(next
);
2668 tail
= &expr
->ident_expression
;
2671 } else if (match_op(token
, '[')) {
2672 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2673 token
= constant_expression(token
->next
, &from
);
2675 sparse_error(token
->pos
, "Expected constant expression");
2678 if (match_op(token
, SPECIAL_ELLIPSIS
))
2679 token
= constant_expression(token
->next
, &to
);
2680 expr
= index_expression(from
, to
);
2682 tail
= &expr
->idx_expression
;
2683 token
= expect(token
, ']', "at end of initializer index");
2690 if (nested
&& !expect_equal
) {
2691 if (!match_op(token
, '='))
2692 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2697 token
= expect(token
, '=', "at end of initializer index");
2699 token
= initializer(tail
, token
);
2705 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2707 struct expression
*expr
;
2710 token
= single_initializer(&expr
, token
);
2713 add_expression(list
, expr
);
2714 if (!match_op(token
, ','))
2716 token
= token
->next
;
2721 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2723 if (match_op(token
, '{')) {
2724 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2726 if (!Wuniversal_initializer
) {
2727 struct token
*next
= token
->next
;
2728 // '{ 0 }' is equivalent to '{ }' except for some
2729 // warnings, like using 0 to initialize a null-pointer.
2730 if (match_token_zero(next
)) {
2731 if (match_op(next
->next
, '}'))
2732 expr
->zero_init
= 1;
2736 token
= initializer_list(&expr
->expr_list
, token
->next
);
2737 return expect(token
, '}', "at end of initializer");
2739 return assignment_expression(token
, tree
);
2742 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2745 sparse_error(sym
->pos
, "no identifier for function argument");
2748 if (sym
->ctype
.base_type
== &incomplete_ctype
) {
2749 sym
->ctype
.base_type
= &int_ctype
;
2751 if (Wimplicit_int
) {
2752 sparse_error(sym
->pos
, "missing type declaration for parameter '%s'",
2753 show_ident(sym
->ident
));
2756 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2759 static int is_syscall(struct symbol
*sym
)
2765 macro
= get_macro_name(sym
->pos
);
2767 (strncmp("SYSCALL_DEFINE", macro
, strlen("SYSCALL_DEFINE")) == 0 ||
2768 strncmp("COMPAT_SYSCALL_DEFINE", macro
, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2771 name
= sym
->ident
->name
;
2773 if (name
&& strncmp(name
, "sys_", 4) ==0)
2776 if (name
&& strncmp(name
, "compat_sys_", 11) == 0)
2783 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2784 struct symbol_list
**list
)
2786 struct symbol_list
**old_symbol_list
;
2787 struct symbol
*base_type
= decl
->ctype
.base_type
;
2788 struct statement
*stmt
, **p
;
2789 struct symbol
*prev
;
2792 old_symbol_list
= function_symbol_list
;
2793 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2794 function_symbol_list
= &decl
->inline_symbol_list
;
2795 p
= &base_type
->inline_stmt
;
2797 function_symbol_list
= &decl
->symbol_list
;
2798 p
= &base_type
->stmt
;
2800 function_computed_target_list
= NULL
;
2801 function_computed_goto_list
= NULL
;
2803 if ((decl
->ctype
.modifiers
& (MOD_EXTERN
|MOD_INLINE
)) == MOD_EXTERN
) {
2804 if (Wexternal_function_has_definition
)
2805 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2807 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2808 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2810 stmt
= start_function(decl
);
2813 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2814 declare_argument(arg
, base_type
);
2815 } END_FOR_EACH_PTR(arg
);
2817 token
= statement_list(token
->next
, &stmt
->stmts
);
2820 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2821 add_symbol(list
, decl
);
2822 else if (is_syscall(decl
)) {
2823 add_symbol(list
, decl
);
2825 printf("parse.c decl: %s\n", decl->ident->name);
2826 char *macro = get_macro_name(decl->pos);
2827 printf("decl macro: %s\n", macro);
2830 check_declaration(decl
);
2831 decl
->definition
= decl
;
2832 prev
= decl
->same_symbol
;
2833 if (prev
&& prev
->definition
) {
2834 warning(decl
->pos
, "multiple definitions for function '%s'",
2835 show_ident(decl
->ident
));
2836 info(prev
->definition
->pos
, " the previous one is here");
2839 rebind_scope(prev
, decl
->scope
);
2840 prev
->definition
= decl
;
2841 prev
= prev
->same_symbol
;
2844 function_symbol_list
= old_symbol_list
;
2845 if (function_computed_goto_list
) {
2846 if (!function_computed_target_list
)
2847 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2849 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2850 stmt
->target_list
= function_computed_target_list
;
2851 } END_FOR_EACH_PTR(stmt
);
2854 return expect(token
, '}', "at end of function");
2857 static void promote_k_r_types(struct symbol
*arg
)
2859 struct symbol
*base
= arg
->ctype
.base_type
;
2860 if (base
&& base
->ctype
.base_type
== &int_type
&& base
->rank
< 0) {
2861 arg
->ctype
.base_type
= &int_ctype
;
2865 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2867 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2870 FOR_EACH_PTR(real_args
, arg
) {
2871 struct symbol
*type
;
2873 /* This is quadratic in the number of arguments. We _really_ don't care */
2874 FOR_EACH_PTR(argtypes
, type
) {
2875 if (type
->ident
== arg
->ident
)
2877 } END_FOR_EACH_PTR(type
);
2878 if (Wimplicit_int
) {
2879 warning(arg
->pos
, "missing type declaration for parameter '%s'",
2880 show_ident(arg
->ident
));
2882 type
= alloc_symbol(arg
->pos
, SYM_NODE
);
2883 type
->ident
= arg
->ident
;
2884 type
->ctype
.base_type
= &int_ctype
;
2887 /* "char" and "short" promote to "int" */
2888 promote_k_r_types(type
);
2890 arg
->ctype
= type
->ctype
;
2891 } END_FOR_EACH_PTR(arg
);
2893 FOR_EACH_PTR(argtypes
, arg
) {
2895 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2896 } END_FOR_EACH_PTR(arg
);
2900 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2901 struct symbol_list
**list
)
2903 struct symbol_list
*args
= NULL
;
2905 if (Wold_style_definition
)
2906 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2909 token
= declaration_list(token
, &args
);
2910 if (!match_op(token
, ';')) {
2911 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2914 token
= token
->next
;
2915 } while (lookup_type(token
));
2917 apply_k_r_types(args
, decl
);
2919 if (!match_op(token
, '{')) {
2920 sparse_error(token
->pos
, "expected function body");
2923 return parse_function_body(token
, decl
, list
);
2926 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2928 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2929 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2930 struct statement
*stmt
;
2932 anon
->ctype
.base_type
= fn
;
2933 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2936 token
= parse_asm_statement(token
, stmt
);
2938 // FIXME: add_symbol(list, anon);
2942 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
,
2943 validate_decl_t validate_decl
)
2945 struct ident
*ident
= NULL
;
2946 struct symbol
*decl
;
2947 struct decl_state ctx
= { .ident
= &ident
};
2949 struct symbol
*base_type
;
2953 if (match_ident(token
, &_Pragma_ident
))
2954 return parse_underscore_Pragma(token
);
2956 /* Top-level inline asm or static assertion? */
2957 if (token_type(token
) == TOKEN_IDENT
) {
2958 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2959 if (s
&& s
->op
->toplevel
)
2960 return s
->op
->toplevel(token
, list
);
2963 /* Parse declaration-specifiers, if any */
2964 token
= declaration_specifiers(token
, &ctx
);
2965 mod
= decl_modifiers(&ctx
);
2966 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2967 /* Just a type declaration? */
2968 if (match_op(token
, ';')) {
2969 apply_modifiers(token
->pos
, &ctx
);
2974 token
= declarator(token
, &ctx
);
2975 token
= handle_asm_name(token
, &ctx
);
2976 token
= handle_attributes(token
, &ctx
);
2977 apply_modifiers(token
->pos
, &ctx
);
2979 decl
->ctype
= ctx
.ctype
;
2980 decl
->ctype
.modifiers
|= mod
;
2981 decl
->endpos
= token
->pos
;
2983 /* Just a type declaration? */
2985 warning(token
->pos
, "missing identifier in declaration");
2986 return expect(token
, ';', "at the end of type declaration");
2989 /* type define declaration? */
2990 is_typedef
= ctx
.storage_class
== MOD_USERTYPE
;
2992 /* Typedefs don't have meaningful storage */
2994 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2996 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2998 base_type
= decl
->ctype
.base_type
;
3001 if (base_type
&& !base_type
->ident
) {
3002 switch (base_type
->type
) {
3007 base_type
->ident
= ident
;
3013 } else if (base_type
&& base_type
->type
== SYM_FN
) {
3014 if (base_type
->ctype
.base_type
== &autotype_ctype
) {
3015 sparse_error(decl
->pos
, "'%s()' has __auto_type return type",
3016 show_ident(decl
->ident
));
3017 base_type
->ctype
.base_type
= &int_ctype
;
3019 if (base_type
->ctype
.base_type
== &incomplete_ctype
) {
3020 warning(decl
->pos
, "'%s()' has implicit return type",
3021 show_ident(decl
->ident
));
3022 base_type
->ctype
.base_type
= &int_ctype
;
3024 /* apply attributes placed after the declarator */
3025 decl
->ctype
.modifiers
|= ctx
.f_modifiers
;
3027 /* K&R argument declaration? */
3028 if (lookup_type(token
))
3029 return parse_k_r_arguments(token
, decl
, list
);
3030 if (match_op(token
, '{'))
3031 return parse_function_body(token
, decl
, list
);
3033 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3034 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3035 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
3036 sparse_error(token
->pos
, "void declaration");
3038 if (base_type
== &incomplete_ctype
) {
3039 warning(decl
->pos
, "'%s' has implicit type", show_ident(decl
->ident
));
3040 decl
->ctype
.base_type
= &int_ctype
;;
3044 if (!is_typedef
&& match_op(token
, '=')) {
3045 struct token
*next
= token
->next
;
3046 token
= initializer(&decl
->initializer
, next
);
3048 sparse_error(token
->pos
, "expression expected before '%s'", show_token(token
));
3052 validate_decl(decl
);
3054 if (decl
->initializer
&& decl
->ctype
.modifiers
& MOD_EXTERN
) {
3055 warning(decl
->pos
, "symbol with external linkage has initializer");
3056 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
3059 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
3060 add_symbol(list
, decl
);
3061 fn_local_symbol(decl
);
3064 check_declaration(decl
);
3065 if (decl
->same_symbol
) {
3066 decl
->definition
= decl
->same_symbol
->definition
;
3067 decl
->op
= decl
->same_symbol
->op
;
3069 // TODO: handle -std=c89 --pedantic
3070 check_duplicates(decl
);
3075 const char *msg
= NULL
;
3076 if (decl
->ctype
.base_type
!= &autotype_ctype
)
3077 msg
= "on non-identifier";
3078 else if (match_op(token
, ','))
3079 msg
= "on declaration list";
3080 else if (!decl
->initializer
)
3081 msg
= "without initializer";
3082 else if (decl
->initializer
->type
== EXPR_SYMBOL
&&
3083 decl
->initializer
->symbol
== decl
)
3084 msg
= "on self-init var";
3086 sparse_error(decl
->pos
, "__auto_type %s", msg
);
3087 decl
->ctype
.base_type
= &bad_ctype
;
3091 if (!match_op(token
, ','))
3094 token
= token
->next
;
3096 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
3098 token
= handle_attributes(token
, &ctx
);
3099 token
= declarator(token
, &ctx
);
3100 token
= handle_asm_name(token
, &ctx
);
3101 token
= handle_attributes(token
, &ctx
);
3102 apply_modifiers(token
->pos
, &ctx
);
3103 decl
->ctype
= ctx
.ctype
;
3104 decl
->ctype
.modifiers
|= mod
;
3105 decl
->endpos
= token
->pos
;
3107 sparse_error(token
->pos
, "expected identifier name in type definition");
3112 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
3114 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
3116 /* Function declarations are automatically extern unless specifically static */
3117 base_type
= decl
->ctype
.base_type
;
3118 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
3119 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
3120 decl
->ctype
.modifiers
|= MOD_EXTERN
;
3123 return expect(token
, ';', "at end of declaration");