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 * Licensed under the Open Software License version 1.1
28 #include "expression.h"
31 static struct symbol_list
**function_symbol_list
;
32 struct symbol_list
*function_computed_target_list
;
33 struct statement_list
*function_computed_goto_list
;
35 static struct token
*statement(struct token
*token
, struct statement
**tree
);
36 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
);
38 typedef struct token
*declarator_t(struct token
*, struct decl_state
*);
40 struct_specifier
, union_specifier
, enum_specifier
,
41 attribute_specifier
, typeof_specifier
, parse_asm_declarator
,
42 typedef_specifier
, inline_specifier
, auto_specifier
,
43 register_specifier
, static_specifier
, extern_specifier
,
44 thread_specifier
, const_qualifier
, volatile_qualifier
;
46 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
47 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
48 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
49 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
50 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
51 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
52 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
53 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
54 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
55 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
56 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
57 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
58 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
59 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
61 typedef struct token
*attr_t(struct token
*, struct symbol
*,
65 attribute_packed
, attribute_aligned
, attribute_modifier
,
66 attribute_address_space
, attribute_context
,
67 attribute_designated_init
,
68 attribute_transparent_union
, ignore_attribute
,
69 attribute_mode
, attribute_force
;
71 typedef struct symbol
*to_mode_t(struct symbol
*);
74 to_QI_mode
, to_HI_mode
, to_SI_mode
, to_DI_mode
, to_TI_mode
, to_word_mode
;
88 Set_Any
= Set_T
| Set_Short
| Set_Long
| Set_Signed
| Set_Unsigned
92 CInt
= 0, CSInt
, CUInt
, CReal
, CChar
, CSChar
, CUChar
96 SNone
= 0, STypedef
, SAuto
, SRegister
, SExtern
, SStatic
, SForced
99 static struct symbol_op typedef_op
= {
101 .declarator
= typedef_specifier
,
104 static struct symbol_op inline_op
= {
106 .declarator
= inline_specifier
,
109 static struct symbol_op auto_op
= {
111 .declarator
= auto_specifier
,
114 static struct symbol_op register_op
= {
116 .declarator
= register_specifier
,
119 static struct symbol_op static_op
= {
121 .declarator
= static_specifier
,
124 static struct symbol_op extern_op
= {
126 .declarator
= extern_specifier
,
129 static struct symbol_op thread_op
= {
131 .declarator
= thread_specifier
,
134 static struct symbol_op const_op
= {
135 .type
= KW_QUALIFIER
,
136 .declarator
= const_qualifier
,
139 static struct symbol_op volatile_op
= {
140 .type
= KW_QUALIFIER
,
141 .declarator
= volatile_qualifier
,
144 static struct symbol_op restrict_op
= {
145 .type
= KW_QUALIFIER
,
148 static struct symbol_op typeof_op
= {
149 .type
= KW_SPECIFIER
,
150 .declarator
= typeof_specifier
,
155 static struct symbol_op attribute_op
= {
156 .type
= KW_ATTRIBUTE
,
157 .declarator
= attribute_specifier
,
160 static struct symbol_op struct_op
= {
161 .type
= KW_SPECIFIER
,
162 .declarator
= struct_specifier
,
167 static struct symbol_op union_op
= {
168 .type
= KW_SPECIFIER
,
169 .declarator
= union_specifier
,
174 static struct symbol_op enum_op
= {
175 .type
= KW_SPECIFIER
,
176 .declarator
= enum_specifier
,
181 static struct symbol_op spec_op
= {
182 .type
= KW_SPECIFIER
| KW_EXACT
,
187 static struct symbol_op char_op
= {
188 .type
= KW_SPECIFIER
,
189 .test
= Set_T
|Set_Long
|Set_Short
,
190 .set
= Set_T
|Set_Char
,
194 static struct symbol_op int_op
= {
195 .type
= KW_SPECIFIER
,
197 .set
= Set_T
|Set_Int
,
200 static struct symbol_op double_op
= {
201 .type
= KW_SPECIFIER
,
202 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Vlong
,
203 .set
= Set_T
|Set_Double
,
207 static struct symbol_op float_op
= {
208 .type
= KW_SPECIFIER
| KW_SHORT
,
209 .test
= Set_T
|Set_Signed
|Set_Unsigned
|Set_Short
|Set_Long
,
210 .set
= Set_T
|Set_Float
,
214 static struct symbol_op short_op
= {
215 .type
= KW_SPECIFIER
| KW_SHORT
,
216 .test
= Set_S
|Set_Char
|Set_Float
|Set_Double
|Set_Long
|Set_Short
,
220 static struct symbol_op signed_op
= {
221 .type
= KW_SPECIFIER
,
222 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
227 static struct symbol_op unsigned_op
= {
228 .type
= KW_SPECIFIER
,
229 .test
= Set_S
|Set_Float
|Set_Double
|Set_Signed
|Set_Unsigned
,
234 static struct symbol_op long_op
= {
235 .type
= KW_SPECIFIER
| KW_LONG
,
236 .test
= Set_S
|Set_Char
|Set_Float
|Set_Short
|Set_Vlong
,
240 static struct symbol_op if_op
= {
241 .statement
= parse_if_statement
,
244 static struct symbol_op return_op
= {
245 .statement
= parse_return_statement
,
248 static struct symbol_op loop_iter_op
= {
249 .statement
= parse_loop_iterator
,
252 static struct symbol_op default_op
= {
253 .statement
= parse_default_statement
,
256 static struct symbol_op case_op
= {
257 .statement
= parse_case_statement
,
260 static struct symbol_op switch_op
= {
261 .statement
= parse_switch_statement
,
264 static struct symbol_op for_op
= {
265 .statement
= parse_for_statement
,
268 static struct symbol_op while_op
= {
269 .statement
= parse_while_statement
,
272 static struct symbol_op do_op
= {
273 .statement
= parse_do_statement
,
276 static struct symbol_op goto_op
= {
277 .statement
= parse_goto_statement
,
280 static struct symbol_op __context___op
= {
281 .statement
= parse_context_statement
,
284 static struct symbol_op range_op
= {
285 .statement
= parse_range_statement
,
288 static struct symbol_op asm_op
= {
290 .declarator
= parse_asm_declarator
,
291 .statement
= parse_asm_statement
,
292 .toplevel
= toplevel_asm_declaration
,
295 static struct symbol_op packed_op
= {
296 .attribute
= attribute_packed
,
299 static struct symbol_op aligned_op
= {
300 .attribute
= attribute_aligned
,
303 static struct symbol_op attr_mod_op
= {
304 .attribute
= attribute_modifier
,
307 static struct symbol_op attr_force_op
= {
308 .attribute
= attribute_force
,
311 static struct symbol_op address_space_op
= {
312 .attribute
= attribute_address_space
,
315 static struct symbol_op mode_op
= {
316 .attribute
= attribute_mode
,
319 static struct symbol_op context_op
= {
320 .attribute
= attribute_context
,
323 static struct symbol_op designated_init_op
= {
324 .attribute
= attribute_designated_init
,
327 static struct symbol_op transparent_union_op
= {
328 .attribute
= attribute_transparent_union
,
331 static struct symbol_op ignore_attr_op
= {
332 .attribute
= ignore_attribute
,
335 static struct symbol_op mode_QI_op
= {
337 .to_mode
= to_QI_mode
340 static struct symbol_op mode_HI_op
= {
342 .to_mode
= to_HI_mode
345 static struct symbol_op mode_SI_op
= {
347 .to_mode
= to_SI_mode
350 static struct symbol_op mode_DI_op
= {
352 .to_mode
= to_DI_mode
355 static struct symbol_op mode_TI_op
= {
357 .to_mode
= to_TI_mode
360 static struct symbol_op mode_word_op
= {
362 .to_mode
= to_word_mode
365 static struct init_keyword
{
368 unsigned long modifiers
;
369 struct symbol_op
*op
;
371 } keyword_table
[] = {
372 /* Type qualifiers */
373 { "const", NS_TYPEDEF
, .op
= &const_op
},
374 { "__const", NS_TYPEDEF
, .op
= &const_op
},
375 { "__const__", NS_TYPEDEF
, .op
= &const_op
},
376 { "volatile", NS_TYPEDEF
, .op
= &volatile_op
},
377 { "__volatile", NS_TYPEDEF
, .op
= &volatile_op
},
378 { "__volatile__", NS_TYPEDEF
, .op
= &volatile_op
},
381 { "typedef", NS_TYPEDEF
, .op
= &typedef_op
},
383 /* Type specifiers */
384 { "void", NS_TYPEDEF
, .type
= &void_ctype
, .op
= &spec_op
},
385 { "char", NS_TYPEDEF
, .op
= &char_op
},
386 { "short", NS_TYPEDEF
, .op
= &short_op
},
387 { "int", NS_TYPEDEF
, .op
= &int_op
},
388 { "long", NS_TYPEDEF
, .op
= &long_op
},
389 { "float", NS_TYPEDEF
, .op
= &float_op
},
390 { "double", NS_TYPEDEF
, .op
= &double_op
},
391 { "signed", NS_TYPEDEF
, .op
= &signed_op
},
392 { "__signed", NS_TYPEDEF
, .op
= &signed_op
},
393 { "__signed__", NS_TYPEDEF
, .op
= &signed_op
},
394 { "unsigned", NS_TYPEDEF
, .op
= &unsigned_op
},
395 { "_Bool", NS_TYPEDEF
, .type
= &bool_ctype
, .op
= &spec_op
},
397 /* Predeclared types */
398 { "__builtin_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
399 { "__builtin_ms_va_list", NS_TYPEDEF
, .type
= &ptr_ctype
, .op
= &spec_op
},
400 { "__int128_t", NS_TYPEDEF
, .type
= &lllong_ctype
, .op
= &spec_op
},
401 { "__uint128_t",NS_TYPEDEF
, .type
= &ulllong_ctype
, .op
= &spec_op
},
404 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
405 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
406 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
408 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
409 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
411 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
412 { "union", NS_TYPEDEF
, .op
= &union_op
},
413 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
415 { "inline", NS_TYPEDEF
, .op
= &inline_op
},
416 { "__inline", NS_TYPEDEF
, .op
= &inline_op
},
417 { "__inline__", NS_TYPEDEF
, .op
= &inline_op
},
419 /* Ignored for now.. */
420 { "restrict", NS_TYPEDEF
, .op
= &restrict_op
},
421 { "__restrict", NS_TYPEDEF
, .op
= &restrict_op
},
424 { "auto", NS_TYPEDEF
, .op
= &auto_op
},
425 { "register", NS_TYPEDEF
, .op
= ®ister_op
},
426 { "static", NS_TYPEDEF
, .op
= &static_op
},
427 { "extern", NS_TYPEDEF
, .op
= &extern_op
},
428 { "__thread", NS_TYPEDEF
, .op
= &thread_op
},
431 { "if", NS_KEYWORD
, .op
= &if_op
},
432 { "return", NS_KEYWORD
, .op
= &return_op
},
433 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
434 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
435 { "default", NS_KEYWORD
, .op
= &default_op
},
436 { "case", NS_KEYWORD
, .op
= &case_op
},
437 { "switch", NS_KEYWORD
, .op
= &switch_op
},
438 { "for", NS_KEYWORD
, .op
= &for_op
},
439 { "while", NS_KEYWORD
, .op
= &while_op
},
440 { "do", NS_KEYWORD
, .op
= &do_op
},
441 { "goto", NS_KEYWORD
, .op
= &goto_op
},
442 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
443 { "__range__", NS_KEYWORD
, .op
= &range_op
},
444 { "asm", NS_KEYWORD
, .op
= &asm_op
},
445 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
446 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
449 { "packed", NS_KEYWORD
, .op
= &packed_op
},
450 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
451 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
452 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
453 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
454 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
455 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
456 { "force", NS_KEYWORD
, .op
= &attr_force_op
},
457 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
458 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
459 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
460 { "mode", NS_KEYWORD
, .op
= &mode_op
},
461 { "context", NS_KEYWORD
, .op
= &context_op
},
462 { "designated_init", NS_KEYWORD
, .op
= &designated_init_op
},
463 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
464 { "noreturn", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
465 { "__noreturn__", NS_KEYWORD
, MOD_NORETURN
, .op
= &attr_mod_op
},
466 { "pure", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
467 {"__pure__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
468 {"const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
469 {"__const", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
470 {"__const__", NS_KEYWORD
, MOD_PURE
, .op
= &attr_mod_op
},
472 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
473 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
474 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
475 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
476 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
477 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
478 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
479 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
480 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
481 { "TI", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
482 { "__TI__", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
483 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
484 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
487 const char *ignored_attributes
[] = {
512 "externally_visible",
513 "__externally_visible__",
539 "__ms_hook_prologue__",
542 "no_instrument_function",
543 "__no_instrument_function__",
563 "__syscall_linkage__",
573 "warn_unused_result",
574 "__warn_unused_result__",
582 void init_parser(int stream
)
585 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
586 struct init_keyword
*ptr
= keyword_table
+ i
;
587 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
588 sym
->ident
->keyword
= 1;
589 if (ptr
->ns
== NS_TYPEDEF
)
590 sym
->ident
->reserved
= 1;
591 sym
->ctype
.modifiers
= ptr
->modifiers
;
592 sym
->ctype
.base_type
= ptr
->type
;
596 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
597 const char * name
= ignored_attributes
[i
];
598 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
600 sym
->ident
->keyword
= 1;
601 sym
->op
= &ignore_attr_op
;
606 // Add a symbol to the list of function-local symbols
607 static void fn_local_symbol(struct symbol
*sym
)
609 if (function_symbol_list
)
610 add_symbol(function_symbol_list
, sym
);
613 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
618 if (token_type(token
) != TOKEN_IDENT
)
621 va_start(args
, token
);
623 next
= va_arg(args
, struct ident
*);
624 } while (next
&& token
->ident
!= next
);
627 return next
&& token
->ident
== next
;
631 struct statement
*alloc_statement(struct position pos
, int type
)
633 struct statement
*stmt
= __alloc_statement(0);
639 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
641 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
643 struct symbol
*ctype
;
646 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
648 sparse_error(pos
, "don't know how to apply mode to %s",
649 show_typename(ctx
->ctype
.base_type
));
651 ctx
->ctype
.base_type
= ctype
;
655 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
657 struct symbol
*sym
= alloc_symbol(pos
, type
);
659 sym
->ctype
.base_type
= ctype
->base_type
;
660 sym
->ctype
.modifiers
= ctype
->modifiers
;
662 ctype
->base_type
= sym
;
663 ctype
->modifiers
= 0;
668 * NOTE! NS_LABEL is not just a different namespace,
669 * it also ends up using function scope instead of the
670 * regular symbol scope.
672 struct symbol
*label_symbol(struct token
*token
)
674 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
676 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
677 bind_symbol(sym
, token
->ident
, NS_LABEL
);
678 fn_local_symbol(sym
);
683 static struct token
*struct_union_enum_specifier(enum type type
,
684 struct token
*token
, struct decl_state
*ctx
,
685 struct token
*(*parse
)(struct token
*, struct symbol
*))
688 struct position
*repos
;
690 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
691 if (token_type(token
) == TOKEN_IDENT
) {
692 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
694 (is_outer_scope(sym
->scope
) &&
695 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
696 // Either a new symbol, or else an out-of-scope
697 // symbol being redefined.
698 sym
= alloc_symbol(token
->pos
, type
);
699 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
701 if (sym
->type
!= type
)
702 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
703 ctx
->ctype
.base_type
= sym
;
706 if (match_op(token
, '{')) {
707 // The following test is actually wrong for empty
708 // structs, but (1) they are not C99, (2) gcc does
709 // the same thing, and (3) it's easier.
710 if (sym
->symbol_list
)
711 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
713 token
= parse(token
->next
, sym
);
714 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
716 // Mark the structure as needing re-examination
718 sym
->endpos
= token
->pos
;
723 // private struct/union/enum type
724 if (!match_op(token
, '{')) {
725 sparse_error(token
->pos
, "expected declaration");
726 ctx
->ctype
.base_type
= &bad_ctype
;
730 sym
= alloc_symbol(token
->pos
, type
);
731 token
= parse(token
->next
, sym
);
732 ctx
->ctype
.base_type
= sym
;
733 token
= expect(token
, '}', "at end of specifier");
734 sym
->endpos
= token
->pos
;
739 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
741 struct symbol
*field
, *last
= NULL
;
743 res
= struct_declaration_list(token
, &sym
->symbol_list
);
744 FOR_EACH_PTR(sym
->symbol_list
, field
) {
746 struct symbol
*base
= field
->ctype
.base_type
;
747 if (base
&& base
->type
== SYM_BITFIELD
)
751 last
->next_subobject
= field
;
753 } END_FOR_EACH_PTR(field
);
757 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
759 return struct_declaration_list(token
, &sym
->symbol_list
);
762 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
764 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
767 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
769 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
775 unsigned long long y
;
778 static void upper_boundary(Num
*n
, Num
*v
)
790 static void lower_boundary(Num
*n
, Num
*v
)
802 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
804 int shift
= type
->bit_size
;
805 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
809 if (upper
->x
== 0 && upper
->y
>> shift
)
811 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
816 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
818 if (s1
->bit_size
< s2
->bit_size
) {
820 } else if (s1
->bit_size
== s2
->bit_size
) {
821 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
824 if (s1
->bit_size
< bits_in_int
)
829 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
833 FOR_EACH_PTR(list
, sym
) {
834 struct expression
*expr
= sym
->initializer
;
835 struct symbol
*ctype
;
836 if (expr
->type
!= EXPR_VALUE
)
839 if (ctype
->bit_size
== base_type
->bit_size
)
841 cast_value(expr
, base_type
, expr
, ctype
);
842 } END_FOR_EACH_PTR(sym
);
845 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
847 unsigned long long lastval
= 0;
848 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
849 Num upper
= {-1, 0}, lower
= {1, 0};
851 parent
->examined
= 1;
852 parent
->ctype
.base_type
= &int_ctype
;
853 while (token_type(token
) == TOKEN_IDENT
) {
854 struct expression
*expr
= NULL
;
855 struct token
*next
= token
->next
;
858 if (match_op(next
, '=')) {
859 next
= constant_expression(next
->next
, &expr
);
860 lastval
= get_expression_value(expr
);
862 if (expr
&& expr
->ctype
)
866 } else if (is_int_type(ctype
)) {
869 error_die(token
->pos
, "can't increment the last enum member");
873 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
874 expr
->value
= lastval
;
878 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
879 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
880 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
881 sym
->initializer
= expr
;
882 sym
->enum_member
= 1;
883 sym
->ctype
.base_type
= parent
;
884 add_ptr_list(&parent
->symbol_list
, sym
);
886 if (base_type
!= &bad_ctype
) {
887 if (ctype
->type
== SYM_NODE
)
888 ctype
= ctype
->ctype
.base_type
;
889 if (ctype
->type
== SYM_ENUM
) {
893 ctype
= ctype
->ctype
.base_type
;
897 * - if all enums are of the same type, then
898 * the base_type is that type (two first
900 * - if enums are of different types, they
901 * all have to be integer types, and the
902 * base type is at least "int_ctype".
903 * - otherwise the base_type is "bad_ctype".
907 } else if (ctype
== base_type
) {
909 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
910 base_type
= bigger_enum_type(base_type
, ctype
);
912 base_type
= &bad_ctype
;
913 parent
->ctype
.base_type
= base_type
;
915 if (is_int_type(base_type
)) {
916 Num v
= {.y
= lastval
};
917 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
919 else if ((long long)lastval
>= 0)
923 upper_boundary(&upper
, &v
);
924 lower_boundary(&lower
, &v
);
928 sym
->endpos
= token
->pos
;
930 if (!match_op(token
, ','))
935 sparse_error(token
->pos
, "bad enum definition");
936 base_type
= &bad_ctype
;
938 else if (!is_int_type(base_type
))
939 base_type
= base_type
;
940 else if (type_is_ok(base_type
, &upper
, &lower
))
941 base_type
= base_type
;
942 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
943 base_type
= &int_ctype
;
944 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
945 base_type
= &uint_ctype
;
946 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
947 base_type
= &long_ctype
;
948 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
949 base_type
= &ulong_ctype
;
950 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
951 base_type
= &llong_ctype
;
952 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
953 base_type
= &ullong_ctype
;
955 base_type
= &bad_ctype
;
956 parent
->ctype
.base_type
= base_type
;
957 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
958 parent
->examined
= 0;
960 cast_enum_list(parent
->symbol_list
, base_type
);
965 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
967 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
968 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
970 if (!ctype
->base_type
)
971 ctype
->base_type
= &incomplete_ctype
;
976 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
978 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
982 if (!match_op(token
, '(')) {
983 sparse_error(token
->pos
, "expected '(' after typeof");
986 if (lookup_type(token
->next
)) {
987 token
= typename(token
->next
, &sym
, NULL
);
988 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
989 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
991 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
992 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
994 typeof_sym
->endpos
= token
->pos
;
995 if (!typeof_sym
->initializer
) {
996 sparse_error(token
->pos
, "expected expression after the '(' token");
997 typeof_sym
= &bad_ctype
;
999 ctx
->ctype
.base_type
= typeof_sym
;
1001 return expect(token
, ')', "after typeof");
1004 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1006 struct expression
*expr
= NULL
;
1007 if (match_op(token
, '('))
1008 token
= parens_expression(token
, &expr
, "in attribute");
1012 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1014 if (!ctx
->ctype
.alignment
)
1015 ctx
->ctype
.alignment
= 1;
1019 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1021 int alignment
= max_alignment
;
1022 struct expression
*expr
= NULL
;
1024 if (match_op(token
, '(')) {
1025 token
= parens_expression(token
, &expr
, "in attribute");
1027 alignment
= const_expression_value(expr
);
1029 if (alignment
& (alignment
-1)) {
1030 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1032 } else if (alignment
> ctx
->ctype
.alignment
)
1033 ctx
->ctype
.alignment
= alignment
;
1037 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1039 if (ctx
->modifiers
& qual
)
1040 warning(*pos
, "duplicate %s", modifier_string(qual
));
1041 ctx
->modifiers
|= qual
;
1044 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1046 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1050 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1052 struct expression
*expr
= NULL
;
1054 token
= expect(token
, '(', "after address_space attribute");
1055 token
= conditional_expression(token
, &expr
);
1057 as
= const_expression_value(expr
);
1058 if (Waddress_space
&& as
)
1061 token
= expect(token
, ')', "after address_space attribute");
1065 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1067 if (ctype
->ctype
.base_type
!= &int_type
)
1069 if (ctype
== &char_ctype
)
1071 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1075 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1077 if (ctype
->ctype
.base_type
!= &int_type
)
1079 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1083 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1085 if (ctype
->ctype
.base_type
!= &int_type
)
1087 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1091 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1093 if (ctype
->ctype
.base_type
!= &int_type
)
1095 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1099 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1101 if (ctype
->ctype
.base_type
!= &int_type
)
1103 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1107 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1109 if (ctype
->ctype
.base_type
!= &int_type
)
1111 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1115 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1117 token
= expect(token
, '(', "after mode attribute");
1118 if (token_type(token
) == TOKEN_IDENT
) {
1119 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1120 if (mode
&& mode
->op
->type
== KW_MODE
)
1121 ctx
->mode
= mode
->op
;
1123 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1124 token
= token
->next
;
1126 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1127 token
= expect(token
, ')', "after mode attribute");
1131 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1133 struct context
*context
= alloc_context();
1134 struct expression
*args
[3];
1137 token
= expect(token
, '(', "after context attribute");
1138 while (!match_op(token
, ')')) {
1139 struct expression
*expr
= NULL
;
1140 token
= conditional_expression(token
, &expr
);
1144 args
[argc
++] = expr
;
1145 if (!match_op(token
, ','))
1147 token
= token
->next
;
1152 sparse_error(token
->pos
, "expected context input/output values");
1155 context
->in
= get_expression_value(args
[0]);
1158 context
->in
= get_expression_value(args
[0]);
1159 context
->out
= get_expression_value(args
[1]);
1162 context
->context
= args
[0];
1163 context
->in
= get_expression_value(args
[1]);
1164 context
->out
= get_expression_value(args
[2]);
1169 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1171 token
= expect(token
, ')', "after context attribute");
1175 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1177 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1178 ctx
->ctype
.base_type
->designated_init
= 1;
1180 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1184 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1186 if (Wtransparent_union
)
1187 warning(token
->pos
, "ignoring attribute __transparent_union__");
1191 static struct token
*recover_unknown_attribute(struct token
*token
)
1193 struct expression
*expr
= NULL
;
1195 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1196 token
= token
->next
;
1197 if (match_op(token
, '('))
1198 token
= parens_expression(token
, &expr
, "in attribute");
1202 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1204 token
= expect(token
, '(', "after attribute");
1205 token
= expect(token
, '(', "after attribute");
1208 struct ident
*attribute_name
;
1209 struct symbol
*attr
;
1211 if (eof_token(token
))
1213 if (match_op(token
, ';'))
1215 if (token_type(token
) != TOKEN_IDENT
)
1217 attribute_name
= token
->ident
;
1218 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1219 if (attr
&& attr
->op
->attribute
)
1220 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1222 token
= recover_unknown_attribute(token
);
1224 if (!match_op(token
, ','))
1226 token
= token
->next
;
1229 token
= expect(token
, ')', "after attribute");
1230 token
= expect(token
, ')', "after attribute");
1234 static const char *storage_class
[] =
1236 [STypedef
] = "typedef",
1238 [SExtern
] = "extern",
1239 [SStatic
] = "static",
1240 [SRegister
] = "register",
1241 [SForced
] = "[force]"
1244 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1246 static unsigned long mod
[] =
1249 [SExtern
] = MOD_EXTERN
,
1250 [SStatic
] = MOD_STATIC
,
1251 [SRegister
] = MOD_REGISTER
1253 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1254 | (ctx
->is_tls
? MOD_TLS
: 0);
1257 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1259 /* __thread can be used alone, or with extern or static */
1260 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1261 sparse_error(*pos
, "__thread can only be used alone, or with "
1262 "extern or static");
1266 if (!ctx
->storage_class
) {
1267 ctx
->storage_class
= class;
1270 if (ctx
->storage_class
== class)
1271 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1273 sparse_error(*pos
, "multiple storage classes");
1276 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1278 set_storage_class(&next
->pos
, ctx
, STypedef
);
1282 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1284 set_storage_class(&next
->pos
, ctx
, SAuto
);
1288 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1290 set_storage_class(&next
->pos
, ctx
, SRegister
);
1294 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1296 set_storage_class(&next
->pos
, ctx
, SStatic
);
1300 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1302 set_storage_class(&next
->pos
, ctx
, SExtern
);
1306 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1308 /* This GCC extension can be used alone, or with extern or static */
1309 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1310 || ctx
->storage_class
== SExtern
) {
1313 sparse_error(next
->pos
, "__thread can only be used alone, or "
1314 "with extern or static");
1320 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1322 set_storage_class(&token
->pos
, ctx
, SForced
);
1326 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1332 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1334 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1338 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1340 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1344 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1346 unsigned long mod
= thistype
->modifiers
;
1349 apply_qualifier(&pos
, ctype
, mod
);
1352 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1353 (struct ptr_list
**)&ctype
->contexts
);
1356 if (thistype
->alignment
> ctype
->alignment
)
1357 ctype
->alignment
= thistype
->alignment
;
1361 ctype
->as
= thistype
->as
;
1364 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1367 if (what
& (Set_S
| Set_T
))
1369 if (what
& Set_Char
)
1371 else if (what
& Set_Double
)
1373 else if (what
& Set_Float
)
1375 else if (what
& Set_Signed
)
1377 else if (what
& Set_Unsigned
)
1379 else if (what
& Set_Short
)
1381 else if (what
& Set_Long
)
1385 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1386 old
, show_ident(new));
1390 sparse_error(pos
, "two or more data types in declaration specifiers");
1393 static struct symbol
* const int_types
[] =
1394 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1395 static struct symbol
* const signed_types
[] =
1396 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1398 static struct symbol
* const unsigned_types
[] =
1399 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1401 static struct symbol
* const real_types
[] =
1402 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1403 static struct symbol
* const char_types
[] =
1404 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1405 static struct symbol
* const * const types
[] = {
1406 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1407 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1410 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1412 return types
[want_unsigned
? CUInt
: CInt
][size
];
1415 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1417 while (token_type(t
) == TOKEN_IDENT
) {
1418 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1421 if (s
->type
!= SYM_KEYWORD
)
1423 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1426 if (s
->op
->declarator
)
1427 t
= s
->op
->declarator(t
, ctx
);
1432 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1438 while (token_type(token
) == TOKEN_IDENT
) {
1439 struct symbol
*s
= lookup_symbol(token
->ident
,
1440 NS_TYPEDEF
| NS_SYMBOL
);
1441 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1443 if (s
->type
!= SYM_KEYWORD
) {
1446 seen
|= Set_S
| Set_T
;
1447 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1448 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1449 token
= token
->next
;
1452 if (s
->op
->type
& KW_SPECIFIER
) {
1453 if (seen
& s
->op
->test
) {
1454 specifier_conflict(token
->pos
,
1460 class += s
->op
->class;
1461 if (s
->op
->type
& KW_SHORT
) {
1463 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1464 if (class == CReal
) {
1465 specifier_conflict(token
->pos
,
1473 token
= token
->next
;
1474 if (s
->op
->declarator
)
1475 token
= s
->op
->declarator(token
, ctx
);
1476 if (s
->op
->type
& KW_EXACT
) {
1477 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1478 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1482 if (!(seen
& Set_S
)) { /* not set explicitly? */
1483 struct symbol
*base
= &incomplete_ctype
;
1485 base
= types
[class][size
];
1486 ctx
->ctype
.base_type
= base
;
1489 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1490 struct symbol
*type
;
1491 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1492 if (!is_int_type(ctx
->ctype
.base_type
)) {
1493 sparse_error(token
->pos
, "invalid modifier");
1496 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1497 *type
= *ctx
->ctype
.base_type
;
1498 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1499 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1500 type
->type
= SYM_RESTRICT
;
1501 ctx
->ctype
.base_type
= type
;
1502 create_fouled(type
);
1507 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1509 struct expression
*expr
= NULL
;
1511 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1512 token
= token
->next
;
1513 token
= parse_expression(token
, &expr
);
1514 sym
->array_size
= expr
;
1518 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1519 static struct token
*identifier_list(struct token
*, struct symbol
*);
1520 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1522 static struct token
*skip_attribute(struct token
*token
)
1524 token
= token
->next
;
1525 if (match_op(token
, '(')) {
1527 token
= token
->next
;
1528 while (depth
&& !eof_token(token
)) {
1529 if (token_type(token
) == TOKEN_SPECIAL
) {
1530 if (token
->special
== '(')
1532 else if (token
->special
== ')')
1535 token
= token
->next
;
1541 static struct token
*skip_attributes(struct token
*token
)
1543 struct symbol
*keyword
;
1545 if (token_type(token
) != TOKEN_IDENT
)
1547 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1548 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1550 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1552 token
= expect(token
->next
, '(', "after attribute");
1553 token
= expect(token
, '(', "after attribute");
1555 if (eof_token(token
))
1557 if (match_op(token
, ';'))
1559 if (token_type(token
) != TOKEN_IDENT
)
1561 token
= skip_attribute(token
);
1562 if (!match_op(token
, ','))
1564 token
= token
->next
;
1566 token
= expect(token
, ')', "after attribute");
1567 token
= expect(token
, ')', "after attribute");
1572 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1574 struct symbol
*keyword
;
1576 if (token_type(token
) != TOKEN_IDENT
)
1578 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1579 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1581 if (!(keyword
->op
->type
& keywords
))
1583 token
= keyword
->op
->declarator(token
->next
, ctx
);
1584 keywords
&= KW_ATTRIBUTE
;
1589 static int is_nested(struct token
*token
, struct token
**p
,
1590 int prefer_abstract
)
1593 * This can be either a parameter list or a grouping.
1594 * For the direct (non-abstract) case, we know if must be
1595 * a parameter list if we already saw the identifier.
1596 * For the abstract case, we know if must be a parameter
1597 * list if it is empty or starts with a type.
1599 struct token
*next
= token
->next
;
1601 *p
= next
= skip_attributes(next
);
1603 if (token_type(next
) == TOKEN_IDENT
) {
1604 if (lookup_type(next
))
1605 return !prefer_abstract
;
1609 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1616 Empty
, K_R
, Proto
, Bad_Func
,
1619 static enum kind
which_func(struct token
*token
,
1621 int prefer_abstract
)
1623 struct token
*next
= token
->next
;
1625 if (token_type(next
) == TOKEN_IDENT
) {
1626 if (lookup_type(next
))
1628 /* identifier list not in definition; complain */
1629 if (prefer_abstract
)
1631 "identifier list not in definition");
1635 if (token_type(next
) != TOKEN_SPECIAL
)
1638 if (next
->special
== ')') {
1639 /* don't complain about those */
1640 if (!n
|| match_op(next
->next
, ';'))
1643 "non-ANSI function declaration of function '%s'",
1648 if (next
->special
== SPECIAL_ELLIPSIS
) {
1650 "variadic functions must have one named argument");
1657 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1659 struct ctype
*ctype
= &ctx
->ctype
;
1661 struct ident
**p
= ctx
->ident
;
1663 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1664 *ctx
->ident
= token
->ident
;
1665 token
= token
->next
;
1666 } else if (match_op(token
, '(') &&
1667 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1668 struct symbol
*base_type
= ctype
->base_type
;
1669 if (token
->next
!= next
)
1670 next
= handle_attributes(token
->next
, ctx
,
1672 token
= declarator(next
, ctx
);
1673 token
= expect(token
, ')', "in nested declarator");
1674 while (ctype
->base_type
!= base_type
)
1675 ctype
= &ctype
->base_type
->ctype
;
1679 if (match_op(token
, '(')) {
1680 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1682 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1683 token
= token
->next
;
1685 token
= identifier_list(token
, fn
);
1686 else if (kind
== Proto
)
1687 token
= parameter_type_list(token
, fn
);
1688 token
= expect(token
, ')', "in function declarator");
1689 fn
->endpos
= token
->pos
;
1693 while (match_op(token
, '[')) {
1694 struct symbol
*array
;
1695 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1696 token
= abstract_array_declarator(token
->next
, array
);
1697 token
= expect(token
, ']', "in abstract_array_declarator");
1698 array
->endpos
= token
->pos
;
1699 ctype
= &array
->ctype
;
1704 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1706 while (match_op(token
,'*')) {
1707 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1708 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1709 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1710 ptr
->ctype
.as
= ctx
->ctype
.as
;
1711 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1712 ctx
->ctype
.modifiers
= 0;
1713 ctx
->ctype
.base_type
= ptr
;
1715 ctx
->ctype
.contexts
= NULL
;
1716 ctx
->ctype
.alignment
= 0;
1718 token
= handle_qualifiers(token
->next
, ctx
);
1719 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1724 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1726 token
= pointer(token
, ctx
);
1727 return direct_declarator(token
, ctx
);
1730 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1732 struct ctype
*ctype
= &ctx
->ctype
;
1733 struct expression
*expr
;
1734 struct symbol
*bitfield
;
1737 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1738 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1739 show_typename(ctype
->base_type
));
1740 // Parse this to recover gracefully.
1741 return conditional_expression(token
->next
, &expr
);
1744 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1745 token
= conditional_expression(token
->next
, &expr
);
1746 width
= const_expression_value(expr
);
1747 bitfield
->bit_size
= width
;
1749 if (width
< 0 || width
> INT_MAX
) {
1750 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1752 } else if (*ctx
->ident
&& width
== 0) {
1753 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1754 show_ident(*ctx
->ident
));
1756 } else if (*ctx
->ident
) {
1757 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1758 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1759 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1760 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1761 // Valid values are either {-1;0} or {0}, depending on integer
1762 // representation. The latter makes for very efficient code...
1763 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1765 if (Wdefault_bitfield_sign
&&
1766 bitfield_type
->type
!= SYM_ENUM
&&
1767 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1769 // The sign of bitfields is unspecified by default.
1770 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1773 bitfield
->bit_size
= width
;
1774 bitfield
->endpos
= token
->pos
;
1778 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1780 struct decl_state ctx
= {.prefer_abstract
= 0};
1784 token
= declaration_specifiers(token
, &ctx
);
1785 mod
= storage_modifiers(&ctx
);
1788 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1789 ctx
.ident
= &decl
->ident
;
1791 token
= declarator(token
, &ctx
);
1792 if (match_op(token
, ':'))
1793 token
= handle_bitfield(token
, &ctx
);
1795 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1796 apply_modifiers(token
->pos
, &ctx
);
1798 decl
->ctype
= ctx
.ctype
;
1799 decl
->ctype
.modifiers
|= mod
;
1800 decl
->endpos
= token
->pos
;
1801 add_symbol(list
, decl
);
1802 if (!match_op(token
, ','))
1804 token
= token
->next
;
1810 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1812 while (!match_op(token
, '}')) {
1813 if (!match_op(token
, ';'))
1814 token
= declaration_list(token
, list
);
1815 if (!match_op(token
, ';')) {
1816 sparse_error(token
->pos
, "expected ; at end of declaration");
1819 token
= token
->next
;
1824 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1826 struct decl_state ctx
= {.prefer_abstract
= 1};
1828 token
= declaration_specifiers(token
, &ctx
);
1829 ctx
.ident
= &sym
->ident
;
1830 token
= declarator(token
, &ctx
);
1831 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1832 apply_modifiers(token
->pos
, &ctx
);
1833 sym
->ctype
= ctx
.ctype
;
1834 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1835 sym
->endpos
= token
->pos
;
1839 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1841 struct decl_state ctx
= {.prefer_abstract
= 1};
1843 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1845 token
= declaration_specifiers(token
, &ctx
);
1846 token
= declarator(token
, &ctx
);
1847 apply_modifiers(token
->pos
, &ctx
);
1848 sym
->ctype
= ctx
.ctype
;
1849 sym
->endpos
= token
->pos
;
1850 class = ctx
.storage_class
;
1853 if (class == SForced
) {
1859 warning(sym
->pos
, "storage class in typename (%s %s)",
1860 storage_class
[class], show_typename(sym
));
1864 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1866 token
= parse_expression(token
, tree
);
1867 return expect(token
, ';', "at end of statement");
1870 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1871 struct expression_list
**inout
)
1873 struct expression
*expr
;
1875 /* Allow empty operands */
1876 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1879 struct ident
*ident
= NULL
;
1880 if (match_op(token
->next
, '[') &&
1881 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1882 match_op(token
->next
->next
->next
, ']')) {
1883 ident
= token
->next
->next
->ident
;
1884 token
= token
->next
->next
->next
;
1886 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1887 token
= primary_expression(token
->next
, &expr
);
1888 add_expression(inout
, expr
);
1889 token
= parens_expression(token
, &expr
, "in asm parameter");
1890 add_expression(inout
, expr
);
1891 } while (match_op(token
, ','));
1895 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1896 struct expression_list
**clobbers
)
1898 struct expression
*expr
;
1901 token
= primary_expression(token
->next
, &expr
);
1903 add_expression(clobbers
, expr
);
1904 } while (match_op(token
, ','));
1908 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1909 struct symbol_list
**labels
)
1911 struct symbol
*label
;
1914 token
= token
->next
; /* skip ':' and ',' */
1915 if (token_type(token
) != TOKEN_IDENT
)
1917 label
= label_symbol(token
);
1918 add_symbol(labels
, label
);
1919 token
= token
->next
;
1920 } while (match_op(token
, ','));
1924 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1928 token
= token
->next
;
1929 stmt
->type
= STMT_ASM
;
1930 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1931 token
= token
->next
;
1933 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
1935 token
= token
->next
;
1937 token
= expect(token
, '(', "after asm");
1938 token
= parse_expression(token
, &stmt
->asm_string
);
1939 if (match_op(token
, ':'))
1940 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1941 if (match_op(token
, ':'))
1942 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1943 if (match_op(token
, ':'))
1944 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1945 if (is_goto
&& match_op(token
, ':'))
1946 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
1947 token
= expect(token
, ')', "after asm");
1948 return expect(token
, ';', "at end of asm-statement");
1951 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1953 struct expression
*expr
;
1954 token
= expect(token
, '(', "after asm");
1955 token
= parse_expression(token
->next
, &expr
);
1956 token
= expect(token
, ')', "after asm");
1960 /* Make a statement out of an expression */
1961 static struct statement
*make_statement(struct expression
*expr
)
1963 struct statement
*stmt
;
1967 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1968 stmt
->expression
= expr
;
1973 * All iterators have two symbols associated with them:
1974 * the "continue" and "break" symbols, which are targets
1975 * for continue and break statements respectively.
1977 * They are in a special name-space, but they follow
1978 * all the normal visibility rules, so nested iterators
1979 * automatically work right.
1981 static void start_iterator(struct statement
*stmt
)
1983 struct symbol
*cont
, *brk
;
1985 start_symbol_scope(stmt
->pos
);
1986 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1987 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1988 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1989 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1991 stmt
->type
= STMT_ITERATOR
;
1992 stmt
->iterator_break
= brk
;
1993 stmt
->iterator_continue
= cont
;
1994 fn_local_symbol(brk
);
1995 fn_local_symbol(cont
);
1998 static void end_iterator(struct statement
*stmt
)
2003 static struct statement
*start_function(struct symbol
*sym
)
2006 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2008 start_function_scope(stmt
->pos
);
2009 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2010 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2011 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2012 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2013 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2015 fn_local_symbol(ret
);
2017 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2023 static void end_function(struct symbol
*sym
)
2026 end_function_scope();
2030 * A "switch()" statement, like an iterator, has a
2031 * the "break" symbol associated with it. It works
2032 * exactly like the iterator break - it's the target
2033 * for any break-statements in scope, and means that
2034 * "break" handling doesn't even need to know whether
2035 * it's breaking out of an iterator or a switch.
2037 * In addition, the "case" symbol is a marker for the
2038 * case/default statements to find the switch statement
2039 * that they are associated with.
2041 static void start_switch(struct statement
*stmt
)
2043 struct symbol
*brk
, *switch_case
;
2045 start_symbol_scope(stmt
->pos
);
2046 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2047 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2049 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2050 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2051 switch_case
->stmt
= stmt
;
2053 stmt
->type
= STMT_SWITCH
;
2054 stmt
->switch_break
= brk
;
2055 stmt
->switch_case
= switch_case
;
2057 fn_local_symbol(brk
);
2058 fn_local_symbol(switch_case
);
2061 static void end_switch(struct statement
*stmt
)
2063 if (!stmt
->switch_case
->symbol_list
)
2064 warning(stmt
->pos
, "switch with no cases");
2068 static void add_case_statement(struct statement
*stmt
)
2070 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2074 sparse_error(stmt
->pos
, "not in switch scope");
2075 stmt
->type
= STMT_NONE
;
2078 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2079 add_symbol(&target
->symbol_list
, sym
);
2081 stmt
->case_label
= sym
;
2082 fn_local_symbol(sym
);
2085 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2087 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2090 error_die(token
->pos
, "internal error: return without a function target");
2091 stmt
->type
= STMT_RETURN
;
2092 stmt
->ret_target
= target
;
2093 return expression_statement(token
->next
, &stmt
->ret_value
);
2096 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2098 struct symbol_list
*syms
;
2099 struct expression
*e1
, *e2
, *e3
;
2100 struct statement
*iterator
;
2102 start_iterator(stmt
);
2103 token
= expect(token
->next
, '(', "after 'for'");
2107 /* C99 variable declaration? */
2108 if (lookup_type(token
)) {
2109 token
= external_declaration(token
, &syms
);
2111 token
= parse_expression(token
, &e1
);
2112 token
= expect(token
, ';', "in 'for'");
2114 token
= parse_expression(token
, &e2
);
2115 token
= expect(token
, ';', "in 'for'");
2116 token
= parse_expression(token
, &e3
);
2117 token
= expect(token
, ')', "in 'for'");
2118 token
= statement(token
, &iterator
);
2120 stmt
->iterator_syms
= syms
;
2121 stmt
->iterator_pre_statement
= make_statement(e1
);
2122 stmt
->iterator_pre_condition
= e2
;
2123 stmt
->iterator_post_statement
= make_statement(e3
);
2124 stmt
->iterator_post_condition
= NULL
;
2125 stmt
->iterator_statement
= iterator
;
2131 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2133 struct expression
*expr
;
2134 struct statement
*iterator
;
2136 start_iterator(stmt
);
2137 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2138 token
= statement(token
, &iterator
);
2140 stmt
->iterator_pre_condition
= expr
;
2141 stmt
->iterator_post_condition
= NULL
;
2142 stmt
->iterator_statement
= iterator
;
2148 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2150 struct expression
*expr
;
2151 struct statement
*iterator
;
2153 start_iterator(stmt
);
2154 token
= statement(token
->next
, &iterator
);
2155 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2156 token
= token
->next
;
2158 sparse_error(token
->pos
, "expected 'while' after 'do'");
2159 token
= parens_expression(token
, &expr
, "after 'do-while'");
2161 stmt
->iterator_post_condition
= expr
;
2162 stmt
->iterator_statement
= iterator
;
2165 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2166 warning(iterator
->pos
, "do-while statement is not a compound statement");
2168 return expect(token
, ';', "after statement");
2171 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2173 stmt
->type
= STMT_IF
;
2174 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2175 token
= statement(token
, &stmt
->if_true
);
2176 if (token_type(token
) != TOKEN_IDENT
)
2178 if (token
->ident
!= &else_ident
)
2180 return statement(token
->next
, &stmt
->if_false
);
2183 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2185 stmt
->type
= STMT_CASE
;
2186 token
= expect(token
, ':', "after default/case");
2187 add_case_statement(stmt
);
2188 return statement(token
, &stmt
->case_statement
);
2191 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2193 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2194 if (match_op(token
, SPECIAL_ELLIPSIS
))
2195 token
= parse_expression(token
->next
, &stmt
->case_to
);
2196 return case_statement(token
, stmt
);
2199 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2201 return case_statement(token
->next
, stmt
);
2204 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2206 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2207 stmt
->type
= STMT_GOTO
;
2208 stmt
->goto_label
= target
;
2210 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2211 return expect(token
->next
, ';', "at end of statement");
2214 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2216 stmt
->type
= STMT_SWITCH
;
2218 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2219 token
= statement(token
, &stmt
->switch_statement
);
2224 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2226 stmt
->type
= STMT_GOTO
;
2227 token
= token
->next
;
2228 if (match_op(token
, '*')) {
2229 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2230 add_statement(&function_computed_goto_list
, stmt
);
2231 } else if (token_type(token
) == TOKEN_IDENT
) {
2232 stmt
->goto_label
= label_symbol(token
);
2233 token
= token
->next
;
2235 sparse_error(token
->pos
, "Expected identifier or goto expression");
2237 return expect(token
, ';', "at end of statement");
2240 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2242 stmt
->type
= STMT_CONTEXT
;
2243 token
= parse_expression(token
->next
, &stmt
->expression
);
2244 if (stmt
->expression
->type
== EXPR_PREOP
2245 && stmt
->expression
->op
== '('
2246 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2247 struct expression
*expr
;
2248 expr
= stmt
->expression
->unop
;
2249 stmt
->context
= expr
->left
;
2250 stmt
->expression
= expr
->right
;
2252 return expect(token
, ';', "at end of statement");
2255 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2257 stmt
->type
= STMT_RANGE
;
2258 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2259 token
= expect(token
, ',', "after range expression");
2260 token
= assignment_expression(token
, &stmt
->range_low
);
2261 token
= expect(token
, ',', "after low range");
2262 token
= assignment_expression(token
, &stmt
->range_high
);
2263 return expect(token
, ';', "after range statement");
2266 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2268 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2271 if (token_type(token
) == TOKEN_IDENT
) {
2272 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2273 if (s
&& s
->op
->statement
)
2274 return s
->op
->statement(token
, stmt
);
2276 if (match_op(token
->next
, ':')) {
2277 stmt
->type
= STMT_LABEL
;
2278 stmt
->label_identifier
= label_symbol(token
);
2279 token
= skip_attributes(token
->next
->next
);
2280 return statement(token
, &stmt
->label_statement
);
2284 if (match_op(token
, '{')) {
2285 stmt
->type
= STMT_COMPOUND
;
2286 start_symbol_scope(stmt
->pos
);
2287 token
= compound_statement(token
->next
, stmt
);
2290 return expect(token
, '}', "at end of compound statement");
2293 stmt
->type
= STMT_EXPRESSION
;
2294 return expression_statement(token
, &stmt
->expression
);
2297 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2298 static struct token
*label_statement(struct token
*token
)
2300 while (token_type(token
) == TOKEN_IDENT
) {
2301 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2302 /* it's block-scope, but we want label namespace */
2303 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2304 sym
->namespace = NS_LABEL
;
2305 fn_local_symbol(sym
);
2306 token
= token
->next
;
2307 if (!match_op(token
, ','))
2309 token
= token
->next
;
2311 return expect(token
, ';', "at end of label declaration");
2314 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2316 int seen_statement
= 0;
2317 while (token_type(token
) == TOKEN_IDENT
&&
2318 token
->ident
== &__label___ident
)
2319 token
= label_statement(token
->next
);
2321 struct statement
* stmt
;
2322 if (eof_token(token
))
2324 if (match_op(token
, '}'))
2326 if (lookup_type(token
)) {
2327 if (seen_statement
) {
2328 warning(token
->pos
, "mixing declarations and code");
2331 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2332 token
= external_declaration(token
, &stmt
->declaration
);
2334 seen_statement
= Wdeclarationafterstatement
;
2335 token
= statement(token
, &stmt
);
2337 add_statement(list
, stmt
);
2342 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2344 struct symbol_list
**list
= &fn
->arguments
;
2346 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2347 sym
->ident
= token
->ident
;
2348 token
= token
->next
;
2349 sym
->endpos
= token
->pos
;
2350 sym
->ctype
.base_type
= &incomplete_ctype
;
2351 add_symbol(list
, sym
);
2352 if (!match_op(token
, ',') ||
2353 token_type(token
->next
) != TOKEN_IDENT
||
2354 lookup_type(token
->next
))
2356 token
= token
->next
;
2361 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2363 struct symbol_list
**list
= &fn
->arguments
;
2368 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2370 token
= token
->next
;
2374 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2375 token
= parameter_declaration(token
, sym
);
2376 if (sym
->ctype
.base_type
== &void_ctype
) {
2377 /* Special case: (void) */
2378 if (!*list
&& !sym
->ident
)
2380 warning(token
->pos
, "void parameter");
2382 add_symbol(list
, sym
);
2383 if (!match_op(token
, ','))
2385 token
= token
->next
;
2390 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2392 token
= statement_list(token
, &stmt
->stmts
);
2396 static struct expression
*identifier_expression(struct token
*token
)
2398 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2399 expr
->expr_ident
= token
->ident
;
2403 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2405 int idx_from
, idx_to
;
2406 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2408 idx_from
= const_expression_value(from
);
2411 idx_to
= const_expression_value(to
);
2412 if (idx_to
< idx_from
|| idx_from
< 0)
2413 warning(from
->pos
, "nonsense array initializer index range");
2415 expr
->idx_from
= idx_from
;
2416 expr
->idx_to
= idx_to
;
2420 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2422 int expect_equal
= 0;
2423 struct token
*next
= token
->next
;
2424 struct expression
**tail
= ep
;
2429 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2430 struct expression
*expr
= identifier_expression(token
);
2431 if (Wold_initializer
)
2432 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2433 token
= initializer(&expr
->ident_expression
, next
->next
);
2434 if (expr
->ident_expression
)
2439 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2440 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2441 struct expression
*expr
= identifier_expression(next
);
2443 tail
= &expr
->ident_expression
;
2446 } else if (match_op(token
, '[')) {
2447 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2448 token
= constant_expression(token
->next
, &from
);
2450 sparse_error(token
->pos
, "Expected constant expression");
2453 if (match_op(token
, SPECIAL_ELLIPSIS
))
2454 token
= constant_expression(token
->next
, &to
);
2455 expr
= index_expression(from
, to
);
2457 tail
= &expr
->idx_expression
;
2458 token
= expect(token
, ']', "at end of initializer index");
2465 if (nested
&& !expect_equal
) {
2466 if (!match_op(token
, '='))
2467 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2472 token
= expect(token
, '=', "at end of initializer index");
2474 token
= initializer(tail
, token
);
2480 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2482 struct expression
*expr
;
2485 token
= single_initializer(&expr
, token
);
2488 add_expression(list
, expr
);
2489 if (!match_op(token
, ','))
2491 token
= token
->next
;
2496 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2498 if (match_op(token
, '{')) {
2499 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2501 token
= initializer_list(&expr
->expr_list
, token
->next
);
2502 return expect(token
, '}', "at end of initializer");
2504 return assignment_expression(token
, tree
);
2507 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2510 sparse_error(sym
->pos
, "no identifier for function argument");
2513 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2516 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2517 struct symbol_list
**list
)
2519 struct symbol_list
**old_symbol_list
;
2520 struct symbol
*base_type
= decl
->ctype
.base_type
;
2521 struct statement
*stmt
, **p
;
2522 struct symbol
*prev
;
2525 old_symbol_list
= function_symbol_list
;
2526 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2527 function_symbol_list
= &decl
->inline_symbol_list
;
2528 p
= &base_type
->inline_stmt
;
2530 function_symbol_list
= &decl
->symbol_list
;
2531 p
= &base_type
->stmt
;
2533 function_computed_target_list
= NULL
;
2534 function_computed_goto_list
= NULL
;
2536 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2537 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2538 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2540 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2541 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2543 stmt
= start_function(decl
);
2546 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2547 declare_argument(arg
, base_type
);
2548 } END_FOR_EACH_PTR(arg
);
2550 token
= compound_statement(token
->next
, stmt
);
2553 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2554 add_symbol(list
, decl
);
2555 check_declaration(decl
);
2556 decl
->definition
= decl
;
2557 prev
= decl
->same_symbol
;
2558 if (prev
&& prev
->definition
) {
2559 warning(decl
->pos
, "multiple definitions for function '%s'",
2560 show_ident(decl
->ident
));
2561 info(prev
->definition
->pos
, " the previous one is here");
2564 prev
->definition
= decl
;
2565 prev
= prev
->same_symbol
;
2568 function_symbol_list
= old_symbol_list
;
2569 if (function_computed_goto_list
) {
2570 if (!function_computed_target_list
)
2571 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2573 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2574 stmt
->target_list
= function_computed_target_list
;
2575 } END_FOR_EACH_PTR(stmt
);
2578 return expect(token
, '}', "at end of function");
2581 static void promote_k_r_types(struct symbol
*arg
)
2583 struct symbol
*base
= arg
->ctype
.base_type
;
2584 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2585 arg
->ctype
.base_type
= &int_ctype
;
2589 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2591 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2594 FOR_EACH_PTR(real_args
, arg
) {
2595 struct symbol
*type
;
2597 /* This is quadratic in the number of arguments. We _really_ don't care */
2598 FOR_EACH_PTR(argtypes
, type
) {
2599 if (type
->ident
== arg
->ident
)
2601 } END_FOR_EACH_PTR(type
);
2602 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2606 /* "char" and "short" promote to "int" */
2607 promote_k_r_types(type
);
2609 arg
->ctype
= type
->ctype
;
2610 } END_FOR_EACH_PTR(arg
);
2612 FOR_EACH_PTR(argtypes
, arg
) {
2614 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2615 } END_FOR_EACH_PTR(arg
);
2619 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2620 struct symbol_list
**list
)
2622 struct symbol_list
*args
= NULL
;
2624 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2626 token
= declaration_list(token
, &args
);
2627 if (!match_op(token
, ';')) {
2628 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2631 token
= token
->next
;
2632 } while (lookup_type(token
));
2634 apply_k_r_types(args
, decl
);
2636 if (!match_op(token
, '{')) {
2637 sparse_error(token
->pos
, "expected function body");
2640 return parse_function_body(token
, decl
, list
);
2643 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2645 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2646 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2647 struct statement
*stmt
;
2649 anon
->ctype
.base_type
= fn
;
2650 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2653 token
= parse_asm_statement(token
, stmt
);
2655 add_symbol(list
, anon
);
2659 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2661 struct ident
*ident
= NULL
;
2662 struct symbol
*decl
;
2663 struct decl_state ctx
= { .ident
= &ident
};
2665 struct symbol
*base_type
;
2669 /* Top-level inline asm? */
2670 if (token_type(token
) == TOKEN_IDENT
) {
2671 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2672 if (s
&& s
->op
->toplevel
)
2673 return s
->op
->toplevel(token
, list
);
2676 /* Parse declaration-specifiers, if any */
2677 token
= declaration_specifiers(token
, &ctx
);
2678 mod
= storage_modifiers(&ctx
);
2679 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2680 /* Just a type declaration? */
2681 if (match_op(token
, ';')) {
2682 apply_modifiers(token
->pos
, &ctx
);
2687 token
= declarator(token
, &ctx
);
2688 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2689 apply_modifiers(token
->pos
, &ctx
);
2691 decl
->ctype
= ctx
.ctype
;
2692 decl
->ctype
.modifiers
|= mod
;
2693 decl
->endpos
= token
->pos
;
2695 /* Just a type declaration? */
2697 warning(token
->pos
, "missing identifier in declaration");
2698 return expect(token
, ';', "at the end of type declaration");
2701 /* type define declaration? */
2702 is_typedef
= ctx
.storage_class
== STypedef
;
2704 /* Typedefs don't have meaningful storage */
2706 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2708 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2710 base_type
= decl
->ctype
.base_type
;
2713 if (base_type
&& !base_type
->ident
) {
2714 switch (base_type
->type
) {
2719 base_type
->ident
= ident
;
2722 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2723 /* K&R argument declaration? */
2724 if (lookup_type(token
))
2725 return parse_k_r_arguments(token
, decl
, list
);
2726 if (match_op(token
, '{'))
2727 return parse_function_body(token
, decl
, list
);
2729 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2730 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2731 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2732 sparse_error(token
->pos
, "void declaration");
2736 if (!is_typedef
&& match_op(token
, '=')) {
2737 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2738 warning(decl
->pos
, "symbol with external linkage has initializer");
2739 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2741 token
= initializer(&decl
->initializer
, token
->next
);
2744 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2745 add_symbol(list
, decl
);
2746 fn_local_symbol(decl
);
2749 check_declaration(decl
);
2750 if (decl
->same_symbol
)
2751 decl
->definition
= decl
->same_symbol
->definition
;
2753 if (!match_op(token
, ','))
2756 token
= token
->next
;
2758 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2760 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2761 token
= declarator(token
, &ctx
);
2762 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2763 apply_modifiers(token
->pos
, &ctx
);
2764 decl
->ctype
= ctx
.ctype
;
2765 decl
->ctype
.modifiers
|= mod
;
2766 decl
->endpos
= token
->pos
;
2768 sparse_error(token
->pos
, "expected identifier name in type definition");
2773 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2775 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2777 /* Function declarations are automatically extern unless specifically static */
2778 base_type
= decl
->ctype
.base_type
;
2779 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2780 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2781 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2784 return expect(token
, ';', "at end of declaration");