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 int input
= get_expression_value(expr
->left
);
438 int output
= get_expression_value(expr
->right
);
439 ctype
->in_context
= input
;
440 ctype
->out_context
= output
;
443 return "expected context input/output values";
445 if (attribute
== &mode_ident
||
446 attribute
== &__mode___ident
) {
447 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
448 struct ident
*ident
= expr
->symbol_name
;
451 * Match against __QI__/__HI__/__SI__/__DI__
453 * FIXME! This is broken - we don't actually get
454 * the type information updated properly at this
455 * stage for some reason.
457 if (ident
== &__QI___ident
||
458 ident
== &QI_ident
) {
459 ctype
->modifiers
|= MOD_CHAR
;
462 if (ident
== &__HI___ident
||
463 ident
== &HI_ident
) {
464 ctype
->modifiers
|= MOD_SHORT
;
467 if (ident
== &__SI___ident
||
468 ident
== &SI_ident
) {
472 if (ident
== &__DI___ident
||
473 ident
== &DI_ident
) {
474 ctype
->modifiers
|= MOD_LONGLONG
;
477 if (ident
== &__word___ident
||
478 ident
== &word_ident
) {
479 ctype
->modifiers
|= MOD_LONG
;
482 return "unknown mode attribute";
484 return "expected attribute mode symbol";
487 /* Throw away for now.. */
488 if (attribute
== &__transparent_union___ident
) {
489 if (Wtransparent_union
)
490 return "ignoring attribute __transparent_union__";
493 if (attribute
== ¬hrow_ident
||
494 attribute
== &__nothrow_ident
||
495 attribute
== &__nothrow___ident
)
497 if (attribute
== &__malloc___ident
)
499 if (attribute
== &nonnull_ident
||
500 attribute
== &__nonnull_ident
||
501 attribute
== &__nonnull___ident
)
503 if (attribute
== &format_ident
||
504 attribute
== &__format___ident
||
505 attribute
== &__format_arg___ident
)
507 if (attribute
== §ion_ident
||
508 attribute
== &__section___ident
)
510 if (attribute
== &unused_ident
||
511 attribute
== &__unused___ident
)
513 if (attribute
== &const_ident
||
514 attribute
== &__const_ident
||
515 attribute
== &__const___ident
)
517 if (attribute
== &noreturn_ident
||
518 attribute
== &__noreturn___ident
)
520 if (attribute
== &no_instrument_function_ident
||
521 attribute
== &__no_instrument_function___ident
)
523 if (attribute
== &sentinel_ident
||
524 attribute
== &__sentinel___ident
)
526 if (attribute
== ®parm_ident
)
528 if (attribute
== &weak_ident
||
529 attribute
== &__weak___ident
)
531 if (attribute
== &alias_ident
)
533 if (attribute
== &pure_ident
||
534 attribute
== &__pure___ident
)
536 if (attribute
== &always_inline_ident
)
538 if (attribute
== &syscall_linkage_ident
)
540 if (attribute
== &visibility_ident
)
542 if (attribute
== &deprecated_ident
||
543 attribute
== &__deprecated___ident
)
545 if (attribute
== &noinline_ident
)
547 if (attribute
== &__used___ident
)
549 if (attribute
== &warn_unused_result_ident
||
550 attribute
== &__warn_unused_result___ident
)
552 if (attribute
== &model_ident
||
553 attribute
== &__model___ident
)
556 return "unknown attribute";
559 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
561 ctype
->modifiers
= 0;
562 token
= expect(token
, '(', "after attribute");
563 token
= expect(token
, '(', "after attribute");
566 const char *error_str
;
567 struct ident
*attribute_name
;
568 struct expression
*attribute_expr
;
570 if (eof_token(token
))
572 if (match_op(token
, ';'))
574 if (token_type(token
) != TOKEN_IDENT
)
576 attribute_name
= token
->ident
;
578 attribute_expr
= NULL
;
579 if (match_op(token
, '('))
580 token
= parens_expression(token
, &attribute_expr
, "in attribute");
581 error_str
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
583 sparse_error(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error_str
);
584 if (!match_op(token
, ','))
589 token
= expect(token
, ')', "after attribute");
590 token
= expect(token
, ')', "after attribute");
594 struct symbol
* ctype_integer(unsigned long spec
)
596 static struct symbol
*const integer_ctypes
[][3] = {
597 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
598 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
599 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
600 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
601 { &int_ctype
, &sint_ctype
, &uint_ctype
},
603 struct symbol
*const (*ctype
)[3];
606 ctype
= integer_ctypes
;
607 if (!(spec
& MOD_LONGLONG
)) {
609 if (!(spec
& MOD_LONG
)) {
611 if (!(spec
& MOD_SHORT
)) {
613 if (!(spec
& MOD_CHAR
))
619 sub
= ((spec
& MOD_UNSIGNED
)
621 : ((spec
& MOD_EXPLICITLY_SIGNED
)
625 return ctype
[0][sub
];
628 struct symbol
* ctype_fp(unsigned long spec
)
630 if (spec
& MOD_LONGLONG
)
631 return &ldouble_ctype
;
633 return &double_ctype
;
637 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
639 unsigned long mod
= thistype
->modifiers
;
642 unsigned long old
= ctype
->modifiers
;
643 unsigned long extra
= 0, dup
, conflict
;
645 if (mod
& old
& MOD_LONG
) {
646 extra
= MOD_LONGLONG
| MOD_LONG
;
650 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
652 sparse_error(pos
, "Just how %sdo you want this type to be?",
653 modifier_string(dup
));
655 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
657 sparse_error(pos
, "You cannot have both long and short modifiers.");
659 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
661 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
663 // Only one storage modifier allowed, except that "inline" doesn't count.
664 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
665 conflict
&= (conflict
- 1);
667 sparse_error(pos
, "multiple storage classes");
669 ctype
->modifiers
= old
| mod
| extra
;
672 /* Context mask and value */
673 ctype
->in_context
+= thistype
->in_context
;
674 ctype
->out_context
+= thistype
->out_context
;
677 if (thistype
->alignment
& (thistype
->alignment
-1)) {
678 warning(pos
, "I don't like non-power-of-2 alignments");
679 thistype
->alignment
= 0;
681 if (thistype
->alignment
> ctype
->alignment
)
682 ctype
->alignment
= thistype
->alignment
;
686 ctype
->as
= thistype
->as
;
689 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
691 unsigned long banned
, wrong
;
692 unsigned long this_mod
= s
->ctype
.modifiers
;
693 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
694 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
696 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
697 banned
= BANNED_SIZE
| BANNED_SIGN
;
698 else if (this_mod
& MOD_SPECIALBITS
)
700 else if (s
->ctype
.base_type
== &fp_type
)
701 banned
= BANNED_SIGN
;
702 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
708 // vector_type <-- whatever that is
709 banned
= BANNED_SIZE
| BANNED_SIGN
;
712 wrong
= mod
& banned
;
714 sparse_error(*pos
, "modifier %sis invalid in this context",
715 modifier_string (wrong
));
719 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
723 while ( (token
= next
) != NULL
) {
724 struct ctype thistype
;
726 struct symbol
*s
, *type
;
730 if (token_type(token
) != TOKEN_IDENT
)
732 ident
= token
->ident
;
734 s
= lookup_symbol(ident
, NS_TYPEDEF
);
738 mod
= thistype
.modifiers
;
739 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
741 if (mod
& MOD_SPECIALBITS
) {
742 if (mod
& MOD_STRUCTOF
)
743 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
744 else if (mod
& MOD_UNIONOF
)
745 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
746 else if (mod
& MOD_ENUMOF
)
747 next
= enum_specifier(next
, &thistype
);
748 else if (mod
& MOD_ATTRIBUTE
)
749 next
= attribute_specifier(next
, &thistype
);
750 else if (mod
& MOD_TYPEOF
)
751 next
= typeof_specifier(next
, &thistype
);
752 mod
= thistype
.modifiers
;
754 type
= thistype
.base_type
;
758 if (ctype
->base_type
)
760 /* User types only mix with qualifiers */
761 if (mod
& MOD_USERTYPE
) {
762 if (ctype
->modifiers
& MOD_SPECIFIER
)
765 ctype
->base_type
= type
;
768 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
769 apply_ctype(token
->pos
, &thistype
, ctype
);
772 /* Turn the "virtual types" into real types with real sizes etc */
773 if (!ctype
->base_type
) {
774 struct symbol
*base
= &incomplete_ctype
;
777 * If we have modifiers, we'll default to an integer
778 * type, and "ctype_integer()" will turn this into
781 if (ctype
->modifiers
& MOD_SPECIFIER
)
783 ctype
->base_type
= base
;
785 if (ctype
->base_type
== &int_type
) {
786 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
787 ctype
->modifiers
&= ~MOD_SPECIFIER
;
788 } else if (ctype
->base_type
== &fp_type
) {
789 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
790 ctype
->modifiers
&= ~MOD_SPECIFIER
;
792 if (ctype
->modifiers
& MOD_BITWISE
) {
794 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
795 if (!is_int_type(ctype
->base_type
)) {
796 sparse_error(token
->pos
, "invalid modifier");
799 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
800 *type
= *ctype
->base_type
;
801 type
->ctype
.base_type
= ctype
->base_type
;
802 type
->type
= SYM_RESTRICT
;
803 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
804 ctype
->base_type
= type
;
809 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
811 struct expression
*expr
= NULL
;
813 token
= parse_expression(token
, &expr
);
814 sym
->array_size
= expr
;
818 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
819 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
821 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
824 if (token_type(token
) != TOKEN_IDENT
)
826 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
827 struct ctype thistype
= { 0, };
828 token
= attribute_specifier(token
->next
, &thistype
);
829 apply_ctype(token
->pos
, &thistype
, ctype
);
832 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
833 struct expression
*expr
;
834 token
= expect(token
->next
, '(', "after asm");
835 token
= parse_expression(token
->next
, &expr
);
836 token
= expect(token
, ')', "after asm");
844 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
846 struct ctype
*ctype
= &decl
->ctype
;
848 if (p
&& token_type(token
) == TOKEN_IDENT
) {
854 token
= handle_attributes(token
, ctype
);
856 if (token_type(token
) != TOKEN_SPECIAL
)
860 * This can be either a parameter list or a grouping.
861 * For the direct (non-abstract) case, we know if must be
862 * a parameter list if we already saw the identifier.
863 * For the abstract case, we know if must be a parameter
864 * list if it is empty or starts with a type.
866 if (token
->special
== '(') {
868 struct token
*next
= token
->next
;
869 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
872 struct symbol
*base_type
= ctype
->base_type
;
873 token
= declarator(next
, decl
, p
);
874 token
= expect(token
, ')', "in nested declarator");
875 while (ctype
->base_type
!= base_type
)
876 ctype
= &ctype
->base_type
->ctype
;
881 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
882 token
= parameter_type_list(next
, sym
, p
);
883 token
= expect(token
, ')', "in function declarator");
886 if (token
->special
== '[') {
887 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
888 token
= abstract_array_declarator(token
->next
, array
);
889 token
= expect(token
, ']', "in abstract_array_declarator");
890 ctype
= &array
->ctype
;
898 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
900 unsigned long modifiers
;
901 struct symbol
*base_type
;
903 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
904 base_type
= ctype
->base_type
;
905 ctype
->modifiers
= modifiers
;
907 while (match_op(token
,'*')) {
908 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
909 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
910 ptr
->ctype
.as
= ctype
->as
;
911 ptr
->ctype
.in_context
+= ctype
->in_context
;
912 ptr
->ctype
.out_context
+= ctype
->out_context
;
913 ptr
->ctype
.base_type
= base_type
;
916 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
917 ctype
->base_type
= base_type
;
919 ctype
->in_context
= 0;
920 ctype
->out_context
= 0;
922 token
= declaration_specifiers(token
->next
, ctype
, 1);
923 modifiers
= ctype
->modifiers
;
928 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
930 token
= pointer(token
, &sym
->ctype
);
931 return direct_declarator(token
, sym
, p
);
934 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
936 struct ctype
*ctype
= &decl
->ctype
;
937 struct expression
*expr
;
938 struct symbol
*bitfield
;
941 if (!is_int_type(ctype
->base_type
)) {
942 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
943 show_typename(ctype
->base_type
));
944 // Parse this to recover gracefully.
945 return conditional_expression(token
->next
, &expr
);
948 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
949 token
= conditional_expression(token
->next
, &expr
);
950 width
= get_expression_value(expr
);
951 bitfield
->bit_size
= width
;
953 if (width
< 0 || width
> INT_MAX
) {
954 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
956 } else if (decl
->ident
&& width
== 0) {
957 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
958 show_ident(decl
->ident
));
960 } else if (decl
->ident
) {
961 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
962 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
963 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
964 // Valid values are either {-1;0} or {0}, depending on integer
965 // representation. The latter makes for very efficient code...
966 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
968 if (Wdefault_bitfield_sign
&&
969 base_type
->type
!= SYM_ENUM
&&
970 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
972 // The sign of bitfields is unspecified by default.
973 sparse_error(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
976 bitfield
->bit_size
= width
;
980 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
982 struct ctype ctype
= {0, };
984 token
= declaration_specifiers(token
, &ctype
, 0);
986 struct ident
*ident
= NULL
;
987 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
989 token
= declarator(token
, decl
, &ident
);
991 if (match_op(token
, ':')) {
992 token
= handle_bitfield(token
, decl
);
993 token
= handle_attributes(token
, &decl
->ctype
);
995 add_symbol(list
, decl
);
996 if (!match_op(token
, ','))
1003 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1005 while (!match_op(token
, '}')) {
1006 token
= declaration_list(token
, list
);
1007 if (!match_op(token
, ';')) {
1008 sparse_error(token
->pos
, "expected ; at end of declaration");
1011 token
= token
->next
;
1016 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
1018 struct ident
*ident
= NULL
;
1020 struct ctype ctype
= { 0, };
1022 token
= declaration_specifiers(token
, &ctype
, 0);
1023 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1026 token
= declarator(token
, sym
, &ident
);
1031 struct token
*typename(struct token
*token
, struct symbol
**p
)
1033 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1035 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
1036 return declarator(token
, sym
, NULL
);
1039 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1041 token
= parse_expression(token
, tree
);
1042 return expect(token
, ';', "at end of statement");
1045 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1046 struct expression_list
**inout
)
1048 struct expression
*expr
;
1050 /* Allow empty operands */
1051 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1054 struct ident
*ident
= NULL
;
1055 if (match_op(token
->next
, '[') &&
1056 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1057 match_op(token
->next
->next
->next
, ']')) {
1058 ident
= token
->next
->next
->ident
;
1059 token
= token
->next
->next
->next
;
1061 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1062 token
= primary_expression(token
->next
, &expr
);
1063 add_expression(inout
, expr
);
1064 token
= parens_expression(token
, &expr
, "in asm parameter");
1065 add_expression(inout
, expr
);
1066 } while (match_op(token
, ','));
1070 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1071 struct expression_list
**clobbers
)
1073 struct expression
*expr
;
1076 token
= primary_expression(token
->next
, &expr
);
1077 add_expression(clobbers
, expr
);
1078 } while (match_op(token
, ','));
1082 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
1084 stmt
->type
= STMT_ASM
;
1085 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1086 token
= token
->next
;
1088 token
= expect(token
, '(', "after asm");
1089 token
= parse_expression(token
, &stmt
->asm_string
);
1090 if (match_op(token
, ':'))
1091 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1092 if (match_op(token
, ':'))
1093 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1094 if (match_op(token
, ':'))
1095 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1096 token
= expect(token
, ')', "after asm");
1097 return expect(token
, ';', "at end of asm-statement");
1100 /* Make a statement out of an expression */
1101 static struct statement
*make_statement(struct expression
*expr
)
1103 struct statement
*stmt
;
1107 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1108 stmt
->expression
= expr
;
1113 * All iterators have two symbols associated with them:
1114 * the "continue" and "break" symbols, which are targets
1115 * for continue and break statements respectively.
1117 * They are in a special name-space, but they follow
1118 * all the normal visibility rules, so nested iterators
1119 * automatically work right.
1121 static void start_iterator(struct statement
*stmt
)
1123 struct symbol
*cont
, *brk
;
1125 start_symbol_scope();
1126 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1127 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1128 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1129 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1131 stmt
->type
= STMT_ITERATOR
;
1132 stmt
->iterator_break
= brk
;
1133 stmt
->iterator_continue
= cont
;
1134 fn_local_symbol(brk
);
1135 fn_local_symbol(cont
);
1138 static void end_iterator(struct statement
*stmt
)
1143 static struct statement
*start_function(struct symbol
*sym
)
1146 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1148 start_function_scope();
1149 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1150 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1151 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1152 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1153 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1155 fn_local_symbol(ret
);
1157 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1163 static void end_function(struct symbol
*sym
)
1166 end_function_scope();
1170 * A "switch()" statement, like an iterator, has a
1171 * the "break" symbol associated with it. It works
1172 * exactly like the iterator break - it's the target
1173 * for any break-statements in scope, and means that
1174 * "break" handling doesn't even need to know whether
1175 * it's breaking out of an iterator or a switch.
1177 * In addition, the "case" symbol is a marker for the
1178 * case/default statements to find the switch statement
1179 * that they are associated with.
1181 static void start_switch(struct statement
*stmt
)
1183 struct symbol
*brk
, *switch_case
;
1185 start_symbol_scope();
1186 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1187 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1189 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1190 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1191 switch_case
->stmt
= stmt
;
1193 stmt
->type
= STMT_SWITCH
;
1194 stmt
->switch_break
= brk
;
1195 stmt
->switch_case
= switch_case
;
1197 fn_local_symbol(brk
);
1198 fn_local_symbol(switch_case
);
1201 static void end_switch(struct statement
*stmt
)
1203 if (!stmt
->switch_case
->symbol_list
)
1204 warning(stmt
->pos
, "switch with no cases");
1208 static void add_case_statement(struct statement
*stmt
)
1210 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1214 sparse_error(stmt
->pos
, "not in switch scope");
1215 stmt
->type
= STMT_NONE
;
1218 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1219 add_symbol(&target
->symbol_list
, sym
);
1221 stmt
->case_label
= sym
;
1222 fn_local_symbol(sym
);
1225 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1227 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1230 error_die(token
->pos
, "internal error: return without a function target");
1231 stmt
->type
= STMT_RETURN
;
1232 stmt
->ret_target
= target
;
1233 return expression_statement(token
->next
, &stmt
->ret_value
);
1236 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1238 struct symbol_list
*syms
;
1239 struct expression
*e1
, *e2
, *e3
;
1240 struct statement
*iterator
;
1242 start_iterator(stmt
);
1243 token
= expect(token
->next
, '(', "after 'for'");
1247 /* C99 variable declaration? */
1248 if (lookup_type(token
)) {
1249 token
= external_declaration(token
, &syms
);
1251 token
= parse_expression(token
, &e1
);
1252 token
= expect(token
, ';', "in 'for'");
1254 token
= parse_expression(token
, &e2
);
1255 token
= expect(token
, ';', "in 'for'");
1256 token
= parse_expression(token
, &e3
);
1257 token
= expect(token
, ')', "in 'for'");
1258 token
= statement(token
, &iterator
);
1260 stmt
->iterator_syms
= syms
;
1261 stmt
->iterator_pre_statement
= make_statement(e1
);
1262 stmt
->iterator_pre_condition
= e2
;
1263 stmt
->iterator_post_statement
= make_statement(e3
);
1264 stmt
->iterator_post_condition
= NULL
;
1265 stmt
->iterator_statement
= iterator
;
1271 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1273 struct expression
*expr
;
1274 struct statement
*iterator
;
1276 start_iterator(stmt
);
1277 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1278 token
= statement(token
, &iterator
);
1280 stmt
->iterator_pre_condition
= expr
;
1281 stmt
->iterator_post_condition
= NULL
;
1282 stmt
->iterator_statement
= iterator
;
1288 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1290 struct expression
*expr
;
1291 struct statement
*iterator
;
1293 start_iterator(stmt
);
1294 token
= statement(token
->next
, &iterator
);
1295 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1296 token
= token
->next
;
1298 sparse_error(token
->pos
, "expected 'while' after 'do'");
1299 token
= parens_expression(token
, &expr
, "after 'do-while'");
1301 stmt
->iterator_post_condition
= expr
;
1302 stmt
->iterator_statement
= iterator
;
1305 return expect(token
, ';', "after statement");
1308 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1310 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1313 if (token_type(token
) == TOKEN_IDENT
) {
1314 if (token
->ident
== &if_ident
) {
1315 stmt
->type
= STMT_IF
;
1316 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1317 token
= statement(token
, &stmt
->if_true
);
1318 if (token_type(token
) != TOKEN_IDENT
)
1320 if (token
->ident
!= &else_ident
)
1322 return statement(token
->next
, &stmt
->if_false
);
1325 if (token
->ident
== &return_ident
)
1326 return parse_return_statement(token
, stmt
);
1328 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1329 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1330 stmt
->type
= STMT_GOTO
;
1331 stmt
->goto_label
= target
;
1333 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1334 return expect(token
->next
, ';', "at end of statement");
1336 if (token
->ident
== &default_ident
) {
1337 token
= token
->next
;
1338 goto default_statement
;
1340 if (token
->ident
== &case_ident
) {
1341 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1342 if (match_op(token
, SPECIAL_ELLIPSIS
))
1343 token
= parse_expression(token
->next
, &stmt
->case_to
);
1345 stmt
->type
= STMT_CASE
;
1346 token
= expect(token
, ':', "after default/case");
1347 add_case_statement(stmt
);
1348 return statement(token
, &stmt
->case_statement
);
1350 if (token
->ident
== &switch_ident
) {
1351 stmt
->type
= STMT_SWITCH
;
1353 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1354 token
= statement(token
, &stmt
->switch_statement
);
1358 if (token
->ident
== &for_ident
)
1359 return parse_for_statement(token
, stmt
);
1361 if (token
->ident
== &while_ident
)
1362 return parse_while_statement(token
, stmt
);
1364 if (token
->ident
== &do_ident
)
1365 return parse_do_statement(token
, stmt
);
1367 if (token
->ident
== &goto_ident
) {
1368 stmt
->type
= STMT_GOTO
;
1369 token
= token
->next
;
1370 if (match_op(token
, '*')) {
1371 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1372 add_statement(&function_computed_goto_list
, stmt
);
1373 } else if (token_type(token
) == TOKEN_IDENT
) {
1374 stmt
->goto_label
= label_symbol(token
);
1375 token
= token
->next
;
1377 sparse_error(token
->pos
, "Expected identifier or goto expression");
1379 return expect(token
, ';', "at end of statement");
1381 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1382 return parse_asm(token
->next
, stmt
);
1384 if (token
->ident
== &__context___ident
) {
1385 stmt
->type
= STMT_CONTEXT
;
1386 token
= parse_expression(token
->next
, &stmt
->expression
);
1387 return expect(token
, ';', "at end of statement");
1389 if (token
->ident
== &__range___ident
) {
1390 stmt
->type
= STMT_RANGE
;
1391 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1392 token
= expect(token
, ',', "after range expression");
1393 token
= assignment_expression(token
, &stmt
->range_low
);
1394 token
= expect(token
, ',', "after low range");
1395 token
= assignment_expression(token
, &stmt
->range_high
);
1396 return expect(token
, ';', "after range statement");
1398 if (match_op(token
->next
, ':')) {
1399 stmt
->type
= STMT_LABEL
;
1400 stmt
->label_identifier
= label_symbol(token
);
1401 return statement(token
->next
->next
, &stmt
->label_statement
);
1405 if (match_op(token
, '{')) {
1406 stmt
->type
= STMT_COMPOUND
;
1407 start_symbol_scope();
1408 token
= compound_statement(token
->next
, stmt
);
1411 return expect(token
, '}', "at end of compound statement");
1414 stmt
->type
= STMT_EXPRESSION
;
1415 return expression_statement(token
, &stmt
->expression
);
1418 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1420 int seen_statement
= 0;
1422 struct statement
* stmt
;
1423 if (eof_token(token
))
1425 if (match_op(token
, '}'))
1427 if (lookup_type(token
)) {
1428 if (seen_statement
) {
1429 warning(token
->pos
, "mixing declarations and code");
1432 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1433 token
= external_declaration(token
, &stmt
->declaration
);
1435 seen_statement
= warn_on_mixed
;
1436 token
= statement(token
, &stmt
);
1438 add_statement(list
, stmt
);
1443 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1445 struct symbol_list
**list
= &fn
->arguments
;
1447 if (match_op(token
, ')')) {
1448 // No warning for "void oink ();"
1449 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1450 if (p
&& !match_op(token
->next
, ';'))
1451 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1458 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1460 warning(token
->pos
, "variadic functions must have one named argument");
1462 token
= token
->next
;
1466 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1467 token
= parameter_declaration(token
, &sym
);
1468 if (sym
->ctype
.base_type
== &void_ctype
) {
1469 /* Special case: (void) */
1470 if (!*list
&& !sym
->ident
)
1472 warning(token
->pos
, "void parameter");
1474 add_symbol(list
, sym
);
1475 if (!match_op(token
, ','))
1477 token
= token
->next
;
1483 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1485 token
= statement_list(token
, &stmt
->stmts
);
1489 static struct expression
*identifier_expression(struct token
*token
)
1491 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1492 expr
->expr_ident
= token
->ident
;
1496 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1498 int idx_from
, idx_to
;
1499 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1501 idx_from
= get_expression_value(from
);
1504 idx_to
= get_expression_value(to
);
1505 if (idx_to
< idx_from
|| idx_from
< 0)
1506 warning(from
->pos
, "nonsense array initializer index range");
1508 expr
->idx_from
= idx_from
;
1509 expr
->idx_to
= idx_to
;
1513 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1515 int expect_equal
= 0;
1516 struct token
*next
= token
->next
;
1517 struct expression
**tail
= ep
;
1522 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1523 struct expression
*expr
= identifier_expression(token
);
1524 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1525 token
= initializer(&expr
->ident_expression
, next
->next
);
1526 if (expr
->ident_expression
)
1531 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1532 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1533 struct expression
*expr
= identifier_expression(next
);
1535 tail
= &expr
->ident_expression
;
1538 } else if (match_op(token
, '[')) {
1539 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1540 token
= constant_expression(token
->next
, &from
);
1542 sparse_error(token
->pos
, "Expected constant expression");
1545 if (match_op(token
, SPECIAL_ELLIPSIS
))
1546 token
= constant_expression(token
->next
, &to
);
1547 expr
= index_expression(from
, to
);
1549 tail
= &expr
->idx_expression
;
1550 token
= expect(token
, ']', "at end of initializer index");
1557 if (nested
&& !expect_equal
) {
1558 if (!match_op(token
, '='))
1559 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1564 token
= expect(token
, '=', "at end of initializer index");
1566 token
= initializer(tail
, token
);
1572 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1574 struct expression
*expr
;
1577 token
= single_initializer(&expr
, token
);
1580 add_expression(list
, expr
);
1581 if (!match_op(token
, ','))
1583 token
= token
->next
;
1588 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1590 if (match_op(token
, '{')) {
1591 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1593 token
= initializer_list(&expr
->expr_list
, token
->next
);
1594 return expect(token
, '}', "at end of initializer");
1596 return assignment_expression(token
, tree
);
1599 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1602 sparse_error(sym
->pos
, "no identifier for function argument");
1605 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1608 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1609 struct symbol_list
**list
)
1611 struct symbol_list
**old_symbol_list
;
1612 struct symbol
*base_type
= decl
->ctype
.base_type
;
1613 struct statement
*stmt
, **p
;
1616 old_symbol_list
= function_symbol_list
;
1617 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1618 function_symbol_list
= &decl
->inline_symbol_list
;
1619 p
= &base_type
->inline_stmt
;
1621 function_symbol_list
= &decl
->symbol_list
;
1622 p
= &base_type
->stmt
;
1624 function_computed_target_list
= NULL
;
1625 function_computed_goto_list
= NULL
;
1627 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1628 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1629 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1631 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1632 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1634 stmt
= start_function(decl
);
1637 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1638 declare_argument(arg
, base_type
);
1639 } END_FOR_EACH_PTR(arg
);
1641 token
= compound_statement(token
->next
, stmt
);
1644 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1645 add_symbol(list
, decl
);
1646 check_declaration(decl
);
1647 function_symbol_list
= old_symbol_list
;
1648 if (function_computed_goto_list
) {
1649 if (!function_computed_target_list
)
1650 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1652 struct statement
*stmt
;
1653 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1654 stmt
->target_list
= function_computed_target_list
;
1655 } END_FOR_EACH_PTR(stmt
);
1658 return expect(token
, '}', "at end of function");
1661 static void promote_k_r_types(struct symbol
*arg
)
1663 struct symbol
*base
= arg
->ctype
.base_type
;
1664 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1665 arg
->ctype
.base_type
= &int_ctype
;
1669 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1671 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1674 FOR_EACH_PTR(real_args
, arg
) {
1675 struct symbol
*type
;
1677 /* This is quadratic in the number of arguments. We _really_ don't care */
1678 FOR_EACH_PTR(argtypes
, type
) {
1679 if (type
->ident
== arg
->ident
)
1681 } END_FOR_EACH_PTR(type
);
1682 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1686 /* "char" and "short" promote to "int" */
1687 promote_k_r_types(type
);
1689 arg
->ctype
= type
->ctype
;
1690 } END_FOR_EACH_PTR(arg
);
1692 FOR_EACH_PTR(argtypes
, arg
) {
1694 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1695 } END_FOR_EACH_PTR(arg
);
1699 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1700 struct symbol_list
**list
)
1702 struct symbol_list
*args
= NULL
;
1704 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
1706 token
= declaration_list(token
, &args
);
1707 if (!match_op(token
, ';')) {
1708 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
1711 token
= token
->next
;
1712 } while (lookup_type(token
));
1714 apply_k_r_types(args
, decl
);
1716 if (!match_op(token
, '{')) {
1717 sparse_error(token
->pos
, "expected function body");
1720 return parse_function_body(token
, decl
, list
);
1724 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1726 struct ident
*ident
= NULL
;
1727 struct symbol
*decl
;
1728 struct ctype ctype
= { 0, };
1729 struct symbol
*base_type
;
1732 /* Top-level inline asm? */
1733 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1734 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1735 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1736 struct statement
*stmt
;
1738 anon
->ctype
.base_type
= fn
;
1739 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1742 token
= parse_asm(token
->next
, stmt
);
1744 add_symbol(list
, anon
);
1748 /* Parse declaration-specifiers, if any */
1749 token
= declaration_specifiers(token
, &ctype
, 0);
1750 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1751 decl
->ctype
= ctype
;
1752 token
= declarator(token
, decl
, &ident
);
1754 /* Just a type declaration? */
1756 return expect(token
, ';', "end of type declaration");
1758 /* type define declaration? */
1759 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1761 /* Typedef's don't have meaningful storage */
1763 ctype
.modifiers
&= ~MOD_STORAGE
;
1764 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1765 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1768 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1770 base_type
= decl
->ctype
.base_type
;
1773 if (base_type
&& !base_type
->ident
)
1774 base_type
->ident
= ident
;
1775 } else if (base_type
&& base_type
->type
== SYM_FN
) {
1776 /* K&R argument declaration? */
1777 if (lookup_type(token
))
1778 return parse_k_r_arguments(token
, decl
, list
);
1779 if (match_op(token
, '{'))
1780 return parse_function_body(token
, decl
, list
);
1782 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1783 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1784 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1785 sparse_error(token
->pos
, "void declaration");
1789 if (!is_typedef
&& match_op(token
, '=')) {
1790 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1791 warning(decl
->pos
, "symbol with external linkage has initializer");
1792 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1794 token
= initializer(&decl
->initializer
, token
->next
);
1797 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1798 add_symbol(list
, decl
);
1799 fn_local_symbol(decl
);
1802 check_declaration(decl
);
1804 if (!match_op(token
, ','))
1807 token
= token
->next
;
1809 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1810 decl
->ctype
= ctype
;
1811 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1812 token
= declarator(token
, decl
, &ident
);
1814 sparse_error(token
->pos
, "expected identifier name in type definition");
1818 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1820 /* Function declarations are automatically extern unless specifically static */
1821 base_type
= decl
->ctype
.base_type
;
1822 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1823 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1824 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1827 return expect(token
, ';', "at end of declaration");