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__",
528 "no_instrument_function",
529 "__no_instrument_function__",
551 "__syscall_linkage__",
560 "warn_unused_result",
561 "__warn_unused_result__",
569 void init_parser(int stream
)
572 for (i
= 0; i
< ARRAY_SIZE(keyword_table
); i
++) {
573 struct init_keyword
*ptr
= keyword_table
+ i
;
574 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
575 sym
->ident
->keyword
= 1;
576 if (ptr
->ns
== NS_TYPEDEF
)
577 sym
->ident
->reserved
= 1;
578 sym
->ctype
.modifiers
= ptr
->modifiers
;
579 sym
->ctype
.base_type
= ptr
->type
;
583 for (i
= 0; i
< ARRAY_SIZE(ignored_attributes
); i
++) {
584 const char * name
= ignored_attributes
[i
];
585 struct symbol
*sym
= create_symbol(stream
, name
, SYM_KEYWORD
,
587 sym
->ident
->keyword
= 1;
588 sym
->op
= &ignore_attr_op
;
593 // Add a symbol to the list of function-local symbols
594 static void fn_local_symbol(struct symbol
*sym
)
596 if (function_symbol_list
)
597 add_symbol(function_symbol_list
, sym
);
600 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
605 if (token_type(token
) != TOKEN_IDENT
)
608 va_start(args
, token
);
610 next
= va_arg(args
, struct ident
*);
611 } while (next
&& token
->ident
!= next
);
614 return next
&& token
->ident
== next
;
618 struct statement
*alloc_statement(struct position pos
, int type
)
620 struct statement
*stmt
= __alloc_statement(0);
626 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
628 static void apply_modifiers(struct position pos
, struct decl_state
*ctx
)
630 struct symbol
*ctype
;
633 ctype
= ctx
->mode
->to_mode(ctx
->ctype
.base_type
);
635 sparse_error(pos
, "don't know how to apply mode to %s",
636 show_typename(ctx
->ctype
.base_type
));
638 ctx
->ctype
.base_type
= ctype
;
642 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
644 struct symbol
*sym
= alloc_symbol(pos
, type
);
646 sym
->ctype
.base_type
= ctype
->base_type
;
647 sym
->ctype
.modifiers
= ctype
->modifiers
;
649 ctype
->base_type
= sym
;
650 ctype
->modifiers
= 0;
655 * NOTE! NS_LABEL is not just a different namespace,
656 * it also ends up using function scope instead of the
657 * regular symbol scope.
659 struct symbol
*label_symbol(struct token
*token
)
661 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_LABEL
);
663 sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
664 bind_symbol(sym
, token
->ident
, NS_LABEL
);
665 fn_local_symbol(sym
);
670 static struct token
*struct_union_enum_specifier(enum type type
,
671 struct token
*token
, struct decl_state
*ctx
,
672 struct token
*(*parse
)(struct token
*, struct symbol
*))
675 struct position
*repos
;
677 token
= handle_attributes(token
, ctx
, KW_ATTRIBUTE
);
678 if (token_type(token
) == TOKEN_IDENT
) {
679 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
681 (is_outer_scope(sym
->scope
) &&
682 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
683 // Either a new symbol, or else an out-of-scope
684 // symbol being redefined.
685 sym
= alloc_symbol(token
->pos
, type
);
686 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
688 if (sym
->type
!= type
)
689 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
690 ctx
->ctype
.base_type
= sym
;
693 if (match_op(token
, '{')) {
694 // The following test is actually wrong for empty
695 // structs, but (1) they are not C99, (2) gcc does
696 // the same thing, and (3) it's easier.
697 if (sym
->symbol_list
)
698 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
700 token
= parse(token
->next
, sym
);
701 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
703 // Mark the structure as needing re-examination
705 sym
->endpos
= token
->pos
;
710 // private struct/union/enum type
711 if (!match_op(token
, '{')) {
712 sparse_error(token
->pos
, "expected declaration");
713 ctx
->ctype
.base_type
= &bad_ctype
;
717 sym
= alloc_symbol(token
->pos
, type
);
718 token
= parse(token
->next
, sym
);
719 ctx
->ctype
.base_type
= sym
;
720 token
= expect(token
, '}', "at end of specifier");
721 sym
->endpos
= token
->pos
;
726 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
728 struct symbol
*field
, *last
= NULL
;
730 res
= struct_declaration_list(token
, &sym
->symbol_list
);
731 FOR_EACH_PTR(sym
->symbol_list
, field
) {
733 struct symbol
*base
= field
->ctype
.base_type
;
734 if (base
&& base
->type
== SYM_BITFIELD
)
738 last
->next_subobject
= field
;
740 } END_FOR_EACH_PTR(field
);
744 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
746 return struct_declaration_list(token
, &sym
->symbol_list
);
749 static struct token
*struct_specifier(struct token
*token
, struct decl_state
*ctx
)
751 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctx
, parse_struct_declaration
);
754 static struct token
*union_specifier(struct token
*token
, struct decl_state
*ctx
)
756 return struct_union_enum_specifier(SYM_UNION
, token
, ctx
, parse_union_declaration
);
762 unsigned long long y
;
765 static void upper_boundary(Num
*n
, Num
*v
)
777 static void lower_boundary(Num
*n
, Num
*v
)
789 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
791 int shift
= type
->bit_size
;
792 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
796 if (upper
->x
== 0 && upper
->y
>> shift
)
798 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
803 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
805 if (s1
->bit_size
< s2
->bit_size
) {
807 } else if (s1
->bit_size
== s2
->bit_size
) {
808 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
811 if (s1
->bit_size
< bits_in_int
)
816 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
820 FOR_EACH_PTR(list
, sym
) {
821 struct expression
*expr
= sym
->initializer
;
822 struct symbol
*ctype
;
823 if (expr
->type
!= EXPR_VALUE
)
826 if (ctype
->bit_size
== base_type
->bit_size
)
828 cast_value(expr
, base_type
, expr
, ctype
);
829 } END_FOR_EACH_PTR(sym
);
832 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
834 unsigned long long lastval
= 0;
835 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
836 Num upper
= {-1, 0}, lower
= {1, 0};
838 parent
->examined
= 1;
839 parent
->ctype
.base_type
= &int_ctype
;
840 while (token_type(token
) == TOKEN_IDENT
) {
841 struct expression
*expr
= NULL
;
842 struct token
*next
= token
->next
;
845 if (match_op(next
, '=')) {
846 next
= constant_expression(next
->next
, &expr
);
847 lastval
= get_expression_value(expr
);
849 if (expr
&& expr
->ctype
)
853 } else if (is_int_type(ctype
)) {
856 error_die(token
->pos
, "can't increment the last enum member");
860 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
861 expr
->value
= lastval
;
865 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
866 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
867 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
868 sym
->initializer
= expr
;
869 sym
->enum_member
= 1;
870 sym
->ctype
.base_type
= parent
;
871 add_ptr_list(&parent
->symbol_list
, sym
);
873 if (base_type
!= &bad_ctype
) {
874 if (ctype
->type
== SYM_NODE
)
875 ctype
= ctype
->ctype
.base_type
;
876 if (ctype
->type
== SYM_ENUM
) {
880 ctype
= ctype
->ctype
.base_type
;
884 * - if all enums are of the same type, then
885 * the base_type is that type (two first
887 * - if enums are of different types, they
888 * all have to be integer types, and the
889 * base type is at least "int_ctype".
890 * - otherwise the base_type is "bad_ctype".
894 } else if (ctype
== base_type
) {
896 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
897 base_type
= bigger_enum_type(base_type
, ctype
);
899 base_type
= &bad_ctype
;
900 parent
->ctype
.base_type
= base_type
;
902 if (is_int_type(base_type
)) {
903 Num v
= {.y
= lastval
};
904 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
906 else if ((long long)lastval
>= 0)
910 upper_boundary(&upper
, &v
);
911 lower_boundary(&lower
, &v
);
915 sym
->endpos
= token
->pos
;
917 if (!match_op(token
, ','))
922 sparse_error(token
->pos
, "bad enum definition");
923 base_type
= &bad_ctype
;
925 else if (!is_int_type(base_type
))
926 base_type
= base_type
;
927 else if (type_is_ok(base_type
, &upper
, &lower
))
928 base_type
= base_type
;
929 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
930 base_type
= &int_ctype
;
931 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
932 base_type
= &uint_ctype
;
933 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
934 base_type
= &long_ctype
;
935 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
936 base_type
= &ulong_ctype
;
937 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
938 base_type
= &llong_ctype
;
939 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
940 base_type
= &ullong_ctype
;
942 base_type
= &bad_ctype
;
943 parent
->ctype
.base_type
= base_type
;
944 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
945 parent
->examined
= 0;
947 cast_enum_list(parent
->symbol_list
, base_type
);
952 static struct token
*enum_specifier(struct token
*token
, struct decl_state
*ctx
)
954 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctx
, parse_enum_declaration
);
955 struct ctype
*ctype
= &ctx
->ctype
.base_type
->ctype
;
957 if (!ctype
->base_type
)
958 ctype
->base_type
= &incomplete_ctype
;
963 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
);
965 static struct token
*typeof_specifier(struct token
*token
, struct decl_state
*ctx
)
969 if (!match_op(token
, '(')) {
970 sparse_error(token
->pos
, "expected '(' after typeof");
973 if (lookup_type(token
->next
)) {
974 token
= typename(token
->next
, &sym
, NULL
);
975 ctx
->ctype
.base_type
= sym
->ctype
.base_type
;
976 apply_ctype(token
->pos
, &sym
->ctype
, &ctx
->ctype
);
978 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
979 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
981 typeof_sym
->endpos
= token
->pos
;
982 if (!typeof_sym
->initializer
) {
983 sparse_error(token
->pos
, "expected expression after the '(' token");
984 typeof_sym
= &bad_ctype
;
986 ctx
->ctype
.base_type
= typeof_sym
;
988 return expect(token
, ')', "after typeof");
991 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
993 struct expression
*expr
= NULL
;
994 if (match_op(token
, '('))
995 token
= parens_expression(token
, &expr
, "in attribute");
999 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1001 if (!ctx
->ctype
.alignment
)
1002 ctx
->ctype
.alignment
= 1;
1006 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1008 int alignment
= max_alignment
;
1009 struct expression
*expr
= NULL
;
1011 if (match_op(token
, '(')) {
1012 token
= parens_expression(token
, &expr
, "in attribute");
1014 alignment
= const_expression_value(expr
);
1016 if (alignment
& (alignment
-1)) {
1017 warning(token
->pos
, "I don't like non-power-of-2 alignments");
1019 } else if (alignment
> ctx
->ctype
.alignment
)
1020 ctx
->ctype
.alignment
= alignment
;
1024 static void apply_qualifier(struct position
*pos
, struct ctype
*ctx
, unsigned long qual
)
1026 if (ctx
->modifiers
& qual
)
1027 warning(*pos
, "duplicate %s", modifier_string(qual
));
1028 ctx
->modifiers
|= qual
;
1031 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1033 apply_qualifier(&token
->pos
, &ctx
->ctype
, attr
->ctype
.modifiers
);
1037 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1039 struct expression
*expr
= NULL
;
1041 token
= expect(token
, '(', "after address_space attribute");
1042 token
= conditional_expression(token
, &expr
);
1044 as
= const_expression_value(expr
);
1045 if (Waddress_space
&& as
)
1048 token
= expect(token
, ')', "after address_space attribute");
1052 static struct symbol
*to_QI_mode(struct symbol
*ctype
)
1054 if (ctype
->ctype
.base_type
!= &int_type
)
1056 if (ctype
== &char_ctype
)
1058 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uchar_ctype
1062 static struct symbol
*to_HI_mode(struct symbol
*ctype
)
1064 if (ctype
->ctype
.base_type
!= &int_type
)
1066 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ushort_ctype
1070 static struct symbol
*to_SI_mode(struct symbol
*ctype
)
1072 if (ctype
->ctype
.base_type
!= &int_type
)
1074 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &uint_ctype
1078 static struct symbol
*to_DI_mode(struct symbol
*ctype
)
1080 if (ctype
->ctype
.base_type
!= &int_type
)
1082 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ullong_ctype
1086 static struct symbol
*to_TI_mode(struct symbol
*ctype
)
1088 if (ctype
->ctype
.base_type
!= &int_type
)
1090 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulllong_ctype
1094 static struct symbol
*to_word_mode(struct symbol
*ctype
)
1096 if (ctype
->ctype
.base_type
!= &int_type
)
1098 return ctype
->ctype
.modifiers
& MOD_UNSIGNED
? &ulong_ctype
1102 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1104 token
= expect(token
, '(', "after mode attribute");
1105 if (token_type(token
) == TOKEN_IDENT
) {
1106 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1107 if (mode
&& mode
->op
->type
== KW_MODE
)
1108 ctx
->mode
= mode
->op
;
1110 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
1111 token
= token
->next
;
1113 sparse_error(token
->pos
, "expect attribute mode symbol\n");
1114 token
= expect(token
, ')', "after mode attribute");
1118 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1120 struct context
*context
= alloc_context();
1121 struct expression
*args
[3];
1124 token
= expect(token
, '(', "after context attribute");
1125 while (!match_op(token
, ')')) {
1126 struct expression
*expr
= NULL
;
1127 token
= conditional_expression(token
, &expr
);
1131 args
[argc
++] = expr
;
1132 if (!match_op(token
, ','))
1134 token
= token
->next
;
1139 sparse_error(token
->pos
, "expected context input/output values");
1142 context
->in
= get_expression_value(args
[0]);
1145 context
->in
= get_expression_value(args
[0]);
1146 context
->out
= get_expression_value(args
[1]);
1149 context
->context
= args
[0];
1150 context
->in
= get_expression_value(args
[1]);
1151 context
->out
= get_expression_value(args
[2]);
1156 add_ptr_list(&ctx
->ctype
.contexts
, context
);
1158 token
= expect(token
, ')', "after context attribute");
1162 static struct token
*attribute_designated_init(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1164 if (ctx
->ctype
.base_type
&& ctx
->ctype
.base_type
->type
== SYM_STRUCT
)
1165 ctx
->ctype
.base_type
->designated_init
= 1;
1167 warning(token
->pos
, "attribute designated_init applied to non-structure type");
1171 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1173 if (Wtransparent_union
)
1174 warning(token
->pos
, "ignoring attribute __transparent_union__");
1178 static struct token
*recover_unknown_attribute(struct token
*token
)
1180 struct expression
*expr
= NULL
;
1182 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
1183 token
= token
->next
;
1184 if (match_op(token
, '('))
1185 token
= parens_expression(token
, &expr
, "in attribute");
1189 static struct token
*attribute_specifier(struct token
*token
, struct decl_state
*ctx
)
1191 token
= expect(token
, '(', "after attribute");
1192 token
= expect(token
, '(', "after attribute");
1195 struct ident
*attribute_name
;
1196 struct symbol
*attr
;
1198 if (eof_token(token
))
1200 if (match_op(token
, ';'))
1202 if (token_type(token
) != TOKEN_IDENT
)
1204 attribute_name
= token
->ident
;
1205 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
1206 if (attr
&& attr
->op
->attribute
)
1207 token
= attr
->op
->attribute(token
->next
, attr
, ctx
);
1209 token
= recover_unknown_attribute(token
);
1211 if (!match_op(token
, ','))
1213 token
= token
->next
;
1216 token
= expect(token
, ')', "after attribute");
1217 token
= expect(token
, ')', "after attribute");
1221 static const char *storage_class
[] =
1223 [STypedef
] = "typedef",
1225 [SExtern
] = "extern",
1226 [SStatic
] = "static",
1227 [SRegister
] = "register",
1228 [SForced
] = "[force]"
1231 static unsigned long storage_modifiers(struct decl_state
*ctx
)
1233 static unsigned long mod
[] =
1236 [SExtern
] = MOD_EXTERN
,
1237 [SStatic
] = MOD_STATIC
,
1238 [SRegister
] = MOD_REGISTER
1240 return mod
[ctx
->storage_class
] | (ctx
->is_inline
? MOD_INLINE
: 0)
1241 | (ctx
->is_tls
? MOD_TLS
: 0);
1244 static void set_storage_class(struct position
*pos
, struct decl_state
*ctx
, int class)
1246 /* __thread can be used alone, or with extern or static */
1247 if (ctx
->is_tls
&& (class != SStatic
&& class != SExtern
)) {
1248 sparse_error(*pos
, "__thread can only be used alone, or with "
1249 "extern or static");
1253 if (!ctx
->storage_class
) {
1254 ctx
->storage_class
= class;
1257 if (ctx
->storage_class
== class)
1258 sparse_error(*pos
, "duplicate %s", storage_class
[class]);
1260 sparse_error(*pos
, "multiple storage classes");
1263 static struct token
*typedef_specifier(struct token
*next
, struct decl_state
*ctx
)
1265 set_storage_class(&next
->pos
, ctx
, STypedef
);
1269 static struct token
*auto_specifier(struct token
*next
, struct decl_state
*ctx
)
1271 set_storage_class(&next
->pos
, ctx
, SAuto
);
1275 static struct token
*register_specifier(struct token
*next
, struct decl_state
*ctx
)
1277 set_storage_class(&next
->pos
, ctx
, SRegister
);
1281 static struct token
*static_specifier(struct token
*next
, struct decl_state
*ctx
)
1283 set_storage_class(&next
->pos
, ctx
, SStatic
);
1287 static struct token
*extern_specifier(struct token
*next
, struct decl_state
*ctx
)
1289 set_storage_class(&next
->pos
, ctx
, SExtern
);
1293 static struct token
*thread_specifier(struct token
*next
, struct decl_state
*ctx
)
1295 /* This GCC extension can be used alone, or with extern or static */
1296 if (!ctx
->storage_class
|| ctx
->storage_class
== SStatic
1297 || ctx
->storage_class
== SExtern
) {
1300 sparse_error(next
->pos
, "__thread can only be used alone, or "
1301 "with extern or static");
1307 static struct token
*attribute_force(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
)
1309 set_storage_class(&token
->pos
, ctx
, SForced
);
1313 static struct token
*inline_specifier(struct token
*next
, struct decl_state
*ctx
)
1319 static struct token
*const_qualifier(struct token
*next
, struct decl_state
*ctx
)
1321 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_CONST
);
1325 static struct token
*volatile_qualifier(struct token
*next
, struct decl_state
*ctx
)
1327 apply_qualifier(&next
->pos
, &ctx
->ctype
, MOD_VOLATILE
);
1331 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1333 unsigned long mod
= thistype
->modifiers
;
1336 apply_qualifier(&pos
, ctype
, mod
);
1339 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1340 (struct ptr_list
**)&ctype
->contexts
);
1343 if (thistype
->alignment
> ctype
->alignment
)
1344 ctype
->alignment
= thistype
->alignment
;
1348 ctype
->as
= thistype
->as
;
1351 static void specifier_conflict(struct position pos
, int what
, struct ident
*new)
1354 if (what
& (Set_S
| Set_T
))
1356 if (what
& Set_Char
)
1358 else if (what
& Set_Double
)
1360 else if (what
& Set_Float
)
1362 else if (what
& Set_Signed
)
1364 else if (what
& Set_Unsigned
)
1366 else if (what
& Set_Short
)
1368 else if (what
& Set_Long
)
1372 sparse_error(pos
, "impossible combination of type specifiers: %s %s",
1373 old
, show_ident(new));
1377 sparse_error(pos
, "two or more data types in declaration specifiers");
1380 static struct symbol
* const int_types
[] =
1381 {&short_ctype
, &int_ctype
, &long_ctype
, &llong_ctype
};
1382 static struct symbol
* const signed_types
[] =
1383 {&sshort_ctype
, &sint_ctype
, &slong_ctype
, &sllong_ctype
,
1385 static struct symbol
* const unsigned_types
[] =
1386 {&ushort_ctype
, &uint_ctype
, &ulong_ctype
, &ullong_ctype
,
1388 static struct symbol
* const real_types
[] =
1389 {&float_ctype
, &double_ctype
, &ldouble_ctype
};
1390 static struct symbol
* const char_types
[] =
1391 {&char_ctype
, &schar_ctype
, &uchar_ctype
};
1392 static struct symbol
* const * const types
[] = {
1393 int_types
+ 1, signed_types
+ 1, unsigned_types
+ 1,
1394 real_types
+ 1, char_types
, char_types
+ 1, char_types
+ 2
1397 struct symbol
*ctype_integer(int size
, int want_unsigned
)
1399 return types
[want_unsigned
? CUInt
: CInt
][size
];
1402 static struct token
*handle_qualifiers(struct token
*t
, struct decl_state
*ctx
)
1404 while (token_type(t
) == TOKEN_IDENT
) {
1405 struct symbol
*s
= lookup_symbol(t
->ident
, NS_TYPEDEF
);
1408 if (s
->type
!= SYM_KEYWORD
)
1410 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1413 if (s
->op
->declarator
)
1414 t
= s
->op
->declarator(t
, ctx
);
1419 static struct token
*declaration_specifiers(struct token
*token
, struct decl_state
*ctx
)
1425 while (token_type(token
) == TOKEN_IDENT
) {
1426 struct symbol
*s
= lookup_symbol(token
->ident
,
1427 NS_TYPEDEF
| NS_SYMBOL
);
1428 if (!s
|| !(s
->namespace & NS_TYPEDEF
))
1430 if (s
->type
!= SYM_KEYWORD
) {
1433 seen
|= Set_S
| Set_T
;
1434 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1435 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1436 token
= token
->next
;
1439 if (s
->op
->type
& KW_SPECIFIER
) {
1440 if (seen
& s
->op
->test
) {
1441 specifier_conflict(token
->pos
,
1447 class += s
->op
->class;
1448 if (s
->op
->type
& KW_SHORT
) {
1450 } else if (s
->op
->type
& KW_LONG
&& size
++) {
1451 if (class == CReal
) {
1452 specifier_conflict(token
->pos
,
1460 token
= token
->next
;
1461 if (s
->op
->declarator
)
1462 token
= s
->op
->declarator(token
, ctx
);
1463 if (s
->op
->type
& KW_EXACT
) {
1464 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1465 ctx
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1469 if (!(seen
& Set_S
)) { /* not set explicitly? */
1470 struct symbol
*base
= &incomplete_ctype
;
1472 base
= types
[class][size
];
1473 ctx
->ctype
.base_type
= base
;
1476 if (ctx
->ctype
.modifiers
& MOD_BITWISE
) {
1477 struct symbol
*type
;
1478 ctx
->ctype
.modifiers
&= ~MOD_BITWISE
;
1479 if (!is_int_type(ctx
->ctype
.base_type
)) {
1480 sparse_error(token
->pos
, "invalid modifier");
1483 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
1484 *type
= *ctx
->ctype
.base_type
;
1485 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
1486 type
->ctype
.base_type
= ctx
->ctype
.base_type
;
1487 type
->type
= SYM_RESTRICT
;
1488 ctx
->ctype
.base_type
= type
;
1489 create_fouled(type
);
1494 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1496 struct expression
*expr
= NULL
;
1498 if (match_idents(token
, &restrict_ident
, &__restrict_ident
, NULL
))
1499 token
= token
->next
;
1500 token
= parse_expression(token
, &expr
);
1501 sym
->array_size
= expr
;
1505 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1506 static struct token
*identifier_list(struct token
*, struct symbol
*);
1507 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1509 static struct token
*skip_attribute(struct token
*token
)
1511 token
= token
->next
;
1512 if (match_op(token
, '(')) {
1514 token
= token
->next
;
1515 while (depth
&& !eof_token(token
)) {
1516 if (token_type(token
) == TOKEN_SPECIAL
) {
1517 if (token
->special
== '(')
1519 else if (token
->special
== ')')
1522 token
= token
->next
;
1528 static struct token
*skip_attributes(struct token
*token
)
1530 struct symbol
*keyword
;
1532 if (token_type(token
) != TOKEN_IDENT
)
1534 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1535 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1537 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1539 token
= expect(token
->next
, '(', "after attribute");
1540 token
= expect(token
, '(', "after attribute");
1542 if (eof_token(token
))
1544 if (match_op(token
, ';'))
1546 if (token_type(token
) != TOKEN_IDENT
)
1548 token
= skip_attribute(token
);
1549 if (!match_op(token
, ','))
1551 token
= token
->next
;
1553 token
= expect(token
, ')', "after attribute");
1554 token
= expect(token
, ')', "after attribute");
1559 static struct token
*handle_attributes(struct token
*token
, struct decl_state
*ctx
, unsigned int keywords
)
1561 struct symbol
*keyword
;
1563 if (token_type(token
) != TOKEN_IDENT
)
1565 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1566 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1568 if (!(keyword
->op
->type
& keywords
))
1570 token
= keyword
->op
->declarator(token
->next
, ctx
);
1571 keywords
&= KW_ATTRIBUTE
;
1576 static int is_nested(struct token
*token
, struct token
**p
,
1577 int prefer_abstract
)
1580 * This can be either a parameter list or a grouping.
1581 * For the direct (non-abstract) case, we know if must be
1582 * a parameter list if we already saw the identifier.
1583 * For the abstract case, we know if must be a parameter
1584 * list if it is empty or starts with a type.
1586 struct token
*next
= token
->next
;
1588 *p
= next
= skip_attributes(next
);
1590 if (token_type(next
) == TOKEN_IDENT
) {
1591 if (lookup_type(next
))
1592 return !prefer_abstract
;
1596 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1603 Empty
, K_R
, Proto
, Bad_Func
,
1606 static enum kind
which_func(struct token
*token
,
1608 int prefer_abstract
)
1610 struct token
*next
= token
->next
;
1612 if (token_type(next
) == TOKEN_IDENT
) {
1613 if (lookup_type(next
))
1615 /* identifier list not in definition; complain */
1616 if (prefer_abstract
)
1618 "identifier list not in definition");
1622 if (token_type(next
) != TOKEN_SPECIAL
)
1625 if (next
->special
== ')') {
1626 /* don't complain about those */
1627 if (!n
|| match_op(next
->next
, ';'))
1630 "non-ANSI function declaration of function '%s'",
1635 if (next
->special
== SPECIAL_ELLIPSIS
) {
1637 "variadic functions must have one named argument");
1644 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1646 struct ctype
*ctype
= &ctx
->ctype
;
1648 struct ident
**p
= ctx
->ident
;
1650 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1651 *ctx
->ident
= token
->ident
;
1652 token
= token
->next
;
1653 } else if (match_op(token
, '(') &&
1654 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1655 struct symbol
*base_type
= ctype
->base_type
;
1656 if (token
->next
!= next
)
1657 next
= handle_attributes(token
->next
, ctx
,
1659 token
= declarator(next
, ctx
);
1660 token
= expect(token
, ')', "in nested declarator");
1661 while (ctype
->base_type
!= base_type
)
1662 ctype
= &ctype
->base_type
->ctype
;
1666 if (match_op(token
, '(')) {
1667 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1669 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1670 token
= token
->next
;
1672 token
= identifier_list(token
, fn
);
1673 else if (kind
== Proto
)
1674 token
= parameter_type_list(token
, fn
);
1675 token
= expect(token
, ')', "in function declarator");
1676 fn
->endpos
= token
->pos
;
1680 while (match_op(token
, '[')) {
1681 struct symbol
*array
;
1682 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1683 token
= abstract_array_declarator(token
->next
, array
);
1684 token
= expect(token
, ']', "in abstract_array_declarator");
1685 array
->endpos
= token
->pos
;
1686 ctype
= &array
->ctype
;
1691 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1693 while (match_op(token
,'*')) {
1694 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1695 ptr
->ctype
.modifiers
= ctx
->ctype
.modifiers
;
1696 ptr
->ctype
.base_type
= ctx
->ctype
.base_type
;
1697 ptr
->ctype
.as
= ctx
->ctype
.as
;
1698 ptr
->ctype
.contexts
= ctx
->ctype
.contexts
;
1699 ctx
->ctype
.modifiers
= 0;
1700 ctx
->ctype
.base_type
= ptr
;
1702 ctx
->ctype
.contexts
= NULL
;
1703 ctx
->ctype
.alignment
= 0;
1705 token
= handle_qualifiers(token
->next
, ctx
);
1706 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1711 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1713 token
= pointer(token
, ctx
);
1714 return direct_declarator(token
, ctx
);
1717 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1719 struct ctype
*ctype
= &ctx
->ctype
;
1720 struct expression
*expr
;
1721 struct symbol
*bitfield
;
1724 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1725 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1726 show_typename(ctype
->base_type
));
1727 // Parse this to recover gracefully.
1728 return conditional_expression(token
->next
, &expr
);
1731 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1732 token
= conditional_expression(token
->next
, &expr
);
1733 width
= const_expression_value(expr
);
1734 bitfield
->bit_size
= width
;
1736 if (width
< 0 || width
> INT_MAX
) {
1737 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1739 } else if (*ctx
->ident
&& width
== 0) {
1740 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1741 show_ident(*ctx
->ident
));
1743 } else if (*ctx
->ident
) {
1744 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1745 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1746 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1747 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1748 // Valid values are either {-1;0} or {0}, depending on integer
1749 // representation. The latter makes for very efficient code...
1750 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1752 if (Wdefault_bitfield_sign
&&
1753 bitfield_type
->type
!= SYM_ENUM
&&
1754 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1756 // The sign of bitfields is unspecified by default.
1757 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1760 bitfield
->bit_size
= width
;
1761 bitfield
->endpos
= token
->pos
;
1765 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1767 struct decl_state ctx
= {.prefer_abstract
= 0};
1771 token
= declaration_specifiers(token
, &ctx
);
1772 mod
= storage_modifiers(&ctx
);
1775 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1776 ctx
.ident
= &decl
->ident
;
1778 token
= declarator(token
, &ctx
);
1779 if (match_op(token
, ':'))
1780 token
= handle_bitfield(token
, &ctx
);
1782 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1783 apply_modifiers(token
->pos
, &ctx
);
1785 decl
->ctype
= ctx
.ctype
;
1786 decl
->ctype
.modifiers
|= mod
;
1787 decl
->endpos
= token
->pos
;
1788 add_symbol(list
, decl
);
1789 if (!match_op(token
, ','))
1791 token
= token
->next
;
1797 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1799 while (!match_op(token
, '}')) {
1800 if (!match_op(token
, ';'))
1801 token
= declaration_list(token
, list
);
1802 if (!match_op(token
, ';')) {
1803 sparse_error(token
->pos
, "expected ; at end of declaration");
1806 token
= token
->next
;
1811 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1813 struct decl_state ctx
= {.prefer_abstract
= 1};
1815 token
= declaration_specifiers(token
, &ctx
);
1816 ctx
.ident
= &sym
->ident
;
1817 token
= declarator(token
, &ctx
);
1818 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
1819 apply_modifiers(token
->pos
, &ctx
);
1820 sym
->ctype
= ctx
.ctype
;
1821 sym
->ctype
.modifiers
|= storage_modifiers(&ctx
);
1822 sym
->endpos
= token
->pos
;
1826 struct token
*typename(struct token
*token
, struct symbol
**p
, int *forced
)
1828 struct decl_state ctx
= {.prefer_abstract
= 1};
1830 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1832 token
= declaration_specifiers(token
, &ctx
);
1833 token
= declarator(token
, &ctx
);
1834 apply_modifiers(token
->pos
, &ctx
);
1835 sym
->ctype
= ctx
.ctype
;
1836 sym
->endpos
= token
->pos
;
1837 class = ctx
.storage_class
;
1840 if (class == SForced
) {
1846 warning(sym
->pos
, "storage class in typename (%s %s)",
1847 storage_class
[class], show_typename(sym
));
1851 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1853 token
= parse_expression(token
, tree
);
1854 return expect(token
, ';', "at end of statement");
1857 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1858 struct expression_list
**inout
)
1860 struct expression
*expr
;
1862 /* Allow empty operands */
1863 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1866 struct ident
*ident
= NULL
;
1867 if (match_op(token
->next
, '[') &&
1868 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1869 match_op(token
->next
->next
->next
, ']')) {
1870 ident
= token
->next
->next
->ident
;
1871 token
= token
->next
->next
->next
;
1873 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1874 token
= primary_expression(token
->next
, &expr
);
1875 add_expression(inout
, expr
);
1876 token
= parens_expression(token
, &expr
, "in asm parameter");
1877 add_expression(inout
, expr
);
1878 } while (match_op(token
, ','));
1882 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1883 struct expression_list
**clobbers
)
1885 struct expression
*expr
;
1888 token
= primary_expression(token
->next
, &expr
);
1889 add_expression(clobbers
, expr
);
1890 } while (match_op(token
, ','));
1894 static struct token
*parse_asm_labels(struct token
*token
, struct statement
*stmt
,
1895 struct symbol_list
**labels
)
1897 struct symbol
*label
;
1900 token
= token
->next
; /* skip ':' and ',' */
1901 if (token_type(token
) != TOKEN_IDENT
)
1903 label
= label_symbol(token
);
1904 add_symbol(labels
, label
);
1905 token
= token
->next
;
1906 } while (match_op(token
, ','));
1910 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1914 token
= token
->next
;
1915 stmt
->type
= STMT_ASM
;
1916 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1917 token
= token
->next
;
1919 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &goto_ident
) {
1921 token
= token
->next
;
1923 token
= expect(token
, '(', "after asm");
1924 token
= parse_expression(token
, &stmt
->asm_string
);
1925 if (match_op(token
, ':'))
1926 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1927 if (match_op(token
, ':'))
1928 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1929 if (match_op(token
, ':'))
1930 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1931 if (is_goto
&& match_op(token
, ':'))
1932 token
= parse_asm_labels(token
, stmt
, &stmt
->asm_labels
);
1933 token
= expect(token
, ')', "after asm");
1934 return expect(token
, ';', "at end of asm-statement");
1937 static struct token
*parse_asm_declarator(struct token
*token
, struct decl_state
*ctx
)
1939 struct expression
*expr
;
1940 token
= expect(token
, '(', "after asm");
1941 token
= parse_expression(token
->next
, &expr
);
1942 token
= expect(token
, ')', "after asm");
1946 /* Make a statement out of an expression */
1947 static struct statement
*make_statement(struct expression
*expr
)
1949 struct statement
*stmt
;
1953 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1954 stmt
->expression
= expr
;
1959 * All iterators have two symbols associated with them:
1960 * the "continue" and "break" symbols, which are targets
1961 * for continue and break statements respectively.
1963 * They are in a special name-space, but they follow
1964 * all the normal visibility rules, so nested iterators
1965 * automatically work right.
1967 static void start_iterator(struct statement
*stmt
)
1969 struct symbol
*cont
, *brk
;
1971 start_symbol_scope();
1972 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1973 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1974 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1975 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1977 stmt
->type
= STMT_ITERATOR
;
1978 stmt
->iterator_break
= brk
;
1979 stmt
->iterator_continue
= cont
;
1980 fn_local_symbol(brk
);
1981 fn_local_symbol(cont
);
1984 static void end_iterator(struct statement
*stmt
)
1989 static struct statement
*start_function(struct symbol
*sym
)
1992 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1994 start_function_scope();
1995 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1996 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1997 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_TLS
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1998 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1999 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
2001 fn_local_symbol(ret
);
2003 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2009 static void end_function(struct symbol
*sym
)
2012 end_function_scope();
2016 * A "switch()" statement, like an iterator, has a
2017 * the "break" symbol associated with it. It works
2018 * exactly like the iterator break - it's the target
2019 * for any break-statements in scope, and means that
2020 * "break" handling doesn't even need to know whether
2021 * it's breaking out of an iterator or a switch.
2023 * In addition, the "case" symbol is a marker for the
2024 * case/default statements to find the switch statement
2025 * that they are associated with.
2027 static void start_switch(struct statement
*stmt
)
2029 struct symbol
*brk
, *switch_case
;
2031 start_symbol_scope();
2032 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2033 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
2035 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2036 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
2037 switch_case
->stmt
= stmt
;
2039 stmt
->type
= STMT_SWITCH
;
2040 stmt
->switch_break
= brk
;
2041 stmt
->switch_case
= switch_case
;
2043 fn_local_symbol(brk
);
2044 fn_local_symbol(switch_case
);
2047 static void end_switch(struct statement
*stmt
)
2049 if (!stmt
->switch_case
->symbol_list
)
2050 warning(stmt
->pos
, "switch with no cases");
2054 static void add_case_statement(struct statement
*stmt
)
2056 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
2060 sparse_error(stmt
->pos
, "not in switch scope");
2061 stmt
->type
= STMT_NONE
;
2064 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
2065 add_symbol(&target
->symbol_list
, sym
);
2067 stmt
->case_label
= sym
;
2068 fn_local_symbol(sym
);
2071 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
2073 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
2076 error_die(token
->pos
, "internal error: return without a function target");
2077 stmt
->type
= STMT_RETURN
;
2078 stmt
->ret_target
= target
;
2079 return expression_statement(token
->next
, &stmt
->ret_value
);
2082 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
2084 struct symbol_list
*syms
;
2085 struct expression
*e1
, *e2
, *e3
;
2086 struct statement
*iterator
;
2088 start_iterator(stmt
);
2089 token
= expect(token
->next
, '(', "after 'for'");
2093 /* C99 variable declaration? */
2094 if (lookup_type(token
)) {
2095 token
= external_declaration(token
, &syms
);
2097 token
= parse_expression(token
, &e1
);
2098 token
= expect(token
, ';', "in 'for'");
2100 token
= parse_expression(token
, &e2
);
2101 token
= expect(token
, ';', "in 'for'");
2102 token
= parse_expression(token
, &e3
);
2103 token
= expect(token
, ')', "in 'for'");
2104 token
= statement(token
, &iterator
);
2106 stmt
->iterator_syms
= syms
;
2107 stmt
->iterator_pre_statement
= make_statement(e1
);
2108 stmt
->iterator_pre_condition
= e2
;
2109 stmt
->iterator_post_statement
= make_statement(e3
);
2110 stmt
->iterator_post_condition
= NULL
;
2111 stmt
->iterator_statement
= iterator
;
2117 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
2119 struct expression
*expr
;
2120 struct statement
*iterator
;
2122 start_iterator(stmt
);
2123 token
= parens_expression(token
->next
, &expr
, "after 'while'");
2124 token
= statement(token
, &iterator
);
2126 stmt
->iterator_pre_condition
= expr
;
2127 stmt
->iterator_post_condition
= NULL
;
2128 stmt
->iterator_statement
= iterator
;
2134 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
2136 struct expression
*expr
;
2137 struct statement
*iterator
;
2139 start_iterator(stmt
);
2140 token
= statement(token
->next
, &iterator
);
2141 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
2142 token
= token
->next
;
2144 sparse_error(token
->pos
, "expected 'while' after 'do'");
2145 token
= parens_expression(token
, &expr
, "after 'do-while'");
2147 stmt
->iterator_post_condition
= expr
;
2148 stmt
->iterator_statement
= iterator
;
2151 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
2152 warning(iterator
->pos
, "do-while statement is not a compound statement");
2154 return expect(token
, ';', "after statement");
2157 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
2159 stmt
->type
= STMT_IF
;
2160 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
2161 token
= statement(token
, &stmt
->if_true
);
2162 if (token_type(token
) != TOKEN_IDENT
)
2164 if (token
->ident
!= &else_ident
)
2166 return statement(token
->next
, &stmt
->if_false
);
2169 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
2171 stmt
->type
= STMT_CASE
;
2172 token
= expect(token
, ':', "after default/case");
2173 add_case_statement(stmt
);
2174 return statement(token
, &stmt
->case_statement
);
2177 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
2179 token
= parse_expression(token
->next
, &stmt
->case_expression
);
2180 if (match_op(token
, SPECIAL_ELLIPSIS
))
2181 token
= parse_expression(token
->next
, &stmt
->case_to
);
2182 return case_statement(token
, stmt
);
2185 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
2187 return case_statement(token
->next
, stmt
);
2190 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
2192 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
2193 stmt
->type
= STMT_GOTO
;
2194 stmt
->goto_label
= target
;
2196 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
2197 return expect(token
->next
, ';', "at end of statement");
2200 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
2202 stmt
->type
= STMT_SWITCH
;
2204 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
2205 token
= statement(token
, &stmt
->switch_statement
);
2210 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
2212 stmt
->type
= STMT_GOTO
;
2213 token
= token
->next
;
2214 if (match_op(token
, '*')) {
2215 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
2216 add_statement(&function_computed_goto_list
, stmt
);
2217 } else if (token_type(token
) == TOKEN_IDENT
) {
2218 stmt
->goto_label
= label_symbol(token
);
2219 token
= token
->next
;
2221 sparse_error(token
->pos
, "Expected identifier or goto expression");
2223 return expect(token
, ';', "at end of statement");
2226 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
2228 stmt
->type
= STMT_CONTEXT
;
2229 token
= parse_expression(token
->next
, &stmt
->expression
);
2230 if(stmt
->expression
->type
== EXPR_PREOP
2231 && stmt
->expression
->op
== '('
2232 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
2233 struct expression
*expr
;
2234 expr
= stmt
->expression
->unop
;
2235 stmt
->context
= expr
->left
;
2236 stmt
->expression
= expr
->right
;
2238 return expect(token
, ';', "at end of statement");
2241 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
2243 stmt
->type
= STMT_RANGE
;
2244 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
2245 token
= expect(token
, ',', "after range expression");
2246 token
= assignment_expression(token
, &stmt
->range_low
);
2247 token
= expect(token
, ',', "after low range");
2248 token
= assignment_expression(token
, &stmt
->range_high
);
2249 return expect(token
, ';', "after range statement");
2252 static struct token
*statement(struct token
*token
, struct statement
**tree
)
2254 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2257 if (token_type(token
) == TOKEN_IDENT
) {
2258 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2259 if (s
&& s
->op
->statement
)
2260 return s
->op
->statement(token
, stmt
);
2262 if (match_op(token
->next
, ':')) {
2263 stmt
->type
= STMT_LABEL
;
2264 stmt
->label_identifier
= label_symbol(token
);
2265 token
= skip_attributes(token
->next
->next
);
2266 return statement(token
, &stmt
->label_statement
);
2270 if (match_op(token
, '{')) {
2271 stmt
->type
= STMT_COMPOUND
;
2272 start_symbol_scope();
2273 token
= compound_statement(token
->next
, stmt
);
2276 return expect(token
, '}', "at end of compound statement");
2279 stmt
->type
= STMT_EXPRESSION
;
2280 return expression_statement(token
, &stmt
->expression
);
2283 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2284 static struct token
*label_statement(struct token
*token
)
2286 while (token_type(token
) == TOKEN_IDENT
) {
2287 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_LABEL
);
2288 /* it's block-scope, but we want label namespace */
2289 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
2290 sym
->namespace = NS_LABEL
;
2291 fn_local_symbol(sym
);
2292 token
= token
->next
;
2293 if (!match_op(token
, ','))
2295 token
= token
->next
;
2297 return expect(token
, ';', "at end of label declaration");
2300 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
2302 int seen_statement
= 0;
2303 while (token_type(token
) == TOKEN_IDENT
&&
2304 token
->ident
== &__label___ident
)
2305 token
= label_statement(token
->next
);
2307 struct statement
* stmt
;
2308 if (eof_token(token
))
2310 if (match_op(token
, '}'))
2312 if (lookup_type(token
)) {
2313 if (seen_statement
) {
2314 warning(token
->pos
, "mixing declarations and code");
2317 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
2318 token
= external_declaration(token
, &stmt
->declaration
);
2320 seen_statement
= Wdeclarationafterstatement
;
2321 token
= statement(token
, &stmt
);
2323 add_statement(list
, stmt
);
2328 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
2330 struct symbol_list
**list
= &fn
->arguments
;
2332 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2333 sym
->ident
= token
->ident
;
2334 token
= token
->next
;
2335 sym
->endpos
= token
->pos
;
2336 sym
->ctype
.base_type
= &incomplete_ctype
;
2337 add_symbol(list
, sym
);
2338 if (!match_op(token
, ',') ||
2339 token_type(token
->next
) != TOKEN_IDENT
||
2340 lookup_type(token
->next
))
2342 token
= token
->next
;
2347 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
2349 struct symbol_list
**list
= &fn
->arguments
;
2354 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
2356 token
= token
->next
;
2360 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
2361 token
= parameter_declaration(token
, sym
);
2362 if (sym
->ctype
.base_type
== &void_ctype
) {
2363 /* Special case: (void) */
2364 if (!*list
&& !sym
->ident
)
2366 warning(token
->pos
, "void parameter");
2368 add_symbol(list
, sym
);
2369 if (!match_op(token
, ','))
2371 token
= token
->next
;
2376 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2378 token
= statement_list(token
, &stmt
->stmts
);
2382 static struct expression
*identifier_expression(struct token
*token
)
2384 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2385 expr
->expr_ident
= token
->ident
;
2389 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2391 int idx_from
, idx_to
;
2392 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2394 idx_from
= const_expression_value(from
);
2397 idx_to
= const_expression_value(to
);
2398 if (idx_to
< idx_from
|| idx_from
< 0)
2399 warning(from
->pos
, "nonsense array initializer index range");
2401 expr
->idx_from
= idx_from
;
2402 expr
->idx_to
= idx_to
;
2406 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2408 int expect_equal
= 0;
2409 struct token
*next
= token
->next
;
2410 struct expression
**tail
= ep
;
2415 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2416 struct expression
*expr
= identifier_expression(token
);
2417 if (Wold_initializer
)
2418 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2419 token
= initializer(&expr
->ident_expression
, next
->next
);
2420 if (expr
->ident_expression
)
2425 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2426 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2427 struct expression
*expr
= identifier_expression(next
);
2429 tail
= &expr
->ident_expression
;
2432 } else if (match_op(token
, '[')) {
2433 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2434 token
= constant_expression(token
->next
, &from
);
2436 sparse_error(token
->pos
, "Expected constant expression");
2439 if (match_op(token
, SPECIAL_ELLIPSIS
))
2440 token
= constant_expression(token
->next
, &to
);
2441 expr
= index_expression(from
, to
);
2443 tail
= &expr
->idx_expression
;
2444 token
= expect(token
, ']', "at end of initializer index");
2451 if (nested
&& !expect_equal
) {
2452 if (!match_op(token
, '='))
2453 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2458 token
= expect(token
, '=', "at end of initializer index");
2460 token
= initializer(tail
, token
);
2466 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2468 struct expression
*expr
;
2471 token
= single_initializer(&expr
, token
);
2474 add_expression(list
, expr
);
2475 if (!match_op(token
, ','))
2477 token
= token
->next
;
2482 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2484 if (match_op(token
, '{')) {
2485 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2487 token
= initializer_list(&expr
->expr_list
, token
->next
);
2488 return expect(token
, '}', "at end of initializer");
2490 return assignment_expression(token
, tree
);
2493 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2496 sparse_error(sym
->pos
, "no identifier for function argument");
2499 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2502 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2503 struct symbol_list
**list
)
2505 struct symbol_list
**old_symbol_list
;
2506 struct symbol
*base_type
= decl
->ctype
.base_type
;
2507 struct statement
*stmt
, **p
;
2508 struct symbol
*prev
;
2511 old_symbol_list
= function_symbol_list
;
2512 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2513 function_symbol_list
= &decl
->inline_symbol_list
;
2514 p
= &base_type
->inline_stmt
;
2516 function_symbol_list
= &decl
->symbol_list
;
2517 p
= &base_type
->stmt
;
2519 function_computed_target_list
= NULL
;
2520 function_computed_goto_list
= NULL
;
2522 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2523 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2524 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2526 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2527 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2529 stmt
= start_function(decl
);
2532 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2533 declare_argument(arg
, base_type
);
2534 } END_FOR_EACH_PTR(arg
);
2536 token
= compound_statement(token
->next
, stmt
);
2539 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2540 add_symbol(list
, decl
);
2541 check_declaration(decl
);
2542 decl
->definition
= decl
;
2543 prev
= decl
->same_symbol
;
2544 if (prev
&& prev
->definition
) {
2545 warning(decl
->pos
, "multiple definitions for function '%s'",
2546 show_ident(decl
->ident
));
2547 info(prev
->definition
->pos
, " the previous one is here");
2550 prev
->definition
= decl
;
2551 prev
= prev
->same_symbol
;
2554 function_symbol_list
= old_symbol_list
;
2555 if (function_computed_goto_list
) {
2556 if (!function_computed_target_list
)
2557 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2559 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2560 stmt
->target_list
= function_computed_target_list
;
2561 } END_FOR_EACH_PTR(stmt
);
2564 return expect(token
, '}', "at end of function");
2567 static void promote_k_r_types(struct symbol
*arg
)
2569 struct symbol
*base
= arg
->ctype
.base_type
;
2570 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2571 arg
->ctype
.base_type
= &int_ctype
;
2575 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2577 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2580 FOR_EACH_PTR(real_args
, arg
) {
2581 struct symbol
*type
;
2583 /* This is quadratic in the number of arguments. We _really_ don't care */
2584 FOR_EACH_PTR(argtypes
, type
) {
2585 if (type
->ident
== arg
->ident
)
2587 } END_FOR_EACH_PTR(type
);
2588 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2592 /* "char" and "short" promote to "int" */
2593 promote_k_r_types(type
);
2595 arg
->ctype
= type
->ctype
;
2596 } END_FOR_EACH_PTR(arg
);
2598 FOR_EACH_PTR(argtypes
, arg
) {
2600 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2601 } END_FOR_EACH_PTR(arg
);
2605 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2606 struct symbol_list
**list
)
2608 struct symbol_list
*args
= NULL
;
2610 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2612 token
= declaration_list(token
, &args
);
2613 if (!match_op(token
, ';')) {
2614 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2617 token
= token
->next
;
2618 } while (lookup_type(token
));
2620 apply_k_r_types(args
, decl
);
2622 if (!match_op(token
, '{')) {
2623 sparse_error(token
->pos
, "expected function body");
2626 return parse_function_body(token
, decl
, list
);
2629 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2631 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2632 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2633 struct statement
*stmt
;
2635 anon
->ctype
.base_type
= fn
;
2636 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2639 token
= parse_asm_statement(token
, stmt
);
2641 add_symbol(list
, anon
);
2645 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2647 struct ident
*ident
= NULL
;
2648 struct symbol
*decl
;
2649 struct decl_state ctx
= { .ident
= &ident
};
2651 struct symbol
*base_type
;
2655 /* Top-level inline asm? */
2656 if (token_type(token
) == TOKEN_IDENT
) {
2657 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2658 if (s
&& s
->op
->toplevel
)
2659 return s
->op
->toplevel(token
, list
);
2662 /* Parse declaration-specifiers, if any */
2663 token
= declaration_specifiers(token
, &ctx
);
2664 mod
= storage_modifiers(&ctx
);
2665 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2666 /* Just a type declaration? */
2667 if (match_op(token
, ';')) {
2668 apply_modifiers(token
->pos
, &ctx
);
2673 token
= declarator(token
, &ctx
);
2674 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2675 apply_modifiers(token
->pos
, &ctx
);
2677 decl
->ctype
= ctx
.ctype
;
2678 decl
->ctype
.modifiers
|= mod
;
2679 decl
->endpos
= token
->pos
;
2681 /* Just a type declaration? */
2683 warning(token
->pos
, "missing identifier in declaration");
2684 return expect(token
, ';', "at the end of type declaration");
2687 /* type define declaration? */
2688 is_typedef
= ctx
.storage_class
== STypedef
;
2690 /* Typedefs don't have meaningful storage */
2692 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2694 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2696 base_type
= decl
->ctype
.base_type
;
2699 if (base_type
&& !base_type
->ident
) {
2700 switch (base_type
->type
) {
2705 base_type
->ident
= ident
;
2708 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2709 /* K&R argument declaration? */
2710 if (lookup_type(token
))
2711 return parse_k_r_arguments(token
, decl
, list
);
2712 if (match_op(token
, '{'))
2713 return parse_function_body(token
, decl
, list
);
2715 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2716 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2717 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2718 sparse_error(token
->pos
, "void declaration");
2722 if (!is_typedef
&& match_op(token
, '=')) {
2723 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2724 warning(decl
->pos
, "symbol with external linkage has initializer");
2725 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2727 token
= initializer(&decl
->initializer
, token
->next
);
2730 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2731 add_symbol(list
, decl
);
2732 fn_local_symbol(decl
);
2735 check_declaration(decl
);
2736 if (decl
->same_symbol
)
2737 decl
->definition
= decl
->same_symbol
->definition
;
2739 if (!match_op(token
, ','))
2742 token
= token
->next
;
2744 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2746 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
);
2747 token
= declarator(token
, &ctx
);
2748 token
= handle_attributes(token
, &ctx
, KW_ATTRIBUTE
| KW_ASM
);
2749 apply_modifiers(token
->pos
, &ctx
);
2750 decl
->ctype
= ctx
.ctype
;
2751 decl
->ctype
.modifiers
|= mod
;
2752 decl
->endpos
= token
->pos
;
2754 sparse_error(token
->pos
, "expected identifier name in type definition");
2759 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2761 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2763 /* Function declarations are automatically extern unless specifically static */
2764 base_type
= decl
->ctype
.base_type
;
2765 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2766 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2767 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2770 return expect(token
, ';', "at end of declaration");