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__",
524 "no_instrument_function",
525 "__no_instrument_function__",
547 "__syscall_linkage__",
556 "warn_unused_result",
557 "__warn_unused_result__",
565 void init_parser(int stream
)
568 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
569 struct init_keyword
*ptr
= keyword_table
+ i
;
570 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
571 sym
->ident
->keyword
= 1;
572 if (ptr
->ns
== NS_TYPEDEF
)
573 sym
->ident
->reserved
= 1;
574 sym
->ctype
.modifiers
= ptr
->modifiers
;
575 sym
->ctype
.base_type
= ptr
->type
;
579 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
580 const char * name
= ignored_attributes
[i
];
581 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
583 sym
->ident
->keyword
= 1;
584 sym
->op
= &ignore_attr_op
;
589 // Add a symbol to the list of function-local symbols
590 static void fn_local_symbol(struct symbol
*sym
)
592 if (function_symbol_list
)
593 add_symbol(function_symbol_list
, sym
);
596 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
601 if (token_type(token
) != TOKEN_IDENT
)
604 va_start(args
, token
);
606 next
= va_arg(args
, struct ident
*);
607 } while (next
&& token
->ident
!= next
);
610 return next
&& token
->ident
== next
;
614 struct statement
*alloc_statement(struct position pos
, int type
)
616 struct statement
*stmt
= __alloc_statement(0);
622 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
624 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
626 struct symbol
*ctype
;
629 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
631 sparse_error(pos
, "don't know how to apply mode to %s",
632 show_typename(ctx
->ctype
.base_type
));
634 ctx
->ctype
.base_type
= ctype
;
638 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
640 struct symbol
*sym
= alloc_symbol(pos
, type
);
642 sym
->ctype
.base_type
= ctype
->base_type
;
643 sym
->ctype
.modifiers
= ctype
->modifiers
;
645 ctype
->base_type
= sym
;
646 ctype
->modifiers
= 0;
651 * NOTE! NS_LABEL is not just a different namespace,
652 * it also ends up using function scope instead of the
653 * regular symbol scope.
655 struct symbol
*label_symbol(struct token
*token
)
657 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
659 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
660 bind_symbol(sym
, token
->ident
, NS_LABEL
);
661 fn_local_symbol(sym
);
666 static struct token
*struct_union_enum_specifier(enum type type
,
667 struct token
*token
, struct decl_state
*ctx
,
668 struct token
*(*parse
)(struct token
*, struct symbol
*))
671 struct position
*repos
;
673 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
674 if (token_type(token
) == TOKEN_IDENT
) {
675 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
677 (is_outer_scope(sym
->scope
) &&
678 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
679 // Either a new symbol, or else an out-of-scope
680 // symbol being redefined.
681 sym
= alloc_symbol(token
->pos
, type
);
682 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
684 if (sym
->type
!= type
)
685 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
686 ctx
->ctype
.base_type
= sym
;
689 if (match_op(token
, '{')) {
690 // The following test is actually wrong for empty
691 // structs, but (1) they are not C99, (2) gcc does
692 // the same thing, and (3) it's easier.
693 if (sym
->symbol_list
)
694 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
696 token
= parse(token
->next
, sym
);
697 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
699 // Mark the structure as needing re-examination
701 sym
->endpos
= token
->pos
;
706 // private struct/union/enum type
707 if (!match_op(token
, '{')) {
708 sparse_error(token
->pos
, "expected declaration");
709 ctx
->ctype
.base_type
= &bad_ctype
;
713 sym
= alloc_symbol(token
->pos
, type
);
714 token
= parse(token
->next
, sym
);
715 ctx
->ctype
.base_type
= sym
;
716 token
= expect(token
, '}', "at end of specifier");
717 sym
->endpos
= token
->pos
;
722 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
724 struct symbol
*field
, *last
= NULL
;
726 res
= struct_declaration_list(token
, &sym
->symbol_list
);
727 FOR_EACH_PTR(sym
->symbol_list
, field
) {
729 struct symbol
*base
= field
->ctype
.base_type
;
730 if (base
&& base
->type
== SYM_BITFIELD
)
734 last
->next_subobject
= field
;
736 } END_FOR_EACH_PTR(field
);
740 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
742 return struct_declaration_list(token
, &sym
->symbol_list
);
745 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
747 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
750 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
752 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
758 unsigned long long y
;
761 static void upper_boundary(Num
*n
, Num
*v
)
773 static void lower_boundary(Num
*n
, Num
*v
)
785 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
787 int shift
= type
->bit_size
;
788 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
792 if (upper
->x
== 0 && upper
->y
>> shift
)
794 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
799 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
801 if (s1
->bit_size
< s2
->bit_size
) {
803 } else if (s1
->bit_size
== s2
->bit_size
) {
804 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
807 if (s1
->bit_size
< bits_in_int
)
812 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
816 FOR_EACH_PTR(list
, sym
) {
817 struct expression
*expr
= sym
->initializer
;
818 struct symbol
*ctype
;
819 if (expr
->type
!= EXPR_VALUE
)
822 if (ctype
->bit_size
== base_type
->bit_size
)
824 cast_value(expr
, base_type
, expr
, ctype
);
825 } END_FOR_EACH_PTR(sym
);
828 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
830 unsigned long long lastval
= 0;
831 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
832 Num upper
= {-1, 0}, lower
= {1, 0};
834 parent
->examined
= 1;
835 parent
->ctype
.base_type
= &int_ctype
;
836 while (token_type(token
) == TOKEN_IDENT
) {
837 struct expression
*expr
= NULL
;
838 struct token
*next
= token
->next
;
841 if (match_op(next
, '=')) {
842 next
= constant_expression(next
->next
, &expr
);
843 lastval
= get_expression_value(expr
);
845 if (expr
&& expr
->ctype
)
849 } else if (is_int_type(ctype
)) {
852 error_die(token
->pos
, "can't increment the last enum member");
856 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
857 expr
->value
= lastval
;
861 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
862 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
863 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
864 sym
->initializer
= expr
;
865 sym
->enum_member
= 1;
866 sym
->ctype
.base_type
= parent
;
867 add_ptr_list(&parent
->symbol_list
, sym
);
869 if (base_type
!= &bad_ctype
) {
870 if (ctype
->type
== SYM_NODE
)
871 ctype
= ctype
->ctype
.base_type
;
872 if (ctype
->type
== SYM_ENUM
) {
876 ctype
= ctype
->ctype
.base_type
;
880 * - if all enums are of the same type, then
881 * the base_type is that type (two first
883 * - if enums are of different types, they
884 * all have to be integer types, and the
885 * base type is at least "int_ctype".
886 * - otherwise the base_type is "bad_ctype".
890 } else if (ctype
== base_type
) {
892 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
893 base_type
= bigger_enum_type(base_type
, ctype
);
895 base_type
= &bad_ctype
;
896 parent
->ctype
.base_type
= base_type
;
898 if (is_int_type(base_type
)) {
899 Num v
= {.y
= lastval
};
900 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
902 else if ((long long)lastval
>= 0)
906 upper_boundary(&upper
, &v
);
907 lower_boundary(&lower
, &v
);
911 sym
->endpos
= token
->pos
;
913 if (!match_op(token
, ','))
918 sparse_error(token
->pos
, "bad enum definition");
919 base_type
= &bad_ctype
;
921 else if (!is_int_type(base_type
))
922 base_type
= base_type
;
923 else if (type_is_ok(base_type
, &upper
, &lower
))
924 base_type
= base_type
;
925 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
926 base_type
= &int_ctype
;
927 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
928 base_type
= &uint_ctype
;
929 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
930 base_type
= &long_ctype
;
931 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
932 base_type
= &ulong_ctype
;
933 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
934 base_type
= &llong_ctype
;
935 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
936 base_type
= &ullong_ctype
;
938 base_type
= &bad_ctype
;
939 parent
->ctype
.base_type
= base_type
;
940 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
941 parent
->examined
= 0;
943 cast_enum_list(parent
->symbol_list
, base_type
);
948 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
950 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
951 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
953 if (!ctype
->base_type
)
954 ctype
->base_type
= &incomplete_ctype
;
959 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
961 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
965 if (!match_op(token
, '(')) {
966 sparse_error(token
->pos
, "expected '(' after typeof");
969 if (lookup_type(token
->next
)) {
970 token
= typename(token
->next
, &sym
, NULL
);
971 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
972 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
974 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
975 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
977 typeof_sym
->endpos
= token
->pos
;
978 if (!typeof_sym
->initializer
) {
979 sparse_error(token
->pos
, "expected expression after the '(' token");
980 typeof_sym
= &bad_ctype
;
982 ctx
->ctype
.base_type
= typeof_sym
;
984 return expect(token
, ')', "after typeof");
987 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
989 struct expression
*expr
= NULL
;
990 if (match_op(token
, '('))
991 token
= parens_expression(token
, &expr
, "in attribute");
995 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
997 if (!ctx
->ctype
.alignment
)
998 ctx
->ctype
.alignment
= 1;
1002 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1004 int alignment
= max_alignment
;
1005 struct expression
*expr
= NULL
;
1007 if (match_op(token
, '(')) {
1008 token
= parens_expression(token
, &expr
, "in attribute");
1010 alignment
= const_expression_value(expr
);
1012 if (alignment
& (alignment
-1)) {
1013 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1015 } else if (alignment
> ctx
->ctype
.alignment
)
1016 ctx
->ctype
.alignment
= alignment
;
1020 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1022 if (ctx
->modifiers
& qual
)
1023 warning(*pos
, "duplicate %s", modifier_string(qual
));
1024 ctx
->modifiers
|= qual
;
1027 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1029 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1033 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1035 struct expression
*expr
= NULL
;
1037 token
= expect(token
, '(', "after address_space attribute");
1038 token
= conditional_expression(token
, &expr
);
1040 as
= const_expression_value(expr
);
1041 if (Waddress_space
&& as
)
1044 token
= expect(token
, ')', "after address_space attribute");
1048 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1050 if (ctype
->ctype
.base_type
!= &int_type
)
1052 if (ctype
== &char_ctype
)
1054 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1058 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1060 if (ctype
->ctype
.base_type
!= &int_type
)
1062 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1066 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1068 if (ctype
->ctype
.base_type
!= &int_type
)
1070 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1074 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1076 if (ctype
->ctype
.base_type
!= &int_type
)
1078 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1082 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1084 if (ctype
->ctype
.base_type
!= &int_type
)
1086 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1090 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1092 if (ctype
->ctype
.base_type
!= &int_type
)
1094 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1098 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1100 token
= expect(token
, '(', "after mode attribute");
1101 if (token_type(token
) == TOKEN_IDENT
) {
1102 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1103 if (mode
&& mode
->op
->type
== KW_MODE
)
1104 ctx
->mode
= mode
->op
;
1106 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1107 token
= token
->next
;
1109 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1110 token
= expect(token
, ')', "after mode attribute");
1114 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1116 struct context
*context
= alloc_context();
1117 struct expression
*args
[3];
1120 token
= expect(token
, '(', "after context attribute");
1121 while (!match_op(token
, ')')) {
1122 struct expression
*expr
= NULL
;
1123 token
= conditional_expression(token
, &expr
);
1127 args
[argc
++] = expr
;
1128 if (!match_op(token
, ','))
1130 token
= token
->next
;
1135 sparse_error(token
->pos
, "expected context input/output values");
1138 context
->in
= get_expression_value(args
[0]);
1141 context
->in
= get_expression_value(args
[0]);
1142 context
->out
= get_expression_value(args
[1]);
1145 context
->context
= args
[0];
1146 context
->in
= get_expression_value(args
[1]);
1147 context
->out
= get_expression_value(args
[2]);
1152 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1154 token
= expect(token
, ')', "after context attribute");
1158 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1160 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1161 ctx
->ctype
.base_type
->designated_init
= 1;
1163 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1167 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1169 if (Wtransparent_union
)
1170 warning(token
->pos
, "ignoring attribute __transparent_union__");
1174 static struct token
*recover_unknown_attribute(struct token
*token
)
1176 struct expression
*expr
= NULL
;
1178 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1179 token
= token
->next
;
1180 if (match_op(token
, '('))
1181 token
= parens_expression(token
, &expr
, "in attribute");
1185 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1187 token
= expect(token
, '(', "after attribute");
1188 token
= expect(token
, '(', "after attribute");
1191 struct ident
*attribute_name
;
1192 struct symbol
*attr
;
1194 if (eof_token(token
))
1196 if (match_op(token
, ';'))
1198 if (token_type(token
) != TOKEN_IDENT
)
1200 attribute_name
= token
->ident
;
1201 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1202 if (attr
&& attr
->op
->attribute
)
1203 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1205 token
= recover_unknown_attribute(token
);
1207 if (!match_op(token
, ','))
1209 token
= token
->next
;
1212 token
= expect(token
, ')', "after attribute");
1213 token
= expect(token
, ')', "after attribute");
1217 static const char *storage_class
[] =
1219 [STypedef
] = "typedef",
1221 [SExtern
] = "extern",
1222 [SStatic
] = "static",
1223 [SRegister
] = "register",
1224 [SForced
] = "[force]"
1227 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1229 static unsigned long mod
[] =
1232 [SExtern
] = MOD_EXTERN
,
1233 [SStatic
] = MOD_STATIC
,
1234 [SRegister
] = MOD_REGISTER
1236 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1237 | (ctx
->is_tls
? MOD_TLS
: 0);
1240 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1242 /* __thread can be used alone, or with extern or static */
1243 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1244 sparse_error(*pos
, "__thread can only be used alone, or with "
1245 "extern or static");
1249 if (!ctx
->storage_class
) {
1250 ctx
->storage_class
= class;
1253 if (ctx
->storage_class
== class)
1254 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1256 sparse_error(*pos
, "multiple storage classes");
1259 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1261 set_storage_class(&next
->pos
, ctx
, STypedef
);
1265 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1267 set_storage_class(&next
->pos
, ctx
, SAuto
);
1271 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1273 set_storage_class(&next
->pos
, ctx
, SRegister
);
1277 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1279 set_storage_class(&next
->pos
, ctx
, SStatic
);
1283 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1285 set_storage_class(&next
->pos
, ctx
, SExtern
);
1289 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1291 /* This GCC extension can be used alone, or with extern or static */
1292 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1293 || ctx
->storage_class
== SExtern
) {
1296 sparse_error(next
->pos
, "__thread can only be used alone, or "
1297 "with extern or static");
1303 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1305 set_storage_class(&token
->pos
, ctx
, SForced
);
1309 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1315 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1317 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1321 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1323 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1327 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1329 unsigned long mod
= thistype
->modifiers
;
1332 apply_qualifier(&pos
, ctype
, mod
);
1335 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1336 (struct ptr_list
**)&ctype
->contexts
);
1339 if (thistype
->alignment
> ctype
->alignment
)
1340 ctype
->alignment
= thistype
->alignment
;
1344 ctype
->as
= thistype
->as
;
1347 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1350 if (what
& (Set_S
| Set_T
))
1352 if (what
& Set_Char
)
1354 else if (what
& Set_Double
)
1356 else if (what
& Set_Float
)
1358 else if (what
& Set_Signed
)
1360 else if (what
& Set_Unsigned
)
1362 else if (what
& Set_Short
)
1364 else if (what
& Set_Long
)
1368 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1369 old
, show_ident(new));
1373 sparse_error(pos
, "two or more data types in declaration specifiers");
1376 static struct symbol
* const int_types
[] =
1377 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1378 static struct symbol
* const signed_types
[] =
1379 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1381 static struct symbol
* const unsigned_types
[] =
1382 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1384 static struct symbol
* const real_types
[] =
1385 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1386 static struct symbol
* const char_types
[] =
1387 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1388 static struct symbol
* const * const types
[] = {
1389 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1390 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1393 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1395 return types
[want_unsigned
? CUInt
: CInt
][size
];
1398 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1400 while (token_type(t
) == TOKEN_IDENT
) {
1401 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1404 if (s
->type
!= SYM_KEYWORD
)
1406 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1409 if (s
->op
->declarator
)
1410 t
= s
->op
->declarator(t
, ctx
);
1415 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1421 while (token_type(token
) == TOKEN_IDENT
) {
1422 struct symbol
*s
= lookup_symbol(token
->ident
,
1423 NS_TYPEDEF
| NS_SYMBOL
);
1424 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1426 if (s
->type
!= SYM_KEYWORD
) {
1429 seen
|= Set_S
| Set_T
;
1430 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1431 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1432 token
= token
->next
;
1435 if (s
->op
->type
& KW_SPECIFIER
) {
1436 if (seen
& s
->op
->test
) {
1437 specifier_conflict(token
->pos
,
1443 class += s
->op
->class;
1444 if (s
->op
->type
& KW_SHORT
) {
1446 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1447 if (class == CReal
) {
1448 specifier_conflict(token
->pos
,
1456 token
= token
->next
;
1457 if (s
->op
->declarator
)
1458 token
= s
->op
->declarator(token
, ctx
);
1459 if (s
->op
->type
& KW_EXACT
) {
1460 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1461 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1465 if (!(seen
& Set_S
)) { /* not set explicitly? */
1466 struct symbol
*base
= &incomplete_ctype
;
1468 base
= types
[class][size
];
1469 ctx
->ctype
.base_type
= base
;
1472 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1473 struct symbol
*type
;
1474 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1475 if (!is_int_type(ctx
->ctype
.base_type
)) {
1476 sparse_error(token
->pos
, "invalid modifier");
1479 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1480 *type
= *ctx
->ctype
.base_type
;
1481 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1482 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1483 type
->type
= SYM_RESTRICT
;
1484 ctx
->ctype
.base_type
= type
;
1485 create_fouled(type
);
1490 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1492 struct expression
*expr
= NULL
;
1494 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1495 token
= token
->next
;
1496 token
= parse_expression(token
, &expr
);
1497 sym
->array_size
= expr
;
1501 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1502 static struct token
*identifier_list(struct token
*, struct symbol
*);
1503 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1505 static struct token
*skip_attribute(struct token
*token
)
1507 token
= token
->next
;
1508 if (match_op(token
, '(')) {
1510 token
= token
->next
;
1511 while (depth
&& !eof_token(token
)) {
1512 if (token_type(token
) == TOKEN_SPECIAL
) {
1513 if (token
->special
== '(')
1515 else if (token
->special
== ')')
1518 token
= token
->next
;
1524 static struct token
*skip_attributes(struct token
*token
)
1526 struct symbol
*keyword
;
1528 if (token_type(token
) != TOKEN_IDENT
)
1530 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1531 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1533 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1535 token
= expect(token
->next
, '(', "after attribute");
1536 token
= expect(token
, '(', "after attribute");
1538 if (eof_token(token
))
1540 if (match_op(token
, ';'))
1542 if (token_type(token
) != TOKEN_IDENT
)
1544 token
= skip_attribute(token
);
1545 if (!match_op(token
, ','))
1547 token
= token
->next
;
1549 token
= expect(token
, ')', "after attribute");
1550 token
= expect(token
, ')', "after attribute");
1555 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1557 struct symbol
*keyword
;
1559 if (token_type(token
) != TOKEN_IDENT
)
1561 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1562 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1564 if (!(keyword
->op
->type
& keywords
))
1566 token
= keyword
->op
->declarator(token
->next
, ctx
);
1567 keywords
&= KW_ATTRIBUTE
;
1572 static int is_nested(struct token
*token
, struct token
**p
,
1573 int prefer_abstract
)
1576 * This can be either a parameter list or a grouping.
1577 * For the direct (non-abstract) case, we know if must be
1578 * a parameter list if we already saw the identifier.
1579 * For the abstract case, we know if must be a parameter
1580 * list if it is empty or starts with a type.
1582 struct token
*next
= token
->next
;
1584 *p
= next
= skip_attributes(next
);
1586 if (token_type(next
) == TOKEN_IDENT
) {
1587 if (lookup_type(next
))
1588 return !prefer_abstract
;
1592 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1599 Empty
, K_R
, Proto
, Bad_Func
,
1602 static enum kind
which_func(struct token
*token
,
1604 int prefer_abstract
)
1606 struct token
*next
= token
->next
;
1608 if (token_type(next
) == TOKEN_IDENT
) {
1609 if (lookup_type(next
))
1611 /* identifier list not in definition; complain */
1612 if (prefer_abstract
)
1614 "identifier list not in definition");
1618 if (token_type(next
) != TOKEN_SPECIAL
)
1621 if (next
->special
== ')') {
1622 /* don't complain about those */
1623 if (!n
|| match_op(next
->next
, ';'))
1626 "non-ANSI function declaration of function '%s'",
1631 if (next
->special
== SPECIAL_ELLIPSIS
) {
1633 "variadic functions must have one named argument");
1640 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1642 struct ctype
*ctype
= &ctx
->ctype
;
1644 struct ident
**p
= ctx
->ident
;
1646 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1647 *ctx
->ident
= token
->ident
;
1648 token
= token
->next
;
1649 } else if (match_op(token
, '(') &&
1650 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1651 struct symbol
*base_type
= ctype
->base_type
;
1652 if (token
->next
!= next
)
1653 next
= handle_attributes(token
->next
, ctx
,
1655 token
= declarator(next
, ctx
);
1656 token
= expect(token
, ')', "in nested declarator");
1657 while (ctype
->base_type
!= base_type
)
1658 ctype
= &ctype
->base_type
->ctype
;
1662 if (match_op(token
, '(')) {
1663 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1665 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1666 token
= token
->next
;
1668 token
= identifier_list(token
, fn
);
1669 else if (kind
== Proto
)
1670 token
= parameter_type_list(token
, fn
);
1671 token
= expect(token
, ')', "in function declarator");
1672 fn
->endpos
= token
->pos
;
1676 while (match_op(token
, '[')) {
1677 struct symbol
*array
;
1678 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1679 token
= abstract_array_declarator(token
->next
, array
);
1680 token
= expect(token
, ']', "in abstract_array_declarator");
1681 array
->endpos
= token
->pos
;
1682 ctype
= &array
->ctype
;
1687 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1689 while (match_op(token
,'*')) {
1690 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1691 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1692 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1693 ptr
->ctype
.as
= ctx
->ctype
.as
;
1694 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1695 ctx
->ctype
.modifiers
= 0;
1696 ctx
->ctype
.base_type
= ptr
;
1698 ctx
->ctype
.contexts
= NULL
;
1700 token
= handle_qualifiers(token
->next
, ctx
);
1701 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1706 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1708 token
= pointer(token
, ctx
);
1709 return direct_declarator(token
, ctx
);
1712 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1714 struct ctype
*ctype
= &ctx
->ctype
;
1715 struct expression
*expr
;
1716 struct symbol
*bitfield
;
1719 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1720 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1721 show_typename(ctype
->base_type
));
1722 // Parse this to recover gracefully.
1723 return conditional_expression(token
->next
, &expr
);
1726 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1727 token
= conditional_expression(token
->next
, &expr
);
1728 width
= const_expression_value(expr
);
1729 bitfield
->bit_size
= width
;
1731 if (width
< 0 || width
> INT_MAX
) {
1732 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1734 } else if (*ctx
->ident
&& width
== 0) {
1735 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1736 show_ident(*ctx
->ident
));
1738 } else if (*ctx
->ident
) {
1739 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1740 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1741 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1742 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1743 // Valid values are either {-1;0} or {0}, depending on integer
1744 // representation. The latter makes for very efficient code...
1745 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1747 if (Wdefault_bitfield_sign
&&
1748 bitfield_type
->type
!= SYM_ENUM
&&
1749 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1751 // The sign of bitfields is unspecified by default.
1752 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1755 bitfield
->bit_size
= width
;
1756 bitfield
->endpos
= token
->pos
;
1760 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1762 struct decl_state ctx
= {.prefer_abstract
= 0};
1766 token
= declaration_specifiers(token
, &ctx
);
1767 mod
= storage_modifiers(&ctx
);
1770 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1771 ctx
.ident
= &decl
->ident
;
1773 token
= declarator(token
, &ctx
);
1774 if (match_op(token
, ':'))
1775 token
= handle_bitfield(token
, &ctx
);
1777 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1778 apply_modifiers(token
->pos
, &ctx
);
1780 decl
->ctype
= ctx
.ctype
;
1781 decl
->ctype
.modifiers
|= mod
;
1782 decl
->endpos
= token
->pos
;
1783 add_symbol(list
, decl
);
1784 if (!match_op(token
, ','))
1786 token
= token
->next
;
1792 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1794 while (!match_op(token
, '}')) {
1795 if (!match_op(token
, ';'))
1796 token
= declaration_list(token
, list
);
1797 if (!match_op(token
, ';')) {
1798 sparse_error(token
->pos
, "expected ; at end of declaration");
1801 token
= token
->next
;
1806 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1808 struct decl_state ctx
= {.prefer_abstract
= 1};
1810 token
= declaration_specifiers(token
, &ctx
);
1811 ctx
.ident
= &sym
->ident
;
1812 token
= declarator(token
, &ctx
);
1813 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1814 apply_modifiers(token
->pos
, &ctx
);
1815 sym
->ctype
= ctx
.ctype
;
1816 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1817 sym
->endpos
= token
->pos
;
1821 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1823 struct decl_state ctx
= {.prefer_abstract
= 1};
1825 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1827 token
= declaration_specifiers(token
, &ctx
);
1828 token
= declarator(token
, &ctx
);
1829 apply_modifiers(token
->pos
, &ctx
);
1830 sym
->ctype
= ctx
.ctype
;
1831 sym
->endpos
= token
->pos
;
1832 class = ctx
.storage_class
;
1835 if (class == SForced
) {
1841 warning(sym
->pos
, "storage class in typename (%s %s)",
1842 storage_class
[class], show_typename(sym
));
1846 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1848 token
= parse_expression(token
, tree
);
1849 return expect(token
, ';', "at end of statement");
1852 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1853 struct expression_list
**inout
)
1855 struct expression
*expr
;
1857 /* Allow empty operands */
1858 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1861 struct ident
*ident
= NULL
;
1862 if (match_op(token
->next
, '[') &&
1863 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1864 match_op(token
->next
->next
->next
, ']')) {
1865 ident
= token
->next
->next
->ident
;
1866 token
= token
->next
->next
->next
;
1868 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1869 token
= primary_expression(token
->next
, &expr
);
1870 add_expression(inout
, expr
);
1871 token
= parens_expression(token
, &expr
, "in asm parameter");
1872 add_expression(inout
, expr
);
1873 } while (match_op(token
, ','));
1877 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1878 struct expression_list
**clobbers
)
1880 struct expression
*expr
;
1883 token
= primary_expression(token
->next
, &expr
);
1884 add_expression(clobbers
, expr
);
1885 } while (match_op(token
, ','));
1889 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1891 token
= token
->next
;
1892 stmt
->type
= STMT_ASM
;
1893 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1894 token
= token
->next
;
1896 token
= expect(token
, '(', "after asm");
1897 token
= parse_expression(token
, &stmt
->asm_string
);
1898 if (match_op(token
, ':'))
1899 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1900 if (match_op(token
, ':'))
1901 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1902 if (match_op(token
, ':'))
1903 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1904 token
= expect(token
, ')', "after asm");
1905 return expect(token
, ';', "at end of asm-statement");
1908 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1910 struct expression
*expr
;
1911 token
= expect(token
, '(', "after asm");
1912 token
= parse_expression(token
->next
, &expr
);
1913 token
= expect(token
, ')', "after asm");
1917 /* Make a statement out of an expression */
1918 static struct statement
*make_statement(struct expression
*expr
)
1920 struct statement
*stmt
;
1924 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1925 stmt
->expression
= expr
;
1930 * All iterators have two symbols associated with them:
1931 * the "continue" and "break" symbols, which are targets
1932 * for continue and break statements respectively.
1934 * They are in a special name-space, but they follow
1935 * all the normal visibility rules, so nested iterators
1936 * automatically work right.
1938 static void start_iterator(struct statement
*stmt
)
1940 struct symbol
*cont
, *brk
;
1942 start_symbol_scope();
1943 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1944 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1945 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1946 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1948 stmt
->type
= STMT_ITERATOR
;
1949 stmt
->iterator_break
= brk
;
1950 stmt
->iterator_continue
= cont
;
1951 fn_local_symbol(brk
);
1952 fn_local_symbol(cont
);
1955 static void end_iterator(struct statement
*stmt
)
1960 static struct statement
*start_function(struct symbol
*sym
)
1963 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1965 start_function_scope();
1966 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1967 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1968 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1969 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1970 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1972 fn_local_symbol(ret
);
1974 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1980 static void end_function(struct symbol
*sym
)
1983 end_function_scope();
1987 * A "switch()" statement, like an iterator, has a
1988 * the "break" symbol associated with it. It works
1989 * exactly like the iterator break - it's the target
1990 * for any break-statements in scope, and means that
1991 * "break" handling doesn't even need to know whether
1992 * it's breaking out of an iterator or a switch.
1994 * In addition, the "case" symbol is a marker for the
1995 * case/default statements to find the switch statement
1996 * that they are associated with.
1998 static void start_switch(struct statement
*stmt
)
2000 struct symbol
*brk
, *switch_case
;
2002 start_symbol_scope();
2003 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2004 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2006 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2007 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2008 switch_case
->stmt
= stmt
;
2010 stmt
->type
= STMT_SWITCH
;
2011 stmt
->switch_break
= brk
;
2012 stmt
->switch_case
= switch_case
;
2014 fn_local_symbol(brk
);
2015 fn_local_symbol(switch_case
);
2018 static void end_switch(struct statement
*stmt
)
2020 if (!stmt
->switch_case
->symbol_list
)
2021 warning(stmt
->pos
, "switch with no cases");
2025 static void add_case_statement(struct statement
*stmt
)
2027 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2031 sparse_error(stmt
->pos
, "not in switch scope");
2032 stmt
->type
= STMT_NONE
;
2035 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2036 add_symbol(&target
->symbol_list
, sym
);
2038 stmt
->case_label
= sym
;
2039 fn_local_symbol(sym
);
2042 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2044 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2047 error_die(token
->pos
, "internal error: return without a function target");
2048 stmt
->type
= STMT_RETURN
;
2049 stmt
->ret_target
= target
;
2050 return expression_statement(token
->next
, &stmt
->ret_value
);
2053 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2055 struct symbol_list
*syms
;
2056 struct expression
*e1
, *e2
, *e3
;
2057 struct statement
*iterator
;
2059 start_iterator(stmt
);
2060 token
= expect(token
->next
, '(', "after 'for'");
2064 /* C99 variable declaration? */
2065 if (lookup_type(token
)) {
2066 token
= external_declaration(token
, &syms
);
2068 token
= parse_expression(token
, &e1
);
2069 token
= expect(token
, ';', "in 'for'");
2071 token
= parse_expression(token
, &e2
);
2072 token
= expect(token
, ';', "in 'for'");
2073 token
= parse_expression(token
, &e3
);
2074 token
= expect(token
, ')', "in 'for'");
2075 token
= statement(token
, &iterator
);
2077 stmt
->iterator_syms
= syms
;
2078 stmt
->iterator_pre_statement
= make_statement(e1
);
2079 stmt
->iterator_pre_condition
= e2
;
2080 stmt
->iterator_post_statement
= make_statement(e3
);
2081 stmt
->iterator_post_condition
= NULL
;
2082 stmt
->iterator_statement
= iterator
;
2088 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2090 struct expression
*expr
;
2091 struct statement
*iterator
;
2093 start_iterator(stmt
);
2094 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2095 token
= statement(token
, &iterator
);
2097 stmt
->iterator_pre_condition
= expr
;
2098 stmt
->iterator_post_condition
= NULL
;
2099 stmt
->iterator_statement
= iterator
;
2105 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2107 struct expression
*expr
;
2108 struct statement
*iterator
;
2110 start_iterator(stmt
);
2111 token
= statement(token
->next
, &iterator
);
2112 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2113 token
= token
->next
;
2115 sparse_error(token
->pos
, "expected 'while' after 'do'");
2116 token
= parens_expression(token
, &expr
, "after 'do-while'");
2118 stmt
->iterator_post_condition
= expr
;
2119 stmt
->iterator_statement
= iterator
;
2122 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2123 warning(iterator
->pos
, "do-while statement is not a compound statement");
2125 return expect(token
, ';', "after statement");
2128 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2130 stmt
->type
= STMT_IF
;
2131 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2132 token
= statement(token
, &stmt
->if_true
);
2133 if (token_type(token
) != TOKEN_IDENT
)
2135 if (token
->ident
!= &else_ident
)
2137 return statement(token
->next
, &stmt
->if_false
);
2140 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2142 stmt
->type
= STMT_CASE
;
2143 token
= expect(token
, ':', "after default/case");
2144 add_case_statement(stmt
);
2145 return statement(token
, &stmt
->case_statement
);
2148 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2150 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2151 if (match_op(token
, SPECIAL_ELLIPSIS
))
2152 token
= parse_expression(token
->next
, &stmt
->case_to
);
2153 return case_statement(token
, stmt
);
2156 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2158 return case_statement(token
->next
, stmt
);
2161 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2163 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2164 stmt
->type
= STMT_GOTO
;
2165 stmt
->goto_label
= target
;
2167 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2168 return expect(token
->next
, ';', "at end of statement");
2171 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2173 stmt
->type
= STMT_SWITCH
;
2175 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2176 token
= statement(token
, &stmt
->switch_statement
);
2181 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2183 stmt
->type
= STMT_GOTO
;
2184 token
= token
->next
;
2185 if (match_op(token
, '*')) {
2186 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2187 add_statement(&function_computed_goto_list
, stmt
);
2188 } else if (token_type(token
) == TOKEN_IDENT
) {
2189 stmt
->goto_label
= label_symbol(token
);
2190 token
= token
->next
;
2192 sparse_error(token
->pos
, "Expected identifier or goto expression");
2194 return expect(token
, ';', "at end of statement");
2197 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2199 stmt
->type
= STMT_CONTEXT
;
2200 token
= parse_expression(token
->next
, &stmt
->expression
);
2201 if(stmt
->expression
->type
== EXPR_PREOP
2202 && stmt
->expression
->op
== '('
2203 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2204 struct expression
*expr
;
2205 expr
= stmt
->expression
->unop
;
2206 stmt
->context
= expr
->left
;
2207 stmt
->expression
= expr
->right
;
2209 return expect(token
, ';', "at end of statement");
2212 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2214 stmt
->type
= STMT_RANGE
;
2215 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2216 token
= expect(token
, ',', "after range expression");
2217 token
= assignment_expression(token
, &stmt
->range_low
);
2218 token
= expect(token
, ',', "after low range");
2219 token
= assignment_expression(token
, &stmt
->range_high
);
2220 return expect(token
, ';', "after range statement");
2223 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2225 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2228 if (token_type(token
) == TOKEN_IDENT
) {
2229 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2230 if (s
&& s
->op
->statement
)
2231 return s
->op
->statement(token
, stmt
);
2233 if (match_op(token
->next
, ':')) {
2234 stmt
->type
= STMT_LABEL
;
2235 stmt
->label_identifier
= label_symbol(token
);
2236 token
= skip_attributes(token
->next
->next
);
2237 return statement(token
, &stmt
->label_statement
);
2241 if (match_op(token
, '{')) {
2242 stmt
->type
= STMT_COMPOUND
;
2243 start_symbol_scope();
2244 token
= compound_statement(token
->next
, stmt
);
2247 return expect(token
, '}', "at end of compound statement");
2250 stmt
->type
= STMT_EXPRESSION
;
2251 return expression_statement(token
, &stmt
->expression
);
2254 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2255 static struct token
*label_statement(struct token
*token
)
2257 while (token_type(token
) == TOKEN_IDENT
) {
2258 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2259 /* it's block-scope, but we want label namespace */
2260 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2261 sym
->namespace = NS_LABEL
;
2262 fn_local_symbol(sym
);
2263 token
= token
->next
;
2264 if (!match_op(token
, ','))
2266 token
= token
->next
;
2268 return expect(token
, ';', "at end of label declaration");
2271 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2273 int seen_statement
= 0;
2274 while (token_type(token
) == TOKEN_IDENT
&&
2275 token
->ident
== &__label___ident
)
2276 token
= label_statement(token
->next
);
2278 struct statement
* stmt
;
2279 if (eof_token(token
))
2281 if (match_op(token
, '}'))
2283 if (lookup_type(token
)) {
2284 if (seen_statement
) {
2285 warning(token
->pos
, "mixing declarations and code");
2288 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2289 token
= external_declaration(token
, &stmt
->declaration
);
2291 seen_statement
= Wdeclarationafterstatement
;
2292 token
= statement(token
, &stmt
);
2294 add_statement(list
, stmt
);
2299 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2301 struct symbol_list
**list
= &fn
->arguments
;
2303 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2304 sym
->ident
= token
->ident
;
2305 token
= token
->next
;
2306 sym
->endpos
= token
->pos
;
2307 sym
->ctype
.base_type
= &incomplete_ctype
;
2308 add_symbol(list
, sym
);
2309 if (!match_op(token
, ',') ||
2310 token_type(token
->next
) != TOKEN_IDENT
||
2311 lookup_type(token
->next
))
2313 token
= token
->next
;
2318 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2320 struct symbol_list
**list
= &fn
->arguments
;
2325 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2327 token
= token
->next
;
2331 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2332 token
= parameter_declaration(token
, sym
);
2333 if (sym
->ctype
.base_type
== &void_ctype
) {
2334 /* Special case: (void) */
2335 if (!*list
&& !sym
->ident
)
2337 warning(token
->pos
, "void parameter");
2339 add_symbol(list
, sym
);
2340 if (!match_op(token
, ','))
2342 token
= token
->next
;
2347 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2349 token
= statement_list(token
, &stmt
->stmts
);
2353 static struct expression
*identifier_expression(struct token
*token
)
2355 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2356 expr
->expr_ident
= token
->ident
;
2360 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2362 int idx_from
, idx_to
;
2363 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2365 idx_from
= const_expression_value(from
);
2368 idx_to
= const_expression_value(to
);
2369 if (idx_to
< idx_from
|| idx_from
< 0)
2370 warning(from
->pos
, "nonsense array initializer index range");
2372 expr
->idx_from
= idx_from
;
2373 expr
->idx_to
= idx_to
;
2377 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2379 int expect_equal
= 0;
2380 struct token
*next
= token
->next
;
2381 struct expression
**tail
= ep
;
2386 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2387 struct expression
*expr
= identifier_expression(token
);
2388 if (Wold_initializer
)
2389 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2390 token
= initializer(&expr
->ident_expression
, next
->next
);
2391 if (expr
->ident_expression
)
2396 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2397 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2398 struct expression
*expr
= identifier_expression(next
);
2400 tail
= &expr
->ident_expression
;
2403 } else if (match_op(token
, '[')) {
2404 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2405 token
= constant_expression(token
->next
, &from
);
2407 sparse_error(token
->pos
, "Expected constant expression");
2410 if (match_op(token
, SPECIAL_ELLIPSIS
))
2411 token
= constant_expression(token
->next
, &to
);
2412 expr
= index_expression(from
, to
);
2414 tail
= &expr
->idx_expression
;
2415 token
= expect(token
, ']', "at end of initializer index");
2422 if (nested
&& !expect_equal
) {
2423 if (!match_op(token
, '='))
2424 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2429 token
= expect(token
, '=', "at end of initializer index");
2431 token
= initializer(tail
, token
);
2437 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2439 struct expression
*expr
;
2442 token
= single_initializer(&expr
, token
);
2445 add_expression(list
, expr
);
2446 if (!match_op(token
, ','))
2448 token
= token
->next
;
2453 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2455 if (match_op(token
, '{')) {
2456 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2458 token
= initializer_list(&expr
->expr_list
, token
->next
);
2459 return expect(token
, '}', "at end of initializer");
2461 return assignment_expression(token
, tree
);
2464 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2467 sparse_error(sym
->pos
, "no identifier for function argument");
2470 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2473 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2474 struct symbol_list
**list
)
2476 struct symbol_list
**old_symbol_list
;
2477 struct symbol
*base_type
= decl
->ctype
.base_type
;
2478 struct statement
*stmt
, **p
;
2479 struct symbol
*prev
;
2482 old_symbol_list
= function_symbol_list
;
2483 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2484 function_symbol_list
= &decl
->inline_symbol_list
;
2485 p
= &base_type
->inline_stmt
;
2487 function_symbol_list
= &decl
->symbol_list
;
2488 p
= &base_type
->stmt
;
2490 function_computed_target_list
= NULL
;
2491 function_computed_goto_list
= NULL
;
2493 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2494 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2495 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2497 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2498 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2500 stmt
= start_function(decl
);
2503 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2504 declare_argument(arg
, base_type
);
2505 } END_FOR_EACH_PTR(arg
);
2507 token
= compound_statement(token
->next
, stmt
);
2510 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2511 add_symbol(list
, decl
);
2512 check_declaration(decl
);
2513 decl
->definition
= decl
;
2514 prev
= decl
->same_symbol
;
2515 if (prev
&& prev
->definition
) {
2516 warning(decl
->pos
, "multiple definitions for function '%s'",
2517 show_ident(decl
->ident
));
2518 info(prev
->definition
->pos
, " the previous one is here");
2521 prev
->definition
= decl
;
2522 prev
= prev
->same_symbol
;
2525 function_symbol_list
= old_symbol_list
;
2526 if (function_computed_goto_list
) {
2527 if (!function_computed_target_list
)
2528 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2530 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2531 stmt
->target_list
= function_computed_target_list
;
2532 } END_FOR_EACH_PTR(stmt
);
2535 return expect(token
, '}', "at end of function");
2538 static void promote_k_r_types(struct symbol
*arg
)
2540 struct symbol
*base
= arg
->ctype
.base_type
;
2541 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2542 arg
->ctype
.base_type
= &int_ctype
;
2546 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2548 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2551 FOR_EACH_PTR(real_args
, arg
) {
2552 struct symbol
*type
;
2554 /* This is quadratic in the number of arguments. We _really_ don't care */
2555 FOR_EACH_PTR(argtypes
, type
) {
2556 if (type
->ident
== arg
->ident
)
2558 } END_FOR_EACH_PTR(type
);
2559 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2563 /* "char" and "short" promote to "int" */
2564 promote_k_r_types(type
);
2566 arg
->ctype
= type
->ctype
;
2567 } END_FOR_EACH_PTR(arg
);
2569 FOR_EACH_PTR(argtypes
, arg
) {
2571 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2572 } END_FOR_EACH_PTR(arg
);
2576 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2577 struct symbol_list
**list
)
2579 struct symbol_list
*args
= NULL
;
2581 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2583 token
= declaration_list(token
, &args
);
2584 if (!match_op(token
, ';')) {
2585 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2588 token
= token
->next
;
2589 } while (lookup_type(token
));
2591 apply_k_r_types(args
, decl
);
2593 if (!match_op(token
, '{')) {
2594 sparse_error(token
->pos
, "expected function body");
2597 return parse_function_body(token
, decl
, list
);
2600 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2602 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2603 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2604 struct statement
*stmt
;
2606 anon
->ctype
.base_type
= fn
;
2607 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2610 token
= parse_asm_statement(token
, stmt
);
2612 add_symbol(list
, anon
);
2616 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2618 struct ident
*ident
= NULL
;
2619 struct symbol
*decl
;
2620 struct decl_state ctx
= { .ident
= &ident
};
2622 struct symbol
*base_type
;
2626 /* Top-level inline asm? */
2627 if (token_type(token
) == TOKEN_IDENT
) {
2628 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2629 if (s
&& s
->op
->toplevel
)
2630 return s
->op
->toplevel(token
, list
);
2633 /* Parse declaration-specifiers, if any */
2634 token
= declaration_specifiers(token
, &ctx
);
2635 mod
= storage_modifiers(&ctx
);
2636 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2637 /* Just a type declaration? */
2638 if (match_op(token
, ';')) {
2639 apply_modifiers(token
->pos
, &ctx
);
2644 token
= declarator(token
, &ctx
);
2645 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2646 apply_modifiers(token
->pos
, &ctx
);
2648 decl
->ctype
= ctx
.ctype
;
2649 decl
->ctype
.modifiers
|= mod
;
2650 decl
->endpos
= token
->pos
;
2652 /* Just a type declaration? */
2654 warning(token
->pos
, "missing identifier in declaration");
2655 return expect(token
, ';', "at the end of type declaration");
2658 /* type define declaration? */
2659 is_typedef
= ctx
.storage_class
== STypedef
;
2661 /* Typedefs don't have meaningful storage */
2663 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2665 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2667 base_type
= decl
->ctype
.base_type
;
2670 if (base_type
&& !base_type
->ident
) {
2671 switch (base_type
->type
) {
2676 base_type
->ident
= ident
;
2679 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2680 /* K&R argument declaration? */
2681 if (lookup_type(token
))
2682 return parse_k_r_arguments(token
, decl
, list
);
2683 if (match_op(token
, '{'))
2684 return parse_function_body(token
, decl
, list
);
2686 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2687 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2688 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2689 sparse_error(token
->pos
, "void declaration");
2693 if (!is_typedef
&& match_op(token
, '=')) {
2694 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2695 warning(decl
->pos
, "symbol with external linkage has initializer");
2696 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2698 token
= initializer(&decl
->initializer
, token
->next
);
2701 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2702 add_symbol(list
, decl
);
2703 fn_local_symbol(decl
);
2706 check_declaration(decl
);
2707 if (decl
->same_symbol
)
2708 decl
->definition
= decl
->same_symbol
->definition
;
2710 if (!match_op(token
, ','))
2713 token
= token
->next
;
2715 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2717 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2718 token
= declarator(token
, &ctx
);
2719 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2720 apply_modifiers(token
->pos
, &ctx
);
2721 decl
->ctype
= ctx
.ctype
;
2722 decl
->ctype
.modifiers
|= mod
;
2723 decl
->endpos
= token
->pos
;
2725 sparse_error(token
->pos
, "expected identifier name in type definition");
2730 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2732 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2734 /* Function declarations are automatically extern unless specifically static */
2735 base_type
= decl
->ctype
.base_type
;
2736 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2737 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2738 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2741 return expect(token
, ';', "at end of declaration");