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
},
467 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
468 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
469 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_QI_op
},
470 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
471 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_HI_op
},
472 { "SI", NS_KEYWORD
, .op
= &mode_SI_op
},
473 { "__SI__", NS_KEYWORD
, .op
= &mode_SI_op
},
474 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
475 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_DI_op
},
476 { "TI", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
477 { "__TI__", NS_KEYWORD
, MOD_LONGLONGLONG
, .op
= &mode_TI_op
},
478 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
479 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_word_op
},
482 const char *ignored_attributes
[] = {
508 "externally_visible",
509 "__externally_visible__",
526 "no_instrument_function",
527 "__no_instrument_function__",
549 "__syscall_linkage__",
558 "warn_unused_result",
559 "__warn_unused_result__",
567 void init_parser(int stream
)
570 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
571 struct init_keyword
*ptr
= keyword_table
+ i
;
572 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
573 sym
->ident
->keyword
= 1;
574 if (ptr
->ns
== NS_TYPEDEF
)
575 sym
->ident
->reserved
= 1;
576 sym
->ctype
.modifiers
= ptr
->modifiers
;
577 sym
->ctype
.base_type
= ptr
->type
;
581 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
582 const char * name
= ignored_attributes
[i
];
583 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
585 sym
->ident
->keyword
= 1;
586 sym
->op
= &ignore_attr_op
;
591 // Add a symbol to the list of function-local symbols
592 static void fn_local_symbol(struct symbol
*sym
)
594 if (function_symbol_list
)
595 add_symbol(function_symbol_list
, sym
);
598 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
603 if (token_type(token
) != TOKEN_IDENT
)
606 va_start(args
, token
);
608 next
= va_arg(args
, struct ident
*);
609 } while (next
&& token
->ident
!= next
);
612 return next
&& token
->ident
== next
;
616 struct statement
*alloc_statement(struct position pos
, int type
)
618 struct statement
*stmt
= __alloc_statement(0);
624 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
626 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
628 struct symbol
*ctype
;
631 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
633 sparse_error(pos
, "don't know how to apply mode to %s",
634 show_typename(ctx
->ctype
.base_type
));
636 ctx
->ctype
.base_type
= ctype
;
640 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
642 struct symbol
*sym
= alloc_symbol(pos
, type
);
644 sym
->ctype
.base_type
= ctype
->base_type
;
645 sym
->ctype
.modifiers
= ctype
->modifiers
;
647 ctype
->base_type
= sym
;
648 ctype
->modifiers
= 0;
653 * NOTE! NS_LABEL is not just a different namespace,
654 * it also ends up using function scope instead of the
655 * regular symbol scope.
657 struct symbol
*label_symbol(struct token
*token
)
659 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
661 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
662 bind_symbol(sym
, token
->ident
, NS_LABEL
);
663 fn_local_symbol(sym
);
668 static struct token
*struct_union_enum_specifier(enum type type
,
669 struct token
*token
, struct decl_state
*ctx
,
670 struct token
*(*parse
)(struct token
*, struct symbol
*))
673 struct position
*repos
;
675 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
676 if (token_type(token
) == TOKEN_IDENT
) {
677 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
679 (is_outer_scope(sym
->scope
) &&
680 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
681 // Either a new symbol, or else an out-of-scope
682 // symbol being redefined.
683 sym
= alloc_symbol(token
->pos
, type
);
684 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
686 if (sym
->type
!= type
)
687 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
688 ctx
->ctype
.base_type
= sym
;
691 if (match_op(token
, '{')) {
692 // The following test is actually wrong for empty
693 // structs, but (1) they are not C99, (2) gcc does
694 // the same thing, and (3) it's easier.
695 if (sym
->symbol_list
)
696 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
698 token
= parse(token
->next
, sym
);
699 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
701 // Mark the structure as needing re-examination
703 sym
->endpos
= token
->pos
;
708 // private struct/union/enum type
709 if (!match_op(token
, '{')) {
710 sparse_error(token
->pos
, "expected declaration");
711 ctx
->ctype
.base_type
= &bad_ctype
;
715 sym
= alloc_symbol(token
->pos
, type
);
716 token
= parse(token
->next
, sym
);
717 ctx
->ctype
.base_type
= sym
;
718 token
= expect(token
, '}', "at end of specifier");
719 sym
->endpos
= token
->pos
;
724 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
726 struct symbol
*field
, *last
= NULL
;
728 res
= struct_declaration_list(token
, &sym
->symbol_list
);
729 FOR_EACH_PTR(sym
->symbol_list
, field
) {
731 struct symbol
*base
= field
->ctype
.base_type
;
732 if (base
&& base
->type
== SYM_BITFIELD
)
736 last
->next_subobject
= field
;
738 } END_FOR_EACH_PTR(field
);
742 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
744 return struct_declaration_list(token
, &sym
->symbol_list
);
747 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
749 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
752 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
754 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
760 unsigned long long y
;
763 static void upper_boundary(Num
*n
, Num
*v
)
775 static void lower_boundary(Num
*n
, Num
*v
)
787 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
789 int shift
= type
->bit_size
;
790 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
794 if (upper
->x
== 0 && upper
->y
>> shift
)
796 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
801 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
803 if (s1
->bit_size
< s2
->bit_size
) {
805 } else if (s1
->bit_size
== s2
->bit_size
) {
806 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
809 if (s1
->bit_size
< bits_in_int
)
814 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
818 FOR_EACH_PTR(list
, sym
) {
819 struct expression
*expr
= sym
->initializer
;
820 struct symbol
*ctype
;
821 if (expr
->type
!= EXPR_VALUE
)
824 if (ctype
->bit_size
== base_type
->bit_size
)
826 cast_value(expr
, base_type
, expr
, ctype
);
827 } END_FOR_EACH_PTR(sym
);
830 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
832 unsigned long long lastval
= 0;
833 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
834 Num upper
= {-1, 0}, lower
= {1, 0};
836 parent
->examined
= 1;
837 parent
->ctype
.base_type
= &int_ctype
;
838 while (token_type(token
) == TOKEN_IDENT
) {
839 struct expression
*expr
= NULL
;
840 struct token
*next
= token
->next
;
843 if (match_op(next
, '=')) {
844 next
= constant_expression(next
->next
, &expr
);
845 lastval
= get_expression_value(expr
);
847 if (expr
&& expr
->ctype
)
851 } else if (is_int_type(ctype
)) {
854 error_die(token
->pos
, "can't increment the last enum member");
858 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
859 expr
->value
= lastval
;
863 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
864 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
865 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
866 sym
->initializer
= expr
;
867 sym
->enum_member
= 1;
868 sym
->ctype
.base_type
= parent
;
869 add_ptr_list(&parent
->symbol_list
, sym
);
871 if (base_type
!= &bad_ctype
) {
872 if (ctype
->type
== SYM_NODE
)
873 ctype
= ctype
->ctype
.base_type
;
874 if (ctype
->type
== SYM_ENUM
) {
878 ctype
= ctype
->ctype
.base_type
;
882 * - if all enums are of the same type, then
883 * the base_type is that type (two first
885 * - if enums are of different types, they
886 * all have to be integer types, and the
887 * base type is at least "int_ctype".
888 * - otherwise the base_type is "bad_ctype".
892 } else if (ctype
== base_type
) {
894 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
895 base_type
= bigger_enum_type(base_type
, ctype
);
897 base_type
= &bad_ctype
;
898 parent
->ctype
.base_type
= base_type
;
900 if (is_int_type(base_type
)) {
901 Num v
= {.y
= lastval
};
902 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
904 else if ((long long)lastval
>= 0)
908 upper_boundary(&upper
, &v
);
909 lower_boundary(&lower
, &v
);
913 sym
->endpos
= token
->pos
;
915 if (!match_op(token
, ','))
920 sparse_error(token
->pos
, "bad enum definition");
921 base_type
= &bad_ctype
;
923 else if (!is_int_type(base_type
))
924 base_type
= base_type
;
925 else if (type_is_ok(base_type
, &upper
, &lower
))
926 base_type
= base_type
;
927 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
928 base_type
= &int_ctype
;
929 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
930 base_type
= &uint_ctype
;
931 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
932 base_type
= &long_ctype
;
933 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
934 base_type
= &ulong_ctype
;
935 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
936 base_type
= &llong_ctype
;
937 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
938 base_type
= &ullong_ctype
;
940 base_type
= &bad_ctype
;
941 parent
->ctype
.base_type
= base_type
;
942 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
943 parent
->examined
= 0;
945 cast_enum_list(parent
->symbol_list
, base_type
);
950 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
952 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
953 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
955 if (!ctype
->base_type
)
956 ctype
->base_type
= &incomplete_ctype
;
961 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
963 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
967 if (!match_op(token
, '(')) {
968 sparse_error(token
->pos
, "expected '(' after typeof");
971 if (lookup_type(token
->next
)) {
972 token
= typename(token
->next
, &sym
, NULL
);
973 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
974 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
976 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
977 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
979 typeof_sym
->endpos
= token
->pos
;
980 if (!typeof_sym
->initializer
) {
981 sparse_error(token
->pos
, "expected expression after the '(' token");
982 typeof_sym
= &bad_ctype
;
984 ctx
->ctype
.base_type
= typeof_sym
;
986 return expect(token
, ')', "after typeof");
989 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
991 struct expression
*expr
= NULL
;
992 if (match_op(token
, '('))
993 token
= parens_expression(token
, &expr
, "in attribute");
997 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
999 if (!ctx
->ctype
.alignment
)
1000 ctx
->ctype
.alignment
= 1;
1004 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1006 int alignment
= max_alignment
;
1007 struct expression
*expr
= NULL
;
1009 if (match_op(token
, '(')) {
1010 token
= parens_expression(token
, &expr
, "in attribute");
1012 alignment
= const_expression_value(expr
);
1014 if (alignment
& (alignment
-1)) {
1015 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1017 } else if (alignment
> ctx
->ctype
.alignment
)
1018 ctx
->ctype
.alignment
= alignment
;
1022 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1024 if (ctx
->modifiers
& qual
)
1025 warning(*pos
, "duplicate %s", modifier_string(qual
));
1026 ctx
->modifiers
|= qual
;
1029 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1031 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1035 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1037 struct expression
*expr
= NULL
;
1039 token
= expect(token
, '(', "after address_space attribute");
1040 token
= conditional_expression(token
, &expr
);
1042 as
= const_expression_value(expr
);
1043 if (Waddress_space
&& as
)
1046 token
= expect(token
, ')', "after address_space attribute");
1050 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1052 if (ctype
->ctype
.base_type
!= &int_type
)
1054 if (ctype
== &char_ctype
)
1056 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1060 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1062 if (ctype
->ctype
.base_type
!= &int_type
)
1064 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1068 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1070 if (ctype
->ctype
.base_type
!= &int_type
)
1072 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1076 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1078 if (ctype
->ctype
.base_type
!= &int_type
)
1080 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1084 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1086 if (ctype
->ctype
.base_type
!= &int_type
)
1088 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1092 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1094 if (ctype
->ctype
.base_type
!= &int_type
)
1096 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1100 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1102 token
= expect(token
, '(', "after mode attribute");
1103 if (token_type(token
) == TOKEN_IDENT
) {
1104 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1105 if (mode
&& mode
->op
->type
== KW_MODE
)
1106 ctx
->mode
= mode
->op
;
1108 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1109 token
= token
->next
;
1111 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1112 token
= expect(token
, ')', "after mode attribute");
1116 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1118 struct context
*context
= alloc_context();
1119 struct expression
*args
[3];
1122 token
= expect(token
, '(', "after context attribute");
1123 while (!match_op(token
, ')')) {
1124 struct expression
*expr
= NULL
;
1125 token
= conditional_expression(token
, &expr
);
1129 args
[argc
++] = expr
;
1130 if (!match_op(token
, ','))
1132 token
= token
->next
;
1137 sparse_error(token
->pos
, "expected context input/output values");
1140 context
->in
= get_expression_value(args
[0]);
1143 context
->in
= get_expression_value(args
[0]);
1144 context
->out
= get_expression_value(args
[1]);
1147 context
->context
= args
[0];
1148 context
->in
= get_expression_value(args
[1]);
1149 context
->out
= get_expression_value(args
[2]);
1154 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1156 token
= expect(token
, ')', "after context attribute");
1160 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1162 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1163 ctx
->ctype
.base_type
->designated_init
= 1;
1165 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1169 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1171 if (Wtransparent_union
)
1172 warning(token
->pos
, "ignoring attribute __transparent_union__");
1176 static struct token
*recover_unknown_attribute(struct token
*token
)
1178 struct expression
*expr
= NULL
;
1180 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1181 token
= token
->next
;
1182 if (match_op(token
, '('))
1183 token
= parens_expression(token
, &expr
, "in attribute");
1187 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1189 token
= expect(token
, '(', "after attribute");
1190 token
= expect(token
, '(', "after attribute");
1193 struct ident
*attribute_name
;
1194 struct symbol
*attr
;
1196 if (eof_token(token
))
1198 if (match_op(token
, ';'))
1200 if (token_type(token
) != TOKEN_IDENT
)
1202 attribute_name
= token
->ident
;
1203 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1204 if (attr
&& attr
->op
->attribute
)
1205 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1207 token
= recover_unknown_attribute(token
);
1209 if (!match_op(token
, ','))
1211 token
= token
->next
;
1214 token
= expect(token
, ')', "after attribute");
1215 token
= expect(token
, ')', "after attribute");
1219 static const char *storage_class
[] =
1221 [STypedef
] = "typedef",
1223 [SExtern
] = "extern",
1224 [SStatic
] = "static",
1225 [SRegister
] = "register",
1226 [SForced
] = "[force]"
1229 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1231 static unsigned long mod
[] =
1234 [SExtern
] = MOD_EXTERN
,
1235 [SStatic
] = MOD_STATIC
,
1236 [SRegister
] = MOD_REGISTER
1238 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1239 | (ctx
->is_tls
? MOD_TLS
: 0);
1242 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1244 /* __thread can be used alone, or with extern or static */
1245 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1246 sparse_error(*pos
, "__thread can only be used alone, or with "
1247 "extern or static");
1251 if (!ctx
->storage_class
) {
1252 ctx
->storage_class
= class;
1255 if (ctx
->storage_class
== class)
1256 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1258 sparse_error(*pos
, "multiple storage classes");
1261 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1263 set_storage_class(&next
->pos
, ctx
, STypedef
);
1267 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1269 set_storage_class(&next
->pos
, ctx
, SAuto
);
1273 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1275 set_storage_class(&next
->pos
, ctx
, SRegister
);
1279 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1281 set_storage_class(&next
->pos
, ctx
, SStatic
);
1285 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1287 set_storage_class(&next
->pos
, ctx
, SExtern
);
1291 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1293 /* This GCC extension can be used alone, or with extern or static */
1294 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1295 || ctx
->storage_class
== SExtern
) {
1298 sparse_error(next
->pos
, "__thread can only be used alone, or "
1299 "with extern or static");
1305 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1307 set_storage_class(&token
->pos
, ctx
, SForced
);
1311 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1317 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1319 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1323 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1325 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1329 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1331 unsigned long mod
= thistype
->modifiers
;
1334 apply_qualifier(&pos
, ctype
, mod
);
1337 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1338 (struct ptr_list
**)&ctype
->contexts
);
1341 if (thistype
->alignment
> ctype
->alignment
)
1342 ctype
->alignment
= thistype
->alignment
;
1346 ctype
->as
= thistype
->as
;
1349 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1352 if (what
& (Set_S
| Set_T
))
1354 if (what
& Set_Char
)
1356 else if (what
& Set_Double
)
1358 else if (what
& Set_Float
)
1360 else if (what
& Set_Signed
)
1362 else if (what
& Set_Unsigned
)
1364 else if (what
& Set_Short
)
1366 else if (what
& Set_Long
)
1370 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1371 old
, show_ident(new));
1375 sparse_error(pos
, "two or more data types in declaration specifiers");
1378 static struct symbol
* const int_types
[] =
1379 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1380 static struct symbol
* const signed_types
[] =
1381 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1383 static struct symbol
* const unsigned_types
[] =
1384 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1386 static struct symbol
* const real_types
[] =
1387 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1388 static struct symbol
* const char_types
[] =
1389 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1390 static struct symbol
* const * const types
[] = {
1391 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1392 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1395 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1397 return types
[want_unsigned
? CUInt
: CInt
][size
];
1400 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1402 while (token_type(t
) == TOKEN_IDENT
) {
1403 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1406 if (s
->type
!= SYM_KEYWORD
)
1408 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1411 if (s
->op
->declarator
)
1412 t
= s
->op
->declarator(t
, ctx
);
1417 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1423 while (token_type(token
) == TOKEN_IDENT
) {
1424 struct symbol
*s
= lookup_symbol(token
->ident
,
1425 NS_TYPEDEF
| NS_SYMBOL
);
1426 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1428 if (s
->type
!= SYM_KEYWORD
) {
1431 seen
|= Set_S
| Set_T
;
1432 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1433 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1434 token
= token
->next
;
1437 if (s
->op
->type
& KW_SPECIFIER
) {
1438 if (seen
& s
->op
->test
) {
1439 specifier_conflict(token
->pos
,
1445 class += s
->op
->class;
1446 if (s
->op
->type
& KW_SHORT
) {
1448 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1449 if (class == CReal
) {
1450 specifier_conflict(token
->pos
,
1458 token
= token
->next
;
1459 if (s
->op
->declarator
)
1460 token
= s
->op
->declarator(token
, ctx
);
1461 if (s
->op
->type
& KW_EXACT
) {
1462 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1463 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1467 if (!(seen
& Set_S
)) { /* not set explicitly? */
1468 struct symbol
*base
= &incomplete_ctype
;
1470 base
= types
[class][size
];
1471 ctx
->ctype
.base_type
= base
;
1474 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1475 struct symbol
*type
;
1476 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1477 if (!is_int_type(ctx
->ctype
.base_type
)) {
1478 sparse_error(token
->pos
, "invalid modifier");
1481 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1482 *type
= *ctx
->ctype
.base_type
;
1483 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1484 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1485 type
->type
= SYM_RESTRICT
;
1486 ctx
->ctype
.base_type
= type
;
1487 create_fouled(type
);
1492 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1494 struct expression
*expr
= NULL
;
1496 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1497 token
= token
->next
;
1498 token
= parse_expression(token
, &expr
);
1499 sym
->array_size
= expr
;
1503 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1504 static struct token
*identifier_list(struct token
*, struct symbol
*);
1505 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1507 static struct token
*skip_attribute(struct token
*token
)
1509 token
= token
->next
;
1510 if (match_op(token
, '(')) {
1512 token
= token
->next
;
1513 while (depth
&& !eof_token(token
)) {
1514 if (token_type(token
) == TOKEN_SPECIAL
) {
1515 if (token
->special
== '(')
1517 else if (token
->special
== ')')
1520 token
= token
->next
;
1526 static struct token
*skip_attributes(struct token
*token
)
1528 struct symbol
*keyword
;
1530 if (token_type(token
) != TOKEN_IDENT
)
1532 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1533 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1535 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1537 token
= expect(token
->next
, '(', "after attribute");
1538 token
= expect(token
, '(', "after attribute");
1540 if (eof_token(token
))
1542 if (match_op(token
, ';'))
1544 if (token_type(token
) != TOKEN_IDENT
)
1546 token
= skip_attribute(token
);
1547 if (!match_op(token
, ','))
1549 token
= token
->next
;
1551 token
= expect(token
, ')', "after attribute");
1552 token
= expect(token
, ')', "after attribute");
1557 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1559 struct symbol
*keyword
;
1561 if (token_type(token
) != TOKEN_IDENT
)
1563 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1564 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1566 if (!(keyword
->op
->type
& keywords
))
1568 token
= keyword
->op
->declarator(token
->next
, ctx
);
1569 keywords
&= KW_ATTRIBUTE
;
1574 static int is_nested(struct token
*token
, struct token
**p
,
1575 int prefer_abstract
)
1578 * This can be either a parameter list or a grouping.
1579 * For the direct (non-abstract) case, we know if must be
1580 * a parameter list if we already saw the identifier.
1581 * For the abstract case, we know if must be a parameter
1582 * list if it is empty or starts with a type.
1584 struct token
*next
= token
->next
;
1586 *p
= next
= skip_attributes(next
);
1588 if (token_type(next
) == TOKEN_IDENT
) {
1589 if (lookup_type(next
))
1590 return !prefer_abstract
;
1594 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1601 Empty
, K_R
, Proto
, Bad_Func
,
1604 static enum kind
which_func(struct token
*token
,
1606 int prefer_abstract
)
1608 struct token
*next
= token
->next
;
1610 if (token_type(next
) == TOKEN_IDENT
) {
1611 if (lookup_type(next
))
1613 /* identifier list not in definition; complain */
1614 if (prefer_abstract
)
1616 "identifier list not in definition");
1620 if (token_type(next
) != TOKEN_SPECIAL
)
1623 if (next
->special
== ')') {
1624 /* don't complain about those */
1625 if (!n
|| match_op(next
->next
, ';'))
1628 "non-ANSI function declaration of function '%s'",
1633 if (next
->special
== SPECIAL_ELLIPSIS
) {
1635 "variadic functions must have one named argument");
1642 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1644 struct ctype
*ctype
= &ctx
->ctype
;
1646 struct ident
**p
= ctx
->ident
;
1648 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1649 *ctx
->ident
= token
->ident
;
1650 token
= token
->next
;
1651 } else if (match_op(token
, '(') &&
1652 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1653 struct symbol
*base_type
= ctype
->base_type
;
1654 if (token
->next
!= next
)
1655 next
= handle_attributes(token
->next
, ctx
,
1657 token
= declarator(next
, ctx
);
1658 token
= expect(token
, ')', "in nested declarator");
1659 while (ctype
->base_type
!= base_type
)
1660 ctype
= &ctype
->base_type
->ctype
;
1664 if (match_op(token
, '(')) {
1665 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1667 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1668 token
= token
->next
;
1670 token
= identifier_list(token
, fn
);
1671 else if (kind
== Proto
)
1672 token
= parameter_type_list(token
, fn
);
1673 token
= expect(token
, ')', "in function declarator");
1674 fn
->endpos
= token
->pos
;
1678 while (match_op(token
, '[')) {
1679 struct symbol
*array
;
1680 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1681 token
= abstract_array_declarator(token
->next
, array
);
1682 token
= expect(token
, ']', "in abstract_array_declarator");
1683 array
->endpos
= token
->pos
;
1684 ctype
= &array
->ctype
;
1689 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1691 while (match_op(token
,'*')) {
1692 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1693 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1694 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1695 ptr
->ctype
.as
= ctx
->ctype
.as
;
1696 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1697 ctx
->ctype
.modifiers
= 0;
1698 ctx
->ctype
.base_type
= ptr
;
1700 ctx
->ctype
.contexts
= NULL
;
1701 ctx
->ctype
.alignment
= 0;
1703 token
= handle_qualifiers(token
->next
, ctx
);
1704 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1709 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1711 token
= pointer(token
, ctx
);
1712 return direct_declarator(token
, ctx
);
1715 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1717 struct ctype
*ctype
= &ctx
->ctype
;
1718 struct expression
*expr
;
1719 struct symbol
*bitfield
;
1722 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1723 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1724 show_typename(ctype
->base_type
));
1725 // Parse this to recover gracefully.
1726 return conditional_expression(token
->next
, &expr
);
1729 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1730 token
= conditional_expression(token
->next
, &expr
);
1731 width
= const_expression_value(expr
);
1732 bitfield
->bit_size
= width
;
1734 if (width
< 0 || width
> INT_MAX
) {
1735 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1737 } else if (*ctx
->ident
&& width
== 0) {
1738 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1739 show_ident(*ctx
->ident
));
1741 } else if (*ctx
->ident
) {
1742 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1743 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1744 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1745 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1746 // Valid values are either {-1;0} or {0}, depending on integer
1747 // representation. The latter makes for very efficient code...
1748 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1750 if (Wdefault_bitfield_sign
&&
1751 bitfield_type
->type
!= SYM_ENUM
&&
1752 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1754 // The sign of bitfields is unspecified by default.
1755 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1758 bitfield
->bit_size
= width
;
1759 bitfield
->endpos
= token
->pos
;
1763 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1765 struct decl_state ctx
= {.prefer_abstract
= 0};
1769 token
= declaration_specifiers(token
, &ctx
);
1770 mod
= storage_modifiers(&ctx
);
1773 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1774 ctx
.ident
= &decl
->ident
;
1776 token
= declarator(token
, &ctx
);
1777 if (match_op(token
, ':'))
1778 token
= handle_bitfield(token
, &ctx
);
1780 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1781 apply_modifiers(token
->pos
, &ctx
);
1783 decl
->ctype
= ctx
.ctype
;
1784 decl
->ctype
.modifiers
|= mod
;
1785 decl
->endpos
= token
->pos
;
1786 add_symbol(list
, decl
);
1787 if (!match_op(token
, ','))
1789 token
= token
->next
;
1795 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1797 while (!match_op(token
, '}')) {
1798 if (!match_op(token
, ';'))
1799 token
= declaration_list(token
, list
);
1800 if (!match_op(token
, ';')) {
1801 sparse_error(token
->pos
, "expected ; at end of declaration");
1804 token
= token
->next
;
1809 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1811 struct decl_state ctx
= {.prefer_abstract
= 1};
1813 token
= declaration_specifiers(token
, &ctx
);
1814 ctx
.ident
= &sym
->ident
;
1815 token
= declarator(token
, &ctx
);
1816 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1817 apply_modifiers(token
->pos
, &ctx
);
1818 sym
->ctype
= ctx
.ctype
;
1819 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1820 sym
->endpos
= token
->pos
;
1824 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1826 struct decl_state ctx
= {.prefer_abstract
= 1};
1828 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1830 token
= declaration_specifiers(token
, &ctx
);
1831 token
= declarator(token
, &ctx
);
1832 apply_modifiers(token
->pos
, &ctx
);
1833 sym
->ctype
= ctx
.ctype
;
1834 sym
->endpos
= token
->pos
;
1835 class = ctx
.storage_class
;
1838 if (class == SForced
) {
1844 warning(sym
->pos
, "storage class in typename (%s %s)",
1845 storage_class
[class], show_typename(sym
));
1849 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1851 token
= parse_expression(token
, tree
);
1852 return expect(token
, ';', "at end of statement");
1855 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1856 struct expression_list
**inout
)
1858 struct expression
*expr
;
1860 /* Allow empty operands */
1861 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1864 struct ident
*ident
= NULL
;
1865 if (match_op(token
->next
, '[') &&
1866 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1867 match_op(token
->next
->next
->next
, ']')) {
1868 ident
= token
->next
->next
->ident
;
1869 token
= token
->next
->next
->next
;
1871 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1872 token
= primary_expression(token
->next
, &expr
);
1873 add_expression(inout
, expr
);
1874 token
= parens_expression(token
, &expr
, "in asm parameter");
1875 add_expression(inout
, expr
);
1876 } while (match_op(token
, ','));
1880 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1881 struct expression_list
**clobbers
)
1883 struct expression
*expr
;
1886 token
= primary_expression(token
->next
, &expr
);
1887 add_expression(clobbers
, expr
);
1888 } while (match_op(token
, ','));
1892 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1894 token
= token
->next
;
1895 stmt
->type
= STMT_ASM
;
1896 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1897 token
= token
->next
;
1899 token
= expect(token
, '(', "after asm");
1900 token
= parse_expression(token
, &stmt
->asm_string
);
1901 if (match_op(token
, ':'))
1902 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1903 if (match_op(token
, ':'))
1904 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1905 if (match_op(token
, ':'))
1906 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1907 token
= expect(token
, ')', "after asm");
1908 return expect(token
, ';', "at end of asm-statement");
1911 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1913 struct expression
*expr
;
1914 token
= expect(token
, '(', "after asm");
1915 token
= parse_expression(token
->next
, &expr
);
1916 token
= expect(token
, ')', "after asm");
1920 /* Make a statement out of an expression */
1921 static struct statement
*make_statement(struct expression
*expr
)
1923 struct statement
*stmt
;
1927 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1928 stmt
->expression
= expr
;
1933 * All iterators have two symbols associated with them:
1934 * the "continue" and "break" symbols, which are targets
1935 * for continue and break statements respectively.
1937 * They are in a special name-space, but they follow
1938 * all the normal visibility rules, so nested iterators
1939 * automatically work right.
1941 static void start_iterator(struct statement
*stmt
)
1943 struct symbol
*cont
, *brk
;
1945 start_symbol_scope();
1946 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1947 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1948 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1949 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1951 stmt
->type
= STMT_ITERATOR
;
1952 stmt
->iterator_break
= brk
;
1953 stmt
->iterator_continue
= cont
;
1954 fn_local_symbol(brk
);
1955 fn_local_symbol(cont
);
1958 static void end_iterator(struct statement
*stmt
)
1963 static struct statement
*start_function(struct symbol
*sym
)
1966 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1968 start_function_scope();
1969 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1970 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1971 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1972 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1973 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1975 fn_local_symbol(ret
);
1977 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1983 static void end_function(struct symbol
*sym
)
1986 end_function_scope();
1990 * A "switch()" statement, like an iterator, has a
1991 * the "break" symbol associated with it. It works
1992 * exactly like the iterator break - it's the target
1993 * for any break-statements in scope, and means that
1994 * "break" handling doesn't even need to know whether
1995 * it's breaking out of an iterator or a switch.
1997 * In addition, the "case" symbol is a marker for the
1998 * case/default statements to find the switch statement
1999 * that they are associated with.
2001 static void start_switch(struct statement
*stmt
)
2003 struct symbol
*brk
, *switch_case
;
2005 start_symbol_scope();
2006 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2007 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2009 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2010 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2011 switch_case
->stmt
= stmt
;
2013 stmt
->type
= STMT_SWITCH
;
2014 stmt
->switch_break
= brk
;
2015 stmt
->switch_case
= switch_case
;
2017 fn_local_symbol(brk
);
2018 fn_local_symbol(switch_case
);
2021 static void end_switch(struct statement
*stmt
)
2023 if (!stmt
->switch_case
->symbol_list
)
2024 warning(stmt
->pos
, "switch with no cases");
2028 static void add_case_statement(struct statement
*stmt
)
2030 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2034 sparse_error(stmt
->pos
, "not in switch scope");
2035 stmt
->type
= STMT_NONE
;
2038 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2039 add_symbol(&target
->symbol_list
, sym
);
2041 stmt
->case_label
= sym
;
2042 fn_local_symbol(sym
);
2045 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2047 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2050 error_die(token
->pos
, "internal error: return without a function target");
2051 stmt
->type
= STMT_RETURN
;
2052 stmt
->ret_target
= target
;
2053 return expression_statement(token
->next
, &stmt
->ret_value
);
2056 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2058 struct symbol_list
*syms
;
2059 struct expression
*e1
, *e2
, *e3
;
2060 struct statement
*iterator
;
2062 start_iterator(stmt
);
2063 token
= expect(token
->next
, '(', "after 'for'");
2067 /* C99 variable declaration? */
2068 if (lookup_type(token
)) {
2069 token
= external_declaration(token
, &syms
);
2071 token
= parse_expression(token
, &e1
);
2072 token
= expect(token
, ';', "in 'for'");
2074 token
= parse_expression(token
, &e2
);
2075 token
= expect(token
, ';', "in 'for'");
2076 token
= parse_expression(token
, &e3
);
2077 token
= expect(token
, ')', "in 'for'");
2078 token
= statement(token
, &iterator
);
2080 stmt
->iterator_syms
= syms
;
2081 stmt
->iterator_pre_statement
= make_statement(e1
);
2082 stmt
->iterator_pre_condition
= e2
;
2083 stmt
->iterator_post_statement
= make_statement(e3
);
2084 stmt
->iterator_post_condition
= NULL
;
2085 stmt
->iterator_statement
= iterator
;
2091 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2093 struct expression
*expr
;
2094 struct statement
*iterator
;
2096 start_iterator(stmt
);
2097 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2098 token
= statement(token
, &iterator
);
2100 stmt
->iterator_pre_condition
= expr
;
2101 stmt
->iterator_post_condition
= NULL
;
2102 stmt
->iterator_statement
= iterator
;
2108 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2110 struct expression
*expr
;
2111 struct statement
*iterator
;
2113 start_iterator(stmt
);
2114 token
= statement(token
->next
, &iterator
);
2115 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2116 token
= token
->next
;
2118 sparse_error(token
->pos
, "expected 'while' after 'do'");
2119 token
= parens_expression(token
, &expr
, "after 'do-while'");
2121 stmt
->iterator_post_condition
= expr
;
2122 stmt
->iterator_statement
= iterator
;
2125 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2126 warning(iterator
->pos
, "do-while statement is not a compound statement");
2128 return expect(token
, ';', "after statement");
2131 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2133 stmt
->type
= STMT_IF
;
2134 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2135 token
= statement(token
, &stmt
->if_true
);
2136 if (token_type(token
) != TOKEN_IDENT
)
2138 if (token
->ident
!= &else_ident
)
2140 return statement(token
->next
, &stmt
->if_false
);
2143 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2145 stmt
->type
= STMT_CASE
;
2146 token
= expect(token
, ':', "after default/case");
2147 add_case_statement(stmt
);
2148 return statement(token
, &stmt
->case_statement
);
2151 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2153 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2154 if (match_op(token
, SPECIAL_ELLIPSIS
))
2155 token
= parse_expression(token
->next
, &stmt
->case_to
);
2156 return case_statement(token
, stmt
);
2159 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2161 return case_statement(token
->next
, stmt
);
2164 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2166 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2167 stmt
->type
= STMT_GOTO
;
2168 stmt
->goto_label
= target
;
2170 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2171 return expect(token
->next
, ';', "at end of statement");
2174 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2176 stmt
->type
= STMT_SWITCH
;
2178 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2179 token
= statement(token
, &stmt
->switch_statement
);
2184 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2186 stmt
->type
= STMT_GOTO
;
2187 token
= token
->next
;
2188 if (match_op(token
, '*')) {
2189 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2190 add_statement(&function_computed_goto_list
, stmt
);
2191 } else if (token_type(token
) == TOKEN_IDENT
) {
2192 stmt
->goto_label
= label_symbol(token
);
2193 token
= token
->next
;
2195 sparse_error(token
->pos
, "Expected identifier or goto expression");
2197 return expect(token
, ';', "at end of statement");
2200 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2202 stmt
->type
= STMT_CONTEXT
;
2203 token
= parse_expression(token
->next
, &stmt
->expression
);
2204 if(stmt
->expression
->type
== EXPR_PREOP
2205 && stmt
->expression
->op
== '('
2206 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2207 struct expression
*expr
;
2208 expr
= stmt
->expression
->unop
;
2209 stmt
->context
= expr
->left
;
2210 stmt
->expression
= expr
->right
;
2212 return expect(token
, ';', "at end of statement");
2215 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2217 stmt
->type
= STMT_RANGE
;
2218 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2219 token
= expect(token
, ',', "after range expression");
2220 token
= assignment_expression(token
, &stmt
->range_low
);
2221 token
= expect(token
, ',', "after low range");
2222 token
= assignment_expression(token
, &stmt
->range_high
);
2223 return expect(token
, ';', "after range statement");
2226 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2228 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2231 if (token_type(token
) == TOKEN_IDENT
) {
2232 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2233 if (s
&& s
->op
->statement
)
2234 return s
->op
->statement(token
, stmt
);
2236 if (match_op(token
->next
, ':')) {
2237 stmt
->type
= STMT_LABEL
;
2238 stmt
->label_identifier
= label_symbol(token
);
2239 token
= skip_attributes(token
->next
->next
);
2240 return statement(token
, &stmt
->label_statement
);
2244 if (match_op(token
, '{')) {
2245 stmt
->type
= STMT_COMPOUND
;
2246 start_symbol_scope();
2247 token
= compound_statement(token
->next
, stmt
);
2250 return expect(token
, '}', "at end of compound statement");
2253 stmt
->type
= STMT_EXPRESSION
;
2254 return expression_statement(token
, &stmt
->expression
);
2257 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2258 static struct token
*label_statement(struct token
*token
)
2260 while (token_type(token
) == TOKEN_IDENT
) {
2261 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2262 /* it's block-scope, but we want label namespace */
2263 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2264 sym
->namespace = NS_LABEL
;
2265 fn_local_symbol(sym
);
2266 token
= token
->next
;
2267 if (!match_op(token
, ','))
2269 token
= token
->next
;
2271 return expect(token
, ';', "at end of label declaration");
2274 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2276 int seen_statement
= 0;
2277 while (token_type(token
) == TOKEN_IDENT
&&
2278 token
->ident
== &__label___ident
)
2279 token
= label_statement(token
->next
);
2281 struct statement
* stmt
;
2282 if (eof_token(token
))
2284 if (match_op(token
, '}'))
2286 if (lookup_type(token
)) {
2287 if (seen_statement
) {
2288 warning(token
->pos
, "mixing declarations and code");
2291 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2292 token
= external_declaration(token
, &stmt
->declaration
);
2294 seen_statement
= Wdeclarationafterstatement
;
2295 token
= statement(token
, &stmt
);
2297 add_statement(list
, stmt
);
2302 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2304 struct symbol_list
**list
= &fn
->arguments
;
2306 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2307 sym
->ident
= token
->ident
;
2308 token
= token
->next
;
2309 sym
->endpos
= token
->pos
;
2310 sym
->ctype
.base_type
= &incomplete_ctype
;
2311 add_symbol(list
, sym
);
2312 if (!match_op(token
, ',') ||
2313 token_type(token
->next
) != TOKEN_IDENT
||
2314 lookup_type(token
->next
))
2316 token
= token
->next
;
2321 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2323 struct symbol_list
**list
= &fn
->arguments
;
2328 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2330 token
= token
->next
;
2334 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2335 token
= parameter_declaration(token
, sym
);
2336 if (sym
->ctype
.base_type
== &void_ctype
) {
2337 /* Special case: (void) */
2338 if (!*list
&& !sym
->ident
)
2340 warning(token
->pos
, "void parameter");
2342 add_symbol(list
, sym
);
2343 if (!match_op(token
, ','))
2345 token
= token
->next
;
2350 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2352 token
= statement_list(token
, &stmt
->stmts
);
2356 static struct expression
*identifier_expression(struct token
*token
)
2358 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2359 expr
->expr_ident
= token
->ident
;
2363 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2365 int idx_from
, idx_to
;
2366 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2368 idx_from
= const_expression_value(from
);
2371 idx_to
= const_expression_value(to
);
2372 if (idx_to
< idx_from
|| idx_from
< 0)
2373 warning(from
->pos
, "nonsense array initializer index range");
2375 expr
->idx_from
= idx_from
;
2376 expr
->idx_to
= idx_to
;
2380 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2382 int expect_equal
= 0;
2383 struct token
*next
= token
->next
;
2384 struct expression
**tail
= ep
;
2389 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2390 struct expression
*expr
= identifier_expression(token
);
2391 if (Wold_initializer
)
2392 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2393 token
= initializer(&expr
->ident_expression
, next
->next
);
2394 if (expr
->ident_expression
)
2399 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2400 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2401 struct expression
*expr
= identifier_expression(next
);
2403 tail
= &expr
->ident_expression
;
2406 } else if (match_op(token
, '[')) {
2407 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2408 token
= constant_expression(token
->next
, &from
);
2410 sparse_error(token
->pos
, "Expected constant expression");
2413 if (match_op(token
, SPECIAL_ELLIPSIS
))
2414 token
= constant_expression(token
->next
, &to
);
2415 expr
= index_expression(from
, to
);
2417 tail
= &expr
->idx_expression
;
2418 token
= expect(token
, ']', "at end of initializer index");
2425 if (nested
&& !expect_equal
) {
2426 if (!match_op(token
, '='))
2427 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2432 token
= expect(token
, '=', "at end of initializer index");
2434 token
= initializer(tail
, token
);
2440 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2442 struct expression
*expr
;
2445 token
= single_initializer(&expr
, token
);
2448 add_expression(list
, expr
);
2449 if (!match_op(token
, ','))
2451 token
= token
->next
;
2456 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2458 if (match_op(token
, '{')) {
2459 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2461 token
= initializer_list(&expr
->expr_list
, token
->next
);
2462 return expect(token
, '}', "at end of initializer");
2464 return assignment_expression(token
, tree
);
2467 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2470 sparse_error(sym
->pos
, "no identifier for function argument");
2473 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2476 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2477 struct symbol_list
**list
)
2479 struct symbol_list
**old_symbol_list
;
2480 struct symbol
*base_type
= decl
->ctype
.base_type
;
2481 struct statement
*stmt
, **p
;
2482 struct symbol
*prev
;
2485 old_symbol_list
= function_symbol_list
;
2486 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2487 function_symbol_list
= &decl
->inline_symbol_list
;
2488 p
= &base_type
->inline_stmt
;
2490 function_symbol_list
= &decl
->symbol_list
;
2491 p
= &base_type
->stmt
;
2493 function_computed_target_list
= NULL
;
2494 function_computed_goto_list
= NULL
;
2496 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2497 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2498 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2500 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2501 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2503 stmt
= start_function(decl
);
2506 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2507 declare_argument(arg
, base_type
);
2508 } END_FOR_EACH_PTR(arg
);
2510 token
= compound_statement(token
->next
, stmt
);
2513 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2514 add_symbol(list
, decl
);
2515 check_declaration(decl
);
2516 decl
->definition
= decl
;
2517 prev
= decl
->same_symbol
;
2518 if (prev
&& prev
->definition
) {
2519 warning(decl
->pos
, "multiple definitions for function '%s'",
2520 show_ident(decl
->ident
));
2521 info(prev
->definition
->pos
, " the previous one is here");
2524 prev
->definition
= decl
;
2525 prev
= prev
->same_symbol
;
2528 function_symbol_list
= old_symbol_list
;
2529 if (function_computed_goto_list
) {
2530 if (!function_computed_target_list
)
2531 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2533 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2534 stmt
->target_list
= function_computed_target_list
;
2535 } END_FOR_EACH_PTR(stmt
);
2538 return expect(token
, '}', "at end of function");
2541 static void promote_k_r_types(struct symbol
*arg
)
2543 struct symbol
*base
= arg
->ctype
.base_type
;
2544 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2545 arg
->ctype
.base_type
= &int_ctype
;
2549 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2551 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2554 FOR_EACH_PTR(real_args
, arg
) {
2555 struct symbol
*type
;
2557 /* This is quadratic in the number of arguments. We _really_ don't care */
2558 FOR_EACH_PTR(argtypes
, type
) {
2559 if (type
->ident
== arg
->ident
)
2561 } END_FOR_EACH_PTR(type
);
2562 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2566 /* "char" and "short" promote to "int" */
2567 promote_k_r_types(type
);
2569 arg
->ctype
= type
->ctype
;
2570 } END_FOR_EACH_PTR(arg
);
2572 FOR_EACH_PTR(argtypes
, arg
) {
2574 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2575 } END_FOR_EACH_PTR(arg
);
2579 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2580 struct symbol_list
**list
)
2582 struct symbol_list
*args
= NULL
;
2584 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2586 token
= declaration_list(token
, &args
);
2587 if (!match_op(token
, ';')) {
2588 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2591 token
= token
->next
;
2592 } while (lookup_type(token
));
2594 apply_k_r_types(args
, decl
);
2596 if (!match_op(token
, '{')) {
2597 sparse_error(token
->pos
, "expected function body");
2600 return parse_function_body(token
, decl
, list
);
2603 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2605 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2606 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2607 struct statement
*stmt
;
2609 anon
->ctype
.base_type
= fn
;
2610 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2613 token
= parse_asm_statement(token
, stmt
);
2615 add_symbol(list
, anon
);
2619 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2621 struct ident
*ident
= NULL
;
2622 struct symbol
*decl
;
2623 struct decl_state ctx
= { .ident
= &ident
};
2625 struct symbol
*base_type
;
2629 /* Top-level inline asm? */
2630 if (token_type(token
) == TOKEN_IDENT
) {
2631 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2632 if (s
&& s
->op
->toplevel
)
2633 return s
->op
->toplevel(token
, list
);
2636 /* Parse declaration-specifiers, if any */
2637 token
= declaration_specifiers(token
, &ctx
);
2638 mod
= storage_modifiers(&ctx
);
2639 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2640 /* Just a type declaration? */
2641 if (match_op(token
, ';')) {
2642 apply_modifiers(token
->pos
, &ctx
);
2647 token
= declarator(token
, &ctx
);
2648 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2649 apply_modifiers(token
->pos
, &ctx
);
2651 decl
->ctype
= ctx
.ctype
;
2652 decl
->ctype
.modifiers
|= mod
;
2653 decl
->endpos
= token
->pos
;
2655 /* Just a type declaration? */
2657 warning(token
->pos
, "missing identifier in declaration");
2658 return expect(token
, ';', "at the end of type declaration");
2661 /* type define declaration? */
2662 is_typedef
= ctx
.storage_class
== STypedef
;
2664 /* Typedefs don't have meaningful storage */
2666 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2668 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2670 base_type
= decl
->ctype
.base_type
;
2673 if (base_type
&& !base_type
->ident
) {
2674 switch (base_type
->type
) {
2679 base_type
->ident
= ident
;
2682 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2683 /* K&R argument declaration? */
2684 if (lookup_type(token
))
2685 return parse_k_r_arguments(token
, decl
, list
);
2686 if (match_op(token
, '{'))
2687 return parse_function_body(token
, decl
, list
);
2689 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2690 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2691 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2692 sparse_error(token
->pos
, "void declaration");
2696 if (!is_typedef
&& match_op(token
, '=')) {
2697 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2698 warning(decl
->pos
, "symbol with external linkage has initializer");
2699 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2701 token
= initializer(&decl
->initializer
, token
->next
);
2704 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2705 add_symbol(list
, decl
);
2706 fn_local_symbol(decl
);
2709 check_declaration(decl
);
2710 if (decl
->same_symbol
)
2711 decl
->definition
= decl
->same_symbol
->definition
;
2713 if (!match_op(token
, ','))
2716 token
= token
->next
;
2718 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2720 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2721 token
= declarator(token
, &ctx
);
2722 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2723 apply_modifiers(token
->pos
, &ctx
);
2724 decl
->ctype
= ctx
.ctype
;
2725 decl
->ctype
.modifiers
|= mod
;
2726 decl
->endpos
= token
->pos
;
2728 sparse_error(token
->pos
, "expected identifier name in type definition");
2733 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2735 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2737 /* Function declarations are automatically extern unless specifically static */
2738 base_type
= decl
->ctype
.base_type
;
2739 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2740 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2741 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2744 return expect(token
, ';', "at end of declaration");