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__",
541 "__ms_hook_prologue__",
544 "no_instrument_function",
545 "__no_instrument_function__",
565 "__syscall_linkage__",
575 "warn_unused_result",
576 "__warn_unused_result__",
584 void init_parser(int stream
)
587 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
588 struct init_keyword
*ptr
= keyword_table
+ i
;
589 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
590 sym
->ident
->keyword
= 1;
591 if (ptr
->ns
== NS_TYPEDEF
)
592 sym
->ident
->reserved
= 1;
593 sym
->ctype
.modifiers
= ptr
->modifiers
;
594 sym
->ctype
.base_type
= ptr
->type
;
598 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
599 const char * name
= ignored_attributes
[i
];
600 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
602 sym
->ident
->keyword
= 1;
603 sym
->op
= &ignore_attr_op
;
608 // Add a symbol to the list of function-local symbols
609 static void fn_local_symbol(struct symbol
*sym
)
611 if (function_symbol_list
)
612 add_symbol(function_symbol_list
, sym
);
615 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
620 if (token_type(token
) != TOKEN_IDENT
)
623 va_start(args
, token
);
625 next
= va_arg(args
, struct ident
*);
626 } while (next
&& token
->ident
!= next
);
629 return next
&& token
->ident
== next
;
633 struct statement
*alloc_statement(struct position pos
, int type
)
635 struct statement
*stmt
= __alloc_statement(0);
641 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
643 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
645 struct symbol
*ctype
;
648 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
650 sparse_error(pos
, "don't know how to apply mode to %s",
651 show_typename(ctx
->ctype
.base_type
));
653 ctx
->ctype
.base_type
= ctype
;
657 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
659 struct symbol
*sym
= alloc_symbol(pos
, type
);
661 sym
->ctype
.base_type
= ctype
->base_type
;
662 sym
->ctype
.modifiers
= ctype
->modifiers
;
664 ctype
->base_type
= sym
;
665 ctype
->modifiers
= 0;
670 * NOTE! NS_LABEL is not just a different namespace,
671 * it also ends up using function scope instead of the
672 * regular symbol scope.
674 struct symbol
*label_symbol(struct token
*token
)
676 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
678 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
679 bind_symbol(sym
, token
->ident
, NS_LABEL
);
680 fn_local_symbol(sym
);
685 static struct token
*struct_union_enum_specifier(enum type type
,
686 struct token
*token
, struct decl_state
*ctx
,
687 struct token
*(*parse
)(struct token
*, struct symbol
*))
690 struct position
*repos
;
692 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
693 if (token_type(token
) == TOKEN_IDENT
) {
694 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
696 (is_outer_scope(sym
->scope
) &&
697 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
698 // Either a new symbol, or else an out-of-scope
699 // symbol being redefined.
700 sym
= alloc_symbol(token
->pos
, type
);
701 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
703 if (sym
->type
!= type
)
704 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
705 ctx
->ctype
.base_type
= sym
;
708 if (match_op(token
, '{')) {
709 // The following test is actually wrong for empty
710 // structs, but (1) they are not C99, (2) gcc does
711 // the same thing, and (3) it's easier.
712 if (sym
->symbol_list
)
713 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
715 token
= parse(token
->next
, sym
);
716 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
718 // Mark the structure as needing re-examination
720 sym
->endpos
= token
->pos
;
725 // private struct/union/enum type
726 if (!match_op(token
, '{')) {
727 sparse_error(token
->pos
, "expected declaration");
728 ctx
->ctype
.base_type
= &bad_ctype
;
732 sym
= alloc_symbol(token
->pos
, type
);
733 token
= parse(token
->next
, sym
);
734 ctx
->ctype
.base_type
= sym
;
735 token
= expect(token
, '}', "at end of specifier");
736 sym
->endpos
= token
->pos
;
741 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
743 struct symbol
*field
, *last
= NULL
;
745 res
= struct_declaration_list(token
, &sym
->symbol_list
);
746 FOR_EACH_PTR(sym
->symbol_list
, field
) {
748 struct symbol
*base
= field
->ctype
.base_type
;
749 if (base
&& base
->type
== SYM_BITFIELD
)
753 last
->next_subobject
= field
;
755 } END_FOR_EACH_PTR(field
);
759 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
761 return struct_declaration_list(token
, &sym
->symbol_list
);
764 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
766 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
769 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
771 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
777 unsigned long long y
;
780 static void upper_boundary(Num
*n
, Num
*v
)
792 static void lower_boundary(Num
*n
, Num
*v
)
804 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
806 int shift
= type
->bit_size
;
807 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
811 if (upper
->x
== 0 && upper
->y
>> shift
)
813 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
818 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
820 if (s1
->bit_size
< s2
->bit_size
) {
822 } else if (s1
->bit_size
== s2
->bit_size
) {
823 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
826 if (s1
->bit_size
< bits_in_int
)
831 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
835 FOR_EACH_PTR(list
, sym
) {
836 struct expression
*expr
= sym
->initializer
;
837 struct symbol
*ctype
;
838 if (expr
->type
!= EXPR_VALUE
)
841 if (ctype
->bit_size
== base_type
->bit_size
)
843 cast_value(expr
, base_type
, expr
, ctype
);
844 } END_FOR_EACH_PTR(sym
);
847 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
849 unsigned long long lastval
= 0;
850 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
851 Num upper
= {-1, 0}, lower
= {1, 0};
853 parent
->examined
= 1;
854 parent
->ctype
.base_type
= &int_ctype
;
855 while (token_type(token
) == TOKEN_IDENT
) {
856 struct expression
*expr
= NULL
;
857 struct token
*next
= token
->next
;
860 if (match_op(next
, '=')) {
861 next
= constant_expression(next
->next
, &expr
);
862 lastval
= get_expression_value(expr
);
864 if (expr
&& expr
->ctype
)
868 } else if (is_int_type(ctype
)) {
871 error_die(token
->pos
, "can't increment the last enum member");
875 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
876 expr
->value
= lastval
;
880 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
881 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
882 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
883 sym
->initializer
= expr
;
884 sym
->enum_member
= 1;
885 sym
->ctype
.base_type
= parent
;
886 add_ptr_list(&parent
->symbol_list
, sym
);
888 if (base_type
!= &bad_ctype
) {
889 if (ctype
->type
== SYM_NODE
)
890 ctype
= ctype
->ctype
.base_type
;
891 if (ctype
->type
== SYM_ENUM
) {
895 ctype
= ctype
->ctype
.base_type
;
899 * - if all enums are of the same type, then
900 * the base_type is that type (two first
902 * - if enums are of different types, they
903 * all have to be integer types, and the
904 * base type is at least "int_ctype".
905 * - otherwise the base_type is "bad_ctype".
909 } else if (ctype
== base_type
) {
911 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
912 base_type
= bigger_enum_type(base_type
, ctype
);
914 base_type
= &bad_ctype
;
915 parent
->ctype
.base_type
= base_type
;
917 if (is_int_type(base_type
)) {
918 Num v
= {.y
= lastval
};
919 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
921 else if ((long long)lastval
>= 0)
925 upper_boundary(&upper
, &v
);
926 lower_boundary(&lower
, &v
);
930 sym
->endpos
= token
->pos
;
932 if (!match_op(token
, ','))
937 sparse_error(token
->pos
, "bad enum definition");
938 base_type
= &bad_ctype
;
940 else if (!is_int_type(base_type
))
941 base_type
= base_type
;
942 else if (type_is_ok(base_type
, &upper
, &lower
))
943 base_type
= base_type
;
944 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
945 base_type
= &int_ctype
;
946 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
947 base_type
= &uint_ctype
;
948 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
949 base_type
= &long_ctype
;
950 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
951 base_type
= &ulong_ctype
;
952 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
953 base_type
= &llong_ctype
;
954 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
955 base_type
= &ullong_ctype
;
957 base_type
= &bad_ctype
;
958 parent
->ctype
.base_type
= base_type
;
959 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
960 parent
->examined
= 0;
962 cast_enum_list(parent
->symbol_list
, base_type
);
967 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
969 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
970 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
972 if (!ctype
->base_type
)
973 ctype
->base_type
= &incomplete_ctype
;
978 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
980 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
984 if (!match_op(token
, '(')) {
985 sparse_error(token
->pos
, "expected '(' after typeof");
988 if (lookup_type(token
->next
)) {
989 token
= typename(token
->next
, &sym
, NULL
);
990 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
991 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
993 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
994 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
996 typeof_sym
->endpos
= token
->pos
;
997 if (!typeof_sym
->initializer
) {
998 sparse_error(token
->pos
, "expected expression after the '(' token");
999 typeof_sym
= &bad_ctype
;
1001 ctx
->ctype
.base_type
= typeof_sym
;
1003 return expect(token
, ')', "after typeof");
1006 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1008 struct expression
*expr
= NULL
;
1009 if (match_op(token
, '('))
1010 token
= parens_expression(token
, &expr
, "in attribute");
1014 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1016 if (!ctx
->ctype
.alignment
)
1017 ctx
->ctype
.alignment
= 1;
1021 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1023 int alignment
= max_alignment
;
1024 struct expression
*expr
= NULL
;
1026 if (match_op(token
, '(')) {
1027 token
= parens_expression(token
, &expr
, "in attribute");
1029 alignment
= const_expression_value(expr
);
1031 if (alignment
& (alignment
-1)) {
1032 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1034 } else if (alignment
> ctx
->ctype
.alignment
)
1035 ctx
->ctype
.alignment
= alignment
;
1039 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1041 if (ctx
->modifiers
& qual
)
1042 warning(*pos
, "duplicate %s", modifier_string(qual
));
1043 ctx
->modifiers
|= qual
;
1046 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1048 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1052 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1054 struct expression
*expr
= NULL
;
1056 token
= expect(token
, '(', "after address_space attribute");
1057 token
= conditional_expression(token
, &expr
);
1059 as
= const_expression_value(expr
);
1060 if (Waddress_space
&& as
)
1063 token
= expect(token
, ')', "after address_space attribute");
1067 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1069 if (ctype
->ctype
.base_type
!= &int_type
)
1071 if (ctype
== &char_ctype
)
1073 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1077 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1079 if (ctype
->ctype
.base_type
!= &int_type
)
1081 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1085 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1087 if (ctype
->ctype
.base_type
!= &int_type
)
1089 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1093 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1095 if (ctype
->ctype
.base_type
!= &int_type
)
1097 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1101 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1103 if (ctype
->ctype
.base_type
!= &int_type
)
1105 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1109 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1111 if (ctype
->ctype
.base_type
!= &int_type
)
1113 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1117 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1119 token
= expect(token
, '(', "after mode attribute");
1120 if (token_type(token
) == TOKEN_IDENT
) {
1121 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1122 if (mode
&& mode
->op
->type
== KW_MODE
)
1123 ctx
->mode
= mode
->op
;
1125 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1126 token
= token
->next
;
1128 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1129 token
= expect(token
, ')', "after mode attribute");
1133 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1135 struct context
*context
= alloc_context();
1136 struct expression
*args
[3];
1139 token
= expect(token
, '(', "after context attribute");
1140 while (!match_op(token
, ')')) {
1141 struct expression
*expr
= NULL
;
1142 token
= conditional_expression(token
, &expr
);
1146 args
[argc
++] = expr
;
1147 if (!match_op(token
, ','))
1149 token
= token
->next
;
1154 sparse_error(token
->pos
, "expected context input/output values");
1157 context
->in
= get_expression_value(args
[0]);
1160 context
->in
= get_expression_value(args
[0]);
1161 context
->out
= get_expression_value(args
[1]);
1164 context
->context
= args
[0];
1165 context
->in
= get_expression_value(args
[1]);
1166 context
->out
= get_expression_value(args
[2]);
1171 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1173 token
= expect(token
, ')', "after context attribute");
1177 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1179 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1180 ctx
->ctype
.base_type
->designated_init
= 1;
1182 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1186 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1188 if (Wtransparent_union
)
1189 warning(token
->pos
, "ignoring attribute __transparent_union__");
1193 static struct token
*recover_unknown_attribute(struct token
*token
)
1195 struct expression
*expr
= NULL
;
1197 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1198 token
= token
->next
;
1199 if (match_op(token
, '('))
1200 token
= parens_expression(token
, &expr
, "in attribute");
1204 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1206 token
= expect(token
, '(', "after attribute");
1207 token
= expect(token
, '(', "after attribute");
1210 struct ident
*attribute_name
;
1211 struct symbol
*attr
;
1213 if (eof_token(token
))
1215 if (match_op(token
, ';'))
1217 if (token_type(token
) != TOKEN_IDENT
)
1219 attribute_name
= token
->ident
;
1220 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1221 if (attr
&& attr
->op
->attribute
)
1222 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1224 token
= recover_unknown_attribute(token
);
1226 if (!match_op(token
, ','))
1228 token
= token
->next
;
1231 token
= expect(token
, ')', "after attribute");
1232 token
= expect(token
, ')', "after attribute");
1236 static const char *storage_class
[] =
1238 [STypedef
] = "typedef",
1240 [SExtern
] = "extern",
1241 [SStatic
] = "static",
1242 [SRegister
] = "register",
1243 [SForced
] = "[force]"
1246 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1248 static unsigned long mod
[] =
1251 [SExtern
] = MOD_EXTERN
,
1252 [SStatic
] = MOD_STATIC
,
1253 [SRegister
] = MOD_REGISTER
1255 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1256 | (ctx
->is_tls
? MOD_TLS
: 0);
1259 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1261 /* __thread can be used alone, or with extern or static */
1262 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1263 sparse_error(*pos
, "__thread can only be used alone, or with "
1264 "extern or static");
1268 if (!ctx
->storage_class
) {
1269 ctx
->storage_class
= class;
1272 if (ctx
->storage_class
== class)
1273 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1275 sparse_error(*pos
, "multiple storage classes");
1278 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1280 set_storage_class(&next
->pos
, ctx
, STypedef
);
1284 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1286 set_storage_class(&next
->pos
, ctx
, SAuto
);
1290 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1292 set_storage_class(&next
->pos
, ctx
, SRegister
);
1296 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1298 set_storage_class(&next
->pos
, ctx
, SStatic
);
1302 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1304 set_storage_class(&next
->pos
, ctx
, SExtern
);
1308 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1310 /* This GCC extension can be used alone, or with extern or static */
1311 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1312 || ctx
->storage_class
== SExtern
) {
1315 sparse_error(next
->pos
, "__thread can only be used alone, or "
1316 "with extern or static");
1322 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1324 set_storage_class(&token
->pos
, ctx
, SForced
);
1328 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1334 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1336 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1340 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1342 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1346 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1348 unsigned long mod
= thistype
->modifiers
;
1351 apply_qualifier(&pos
, ctype
, mod
);
1354 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1355 (struct ptr_list
**)&ctype
->contexts
);
1358 if (thistype
->alignment
> ctype
->alignment
)
1359 ctype
->alignment
= thistype
->alignment
;
1363 ctype
->as
= thistype
->as
;
1366 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1369 if (what
& (Set_S
| Set_T
))
1371 if (what
& Set_Char
)
1373 else if (what
& Set_Double
)
1375 else if (what
& Set_Float
)
1377 else if (what
& Set_Signed
)
1379 else if (what
& Set_Unsigned
)
1381 else if (what
& Set_Short
)
1383 else if (what
& Set_Long
)
1387 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1388 old
, show_ident(new));
1392 sparse_error(pos
, "two or more data types in declaration specifiers");
1395 static struct symbol
* const int_types
[] =
1396 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1397 static struct symbol
* const signed_types
[] =
1398 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1400 static struct symbol
* const unsigned_types
[] =
1401 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1403 static struct symbol
* const real_types
[] =
1404 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1405 static struct symbol
* const char_types
[] =
1406 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1407 static struct symbol
* const * const types
[] = {
1408 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1409 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1412 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1414 return types
[want_unsigned
? CUInt
: CInt
][size
];
1417 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1419 while (token_type(t
) == TOKEN_IDENT
) {
1420 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1423 if (s
->type
!= SYM_KEYWORD
)
1425 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1428 if (s
->op
->declarator
)
1429 t
= s
->op
->declarator(t
, ctx
);
1434 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1440 while (token_type(token
) == TOKEN_IDENT
) {
1441 struct symbol
*s
= lookup_symbol(token
->ident
,
1442 NS_TYPEDEF
| NS_SYMBOL
);
1443 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1445 if (s
->type
!= SYM_KEYWORD
) {
1448 seen
|= Set_S
| Set_T
;
1449 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1450 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1451 token
= token
->next
;
1454 if (s
->op
->type
& KW_SPECIFIER
) {
1455 if (seen
& s
->op
->test
) {
1456 specifier_conflict(token
->pos
,
1462 class += s
->op
->class;
1463 if (s
->op
->type
& KW_SHORT
) {
1465 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1466 if (class == CReal
) {
1467 specifier_conflict(token
->pos
,
1475 token
= token
->next
;
1476 if (s
->op
->declarator
)
1477 token
= s
->op
->declarator(token
, ctx
);
1478 if (s
->op
->type
& KW_EXACT
) {
1479 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1480 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1484 if (!(seen
& Set_S
)) { /* not set explicitly? */
1485 struct symbol
*base
= &incomplete_ctype
;
1487 base
= types
[class][size
];
1488 ctx
->ctype
.base_type
= base
;
1491 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1492 struct symbol
*type
;
1493 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1494 if (!is_int_type(ctx
->ctype
.base_type
)) {
1495 sparse_error(token
->pos
, "invalid modifier");
1498 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1499 *type
= *ctx
->ctype
.base_type
;
1500 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1501 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1502 type
->type
= SYM_RESTRICT
;
1503 ctx
->ctype
.base_type
= type
;
1504 create_fouled(type
);
1509 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1511 struct expression
*expr
= NULL
;
1513 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1514 token
= token
->next
;
1515 token
= parse_expression(token
, &expr
);
1516 sym
->array_size
= expr
;
1520 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1521 static struct token
*identifier_list(struct token
*, struct symbol
*);
1522 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1524 static struct token
*skip_attribute(struct token
*token
)
1526 token
= token
->next
;
1527 if (match_op(token
, '(')) {
1529 token
= token
->next
;
1530 while (depth
&& !eof_token(token
)) {
1531 if (token_type(token
) == TOKEN_SPECIAL
) {
1532 if (token
->special
== '(')
1534 else if (token
->special
== ')')
1537 token
= token
->next
;
1543 static struct token
*skip_attributes(struct token
*token
)
1545 struct symbol
*keyword
;
1547 if (token_type(token
) != TOKEN_IDENT
)
1549 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1550 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1552 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1554 token
= expect(token
->next
, '(', "after attribute");
1555 token
= expect(token
, '(', "after attribute");
1557 if (eof_token(token
))
1559 if (match_op(token
, ';'))
1561 if (token_type(token
) != TOKEN_IDENT
)
1563 token
= skip_attribute(token
);
1564 if (!match_op(token
, ','))
1566 token
= token
->next
;
1568 token
= expect(token
, ')', "after attribute");
1569 token
= expect(token
, ')', "after attribute");
1574 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1576 struct symbol
*keyword
;
1578 if (token_type(token
) != TOKEN_IDENT
)
1580 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1581 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1583 if (!(keyword
->op
->type
& keywords
))
1585 token
= keyword
->op
->declarator(token
->next
, ctx
);
1586 keywords
&= KW_ATTRIBUTE
;
1591 static int is_nested(struct token
*token
, struct token
**p
,
1592 int prefer_abstract
)
1595 * This can be either a parameter list or a grouping.
1596 * For the direct (non-abstract) case, we know if must be
1597 * a parameter list if we already saw the identifier.
1598 * For the abstract case, we know if must be a parameter
1599 * list if it is empty or starts with a type.
1601 struct token
*next
= token
->next
;
1603 *p
= next
= skip_attributes(next
);
1605 if (token_type(next
) == TOKEN_IDENT
) {
1606 if (lookup_type(next
))
1607 return !prefer_abstract
;
1611 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1618 Empty
, K_R
, Proto
, Bad_Func
,
1621 static enum kind
which_func(struct token
*token
,
1623 int prefer_abstract
)
1625 struct token
*next
= token
->next
;
1627 if (token_type(next
) == TOKEN_IDENT
) {
1628 if (lookup_type(next
))
1630 /* identifier list not in definition; complain */
1631 if (prefer_abstract
)
1633 "identifier list not in definition");
1637 if (token_type(next
) != TOKEN_SPECIAL
)
1640 if (next
->special
== ')') {
1641 /* don't complain about those */
1642 if (!n
|| match_op(next
->next
, ';'))
1645 "non-ANSI function declaration of function '%s'",
1650 if (next
->special
== SPECIAL_ELLIPSIS
) {
1652 "variadic functions must have one named argument");
1659 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1661 struct ctype
*ctype
= &ctx
->ctype
;
1663 struct ident
**p
= ctx
->ident
;
1665 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1666 *ctx
->ident
= token
->ident
;
1667 token
= token
->next
;
1668 } else if (match_op(token
, '(') &&
1669 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1670 struct symbol
*base_type
= ctype
->base_type
;
1671 if (token
->next
!= next
)
1672 next
= handle_attributes(token
->next
, ctx
,
1674 token
= declarator(next
, ctx
);
1675 token
= expect(token
, ')', "in nested declarator");
1676 while (ctype
->base_type
!= base_type
)
1677 ctype
= &ctype
->base_type
->ctype
;
1681 if (match_op(token
, '(')) {
1682 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1684 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1685 token
= token
->next
;
1687 token
= identifier_list(token
, fn
);
1688 else if (kind
== Proto
)
1689 token
= parameter_type_list(token
, fn
);
1690 token
= expect(token
, ')', "in function declarator");
1691 fn
->endpos
= token
->pos
;
1695 while (match_op(token
, '[')) {
1696 struct symbol
*array
;
1697 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1698 token
= abstract_array_declarator(token
->next
, array
);
1699 token
= expect(token
, ']', "in abstract_array_declarator");
1700 array
->endpos
= token
->pos
;
1701 ctype
= &array
->ctype
;
1706 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1708 while (match_op(token
,'*')) {
1709 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1710 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1711 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1712 ptr
->ctype
.as
= ctx
->ctype
.as
;
1713 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1714 ctx
->ctype
.modifiers
= 0;
1715 ctx
->ctype
.base_type
= ptr
;
1717 ctx
->ctype
.contexts
= NULL
;
1718 ctx
->ctype
.alignment
= 0;
1720 token
= handle_qualifiers(token
->next
, ctx
);
1721 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1726 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1728 token
= pointer(token
, ctx
);
1729 return direct_declarator(token
, ctx
);
1732 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1734 struct ctype
*ctype
= &ctx
->ctype
;
1735 struct expression
*expr
;
1736 struct symbol
*bitfield
;
1739 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1740 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1741 show_typename(ctype
->base_type
));
1742 // Parse this to recover gracefully.
1743 return conditional_expression(token
->next
, &expr
);
1746 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1747 token
= conditional_expression(token
->next
, &expr
);
1748 width
= const_expression_value(expr
);
1749 bitfield
->bit_size
= width
;
1751 if (width
< 0 || width
> INT_MAX
) {
1752 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1754 } else if (*ctx
->ident
&& width
== 0) {
1755 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1756 show_ident(*ctx
->ident
));
1758 } else if (*ctx
->ident
) {
1759 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1760 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1761 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1762 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1763 // Valid values are either {-1;0} or {0}, depending on integer
1764 // representation. The latter makes for very efficient code...
1765 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1767 if (Wdefault_bitfield_sign
&&
1768 bitfield_type
->type
!= SYM_ENUM
&&
1769 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1771 // The sign of bitfields is unspecified by default.
1772 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1775 bitfield
->bit_size
= width
;
1776 bitfield
->endpos
= token
->pos
;
1780 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1782 struct decl_state ctx
= {.prefer_abstract
= 0};
1786 token
= declaration_specifiers(token
, &ctx
);
1787 mod
= storage_modifiers(&ctx
);
1790 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1791 ctx
.ident
= &decl
->ident
;
1793 token
= declarator(token
, &ctx
);
1794 if (match_op(token
, ':'))
1795 token
= handle_bitfield(token
, &ctx
);
1797 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1798 apply_modifiers(token
->pos
, &ctx
);
1800 decl
->ctype
= ctx
.ctype
;
1801 decl
->ctype
.modifiers
|= mod
;
1802 decl
->endpos
= token
->pos
;
1803 add_symbol(list
, decl
);
1804 if (!match_op(token
, ','))
1806 token
= token
->next
;
1812 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1814 while (!match_op(token
, '}')) {
1815 if (!match_op(token
, ';'))
1816 token
= declaration_list(token
, list
);
1817 if (!match_op(token
, ';')) {
1818 sparse_error(token
->pos
, "expected ; at end of declaration");
1821 token
= token
->next
;
1826 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1828 struct decl_state ctx
= {.prefer_abstract
= 1};
1830 token
= declaration_specifiers(token
, &ctx
);
1831 ctx
.ident
= &sym
->ident
;
1832 token
= declarator(token
, &ctx
);
1833 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1834 apply_modifiers(token
->pos
, &ctx
);
1835 sym
->ctype
= ctx
.ctype
;
1836 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1837 sym
->endpos
= token
->pos
;
1841 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1843 struct decl_state ctx
= {.prefer_abstract
= 1};
1845 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1847 token
= declaration_specifiers(token
, &ctx
);
1848 token
= declarator(token
, &ctx
);
1849 apply_modifiers(token
->pos
, &ctx
);
1850 sym
->ctype
= ctx
.ctype
;
1851 sym
->endpos
= token
->pos
;
1852 class = ctx
.storage_class
;
1855 if (class == SForced
) {
1861 warning(sym
->pos
, "storage class in typename (%s %s)",
1862 storage_class
[class], show_typename(sym
));
1866 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1868 token
= parse_expression(token
, tree
);
1869 return expect(token
, ';', "at end of statement");
1872 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1873 struct expression_list
**inout
)
1875 struct expression
*expr
;
1877 /* Allow empty operands */
1878 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1881 struct ident
*ident
= NULL
;
1882 if (match_op(token
->next
, '[') &&
1883 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1884 match_op(token
->next
->next
->next
, ']')) {
1885 ident
= token
->next
->next
->ident
;
1886 token
= token
->next
->next
->next
;
1888 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1889 token
= primary_expression(token
->next
, &expr
);
1890 add_expression(inout
, expr
);
1891 token
= parens_expression(token
, &expr
, "in asm parameter");
1892 add_expression(inout
, expr
);
1893 } while (match_op(token
, ','));
1897 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1898 struct expression_list
**clobbers
)
1900 struct expression
*expr
;
1903 token
= primary_expression(token
->next
, &expr
);
1905 add_expression(clobbers
, expr
);
1906 } while (match_op(token
, ','));
1910 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1911 struct symbol_list
**labels
)
1913 struct symbol
*label
;
1916 token
= token
->next
; /* skip ':' and ',' */
1917 if (token_type(token
) != TOKEN_IDENT
)
1919 label
= label_symbol(token
);
1920 add_symbol(labels
, label
);
1921 token
= token
->next
;
1922 } while (match_op(token
, ','));
1926 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1930 token
= token
->next
;
1931 stmt
->type
= STMT_ASM
;
1932 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1933 token
= token
->next
;
1935 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
1937 token
= token
->next
;
1939 token
= expect(token
, '(', "after asm");
1940 token
= parse_expression(token
, &stmt
->asm_string
);
1941 if (match_op(token
, ':'))
1942 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1943 if (match_op(token
, ':'))
1944 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1945 if (match_op(token
, ':'))
1946 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1947 if (is_goto
&& match_op(token
, ':'))
1948 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
1949 token
= expect(token
, ')', "after asm");
1950 return expect(token
, ';', "at end of asm-statement");
1953 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1955 struct expression
*expr
;
1956 token
= expect(token
, '(', "after asm");
1957 token
= parse_expression(token
->next
, &expr
);
1958 token
= expect(token
, ')', "after asm");
1962 /* Make a statement out of an expression */
1963 static struct statement
*make_statement(struct expression
*expr
)
1965 struct statement
*stmt
;
1969 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1970 stmt
->expression
= expr
;
1975 * All iterators have two symbols associated with them:
1976 * the "continue" and "break" symbols, which are targets
1977 * for continue and break statements respectively.
1979 * They are in a special name-space, but they follow
1980 * all the normal visibility rules, so nested iterators
1981 * automatically work right.
1983 static void start_iterator(struct statement
*stmt
)
1985 struct symbol
*cont
, *brk
;
1987 start_symbol_scope();
1988 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1989 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1990 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1991 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1993 stmt
->type
= STMT_ITERATOR
;
1994 stmt
->iterator_break
= brk
;
1995 stmt
->iterator_continue
= cont
;
1996 fn_local_symbol(brk
);
1997 fn_local_symbol(cont
);
2000 static void end_iterator(struct statement
*stmt
)
2005 static struct statement
*start_function(struct symbol
*sym
)
2008 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
2010 start_function_scope();
2011 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
2012 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
2013 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
2014 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
2015 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2017 fn_local_symbol(ret
);
2019 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2025 static void end_function(struct symbol
*sym
)
2028 end_function_scope();
2032 * A "switch()" statement, like an iterator, has a
2033 * the "break" symbol associated with it. It works
2034 * exactly like the iterator break - it's the target
2035 * for any break-statements in scope, and means that
2036 * "break" handling doesn't even need to know whether
2037 * it's breaking out of an iterator or a switch.
2039 * In addition, the "case" symbol is a marker for the
2040 * case/default statements to find the switch statement
2041 * that they are associated with.
2043 static void start_switch(struct statement
*stmt
)
2045 struct symbol
*brk
, *switch_case
;
2047 start_symbol_scope();
2048 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2049 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2051 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2052 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2053 switch_case
->stmt
= stmt
;
2055 stmt
->type
= STMT_SWITCH
;
2056 stmt
->switch_break
= brk
;
2057 stmt
->switch_case
= switch_case
;
2059 fn_local_symbol(brk
);
2060 fn_local_symbol(switch_case
);
2063 static void end_switch(struct statement
*stmt
)
2065 if (!stmt
->switch_case
->symbol_list
)
2066 warning(stmt
->pos
, "switch with no cases");
2070 static void add_case_statement(struct statement
*stmt
)
2072 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2076 sparse_error(stmt
->pos
, "not in switch scope");
2077 stmt
->type
= STMT_NONE
;
2080 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2081 add_symbol(&target
->symbol_list
, sym
);
2083 stmt
->case_label
= sym
;
2084 fn_local_symbol(sym
);
2087 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2089 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2092 error_die(token
->pos
, "internal error: return without a function target");
2093 stmt
->type
= STMT_RETURN
;
2094 stmt
->ret_target
= target
;
2095 return expression_statement(token
->next
, &stmt
->ret_value
);
2098 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2100 struct symbol_list
*syms
;
2101 struct expression
*e1
, *e2
, *e3
;
2102 struct statement
*iterator
;
2104 start_iterator(stmt
);
2105 token
= expect(token
->next
, '(', "after 'for'");
2109 /* C99 variable declaration? */
2110 if (lookup_type(token
)) {
2111 token
= external_declaration(token
, &syms
);
2113 token
= parse_expression(token
, &e1
);
2114 token
= expect(token
, ';', "in 'for'");
2116 token
= parse_expression(token
, &e2
);
2117 token
= expect(token
, ';', "in 'for'");
2118 token
= parse_expression(token
, &e3
);
2119 token
= expect(token
, ')', "in 'for'");
2120 token
= statement(token
, &iterator
);
2122 stmt
->iterator_syms
= syms
;
2123 stmt
->iterator_pre_statement
= make_statement(e1
);
2124 stmt
->iterator_pre_condition
= e2
;
2125 stmt
->iterator_post_statement
= make_statement(e3
);
2126 stmt
->iterator_post_condition
= NULL
;
2127 stmt
->iterator_statement
= iterator
;
2133 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2135 struct expression
*expr
;
2136 struct statement
*iterator
;
2138 start_iterator(stmt
);
2139 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2140 token
= statement(token
, &iterator
);
2142 stmt
->iterator_pre_condition
= expr
;
2143 stmt
->iterator_post_condition
= NULL
;
2144 stmt
->iterator_statement
= iterator
;
2150 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2152 struct expression
*expr
;
2153 struct statement
*iterator
;
2155 start_iterator(stmt
);
2156 token
= statement(token
->next
, &iterator
);
2157 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2158 token
= token
->next
;
2160 sparse_error(token
->pos
, "expected 'while' after 'do'");
2161 token
= parens_expression(token
, &expr
, "after 'do-while'");
2163 stmt
->iterator_post_condition
= expr
;
2164 stmt
->iterator_statement
= iterator
;
2167 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2168 warning(iterator
->pos
, "do-while statement is not a compound statement");
2170 return expect(token
, ';', "after statement");
2173 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2175 stmt
->type
= STMT_IF
;
2176 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2177 token
= statement(token
, &stmt
->if_true
);
2178 if (token_type(token
) != TOKEN_IDENT
)
2180 if (token
->ident
!= &else_ident
)
2182 return statement(token
->next
, &stmt
->if_false
);
2185 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2187 stmt
->type
= STMT_CASE
;
2188 token
= expect(token
, ':', "after default/case");
2189 add_case_statement(stmt
);
2190 return statement(token
, &stmt
->case_statement
);
2193 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2195 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2196 if (match_op(token
, SPECIAL_ELLIPSIS
))
2197 token
= parse_expression(token
->next
, &stmt
->case_to
);
2198 return case_statement(token
, stmt
);
2201 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2203 return case_statement(token
->next
, stmt
);
2206 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2208 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2209 stmt
->type
= STMT_GOTO
;
2210 stmt
->goto_label
= target
;
2212 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2213 return expect(token
->next
, ';', "at end of statement");
2216 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2218 stmt
->type
= STMT_SWITCH
;
2220 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2221 token
= statement(token
, &stmt
->switch_statement
);
2226 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2228 stmt
->type
= STMT_GOTO
;
2229 token
= token
->next
;
2230 if (match_op(token
, '*')) {
2231 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2232 add_statement(&function_computed_goto_list
, stmt
);
2233 } else if (token_type(token
) == TOKEN_IDENT
) {
2234 stmt
->goto_label
= label_symbol(token
);
2235 token
= token
->next
;
2237 sparse_error(token
->pos
, "Expected identifier or goto expression");
2239 return expect(token
, ';', "at end of statement");
2242 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2244 stmt
->type
= STMT_CONTEXT
;
2245 token
= parse_expression(token
->next
, &stmt
->expression
);
2246 if (stmt
->expression
->type
== EXPR_PREOP
2247 && stmt
->expression
->op
== '('
2248 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2249 struct expression
*expr
;
2250 expr
= stmt
->expression
->unop
;
2251 stmt
->context
= expr
->left
;
2252 stmt
->expression
= expr
->right
;
2254 return expect(token
, ';', "at end of statement");
2257 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2259 stmt
->type
= STMT_RANGE
;
2260 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2261 token
= expect(token
, ',', "after range expression");
2262 token
= assignment_expression(token
, &stmt
->range_low
);
2263 token
= expect(token
, ',', "after low range");
2264 token
= assignment_expression(token
, &stmt
->range_high
);
2265 return expect(token
, ';', "after range statement");
2268 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2270 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2273 if (token_type(token
) == TOKEN_IDENT
) {
2274 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2275 if (s
&& s
->op
->statement
)
2276 return s
->op
->statement(token
, stmt
);
2278 if (match_op(token
->next
, ':')) {
2279 stmt
->type
= STMT_LABEL
;
2280 stmt
->label_identifier
= label_symbol(token
);
2281 token
= skip_attributes(token
->next
->next
);
2282 return statement(token
, &stmt
->label_statement
);
2286 if (match_op(token
, '{')) {
2287 stmt
->type
= STMT_COMPOUND
;
2288 start_symbol_scope();
2289 token
= compound_statement(token
->next
, stmt
);
2292 return expect(token
, '}', "at end of compound statement");
2295 stmt
->type
= STMT_EXPRESSION
;
2296 return expression_statement(token
, &stmt
->expression
);
2299 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2300 static struct token
*label_statement(struct token
*token
)
2302 while (token_type(token
) == TOKEN_IDENT
) {
2303 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2304 /* it's block-scope, but we want label namespace */
2305 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2306 sym
->namespace = NS_LABEL
;
2307 fn_local_symbol(sym
);
2308 token
= token
->next
;
2309 if (!match_op(token
, ','))
2311 token
= token
->next
;
2313 return expect(token
, ';', "at end of label declaration");
2316 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2318 int seen_statement
= 0;
2319 while (token_type(token
) == TOKEN_IDENT
&&
2320 token
->ident
== &__label___ident
)
2321 token
= label_statement(token
->next
);
2323 struct statement
* stmt
;
2324 if (eof_token(token
))
2326 if (match_op(token
, '}'))
2328 if (lookup_type(token
)) {
2329 if (seen_statement
) {
2330 warning(token
->pos
, "mixing declarations and code");
2333 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2334 token
= external_declaration(token
, &stmt
->declaration
);
2336 seen_statement
= Wdeclarationafterstatement
;
2337 token
= statement(token
, &stmt
);
2339 add_statement(list
, stmt
);
2344 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2346 struct symbol_list
**list
= &fn
->arguments
;
2348 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2349 sym
->ident
= token
->ident
;
2350 token
= token
->next
;
2351 sym
->endpos
= token
->pos
;
2352 sym
->ctype
.base_type
= &incomplete_ctype
;
2353 add_symbol(list
, sym
);
2354 if (!match_op(token
, ',') ||
2355 token_type(token
->next
) != TOKEN_IDENT
||
2356 lookup_type(token
->next
))
2358 token
= token
->next
;
2363 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2365 struct symbol_list
**list
= &fn
->arguments
;
2370 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2372 token
= token
->next
;
2376 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2377 token
= parameter_declaration(token
, sym
);
2378 if (sym
->ctype
.base_type
== &void_ctype
) {
2379 /* Special case: (void) */
2380 if (!*list
&& !sym
->ident
)
2382 warning(token
->pos
, "void parameter");
2384 add_symbol(list
, sym
);
2385 if (!match_op(token
, ','))
2387 token
= token
->next
;
2392 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2394 token
= statement_list(token
, &stmt
->stmts
);
2398 static struct expression
*identifier_expression(struct token
*token
)
2400 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2401 expr
->expr_ident
= token
->ident
;
2405 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2407 int idx_from
, idx_to
;
2408 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2410 idx_from
= const_expression_value(from
);
2413 idx_to
= const_expression_value(to
);
2414 if (idx_to
< idx_from
|| idx_from
< 0)
2415 warning(from
->pos
, "nonsense array initializer index range");
2417 expr
->idx_from
= idx_from
;
2418 expr
->idx_to
= idx_to
;
2422 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2424 int expect_equal
= 0;
2425 struct token
*next
= token
->next
;
2426 struct expression
**tail
= ep
;
2431 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2432 struct expression
*expr
= identifier_expression(token
);
2433 if (Wold_initializer
)
2434 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2435 token
= initializer(&expr
->ident_expression
, next
->next
);
2436 if (expr
->ident_expression
)
2441 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2442 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2443 struct expression
*expr
= identifier_expression(next
);
2445 tail
= &expr
->ident_expression
;
2448 } else if (match_op(token
, '[')) {
2449 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2450 token
= constant_expression(token
->next
, &from
);
2452 sparse_error(token
->pos
, "Expected constant expression");
2455 if (match_op(token
, SPECIAL_ELLIPSIS
))
2456 token
= constant_expression(token
->next
, &to
);
2457 expr
= index_expression(from
, to
);
2459 tail
= &expr
->idx_expression
;
2460 token
= expect(token
, ']', "at end of initializer index");
2467 if (nested
&& !expect_equal
) {
2468 if (!match_op(token
, '='))
2469 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2474 token
= expect(token
, '=', "at end of initializer index");
2476 token
= initializer(tail
, token
);
2482 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2484 struct expression
*expr
;
2487 token
= single_initializer(&expr
, token
);
2490 add_expression(list
, expr
);
2491 if (!match_op(token
, ','))
2493 token
= token
->next
;
2498 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2500 if (match_op(token
, '{')) {
2501 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2503 token
= initializer_list(&expr
->expr_list
, token
->next
);
2504 return expect(token
, '}', "at end of initializer");
2506 return assignment_expression(token
, tree
);
2509 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2512 sparse_error(sym
->pos
, "no identifier for function argument");
2515 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2518 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2519 struct symbol_list
**list
)
2521 struct symbol_list
**old_symbol_list
;
2522 struct symbol
*base_type
= decl
->ctype
.base_type
;
2523 struct statement
*stmt
, **p
;
2524 struct symbol
*prev
;
2527 old_symbol_list
= function_symbol_list
;
2528 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2529 function_symbol_list
= &decl
->inline_symbol_list
;
2530 p
= &base_type
->inline_stmt
;
2532 function_symbol_list
= &decl
->symbol_list
;
2533 p
= &base_type
->stmt
;
2535 function_computed_target_list
= NULL
;
2536 function_computed_goto_list
= NULL
;
2538 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2539 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2540 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2542 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2543 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2545 stmt
= start_function(decl
);
2548 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2549 declare_argument(arg
, base_type
);
2550 } END_FOR_EACH_PTR(arg
);
2552 token
= compound_statement(token
->next
, stmt
);
2555 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2556 add_symbol(list
, decl
);
2557 check_declaration(decl
);
2558 decl
->definition
= decl
;
2559 prev
= decl
->same_symbol
;
2560 if (prev
&& prev
->definition
) {
2561 warning(decl
->pos
, "multiple definitions for function '%s'",
2562 show_ident(decl
->ident
));
2563 info(prev
->definition
->pos
, " the previous one is here");
2566 prev
->definition
= decl
;
2567 prev
= prev
->same_symbol
;
2570 function_symbol_list
= old_symbol_list
;
2571 if (function_computed_goto_list
) {
2572 if (!function_computed_target_list
)
2573 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2575 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2576 stmt
->target_list
= function_computed_target_list
;
2577 } END_FOR_EACH_PTR(stmt
);
2580 return expect(token
, '}', "at end of function");
2583 static void promote_k_r_types(struct symbol
*arg
)
2585 struct symbol
*base
= arg
->ctype
.base_type
;
2586 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2587 arg
->ctype
.base_type
= &int_ctype
;
2591 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2593 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2596 FOR_EACH_PTR(real_args
, arg
) {
2597 struct symbol
*type
;
2599 /* This is quadratic in the number of arguments. We _really_ don't care */
2600 FOR_EACH_PTR(argtypes
, type
) {
2601 if (type
->ident
== arg
->ident
)
2603 } END_FOR_EACH_PTR(type
);
2604 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2608 /* "char" and "short" promote to "int" */
2609 promote_k_r_types(type
);
2611 arg
->ctype
= type
->ctype
;
2612 } END_FOR_EACH_PTR(arg
);
2614 FOR_EACH_PTR(argtypes
, arg
) {
2616 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2617 } END_FOR_EACH_PTR(arg
);
2621 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2622 struct symbol_list
**list
)
2624 struct symbol_list
*args
= NULL
;
2626 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2628 token
= declaration_list(token
, &args
);
2629 if (!match_op(token
, ';')) {
2630 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2633 token
= token
->next
;
2634 } while (lookup_type(token
));
2636 apply_k_r_types(args
, decl
);
2638 if (!match_op(token
, '{')) {
2639 sparse_error(token
->pos
, "expected function body");
2642 return parse_function_body(token
, decl
, list
);
2645 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2647 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2648 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2649 struct statement
*stmt
;
2651 anon
->ctype
.base_type
= fn
;
2652 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2655 token
= parse_asm_statement(token
, stmt
);
2657 add_symbol(list
, anon
);
2661 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2663 struct ident
*ident
= NULL
;
2664 struct symbol
*decl
;
2665 struct decl_state ctx
= { .ident
= &ident
};
2667 struct symbol
*base_type
;
2671 /* Top-level inline asm? */
2672 if (token_type(token
) == TOKEN_IDENT
) {
2673 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2674 if (s
&& s
->op
->toplevel
)
2675 return s
->op
->toplevel(token
, list
);
2678 /* Parse declaration-specifiers, if any */
2679 token
= declaration_specifiers(token
, &ctx
);
2680 mod
= storage_modifiers(&ctx
);
2681 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2682 /* Just a type declaration? */
2683 if (match_op(token
, ';')) {
2684 apply_modifiers(token
->pos
, &ctx
);
2689 token
= declarator(token
, &ctx
);
2690 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2691 apply_modifiers(token
->pos
, &ctx
);
2693 decl
->ctype
= ctx
.ctype
;
2694 decl
->ctype
.modifiers
|= mod
;
2695 decl
->endpos
= token
->pos
;
2697 /* Just a type declaration? */
2699 warning(token
->pos
, "missing identifier in declaration");
2700 return expect(token
, ';', "at the end of type declaration");
2703 /* type define declaration? */
2704 is_typedef
= ctx
.storage_class
== STypedef
;
2706 /* Typedefs don't have meaningful storage */
2708 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2710 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2712 base_type
= decl
->ctype
.base_type
;
2715 if (base_type
&& !base_type
->ident
) {
2716 switch (base_type
->type
) {
2721 base_type
->ident
= ident
;
2724 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2725 /* K&R argument declaration? */
2726 if (lookup_type(token
))
2727 return parse_k_r_arguments(token
, decl
, list
);
2728 if (match_op(token
, '{'))
2729 return parse_function_body(token
, decl
, list
);
2731 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2732 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2733 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2734 sparse_error(token
->pos
, "void declaration");
2738 if (!is_typedef
&& match_op(token
, '=')) {
2739 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2740 warning(decl
->pos
, "symbol with external linkage has initializer");
2741 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2743 token
= initializer(&decl
->initializer
, token
->next
);
2746 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2747 add_symbol(list
, decl
);
2748 fn_local_symbol(decl
);
2751 check_declaration(decl
);
2752 if (decl
->same_symbol
)
2753 decl
->definition
= decl
->same_symbol
->definition
;
2755 if (!match_op(token
, ','))
2758 token
= token
->next
;
2760 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2762 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2763 token
= declarator(token
, &ctx
);
2764 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2765 apply_modifiers(token
->pos
, &ctx
);
2766 decl
->ctype
= ctx
.ctype
;
2767 decl
->ctype
.modifiers
|= mod
;
2768 decl
->endpos
= token
->pos
;
2770 sparse_error(token
->pos
, "expected identifier name in type definition");
2775 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2777 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2779 /* Function declarations are automatically extern unless specifically static */
2780 base_type
= decl
->ctype
.base_type
;
2781 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2782 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2783 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2786 return expect(token
, ';', "at end of declaration");