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
*))
113 ctype
->modifiers
= 0;
114 if (token_type(token
) == TOKEN_IDENT
) {
115 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
117 (sym
->scope
!= block_scope
&&
118 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
119 // Either a new symbol, or else an out-of-scope
120 // symbol being redefined.
121 sym
= alloc_symbol(token
->pos
, type
);
122 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
124 if (sym
->type
!= type
)
125 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
127 ctype
->base_type
= sym
;
128 if (match_op(token
, '{')) {
129 // The following test is actually wrong for empty
130 // structs, but (1) they are not C99, (2) gcc does
131 // the same thing, and (3) it's easier.
132 if (sym
->symbol_list
)
133 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
134 token
= parse(token
->next
, sym
);
135 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
137 // Mark the structure as needing re-examination
143 // private struct/union/enum type
144 if (!match_op(token
, '{')) {
145 sparse_error(token
->pos
, "expected declaration");
146 ctype
->base_type
= &bad_ctype
;
150 sym
= alloc_symbol(token
->pos
, type
);
151 token
= parse(token
->next
, sym
);
152 ctype
->base_type
= sym
;
153 return expect(token
, '}', "at end of specifier");
156 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
158 return struct_declaration_list(token
, &sym
->symbol_list
);
161 static struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
163 return struct_union_enum_specifier(type
, token
, ctype
, parse_struct_declaration
);
168 unsigned long long y
;
171 static void upper_boundary(Num
*n
, Num
*v
)
183 static void lower_boundary(Num
*n
, Num
*v
)
195 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
197 int shift
= type
->bit_size
;
198 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
202 if (upper
->x
== 0 && upper
->y
>> shift
)
204 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
209 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
211 if (s1
->bit_size
< s2
->bit_size
) {
213 } else if (s1
->bit_size
== s2
->bit_size
) {
214 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
217 if (s1
->bit_size
< bits_in_int
)
222 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
226 FOR_EACH_PTR(list
, sym
) {
227 struct expression
*expr
= sym
->initializer
;
228 struct symbol
*ctype
;
229 if (expr
->type
!= EXPR_VALUE
)
232 if (ctype
->bit_size
== base_type
->bit_size
)
234 cast_value(expr
, base_type
, expr
, ctype
);
235 } END_FOR_EACH_PTR(sym
);
238 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
240 unsigned long long lastval
= 0;
241 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
242 Num upper
= {-1, 0}, lower
= {1, 0};
243 struct symbol_list
*entries
= NULL
;
245 parent
->examined
= 1;
246 parent
->ctype
.base_type
= &int_ctype
;
247 while (token_type(token
) == TOKEN_IDENT
) {
248 struct expression
*expr
= NULL
;
249 struct token
*next
= token
->next
;
252 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
253 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
254 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
256 if (match_op(next
, '=')) {
257 next
= constant_expression(next
->next
, &expr
);
258 lastval
= get_expression_value(expr
);
260 if (expr
&& expr
->ctype
)
264 } else if (is_int_type(ctype
)) {
267 error_die(token
->pos
, "can't increment the last enum member");
271 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
272 expr
->value
= lastval
;
276 sym
->initializer
= expr
;
277 sym
->ctype
.base_type
= parent
;
278 add_ptr_list(&entries
, sym
);
280 if (base_type
!= &bad_ctype
) {
281 if (ctype
->type
== SYM_NODE
)
282 ctype
= ctype
->ctype
.base_type
;
283 if (ctype
->type
== SYM_ENUM
) {
287 ctype
= ctype
->ctype
.base_type
;
291 * - if all enum's are of the same type, then
292 * the base_type is that type (two first
294 * - if enums are of different types, they
295 * all have to be integer types, and the
296 * base type is at least "int_ctype".
297 * - otherwise the base_type is "bad_ctype".
301 } else if (ctype
== base_type
) {
303 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
304 base_type
= bigger_enum_type(base_type
, ctype
);
306 base_type
= &bad_ctype
;
307 parent
->ctype
.base_type
= base_type
;
309 if (is_int_type(base_type
)) {
310 Num v
= {.y
= lastval
};
311 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
313 else if ((long long)lastval
>= 0)
317 upper_boundary(&upper
, &v
);
318 lower_boundary(&lower
, &v
);
321 if (!match_op(token
, ','))
326 sparse_error(token
->pos
, "bad enum definition");
327 base_type
= &bad_ctype
;
329 else if (!is_int_type(base_type
))
330 base_type
= base_type
;
331 else if (type_is_ok(base_type
, &upper
, &lower
))
332 base_type
= base_type
;
333 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
334 base_type
= &int_ctype
;
335 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
336 base_type
= &uint_ctype
;
337 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
338 base_type
= &long_ctype
;
339 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
340 base_type
= &ulong_ctype
;
341 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
342 base_type
= &llong_ctype
;
343 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
344 base_type
= &ullong_ctype
;
346 base_type
= &bad_ctype
;
347 parent
->ctype
.base_type
= base_type
;
348 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
349 parent
->examined
= 0;
351 cast_enum_list(entries
, base_type
);
352 free_ptr_list(&entries
);
357 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
359 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
361 ctype
= &ctype
->base_type
->ctype
;
362 if (!ctype
->base_type
)
363 ctype
->base_type
= &incomplete_ctype
;
368 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
372 if (!match_op(token
, '(')) {
373 sparse_error(token
->pos
, "expected '(' after typeof");
376 if (lookup_type(token
->next
)) {
377 token
= typename(token
->next
, &sym
);
380 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
381 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
383 ctype
->modifiers
= 0;
384 ctype
->base_type
= typeof_sym
;
386 return expect(token
, ')', "after typeof");
389 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
391 if (attribute
== &packed_ident
||
392 attribute
== &__packed___ident
) {
393 ctype
->alignment
= 1;
396 if (attribute
== &aligned_ident
||
397 attribute
== &__aligned___ident
) {
398 int alignment
= max_alignment
;
400 alignment
= get_expression_value(expr
);
401 ctype
->alignment
= alignment
;
404 if (attribute
== &nocast_ident
) {
405 ctype
->modifiers
|= MOD_NOCAST
;
408 if (attribute
== &noderef_ident
) {
409 ctype
->modifiers
|= MOD_NODEREF
;
412 if (attribute
== &safe_ident
) {
413 ctype
->modifiers
|= MOD_SAFE
;
416 if (attribute
== &force_ident
) {
417 ctype
->modifiers
|= MOD_FORCE
;
420 if (attribute
== &bitwise_ident
||
421 attribute
== &__bitwise___ident
) {
423 ctype
->modifiers
|= MOD_BITWISE
;
426 if (attribute
== &address_space_ident
) {
428 return "expected address space number";
429 ctype
->as
= get_expression_value(expr
);
432 if (attribute
== &context_ident
) {
433 if (expr
&& expr
->type
== EXPR_COMMA
) {
434 int input
= get_expression_value(expr
->left
);
435 int output
= get_expression_value(expr
->right
);
436 ctype
->in_context
= input
;
437 ctype
->out_context
= output
;
440 return "expected context input/output values";
442 if (attribute
== &mode_ident
||
443 attribute
== &__mode___ident
) {
444 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
445 struct ident
*ident
= expr
->symbol_name
;
448 * Match against __QI__/__HI__/__SI__/__DI__
450 * FIXME! This is broken - we don't actually get
451 * the type information updated properly at this
452 * stage for some reason.
454 if (ident
== &__QI___ident
||
455 ident
== &QI_ident
) {
456 ctype
->modifiers
|= MOD_CHAR
;
459 if (ident
== &__HI___ident
||
460 ident
== &HI_ident
) {
461 ctype
->modifiers
|= MOD_SHORT
;
464 if (ident
== &__SI___ident
||
465 ident
== &SI_ident
) {
469 if (ident
== &__DI___ident
||
470 ident
== &DI_ident
) {
471 ctype
->modifiers
|= MOD_LONGLONG
;
474 if (ident
== &__word___ident
||
475 ident
== &word_ident
) {
476 ctype
->modifiers
|= MOD_LONG
;
479 return "unknown mode attribute";
481 return "expected attribute mode symbol";
484 /* Throw away for now.. */
485 if (attribute
== &__transparent_union___ident
) {
486 if (Wtransparent_union
)
487 return "ignoring attribute __transparent_union__";
490 if (attribute
== ¬hrow_ident
||
491 attribute
== &__nothrow_ident
||
492 attribute
== &__nothrow___ident
)
494 if (attribute
== &__malloc___ident
)
496 if (attribute
== &nonnull_ident
||
497 attribute
== &__nonnull_ident
||
498 attribute
== &__nonnull___ident
)
500 if (attribute
== &format_ident
||
501 attribute
== &__format___ident
||
502 attribute
== &__format_arg___ident
)
504 if (attribute
== §ion_ident
||
505 attribute
== &__section___ident
)
507 if (attribute
== &unused_ident
||
508 attribute
== &__unused___ident
)
510 if (attribute
== &const_ident
||
511 attribute
== &__const_ident
||
512 attribute
== &__const___ident
)
514 if (attribute
== &noreturn_ident
||
515 attribute
== &__noreturn___ident
)
517 if (attribute
== ®parm_ident
)
519 if (attribute
== &weak_ident
||
520 attribute
== &__weak___ident
)
522 if (attribute
== &alias_ident
)
524 if (attribute
== &pure_ident
||
525 attribute
== &__pure___ident
)
527 if (attribute
== &always_inline_ident
)
529 if (attribute
== &syscall_linkage_ident
)
531 if (attribute
== &visibility_ident
)
533 if (attribute
== &deprecated_ident
||
534 attribute
== &__deprecated___ident
)
536 if (attribute
== &noinline_ident
)
538 if (attribute
== &__used___ident
)
540 if (attribute
== &warn_unused_result_ident
||
541 attribute
== &__warn_unused_result___ident
)
543 if (attribute
== &model_ident
||
544 attribute
== &__model___ident
)
547 return "unknown attribute";
550 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
552 ctype
->modifiers
= 0;
553 token
= expect(token
, '(', "after attribute");
554 token
= expect(token
, '(', "after attribute");
557 const char *error_str
;
558 struct ident
*attribute_name
;
559 struct expression
*attribute_expr
;
561 if (eof_token(token
))
563 if (match_op(token
, ';'))
565 if (token_type(token
) != TOKEN_IDENT
)
567 attribute_name
= token
->ident
;
569 attribute_expr
= NULL
;
570 if (match_op(token
, '('))
571 token
= parens_expression(token
, &attribute_expr
, "in attribute");
572 error_str
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
574 sparse_error(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error_str
);
575 if (!match_op(token
, ','))
580 token
= expect(token
, ')', "after attribute");
581 token
= expect(token
, ')', "after attribute");
585 struct symbol
* ctype_integer(unsigned long spec
)
587 static struct symbol
*const integer_ctypes
[][3] = {
588 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
589 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
590 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
591 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
592 { &int_ctype
, &sint_ctype
, &uint_ctype
},
594 struct symbol
*const (*ctype
)[3];
597 ctype
= integer_ctypes
;
598 if (!(spec
& MOD_LONGLONG
)) {
600 if (!(spec
& MOD_LONG
)) {
602 if (!(spec
& MOD_SHORT
)) {
604 if (!(spec
& MOD_CHAR
))
610 sub
= ((spec
& MOD_UNSIGNED
)
612 : ((spec
& MOD_EXPLICITLY_SIGNED
)
616 return ctype
[0][sub
];
619 struct symbol
* ctype_fp(unsigned long spec
)
621 if (spec
& MOD_LONGLONG
)
622 return &ldouble_ctype
;
624 return &double_ctype
;
628 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
630 unsigned long mod
= thistype
->modifiers
;
633 unsigned long old
= ctype
->modifiers
;
634 unsigned long extra
= 0, dup
, conflict
;
636 if (mod
& old
& MOD_LONG
) {
637 extra
= MOD_LONGLONG
| MOD_LONG
;
641 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
643 sparse_error(pos
, "Just how %sdo you want this type to be?",
644 modifier_string(dup
));
646 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
648 sparse_error(pos
, "You cannot have both long and short modifiers.");
650 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
652 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
654 // Only one storage modifier allowed, except that "inline" doesn't count.
655 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
656 conflict
&= (conflict
- 1);
658 sparse_error(pos
, "multiple storage classes");
660 ctype
->modifiers
= old
| mod
| extra
;
663 /* Context mask and value */
664 ctype
->in_context
+= thistype
->in_context
;
665 ctype
->out_context
+= thistype
->out_context
;
668 if (thistype
->alignment
& (thistype
->alignment
-1)) {
669 warning(pos
, "I don't like non-power-of-2 alignments");
670 thistype
->alignment
= 0;
672 if (thistype
->alignment
> ctype
->alignment
)
673 ctype
->alignment
= thistype
->alignment
;
677 ctype
->as
= thistype
->as
;
680 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
682 unsigned long banned
, wrong
;
683 unsigned long this_mod
= s
->ctype
.modifiers
;
684 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
685 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
687 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
688 banned
= BANNED_SIZE
| BANNED_SIGN
;
689 else if (this_mod
& MOD_SPECIALBITS
)
691 else if (s
->ctype
.base_type
== &fp_type
)
692 banned
= BANNED_SIGN
;
693 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
699 // vector_type <-- whatever that is
700 banned
= BANNED_SIZE
| BANNED_SIGN
;
703 wrong
= mod
& banned
;
705 sparse_error(*pos
, "modifier %sis invalid in this context",
706 modifier_string (wrong
));
710 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
714 while ( (token
= next
) != NULL
) {
715 struct ctype thistype
;
717 struct symbol
*s
, *type
;
721 if (token_type(token
) != TOKEN_IDENT
)
723 ident
= token
->ident
;
725 s
= lookup_symbol(ident
, NS_TYPEDEF
);
729 mod
= thistype
.modifiers
;
730 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
732 if (mod
& MOD_SPECIALBITS
) {
733 if (mod
& MOD_STRUCTOF
)
734 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
735 else if (mod
& MOD_UNIONOF
)
736 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
737 else if (mod
& MOD_ENUMOF
)
738 next
= enum_specifier(next
, &thistype
);
739 else if (mod
& MOD_ATTRIBUTE
)
740 next
= attribute_specifier(next
, &thistype
);
741 else if (mod
& MOD_TYPEOF
)
742 next
= typeof_specifier(next
, &thistype
);
743 mod
= thistype
.modifiers
;
745 type
= thistype
.base_type
;
749 if (ctype
->base_type
)
751 /* User types only mix with qualifiers */
752 if (mod
& MOD_USERTYPE
) {
753 if (ctype
->modifiers
& MOD_SPECIFIER
)
756 ctype
->base_type
= type
;
759 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
760 apply_ctype(token
->pos
, &thistype
, ctype
);
763 /* Turn the "virtual types" into real types with real sizes etc */
764 if (!ctype
->base_type
) {
765 struct symbol
*base
= &incomplete_ctype
;
768 * If we have modifiers, we'll default to an integer
769 * type, and "ctype_integer()" will turn this into
772 if (ctype
->modifiers
& MOD_SPECIFIER
)
774 ctype
->base_type
= base
;
776 if (ctype
->base_type
== &int_type
) {
777 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
778 ctype
->modifiers
&= ~MOD_SPECIFIER
;
779 } else if (ctype
->base_type
== &fp_type
) {
780 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
781 ctype
->modifiers
&= ~MOD_SPECIFIER
;
783 if (ctype
->modifiers
& MOD_BITWISE
) {
785 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
786 if (!is_int_type(ctype
->base_type
)) {
787 sparse_error(token
->pos
, "invalid modifier");
790 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
791 *type
= *ctype
->base_type
;
792 type
->ctype
.base_type
= ctype
->base_type
;
793 type
->type
= SYM_RESTRICT
;
794 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
795 ctype
->base_type
= type
;
800 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
802 struct expression
*expr
= NULL
;
804 token
= parse_expression(token
, &expr
);
805 sym
->array_size
= expr
;
809 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
810 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
812 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
815 if (token_type(token
) != TOKEN_IDENT
)
817 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
818 struct ctype thistype
= { 0, };
819 token
= attribute_specifier(token
->next
, &thistype
);
820 apply_ctype(token
->pos
, &thistype
, ctype
);
823 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
824 struct expression
*expr
;
825 token
= expect(token
->next
, '(', "after asm");
826 token
= parse_expression(token
->next
, &expr
);
827 token
= expect(token
, ')', "after asm");
835 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
837 struct ctype
*ctype
= &decl
->ctype
;
839 if (p
&& token_type(token
) == TOKEN_IDENT
) {
845 token
= handle_attributes(token
, ctype
);
847 if (token_type(token
) != TOKEN_SPECIAL
)
851 * This can be either a parameter list or a grouping.
852 * For the direct (non-abstract) case, we know if must be
853 * a parameter list if we already saw the identifier.
854 * For the abstract case, we know if must be a parameter
855 * list if it is empty or starts with a type.
857 if (token
->special
== '(') {
859 struct token
*next
= token
->next
;
860 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
863 struct symbol
*base_type
= ctype
->base_type
;
864 token
= declarator(next
, decl
, p
);
865 token
= expect(token
, ')', "in nested declarator");
866 while (ctype
->base_type
!= base_type
)
867 ctype
= &ctype
->base_type
->ctype
;
872 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
873 token
= parameter_type_list(next
, sym
, p
);
874 token
= expect(token
, ')', "in function declarator");
877 if (token
->special
== '[') {
878 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
879 token
= abstract_array_declarator(token
->next
, array
);
880 token
= expect(token
, ']', "in abstract_array_declarator");
881 ctype
= &array
->ctype
;
889 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
891 unsigned long modifiers
;
892 struct symbol
*base_type
;
894 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
895 base_type
= ctype
->base_type
;
896 ctype
->modifiers
= modifiers
;
898 while (match_op(token
,'*')) {
899 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
900 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
901 ptr
->ctype
.as
= ctype
->as
;
902 ptr
->ctype
.in_context
+= ctype
->in_context
;
903 ptr
->ctype
.out_context
+= ctype
->out_context
;
904 ptr
->ctype
.base_type
= base_type
;
907 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
908 ctype
->base_type
= base_type
;
910 ctype
->in_context
= 0;
911 ctype
->out_context
= 0;
913 token
= declaration_specifiers(token
->next
, ctype
, 1);
914 modifiers
= ctype
->modifiers
;
919 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
921 token
= pointer(token
, &sym
->ctype
);
922 return direct_declarator(token
, sym
, p
);
925 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
927 struct ctype
*ctype
= &decl
->ctype
;
928 struct expression
*expr
;
929 struct symbol
*bitfield
;
932 if (!is_int_type(ctype
->base_type
)) {
933 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
934 show_typename(ctype
->base_type
));
935 // Parse this to recover gracefully.
936 return conditional_expression(token
->next
, &expr
);
939 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
940 token
= conditional_expression(token
->next
, &expr
);
941 width
= get_expression_value(expr
);
942 bitfield
->bit_size
= width
;
944 if (width
< 0 || width
> INT_MAX
) {
945 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
947 } else if (decl
->ident
&& width
== 0) {
948 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
949 show_ident(decl
->ident
));
951 } else if (decl
->ident
) {
952 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
953 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
954 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
955 // Valid values are either {-1;0} or {0}, depending on integer
956 // representation. The latter makes for very efficient code...
957 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
959 if (Wdefault_bitfield_sign
&&
960 base_type
->type
!= SYM_ENUM
&&
961 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
963 // The sign of bitfields is unspecified by default.
964 sparse_error(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
967 bitfield
->bit_size
= width
;
971 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
973 struct ctype ctype
= {0, };
975 token
= declaration_specifiers(token
, &ctype
, 0);
977 struct ident
*ident
= NULL
;
978 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
980 token
= declarator(token
, decl
, &ident
);
982 if (match_op(token
, ':')) {
983 token
= handle_bitfield(token
, decl
);
984 token
= handle_attributes(token
, &decl
->ctype
);
986 add_symbol(list
, decl
);
987 if (!match_op(token
, ','))
994 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
996 while (!match_op(token
, '}')) {
997 token
= declaration_list(token
, list
);
998 if (!match_op(token
, ';')) {
999 sparse_error(token
->pos
, "expected ; at end of declaration");
1002 token
= token
->next
;
1007 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
1009 struct ident
*ident
= NULL
;
1011 struct ctype ctype
= { 0, };
1013 token
= declaration_specifiers(token
, &ctype
, 0);
1014 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1017 token
= declarator(token
, sym
, &ident
);
1022 struct token
*typename(struct token
*token
, struct symbol
**p
)
1024 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1026 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
1027 return declarator(token
, sym
, NULL
);
1030 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1032 token
= parse_expression(token
, tree
);
1033 return expect(token
, ';', "at end of statement");
1036 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1037 struct expression_list
**inout
)
1039 struct expression
*expr
;
1041 /* Allow empty operands */
1042 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1045 struct ident
*ident
= NULL
;
1046 if (match_op(token
->next
, '[') &&
1047 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1048 match_op(token
->next
->next
->next
, ']')) {
1049 ident
= token
->next
->next
->ident
;
1050 token
= token
->next
->next
->next
;
1052 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1053 token
= primary_expression(token
->next
, &expr
);
1054 add_expression(inout
, expr
);
1055 token
= parens_expression(token
, &expr
, "in asm parameter");
1056 add_expression(inout
, expr
);
1057 } while (match_op(token
, ','));
1061 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1062 struct expression_list
**clobbers
)
1064 struct expression
*expr
;
1067 token
= primary_expression(token
->next
, &expr
);
1068 add_expression(clobbers
, expr
);
1069 } while (match_op(token
, ','));
1073 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
1075 stmt
->type
= STMT_ASM
;
1076 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1077 token
= token
->next
;
1079 token
= expect(token
, '(', "after asm");
1080 token
= parse_expression(token
, &stmt
->asm_string
);
1081 if (match_op(token
, ':'))
1082 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1083 if (match_op(token
, ':'))
1084 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1085 if (match_op(token
, ':'))
1086 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1087 token
= expect(token
, ')', "after asm");
1088 return expect(token
, ';', "at end of asm-statement");
1091 /* Make a statement out of an expression */
1092 static struct statement
*make_statement(struct expression
*expr
)
1094 struct statement
*stmt
;
1098 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1099 stmt
->expression
= expr
;
1104 * All iterators have two symbols associated with them:
1105 * the "continue" and "break" symbols, which are targets
1106 * for continue and break statements respectively.
1108 * They are in a special name-space, but they follow
1109 * all the normal visibility rules, so nested iterators
1110 * automatically work right.
1112 static void start_iterator(struct statement
*stmt
)
1114 struct symbol
*cont
, *brk
;
1116 start_symbol_scope();
1117 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1118 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1119 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1120 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1122 stmt
->type
= STMT_ITERATOR
;
1123 stmt
->iterator_break
= brk
;
1124 stmt
->iterator_continue
= cont
;
1125 fn_local_symbol(brk
);
1126 fn_local_symbol(cont
);
1129 static void end_iterator(struct statement
*stmt
)
1134 static struct statement
*start_function(struct symbol
*sym
)
1137 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1139 start_function_scope();
1140 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1141 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1142 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1143 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1144 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1146 fn_local_symbol(ret
);
1148 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1154 static void end_function(struct symbol
*sym
)
1157 end_function_scope();
1161 * A "switch()" statement, like an iterator, has a
1162 * the "break" symbol associated with it. It works
1163 * exactly like the iterator break - it's the target
1164 * for any break-statements in scope, and means that
1165 * "break" handling doesn't even need to know whether
1166 * it's breaking out of an iterator or a switch.
1168 * In addition, the "case" symbol is a marker for the
1169 * case/default statements to find the switch statement
1170 * that they are associated with.
1172 static void start_switch(struct statement
*stmt
)
1174 struct symbol
*brk
, *switch_case
;
1176 start_symbol_scope();
1177 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1178 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1180 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1181 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1182 switch_case
->stmt
= stmt
;
1184 stmt
->type
= STMT_SWITCH
;
1185 stmt
->switch_break
= brk
;
1186 stmt
->switch_case
= switch_case
;
1188 fn_local_symbol(brk
);
1189 fn_local_symbol(switch_case
);
1192 static void end_switch(struct statement
*stmt
)
1194 if (!stmt
->switch_case
->symbol_list
)
1195 warning(stmt
->pos
, "switch with no cases");
1199 static void add_case_statement(struct statement
*stmt
)
1201 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1205 sparse_error(stmt
->pos
, "not in switch scope");
1206 stmt
->type
= STMT_NONE
;
1209 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1210 add_symbol(&target
->symbol_list
, sym
);
1212 stmt
->case_label
= sym
;
1213 fn_local_symbol(sym
);
1216 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1218 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1221 error_die(token
->pos
, "internal error: return without a function target");
1222 stmt
->type
= STMT_RETURN
;
1223 stmt
->ret_target
= target
;
1224 return expression_statement(token
->next
, &stmt
->ret_value
);
1227 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1229 struct symbol_list
*syms
;
1230 struct expression
*e1
, *e2
, *e3
;
1231 struct statement
*iterator
;
1233 start_iterator(stmt
);
1234 token
= expect(token
->next
, '(', "after 'for'");
1238 /* C99 variable declaration? */
1239 if (lookup_type(token
)) {
1240 token
= external_declaration(token
, &syms
);
1242 token
= parse_expression(token
, &e1
);
1243 token
= expect(token
, ';', "in 'for'");
1245 token
= parse_expression(token
, &e2
);
1246 token
= expect(token
, ';', "in 'for'");
1247 token
= parse_expression(token
, &e3
);
1248 token
= expect(token
, ')', "in 'for'");
1249 token
= statement(token
, &iterator
);
1251 stmt
->iterator_syms
= syms
;
1252 stmt
->iterator_pre_statement
= make_statement(e1
);
1253 stmt
->iterator_pre_condition
= e2
;
1254 stmt
->iterator_post_statement
= make_statement(e3
);
1255 stmt
->iterator_post_condition
= NULL
;
1256 stmt
->iterator_statement
= iterator
;
1262 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1264 struct expression
*expr
;
1265 struct statement
*iterator
;
1267 start_iterator(stmt
);
1268 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1269 token
= statement(token
, &iterator
);
1271 stmt
->iterator_pre_condition
= expr
;
1272 stmt
->iterator_post_condition
= NULL
;
1273 stmt
->iterator_statement
= iterator
;
1279 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1281 struct expression
*expr
;
1282 struct statement
*iterator
;
1284 start_iterator(stmt
);
1285 token
= statement(token
->next
, &iterator
);
1286 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1287 token
= token
->next
;
1289 sparse_error(token
->pos
, "expected 'while' after 'do'");
1290 token
= parens_expression(token
, &expr
, "after 'do-while'");
1292 stmt
->iterator_post_condition
= expr
;
1293 stmt
->iterator_statement
= iterator
;
1296 return expect(token
, ';', "after statement");
1299 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1301 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1304 if (token_type(token
) == TOKEN_IDENT
) {
1305 if (token
->ident
== &if_ident
) {
1306 stmt
->type
= STMT_IF
;
1307 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1308 token
= statement(token
, &stmt
->if_true
);
1309 if (token_type(token
) != TOKEN_IDENT
)
1311 if (token
->ident
!= &else_ident
)
1313 return statement(token
->next
, &stmt
->if_false
);
1316 if (token
->ident
== &return_ident
)
1317 return parse_return_statement(token
, stmt
);
1319 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1320 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1321 stmt
->type
= STMT_GOTO
;
1322 stmt
->goto_label
= target
;
1324 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1325 return expect(token
->next
, ';', "at end of statement");
1327 if (token
->ident
== &default_ident
) {
1328 token
= token
->next
;
1329 goto default_statement
;
1331 if (token
->ident
== &case_ident
) {
1332 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1333 if (match_op(token
, SPECIAL_ELLIPSIS
))
1334 token
= parse_expression(token
->next
, &stmt
->case_to
);
1336 stmt
->type
= STMT_CASE
;
1337 token
= expect(token
, ':', "after default/case");
1338 add_case_statement(stmt
);
1339 return statement(token
, &stmt
->case_statement
);
1341 if (token
->ident
== &switch_ident
) {
1342 stmt
->type
= STMT_SWITCH
;
1344 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1345 token
= statement(token
, &stmt
->switch_statement
);
1349 if (token
->ident
== &for_ident
)
1350 return parse_for_statement(token
, stmt
);
1352 if (token
->ident
== &while_ident
)
1353 return parse_while_statement(token
, stmt
);
1355 if (token
->ident
== &do_ident
)
1356 return parse_do_statement(token
, stmt
);
1358 if (token
->ident
== &goto_ident
) {
1359 stmt
->type
= STMT_GOTO
;
1360 token
= token
->next
;
1361 if (match_op(token
, '*')) {
1362 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1363 add_statement(&function_computed_goto_list
, stmt
);
1364 } else if (token_type(token
) == TOKEN_IDENT
) {
1365 stmt
->goto_label
= label_symbol(token
);
1366 token
= token
->next
;
1368 sparse_error(token
->pos
, "Expected identifier or goto expression");
1370 return expect(token
, ';', "at end of statement");
1372 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1373 return parse_asm(token
->next
, stmt
);
1375 if (token
->ident
== &__context___ident
) {
1376 stmt
->type
= STMT_CONTEXT
;
1377 token
= parse_expression(token
->next
, &stmt
->expression
);
1378 return expect(token
, ';', "at end of statement");
1380 if (token
->ident
== &__range___ident
) {
1381 stmt
->type
= STMT_RANGE
;
1382 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1383 token
= expect(token
, ',', "after range expression");
1384 token
= assignment_expression(token
, &stmt
->range_low
);
1385 token
= expect(token
, ',', "after low range");
1386 token
= assignment_expression(token
, &stmt
->range_high
);
1387 return expect(token
, ';', "after range statement");
1389 if (match_op(token
->next
, ':')) {
1390 stmt
->type
= STMT_LABEL
;
1391 stmt
->label_identifier
= label_symbol(token
);
1392 return statement(token
->next
->next
, &stmt
->label_statement
);
1396 if (match_op(token
, '{')) {
1397 stmt
->type
= STMT_COMPOUND
;
1398 start_symbol_scope();
1399 token
= compound_statement(token
->next
, stmt
);
1402 return expect(token
, '}', "at end of compound statement");
1405 stmt
->type
= STMT_EXPRESSION
;
1406 return expression_statement(token
, &stmt
->expression
);
1409 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1412 struct statement
* stmt
;
1413 if (eof_token(token
))
1415 if (match_op(token
, '}'))
1417 if (lookup_type(token
)) {
1418 if (warn_on_mixed
&& *list
)
1419 warning(token
->pos
, "mixing declarations and code");
1420 token
= external_declaration(token
, syms
);
1423 token
= statement(token
, &stmt
);
1424 add_statement(list
, stmt
);
1429 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1431 struct symbol_list
**list
= &fn
->arguments
;
1433 if (match_op(token
, ')')) {
1434 // No warning for "void oink ();"
1435 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1436 if (p
&& !match_op(token
->next
, ';'))
1437 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1444 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1446 warning(token
->pos
, "variadic functions must have one named argument");
1448 token
= token
->next
;
1452 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1453 token
= parameter_declaration(token
, &sym
);
1454 if (sym
->ctype
.base_type
== &void_ctype
) {
1455 /* Special case: (void) */
1456 if (!*list
&& !sym
->ident
)
1458 warning(token
->pos
, "void parameter");
1460 add_symbol(list
, sym
);
1461 if (!match_op(token
, ','))
1463 token
= token
->next
;
1469 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1471 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1475 static struct expression
*identifier_expression(struct token
*token
)
1477 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1478 expr
->expr_ident
= token
->ident
;
1482 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1484 int idx_from
, idx_to
;
1485 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1487 idx_from
= get_expression_value(from
);
1490 idx_to
= get_expression_value(to
);
1491 if (idx_to
< idx_from
|| idx_from
< 0)
1492 warning(from
->pos
, "nonsense array initializer index range");
1494 expr
->idx_from
= idx_from
;
1495 expr
->idx_to
= idx_to
;
1499 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1501 int expect_equal
= 0;
1502 struct token
*next
= token
->next
;
1503 struct expression
**tail
= ep
;
1508 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1509 struct expression
*expr
= identifier_expression(token
);
1510 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1511 token
= initializer(&expr
->ident_expression
, next
->next
);
1512 if (expr
->ident_expression
)
1517 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1518 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1519 struct expression
*expr
= identifier_expression(next
);
1521 tail
= &expr
->ident_expression
;
1524 } else if (match_op(token
, '[')) {
1525 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1526 token
= constant_expression(token
->next
, &from
);
1528 sparse_error(token
->pos
, "Expected constant expression");
1531 if (match_op(token
, SPECIAL_ELLIPSIS
))
1532 token
= constant_expression(token
->next
, &to
);
1533 expr
= index_expression(from
, to
);
1535 tail
= &expr
->idx_expression
;
1536 token
= expect(token
, ']', "at end of initializer index");
1543 if (nested
&& !expect_equal
) {
1544 if (!match_op(token
, '='))
1545 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1550 token
= expect(token
, '=', "at end of initializer index");
1552 token
= initializer(tail
, token
);
1558 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1560 struct expression
*expr
;
1563 token
= single_initializer(&expr
, token
);
1566 add_expression(list
, expr
);
1567 if (!match_op(token
, ','))
1569 token
= token
->next
;
1574 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1576 if (match_op(token
, '{')) {
1577 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1579 token
= initializer_list(&expr
->expr_list
, token
->next
);
1580 return expect(token
, '}', "at end of initializer");
1582 return assignment_expression(token
, tree
);
1585 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1588 sparse_error(sym
->pos
, "no identifier for function argument");
1591 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1594 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1595 struct symbol_list
**list
)
1597 struct symbol_list
**old_symbol_list
;
1598 struct symbol
*base_type
= decl
->ctype
.base_type
;
1599 struct statement
*stmt
, **p
;
1602 old_symbol_list
= function_symbol_list
;
1603 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1604 function_symbol_list
= &decl
->inline_symbol_list
;
1605 p
= &base_type
->inline_stmt
;
1607 function_symbol_list
= &decl
->symbol_list
;
1608 p
= &base_type
->stmt
;
1610 function_computed_target_list
= NULL
;
1611 function_computed_goto_list
= NULL
;
1613 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1614 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1615 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1617 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1618 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1620 stmt
= start_function(decl
);
1623 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1624 declare_argument(arg
, base_type
);
1625 } END_FOR_EACH_PTR(arg
);
1627 token
= compound_statement(token
->next
, stmt
);
1630 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1631 add_symbol(list
, decl
);
1632 check_declaration(decl
);
1633 function_symbol_list
= old_symbol_list
;
1634 if (function_computed_goto_list
) {
1635 if (!function_computed_target_list
)
1636 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1638 struct statement
*stmt
;
1639 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1640 stmt
->target_list
= function_computed_target_list
;
1641 } END_FOR_EACH_PTR(stmt
);
1644 return expect(token
, '}', "at end of function");
1647 static void promote_k_r_types(struct symbol
*arg
)
1649 struct symbol
*base
= arg
->ctype
.base_type
;
1650 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1651 arg
->ctype
.base_type
= &int_ctype
;
1655 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1657 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1660 FOR_EACH_PTR(real_args
, arg
) {
1661 struct symbol
*type
;
1663 /* This is quadratic in the number of arguments. We _really_ don't care */
1664 FOR_EACH_PTR(argtypes
, type
) {
1665 if (type
->ident
== arg
->ident
)
1667 } END_FOR_EACH_PTR(type
);
1668 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1672 /* "char" and "short" promote to "int" */
1673 promote_k_r_types(type
);
1675 arg
->ctype
= type
->ctype
;
1676 } END_FOR_EACH_PTR(arg
);
1678 FOR_EACH_PTR(argtypes
, arg
) {
1680 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1681 } END_FOR_EACH_PTR(arg
);
1685 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1686 struct symbol_list
**list
)
1688 struct symbol_list
*args
= NULL
;
1690 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
1692 token
= declaration_list(token
, &args
);
1693 if (!match_op(token
, ';')) {
1694 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
1697 token
= token
->next
;
1698 } while (lookup_type(token
));
1700 apply_k_r_types(args
, decl
);
1702 if (!match_op(token
, '{')) {
1703 sparse_error(token
->pos
, "expected function body");
1706 return parse_function_body(token
, decl
, list
);
1710 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1712 struct ident
*ident
= NULL
;
1713 struct symbol
*decl
;
1714 struct ctype ctype
= { 0, };
1715 struct symbol
*base_type
;
1718 /* Top-level inline asm? */
1719 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1720 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1721 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1722 struct statement
*stmt
;
1724 anon
->ctype
.base_type
= fn
;
1725 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1728 token
= parse_asm(token
->next
, stmt
);
1730 add_symbol(list
, anon
);
1734 /* Parse declaration-specifiers, if any */
1735 token
= declaration_specifiers(token
, &ctype
, 0);
1736 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1737 decl
->ctype
= ctype
;
1738 token
= declarator(token
, decl
, &ident
);
1740 /* Just a type declaration? */
1742 return expect(token
, ';', "end of type declaration");
1744 /* type define declaration? */
1745 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1747 /* Typedef's don't have meaningful storage */
1749 ctype
.modifiers
&= ~MOD_STORAGE
;
1750 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1751 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1754 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1756 base_type
= decl
->ctype
.base_type
;
1759 if (base_type
&& !base_type
->ident
)
1760 base_type
->ident
= ident
;
1761 } else if (base_type
&& base_type
->type
== SYM_FN
) {
1762 /* K&R argument declaration? */
1763 if (lookup_type(token
))
1764 return parse_k_r_arguments(token
, decl
, list
);
1765 if (match_op(token
, '{'))
1766 return parse_function_body(token
, decl
, list
);
1768 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1769 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1770 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1771 sparse_error(token
->pos
, "void declaration");
1775 if (!is_typedef
&& match_op(token
, '=')) {
1776 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1777 warning(decl
->pos
, "symbol with external linkage has initializer");
1778 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1780 token
= initializer(&decl
->initializer
, token
->next
);
1783 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1784 add_symbol(list
, decl
);
1785 fn_local_symbol(decl
);
1788 check_declaration(decl
);
1790 if (!match_op(token
, ','))
1793 token
= token
->next
;
1795 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1796 decl
->ctype
= ctype
;
1797 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1798 token
= declarator(token
, decl
, &ident
);
1800 sparse_error(token
->pos
, "expected identifier name in type definition");
1804 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1806 /* Function declarations are automatically extern unless specifically static */
1807 base_type
= decl
->ctype
.base_type
;
1808 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1809 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1810 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1813 return expect(token
, ';', "at end of declaration");