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
9 * Licensed under the Open Software License version 1.1
27 #include "expression.h"
30 #define warn_on_mixed (1)
32 static struct symbol_list
**function_symbol_list
;
33 struct symbol_list
*function_computed_target_list
;
34 struct statement_list
*function_computed_goto_list
;
36 static struct token
*statement(struct token
*token
, struct statement
**tree
);
38 // Add a symbol to the list of function-local symbols
39 static void fn_local_symbol(struct symbol
*sym
)
41 if (function_symbol_list
)
42 add_symbol(function_symbol_list
, sym
);
45 static int match_idents(struct token
*token
, ...)
49 if (token_type(token
) != TOKEN_IDENT
)
52 va_start(args
, token
);
54 struct ident
* next
= va_arg(args
, struct ident
*);
57 if (token
->ident
== next
)
63 struct statement
*alloc_statement(struct position pos
, int type
)
65 struct statement
*stmt
= __alloc_statement(0);
71 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
73 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
75 struct symbol
*sym
= alloc_symbol(pos
, type
);
77 sym
->ctype
.base_type
= ctype
->base_type
;
78 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
80 ctype
->base_type
= sym
;
81 ctype
->modifiers
&= MOD_STORAGE
;
85 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
87 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
89 sym
= alloc_symbol(token
->pos
, type
);
90 bind_symbol(sym
, token
->ident
, ns
);
91 if (type
== SYM_LABEL
)
98 * NOTE! NS_LABEL is not just a different namespace,
99 * it also ends up using function scope instead of the
100 * regular symbol scope.
102 struct symbol
*label_symbol(struct token
*token
)
104 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
107 static struct token
*struct_union_enum_specifier(enum type type
,
108 struct token
*token
, struct ctype
*ctype
,
109 struct token
*(*parse
)(struct token
*, struct symbol
*))
112 struct position
*repos
;
114 ctype
->modifiers
= 0;
115 if (token_type(token
) == TOKEN_IDENT
) {
116 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
118 (sym
->scope
!= block_scope
&&
119 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
120 // Either a new symbol, or else an out-of-scope
121 // symbol being redefined.
122 sym
= alloc_symbol(token
->pos
, type
);
123 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
125 if (sym
->type
!= type
)
126 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
127 ctype
->base_type
= sym
;
130 if (match_op(token
, '{')) {
131 // The following test is actually wrong for empty
132 // structs, but (1) they are not C99, (2) gcc does
133 // the same thing, and (3) it's easier.
134 if (sym
->symbol_list
)
135 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
137 token
= parse(token
->next
, sym
);
138 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
140 // Mark the structure as needing re-examination
146 // private struct/union/enum type
147 if (!match_op(token
, '{')) {
148 sparse_error(token
->pos
, "expected declaration");
149 ctype
->base_type
= &bad_ctype
;
153 sym
= alloc_symbol(token
->pos
, type
);
154 token
= parse(token
->next
, sym
);
155 ctype
->base_type
= sym
;
156 return expect(token
, '}', "at end of specifier");
159 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
161 return struct_declaration_list(token
, &sym
->symbol_list
);
164 static struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
166 return struct_union_enum_specifier(type
, token
, ctype
, parse_struct_declaration
);
171 unsigned long long y
;
174 static void upper_boundary(Num
*n
, Num
*v
)
186 static void lower_boundary(Num
*n
, Num
*v
)
198 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
200 int shift
= type
->bit_size
;
201 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
205 if (upper
->x
== 0 && upper
->y
>> shift
)
207 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
212 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
214 if (s1
->bit_size
< s2
->bit_size
) {
216 } else if (s1
->bit_size
== s2
->bit_size
) {
217 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
220 if (s1
->bit_size
< bits_in_int
)
225 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
229 FOR_EACH_PTR(list
, sym
) {
230 struct expression
*expr
= sym
->initializer
;
231 struct symbol
*ctype
;
232 if (expr
->type
!= EXPR_VALUE
)
235 if (ctype
->bit_size
== base_type
->bit_size
)
237 cast_value(expr
, base_type
, expr
, ctype
);
238 } END_FOR_EACH_PTR(sym
);
241 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
243 unsigned long long lastval
= 0;
244 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
245 Num upper
= {-1, 0}, lower
= {1, 0};
246 struct symbol_list
*entries
= NULL
;
248 parent
->examined
= 1;
249 parent
->ctype
.base_type
= &int_ctype
;
250 while (token_type(token
) == TOKEN_IDENT
) {
251 struct expression
*expr
= NULL
;
252 struct token
*next
= token
->next
;
255 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
256 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
257 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
259 if (match_op(next
, '=')) {
260 next
= constant_expression(next
->next
, &expr
);
261 lastval
= get_expression_value(expr
);
263 if (expr
&& expr
->ctype
)
267 } else if (is_int_type(ctype
)) {
270 error_die(token
->pos
, "can't increment the last enum member");
274 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
275 expr
->value
= lastval
;
279 sym
->initializer
= expr
;
280 sym
->ctype
.base_type
= parent
;
281 add_ptr_list(&entries
, sym
);
283 if (base_type
!= &bad_ctype
) {
284 if (ctype
->type
== SYM_NODE
)
285 ctype
= ctype
->ctype
.base_type
;
286 if (ctype
->type
== SYM_ENUM
) {
290 ctype
= ctype
->ctype
.base_type
;
294 * - if all enum's are of the same type, then
295 * the base_type is that type (two first
297 * - if enums are of different types, they
298 * all have to be integer types, and the
299 * base type is at least "int_ctype".
300 * - otherwise the base_type is "bad_ctype".
304 } else if (ctype
== base_type
) {
306 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
307 base_type
= bigger_enum_type(base_type
, ctype
);
309 base_type
= &bad_ctype
;
310 parent
->ctype
.base_type
= base_type
;
312 if (is_int_type(base_type
)) {
313 Num v
= {.y
= lastval
};
314 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
316 else if ((long long)lastval
>= 0)
320 upper_boundary(&upper
, &v
);
321 lower_boundary(&lower
, &v
);
324 if (!match_op(token
, ','))
329 sparse_error(token
->pos
, "bad enum definition");
330 base_type
= &bad_ctype
;
332 else if (!is_int_type(base_type
))
333 base_type
= base_type
;
334 else if (type_is_ok(base_type
, &upper
, &lower
))
335 base_type
= base_type
;
336 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
337 base_type
= &int_ctype
;
338 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
339 base_type
= &uint_ctype
;
340 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
341 base_type
= &long_ctype
;
342 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
343 base_type
= &ulong_ctype
;
344 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
345 base_type
= &llong_ctype
;
346 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
347 base_type
= &ullong_ctype
;
349 base_type
= &bad_ctype
;
350 parent
->ctype
.base_type
= base_type
;
351 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
352 parent
->examined
= 0;
354 cast_enum_list(entries
, base_type
);
355 free_ptr_list(&entries
);
360 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
362 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
364 ctype
= &ctype
->base_type
->ctype
;
365 if (!ctype
->base_type
)
366 ctype
->base_type
= &incomplete_ctype
;
371 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
375 if (!match_op(token
, '(')) {
376 sparse_error(token
->pos
, "expected '(' after typeof");
379 if (lookup_type(token
->next
)) {
380 token
= typename(token
->next
, &sym
);
383 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
384 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
386 ctype
->modifiers
= 0;
387 ctype
->base_type
= typeof_sym
;
389 return expect(token
, ')', "after typeof");
392 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
394 if (attribute
== &packed_ident
||
395 attribute
== &__packed___ident
) {
396 ctype
->alignment
= 1;
399 if (attribute
== &aligned_ident
||
400 attribute
== &__aligned___ident
) {
401 int alignment
= max_alignment
;
403 alignment
= get_expression_value(expr
);
404 ctype
->alignment
= alignment
;
407 if (attribute
== &nocast_ident
) {
408 ctype
->modifiers
|= MOD_NOCAST
;
411 if (attribute
== &noderef_ident
) {
412 ctype
->modifiers
|= MOD_NODEREF
;
415 if (attribute
== &safe_ident
) {
416 ctype
->modifiers
|= MOD_SAFE
;
419 if (attribute
== &force_ident
) {
420 ctype
->modifiers
|= MOD_FORCE
;
423 if (attribute
== &bitwise_ident
||
424 attribute
== &__bitwise___ident
) {
426 ctype
->modifiers
|= MOD_BITWISE
;
429 if (attribute
== &address_space_ident
) {
431 return "expected address space number";
432 ctype
->as
= get_expression_value(expr
);
435 if (attribute
== &context_ident
) {
436 if (expr
&& expr
->type
== EXPR_COMMA
) {
437 struct context
*context
= alloc_context();
438 if(expr
->left
->type
== EXPR_COMMA
) {
439 context
->context
= expr
->left
->left
;
440 context
->in
= get_expression_value(
443 context
->context
= NULL
;
444 context
->in
= get_expression_value(expr
->left
);
446 context
->out
= get_expression_value(expr
->right
);
447 add_ptr_list(&ctype
->contexts
, context
);
450 return "expected context input/output values";
452 if (attribute
== &mode_ident
||
453 attribute
== &__mode___ident
) {
454 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
455 struct ident
*ident
= expr
->symbol_name
;
458 * Match against __QI__/__HI__/__SI__/__DI__
460 * FIXME! This is broken - we don't actually get
461 * the type information updated properly at this
462 * stage for some reason.
464 if (ident
== &__QI___ident
||
465 ident
== &QI_ident
) {
466 ctype
->modifiers
|= MOD_CHAR
;
469 if (ident
== &__HI___ident
||
470 ident
== &HI_ident
) {
471 ctype
->modifiers
|= MOD_SHORT
;
474 if (ident
== &__SI___ident
||
475 ident
== &SI_ident
) {
479 if (ident
== &__DI___ident
||
480 ident
== &DI_ident
) {
481 ctype
->modifiers
|= MOD_LONGLONG
;
484 if (ident
== &__word___ident
||
485 ident
== &word_ident
) {
486 ctype
->modifiers
|= MOD_LONG
;
489 return "unknown mode attribute";
491 return "expected attribute mode symbol";
494 /* Throw away for now.. */
495 if (attribute
== &__transparent_union___ident
) {
496 if (Wtransparent_union
)
497 return "ignoring attribute __transparent_union__";
500 if (attribute
== ¬hrow_ident
||
501 attribute
== &__nothrow_ident
||
502 attribute
== &__nothrow___ident
)
504 if (attribute
== &__malloc___ident
)
506 if (attribute
== &nonnull_ident
||
507 attribute
== &__nonnull_ident
||
508 attribute
== &__nonnull___ident
)
510 if (attribute
== &format_ident
||
511 attribute
== &__format___ident
||
512 attribute
== &__format_arg___ident
)
514 if (attribute
== §ion_ident
||
515 attribute
== &__section___ident
)
517 if (attribute
== &unused_ident
||
518 attribute
== &__unused___ident
)
520 if (attribute
== &const_ident
||
521 attribute
== &__const_ident
||
522 attribute
== &__const___ident
)
524 if (attribute
== &noreturn_ident
||
525 attribute
== &__noreturn___ident
)
527 if (attribute
== &no_instrument_function_ident
||
528 attribute
== &__no_instrument_function___ident
)
530 if (attribute
== &sentinel_ident
||
531 attribute
== &__sentinel___ident
)
533 if (attribute
== ®parm_ident
)
535 if (attribute
== &weak_ident
||
536 attribute
== &__weak___ident
)
538 if (attribute
== &alias_ident
)
540 if (attribute
== &pure_ident
||
541 attribute
== &__pure___ident
)
543 if (attribute
== &always_inline_ident
)
545 if (attribute
== &syscall_linkage_ident
)
547 if (attribute
== &visibility_ident
)
549 if (attribute
== &deprecated_ident
||
550 attribute
== &__deprecated___ident
)
552 if (attribute
== &noinline_ident
)
554 if (attribute
== &__used___ident
)
556 if (attribute
== &warn_unused_result_ident
||
557 attribute
== &__warn_unused_result___ident
)
559 if (attribute
== &model_ident
||
560 attribute
== &__model___ident
)
563 return "unknown attribute";
566 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
568 ctype
->modifiers
= 0;
569 token
= expect(token
, '(', "after attribute");
570 token
= expect(token
, '(', "after attribute");
573 const char *error_str
;
574 struct ident
*attribute_name
;
575 struct expression
*attribute_expr
;
577 if (eof_token(token
))
579 if (match_op(token
, ';'))
581 if (token_type(token
) != TOKEN_IDENT
)
583 attribute_name
= token
->ident
;
585 attribute_expr
= NULL
;
586 if (match_op(token
, '('))
587 token
= parens_expression(token
, &attribute_expr
, "in attribute");
588 error_str
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
590 sparse_error(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error_str
);
591 if (!match_op(token
, ','))
596 token
= expect(token
, ')', "after attribute");
597 token
= expect(token
, ')', "after attribute");
601 struct symbol
* ctype_integer(unsigned long spec
)
603 static struct symbol
*const integer_ctypes
[][3] = {
604 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
605 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
606 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
607 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
608 { &int_ctype
, &sint_ctype
, &uint_ctype
},
610 struct symbol
*const (*ctype
)[3];
613 ctype
= integer_ctypes
;
614 if (!(spec
& MOD_LONGLONG
)) {
616 if (!(spec
& MOD_LONG
)) {
618 if (!(spec
& MOD_SHORT
)) {
620 if (!(spec
& MOD_CHAR
))
626 sub
= ((spec
& MOD_UNSIGNED
)
628 : ((spec
& MOD_EXPLICITLY_SIGNED
)
632 return ctype
[0][sub
];
635 struct symbol
* ctype_fp(unsigned long spec
)
637 if (spec
& MOD_LONGLONG
)
638 return &ldouble_ctype
;
640 return &double_ctype
;
644 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
646 unsigned long mod
= thistype
->modifiers
;
649 unsigned long old
= ctype
->modifiers
;
650 unsigned long extra
= 0, dup
, conflict
;
652 if (mod
& old
& MOD_LONG
) {
653 extra
= MOD_LONGLONG
| MOD_LONG
;
657 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
659 sparse_error(pos
, "Just how %sdo you want this type to be?",
660 modifier_string(dup
));
662 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
664 sparse_error(pos
, "You cannot have both long and short modifiers.");
666 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
668 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
670 // Only one storage modifier allowed, except that "inline" doesn't count.
671 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
672 conflict
&= (conflict
- 1);
674 sparse_error(pos
, "multiple storage classes");
676 ctype
->modifiers
= old
| mod
| extra
;
680 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
681 (struct ptr_list
**)&ctype
->contexts
);
684 if (thistype
->alignment
& (thistype
->alignment
-1)) {
685 warning(pos
, "I don't like non-power-of-2 alignments");
686 thistype
->alignment
= 0;
688 if (thistype
->alignment
> ctype
->alignment
)
689 ctype
->alignment
= thistype
->alignment
;
693 ctype
->as
= thistype
->as
;
696 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
698 unsigned long banned
, wrong
;
699 unsigned long this_mod
= s
->ctype
.modifiers
;
700 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
701 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
703 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
704 banned
= BANNED_SIZE
| BANNED_SIGN
;
705 else if (this_mod
& MOD_SPECIALBITS
)
707 else if (s
->ctype
.base_type
== &fp_type
)
708 banned
= BANNED_SIGN
;
709 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
715 // vector_type <-- whatever that is
716 banned
= BANNED_SIZE
| BANNED_SIGN
;
719 wrong
= mod
& banned
;
721 sparse_error(*pos
, "modifier %sis invalid in this context",
722 modifier_string (wrong
));
726 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
730 while ( (token
= next
) != NULL
) {
731 struct ctype thistype
;
733 struct symbol
*s
, *type
;
737 if (token_type(token
) != TOKEN_IDENT
)
739 ident
= token
->ident
;
741 s
= lookup_symbol(ident
, NS_TYPEDEF
);
745 mod
= thistype
.modifiers
;
746 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
748 if (mod
& MOD_SPECIALBITS
) {
749 if (mod
& MOD_STRUCTOF
)
750 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
751 else if (mod
& MOD_UNIONOF
)
752 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
753 else if (mod
& MOD_ENUMOF
)
754 next
= enum_specifier(next
, &thistype
);
755 else if (mod
& MOD_ATTRIBUTE
)
756 next
= attribute_specifier(next
, &thistype
);
757 else if (mod
& MOD_TYPEOF
)
758 next
= typeof_specifier(next
, &thistype
);
759 mod
= thistype
.modifiers
;
761 type
= thistype
.base_type
;
765 if (ctype
->base_type
)
767 /* User types only mix with qualifiers */
768 if (mod
& MOD_USERTYPE
) {
769 if (ctype
->modifiers
& MOD_SPECIFIER
)
772 ctype
->base_type
= type
;
775 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
776 apply_ctype(token
->pos
, &thistype
, ctype
);
779 /* Turn the "virtual types" into real types with real sizes etc */
780 if (!ctype
->base_type
) {
781 struct symbol
*base
= &incomplete_ctype
;
784 * If we have modifiers, we'll default to an integer
785 * type, and "ctype_integer()" will turn this into
788 if (ctype
->modifiers
& MOD_SPECIFIER
)
790 ctype
->base_type
= base
;
792 if (ctype
->base_type
== &int_type
) {
793 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
794 ctype
->modifiers
&= ~MOD_SPECIFIER
;
795 } else if (ctype
->base_type
== &fp_type
) {
796 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
797 ctype
->modifiers
&= ~MOD_SPECIFIER
;
799 if (ctype
->modifiers
& MOD_BITWISE
) {
801 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
802 if (!is_int_type(ctype
->base_type
)) {
803 sparse_error(token
->pos
, "invalid modifier");
806 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
807 *type
= *ctype
->base_type
;
808 type
->ctype
.base_type
= ctype
->base_type
;
809 type
->type
= SYM_RESTRICT
;
810 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
811 ctype
->base_type
= type
;
817 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
819 struct expression
*expr
= NULL
;
821 token
= parse_expression(token
, &expr
);
822 sym
->array_size
= expr
;
826 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
827 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
829 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
832 if (token_type(token
) != TOKEN_IDENT
)
834 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
835 struct ctype thistype
= { 0, };
836 token
= attribute_specifier(token
->next
, &thistype
);
837 apply_ctype(token
->pos
, &thistype
, ctype
);
840 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
841 struct expression
*expr
;
842 token
= expect(token
->next
, '(', "after asm");
843 token
= parse_expression(token
->next
, &expr
);
844 token
= expect(token
, ')', "after asm");
852 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
854 struct ctype
*ctype
= &decl
->ctype
;
856 if (p
&& token_type(token
) == TOKEN_IDENT
) {
862 token
= handle_attributes(token
, ctype
);
864 if (token_type(token
) != TOKEN_SPECIAL
)
868 * This can be either a parameter list or a grouping.
869 * For the direct (non-abstract) case, we know if must be
870 * a parameter list if we already saw the identifier.
871 * For the abstract case, we know if must be a parameter
872 * list if it is empty or starts with a type.
874 if (token
->special
== '(') {
876 struct token
*next
= token
->next
;
877 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
880 struct symbol
*base_type
= ctype
->base_type
;
881 token
= declarator(next
, decl
, p
);
882 token
= expect(token
, ')', "in nested declarator");
883 while (ctype
->base_type
!= base_type
)
884 ctype
= &ctype
->base_type
->ctype
;
889 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
890 token
= parameter_type_list(next
, sym
, p
);
891 token
= expect(token
, ')', "in function declarator");
894 if (token
->special
== '[') {
895 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
896 token
= abstract_array_declarator(token
->next
, array
);
897 token
= expect(token
, ']', "in abstract_array_declarator");
898 ctype
= &array
->ctype
;
906 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
908 unsigned long modifiers
;
909 struct symbol
*base_type
;
911 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
912 base_type
= ctype
->base_type
;
913 ctype
->modifiers
= modifiers
;
915 while (match_op(token
,'*')) {
916 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
917 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
918 ptr
->ctype
.as
= ctype
->as
;
919 concat_ptr_list((struct ptr_list
*)ctype
->contexts
,
920 (struct ptr_list
**)&ptr
->ctype
.contexts
);
921 ptr
->ctype
.base_type
= base_type
;
924 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
925 ctype
->base_type
= base_type
;
927 free_ptr_list(&ctype
->contexts
);
929 token
= declaration_specifiers(token
->next
, ctype
, 1);
930 modifiers
= ctype
->modifiers
;
935 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
937 token
= pointer(token
, &sym
->ctype
);
938 return direct_declarator(token
, sym
, p
);
941 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
943 struct ctype
*ctype
= &decl
->ctype
;
944 struct expression
*expr
;
945 struct symbol
*bitfield
;
948 if (!is_int_type(ctype
->base_type
)) {
949 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
950 show_typename(ctype
->base_type
));
951 // Parse this to recover gracefully.
952 return conditional_expression(token
->next
, &expr
);
955 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
956 token
= conditional_expression(token
->next
, &expr
);
957 width
= get_expression_value(expr
);
958 bitfield
->bit_size
= width
;
960 if (width
< 0 || width
> INT_MAX
) {
961 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
963 } else if (decl
->ident
&& width
== 0) {
964 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
965 show_ident(decl
->ident
));
967 } else if (decl
->ident
) {
968 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
969 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
970 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
971 // Valid values are either {-1;0} or {0}, depending on integer
972 // representation. The latter makes for very efficient code...
973 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
975 if (Wdefault_bitfield_sign
&&
976 base_type
->type
!= SYM_ENUM
&&
977 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
979 // The sign of bitfields is unspecified by default.
980 sparse_error(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
983 bitfield
->bit_size
= width
;
987 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
989 struct ctype ctype
= {0, };
991 token
= declaration_specifiers(token
, &ctype
, 0);
993 struct ident
*ident
= NULL
;
994 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
996 token
= declarator(token
, decl
, &ident
);
998 if (match_op(token
, ':')) {
999 token
= handle_bitfield(token
, decl
);
1000 token
= handle_attributes(token
, &decl
->ctype
);
1002 add_symbol(list
, decl
);
1003 if (!match_op(token
, ','))
1005 token
= token
->next
;
1010 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1012 while (!match_op(token
, '}')) {
1013 token
= declaration_list(token
, list
);
1014 if (!match_op(token
, ';')) {
1015 sparse_error(token
->pos
, "expected ; at end of declaration");
1018 token
= token
->next
;
1023 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
1025 struct ident
*ident
= NULL
;
1027 struct ctype ctype
= { 0, };
1029 token
= declaration_specifiers(token
, &ctype
, 0);
1030 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1033 token
= declarator(token
, sym
, &ident
);
1038 struct token
*typename(struct token
*token
, struct symbol
**p
)
1040 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1042 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
1043 return declarator(token
, sym
, NULL
);
1046 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1048 token
= parse_expression(token
, tree
);
1049 return expect(token
, ';', "at end of statement");
1052 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1053 struct expression_list
**inout
)
1055 struct expression
*expr
;
1057 /* Allow empty operands */
1058 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1061 struct ident
*ident
= NULL
;
1062 if (match_op(token
->next
, '[') &&
1063 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1064 match_op(token
->next
->next
->next
, ']')) {
1065 ident
= token
->next
->next
->ident
;
1066 token
= token
->next
->next
->next
;
1068 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1069 token
= primary_expression(token
->next
, &expr
);
1070 add_expression(inout
, expr
);
1071 token
= parens_expression(token
, &expr
, "in asm parameter");
1072 add_expression(inout
, expr
);
1073 } while (match_op(token
, ','));
1077 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1078 struct expression_list
**clobbers
)
1080 struct expression
*expr
;
1083 token
= primary_expression(token
->next
, &expr
);
1084 add_expression(clobbers
, expr
);
1085 } while (match_op(token
, ','));
1089 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
1091 stmt
->type
= STMT_ASM
;
1092 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1093 token
= token
->next
;
1095 token
= expect(token
, '(', "after asm");
1096 token
= parse_expression(token
, &stmt
->asm_string
);
1097 if (match_op(token
, ':'))
1098 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1099 if (match_op(token
, ':'))
1100 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1101 if (match_op(token
, ':'))
1102 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1103 token
= expect(token
, ')', "after asm");
1104 return expect(token
, ';', "at end of asm-statement");
1107 /* Make a statement out of an expression */
1108 static struct statement
*make_statement(struct expression
*expr
)
1110 struct statement
*stmt
;
1114 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1115 stmt
->expression
= expr
;
1120 * All iterators have two symbols associated with them:
1121 * the "continue" and "break" symbols, which are targets
1122 * for continue and break statements respectively.
1124 * They are in a special name-space, but they follow
1125 * all the normal visibility rules, so nested iterators
1126 * automatically work right.
1128 static void start_iterator(struct statement
*stmt
)
1130 struct symbol
*cont
, *brk
;
1132 start_symbol_scope();
1133 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1134 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1135 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1136 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1138 stmt
->type
= STMT_ITERATOR
;
1139 stmt
->iterator_break
= brk
;
1140 stmt
->iterator_continue
= cont
;
1141 fn_local_symbol(brk
);
1142 fn_local_symbol(cont
);
1145 static void end_iterator(struct statement
*stmt
)
1150 static struct statement
*start_function(struct symbol
*sym
)
1153 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1155 start_function_scope();
1156 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1157 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1158 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1159 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1160 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1162 fn_local_symbol(ret
);
1164 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1170 static void end_function(struct symbol
*sym
)
1173 end_function_scope();
1177 * A "switch()" statement, like an iterator, has a
1178 * the "break" symbol associated with it. It works
1179 * exactly like the iterator break - it's the target
1180 * for any break-statements in scope, and means that
1181 * "break" handling doesn't even need to know whether
1182 * it's breaking out of an iterator or a switch.
1184 * In addition, the "case" symbol is a marker for the
1185 * case/default statements to find the switch statement
1186 * that they are associated with.
1188 static void start_switch(struct statement
*stmt
)
1190 struct symbol
*brk
, *switch_case
;
1192 start_symbol_scope();
1193 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1194 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1196 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1197 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1198 switch_case
->stmt
= stmt
;
1200 stmt
->type
= STMT_SWITCH
;
1201 stmt
->switch_break
= brk
;
1202 stmt
->switch_case
= switch_case
;
1204 fn_local_symbol(brk
);
1205 fn_local_symbol(switch_case
);
1208 static void end_switch(struct statement
*stmt
)
1210 if (!stmt
->switch_case
->symbol_list
)
1211 warning(stmt
->pos
, "switch with no cases");
1215 static void add_case_statement(struct statement
*stmt
)
1217 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1221 sparse_error(stmt
->pos
, "not in switch scope");
1222 stmt
->type
= STMT_NONE
;
1225 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1226 add_symbol(&target
->symbol_list
, sym
);
1228 stmt
->case_label
= sym
;
1229 fn_local_symbol(sym
);
1232 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1234 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1237 error_die(token
->pos
, "internal error: return without a function target");
1238 stmt
->type
= STMT_RETURN
;
1239 stmt
->ret_target
= target
;
1240 return expression_statement(token
->next
, &stmt
->ret_value
);
1243 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1245 struct symbol_list
*syms
;
1246 struct expression
*e1
, *e2
, *e3
;
1247 struct statement
*iterator
;
1249 start_iterator(stmt
);
1250 token
= expect(token
->next
, '(', "after 'for'");
1254 /* C99 variable declaration? */
1255 if (lookup_type(token
)) {
1256 token
= external_declaration(token
, &syms
);
1258 token
= parse_expression(token
, &e1
);
1259 token
= expect(token
, ';', "in 'for'");
1261 token
= parse_expression(token
, &e2
);
1262 token
= expect(token
, ';', "in 'for'");
1263 token
= parse_expression(token
, &e3
);
1264 token
= expect(token
, ')', "in 'for'");
1265 token
= statement(token
, &iterator
);
1267 stmt
->iterator_syms
= syms
;
1268 stmt
->iterator_pre_statement
= make_statement(e1
);
1269 stmt
->iterator_pre_condition
= e2
;
1270 stmt
->iterator_post_statement
= make_statement(e3
);
1271 stmt
->iterator_post_condition
= NULL
;
1272 stmt
->iterator_statement
= iterator
;
1278 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1280 struct expression
*expr
;
1281 struct statement
*iterator
;
1283 start_iterator(stmt
);
1284 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1285 token
= statement(token
, &iterator
);
1287 stmt
->iterator_pre_condition
= expr
;
1288 stmt
->iterator_post_condition
= NULL
;
1289 stmt
->iterator_statement
= iterator
;
1295 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1297 struct expression
*expr
;
1298 struct statement
*iterator
;
1300 start_iterator(stmt
);
1301 token
= statement(token
->next
, &iterator
);
1302 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1303 token
= token
->next
;
1305 sparse_error(token
->pos
, "expected 'while' after 'do'");
1306 token
= parens_expression(token
, &expr
, "after 'do-while'");
1308 stmt
->iterator_post_condition
= expr
;
1309 stmt
->iterator_statement
= iterator
;
1312 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
1313 warning(iterator
->pos
, "do-while statement is not a compound statement");
1315 return expect(token
, ';', "after statement");
1318 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1320 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1323 if (token_type(token
) == TOKEN_IDENT
) {
1324 if (token
->ident
== &if_ident
) {
1325 stmt
->type
= STMT_IF
;
1326 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1327 token
= statement(token
, &stmt
->if_true
);
1328 if (token_type(token
) != TOKEN_IDENT
)
1330 if (token
->ident
!= &else_ident
)
1332 return statement(token
->next
, &stmt
->if_false
);
1335 if (token
->ident
== &return_ident
)
1336 return parse_return_statement(token
, stmt
);
1338 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1339 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1340 stmt
->type
= STMT_GOTO
;
1341 stmt
->goto_label
= target
;
1343 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1344 return expect(token
->next
, ';', "at end of statement");
1346 if (token
->ident
== &default_ident
) {
1347 token
= token
->next
;
1348 goto default_statement
;
1350 if (token
->ident
== &case_ident
) {
1351 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1352 if (match_op(token
, SPECIAL_ELLIPSIS
))
1353 token
= parse_expression(token
->next
, &stmt
->case_to
);
1355 stmt
->type
= STMT_CASE
;
1356 token
= expect(token
, ':', "after default/case");
1357 add_case_statement(stmt
);
1358 return statement(token
, &stmt
->case_statement
);
1360 if (token
->ident
== &switch_ident
) {
1361 stmt
->type
= STMT_SWITCH
;
1363 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1364 token
= statement(token
, &stmt
->switch_statement
);
1368 if (token
->ident
== &for_ident
)
1369 return parse_for_statement(token
, stmt
);
1371 if (token
->ident
== &while_ident
)
1372 return parse_while_statement(token
, stmt
);
1374 if (token
->ident
== &do_ident
)
1375 return parse_do_statement(token
, stmt
);
1377 if (token
->ident
== &goto_ident
) {
1378 stmt
->type
= STMT_GOTO
;
1379 token
= token
->next
;
1380 if (match_op(token
, '*')) {
1381 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1382 add_statement(&function_computed_goto_list
, stmt
);
1383 } else if (token_type(token
) == TOKEN_IDENT
) {
1384 stmt
->goto_label
= label_symbol(token
);
1385 token
= token
->next
;
1387 sparse_error(token
->pos
, "Expected identifier or goto expression");
1389 return expect(token
, ';', "at end of statement");
1391 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1392 return parse_asm(token
->next
, stmt
);
1394 if (token
->ident
== &__context___ident
) {
1395 stmt
->type
= STMT_CONTEXT
;
1396 token
= parse_expression(token
->next
, &stmt
->expression
);
1397 if(stmt
->expression
->type
== EXPR_PREOP
1398 && stmt
->expression
->op
== '('
1399 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
1400 struct expression
*expr
;
1401 expr
= stmt
->expression
->unop
;
1402 stmt
->context
= expr
->left
;
1403 stmt
->expression
= expr
->right
;
1405 return expect(token
, ';', "at end of statement");
1407 if (token
->ident
== &__range___ident
) {
1408 stmt
->type
= STMT_RANGE
;
1409 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1410 token
= expect(token
, ',', "after range expression");
1411 token
= assignment_expression(token
, &stmt
->range_low
);
1412 token
= expect(token
, ',', "after low range");
1413 token
= assignment_expression(token
, &stmt
->range_high
);
1414 return expect(token
, ';', "after range statement");
1416 if (match_op(token
->next
, ':')) {
1417 stmt
->type
= STMT_LABEL
;
1418 stmt
->label_identifier
= label_symbol(token
);
1419 return statement(token
->next
->next
, &stmt
->label_statement
);
1423 if (match_op(token
, '{')) {
1424 stmt
->type
= STMT_COMPOUND
;
1425 start_symbol_scope();
1426 token
= compound_statement(token
->next
, stmt
);
1429 return expect(token
, '}', "at end of compound statement");
1432 stmt
->type
= STMT_EXPRESSION
;
1433 return expression_statement(token
, &stmt
->expression
);
1436 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1438 int seen_statement
= 0;
1440 struct statement
* stmt
;
1441 if (eof_token(token
))
1443 if (match_op(token
, '}'))
1445 if (lookup_type(token
)) {
1446 if (seen_statement
) {
1447 warning(token
->pos
, "mixing declarations and code");
1450 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1451 token
= external_declaration(token
, &stmt
->declaration
);
1453 seen_statement
= warn_on_mixed
;
1454 token
= statement(token
, &stmt
);
1456 add_statement(list
, stmt
);
1461 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1463 struct symbol_list
**list
= &fn
->arguments
;
1465 if (match_op(token
, ')')) {
1466 // No warning for "void oink ();"
1467 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1468 if (p
&& !match_op(token
->next
, ';'))
1469 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1476 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1478 warning(token
->pos
, "variadic functions must have one named argument");
1480 token
= token
->next
;
1484 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1485 token
= parameter_declaration(token
, &sym
);
1486 if (sym
->ctype
.base_type
== &void_ctype
) {
1487 /* Special case: (void) */
1488 if (!*list
&& !sym
->ident
)
1490 warning(token
->pos
, "void parameter");
1492 add_symbol(list
, sym
);
1493 if (!match_op(token
, ','))
1495 token
= token
->next
;
1501 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1503 token
= statement_list(token
, &stmt
->stmts
);
1507 static struct expression
*identifier_expression(struct token
*token
)
1509 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1510 expr
->expr_ident
= token
->ident
;
1514 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1516 int idx_from
, idx_to
;
1517 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1519 idx_from
= get_expression_value(from
);
1522 idx_to
= get_expression_value(to
);
1523 if (idx_to
< idx_from
|| idx_from
< 0)
1524 warning(from
->pos
, "nonsense array initializer index range");
1526 expr
->idx_from
= idx_from
;
1527 expr
->idx_to
= idx_to
;
1531 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1533 int expect_equal
= 0;
1534 struct token
*next
= token
->next
;
1535 struct expression
**tail
= ep
;
1540 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1541 struct expression
*expr
= identifier_expression(token
);
1542 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1543 token
= initializer(&expr
->ident_expression
, next
->next
);
1544 if (expr
->ident_expression
)
1549 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1550 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1551 struct expression
*expr
= identifier_expression(next
);
1553 tail
= &expr
->ident_expression
;
1556 } else if (match_op(token
, '[')) {
1557 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1558 token
= constant_expression(token
->next
, &from
);
1560 sparse_error(token
->pos
, "Expected constant expression");
1563 if (match_op(token
, SPECIAL_ELLIPSIS
))
1564 token
= constant_expression(token
->next
, &to
);
1565 expr
= index_expression(from
, to
);
1567 tail
= &expr
->idx_expression
;
1568 token
= expect(token
, ']', "at end of initializer index");
1575 if (nested
&& !expect_equal
) {
1576 if (!match_op(token
, '='))
1577 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1582 token
= expect(token
, '=', "at end of initializer index");
1584 token
= initializer(tail
, token
);
1590 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1592 struct expression
*expr
;
1595 token
= single_initializer(&expr
, token
);
1598 add_expression(list
, expr
);
1599 if (!match_op(token
, ','))
1601 token
= token
->next
;
1606 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1608 if (match_op(token
, '{')) {
1609 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1611 token
= initializer_list(&expr
->expr_list
, token
->next
);
1612 return expect(token
, '}', "at end of initializer");
1614 return assignment_expression(token
, tree
);
1617 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1620 sparse_error(sym
->pos
, "no identifier for function argument");
1623 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1626 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1627 struct symbol_list
**list
)
1629 struct symbol_list
**old_symbol_list
;
1630 struct symbol
*base_type
= decl
->ctype
.base_type
;
1631 struct statement
*stmt
, **p
;
1634 old_symbol_list
= function_symbol_list
;
1635 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1636 function_symbol_list
= &decl
->inline_symbol_list
;
1637 p
= &base_type
->inline_stmt
;
1639 function_symbol_list
= &decl
->symbol_list
;
1640 p
= &base_type
->stmt
;
1642 function_computed_target_list
= NULL
;
1643 function_computed_goto_list
= NULL
;
1645 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1646 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1647 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1649 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1650 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1652 stmt
= start_function(decl
);
1655 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1656 declare_argument(arg
, base_type
);
1657 } END_FOR_EACH_PTR(arg
);
1659 token
= compound_statement(token
->next
, stmt
);
1662 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1663 add_symbol(list
, decl
);
1664 check_declaration(decl
);
1665 function_symbol_list
= old_symbol_list
;
1666 if (function_computed_goto_list
) {
1667 if (!function_computed_target_list
)
1668 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1670 struct statement
*stmt
;
1671 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1672 stmt
->target_list
= function_computed_target_list
;
1673 } END_FOR_EACH_PTR(stmt
);
1676 return expect(token
, '}', "at end of function");
1679 static void promote_k_r_types(struct symbol
*arg
)
1681 struct symbol
*base
= arg
->ctype
.base_type
;
1682 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1683 arg
->ctype
.base_type
= &int_ctype
;
1687 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1689 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1692 FOR_EACH_PTR(real_args
, arg
) {
1693 struct symbol
*type
;
1695 /* This is quadratic in the number of arguments. We _really_ don't care */
1696 FOR_EACH_PTR(argtypes
, type
) {
1697 if (type
->ident
== arg
->ident
)
1699 } END_FOR_EACH_PTR(type
);
1700 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1704 /* "char" and "short" promote to "int" */
1705 promote_k_r_types(type
);
1707 arg
->ctype
= type
->ctype
;
1708 } END_FOR_EACH_PTR(arg
);
1710 FOR_EACH_PTR(argtypes
, arg
) {
1712 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1713 } END_FOR_EACH_PTR(arg
);
1717 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1718 struct symbol_list
**list
)
1720 struct symbol_list
*args
= NULL
;
1722 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
1724 token
= declaration_list(token
, &args
);
1725 if (!match_op(token
, ';')) {
1726 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
1729 token
= token
->next
;
1730 } while (lookup_type(token
));
1732 apply_k_r_types(args
, decl
);
1734 if (!match_op(token
, '{')) {
1735 sparse_error(token
->pos
, "expected function body");
1738 return parse_function_body(token
, decl
, list
);
1742 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1744 struct ident
*ident
= NULL
;
1745 struct symbol
*decl
;
1746 struct ctype ctype
= { 0, };
1747 struct symbol
*base_type
;
1750 /* Top-level inline asm? */
1751 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1752 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1753 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1754 struct statement
*stmt
;
1756 anon
->ctype
.base_type
= fn
;
1757 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1760 token
= parse_asm(token
->next
, stmt
);
1762 add_symbol(list
, anon
);
1766 /* Parse declaration-specifiers, if any */
1767 token
= declaration_specifiers(token
, &ctype
, 0);
1768 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1769 decl
->ctype
= ctype
;
1770 token
= declarator(token
, decl
, &ident
);
1772 /* Just a type declaration? */
1774 return expect(token
, ';', "end of type declaration");
1776 /* type define declaration? */
1777 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1779 /* Typedef's don't have meaningful storage */
1781 ctype
.modifiers
&= ~MOD_STORAGE
;
1782 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1783 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1786 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1788 base_type
= decl
->ctype
.base_type
;
1791 if (base_type
&& !base_type
->ident
)
1792 base_type
->ident
= ident
;
1793 } else if (base_type
&& base_type
->type
== SYM_FN
) {
1794 /* K&R argument declaration? */
1795 if (lookup_type(token
))
1796 return parse_k_r_arguments(token
, decl
, list
);
1797 if (match_op(token
, '{'))
1798 return parse_function_body(token
, decl
, list
);
1800 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1801 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1802 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1803 sparse_error(token
->pos
, "void declaration");
1807 if (!is_typedef
&& match_op(token
, '=')) {
1808 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1809 warning(decl
->pos
, "symbol with external linkage has initializer");
1810 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1812 token
= initializer(&decl
->initializer
, token
->next
);
1815 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1816 add_symbol(list
, decl
);
1817 fn_local_symbol(decl
);
1820 check_declaration(decl
);
1822 if (!match_op(token
, ','))
1825 token
= token
->next
;
1827 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1828 decl
->ctype
= ctype
;
1829 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1830 token
= declarator(token
, decl
, &ident
);
1832 sparse_error(token
->pos
, "expected identifier name in type definition");
1836 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1838 /* Function declarations are automatically extern unless specifically static */
1839 base_type
= decl
->ctype
.base_type
;
1840 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1841 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1842 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1845 return expect(token
, ';', "at end of declaration");