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
);
37 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
39 // Add a symbol to the list of function-local symbols
40 static void fn_local_symbol(struct symbol
*sym
)
42 if (function_symbol_list
)
43 add_symbol(function_symbol_list
, sym
);
46 static int match_idents(struct token
*token
, ...)
50 if (token_type(token
) != TOKEN_IDENT
)
53 va_start(args
, token
);
55 struct ident
* next
= va_arg(args
, struct ident
*);
58 if (token
->ident
== next
)
64 struct statement
*alloc_statement(struct position pos
, int type
)
66 struct statement
*stmt
= __alloc_statement(0);
72 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
74 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
76 struct symbol
*sym
= alloc_symbol(pos
, type
);
78 sym
->ctype
.base_type
= ctype
->base_type
;
79 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
81 ctype
->base_type
= sym
;
82 ctype
->modifiers
&= MOD_STORAGE
;
86 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
88 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
90 sym
= alloc_symbol(token
->pos
, type
);
91 bind_symbol(sym
, token
->ident
, ns
);
92 if (type
== SYM_LABEL
)
99 * NOTE! NS_LABEL is not just a different namespace,
100 * it also ends up using function scope instead of the
101 * regular symbol scope.
103 struct symbol
*label_symbol(struct token
*token
)
105 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
108 struct token
*struct_union_enum_specifier(enum type type
,
109 struct token
*token
, struct ctype
*ctype
,
110 struct token
*(*parse
)(struct token
*, struct symbol
*))
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
));
128 ctype
->base_type
= sym
;
129 if (match_op(token
, '{')) {
130 // The following test is actually wrong for empty
131 // structs, but (1) they are not C99, (2) gcc does
132 // the same thing, and (3) it's easier.
133 if (sym
->symbol_list
)
134 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
135 token
= parse(token
->next
, sym
);
136 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
138 // Mark the structure as needing re-examination
144 // private struct/union/enum type
145 if (!match_op(token
, '{')) {
146 warning(token
->pos
, "expected declaration");
147 ctype
->base_type
= &bad_ctype
;
151 sym
= alloc_symbol(token
->pos
, type
);
152 token
= parse(token
->next
, sym
);
153 ctype
->base_type
= sym
;
154 return expect(token
, '}', "at end of specifier");
157 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
159 return struct_declaration_list(token
, &sym
->symbol_list
);
162 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
164 return struct_union_enum_specifier(type
, token
, ctype
, parse_struct_declaration
);
169 unsigned long long y
;
172 static void upper_boundary(Num
*n
, Num
*v
)
184 static void lower_boundary(Num
*n
, Num
*v
)
196 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
198 int shift
= type
->bit_size
;
199 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
203 if (upper
->x
== 0 && upper
->y
>> shift
)
205 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
210 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
212 unsigned long long lastval
= 0;
213 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
214 Num upper
= {-1, 0}, lower
= {1, 0};
216 while (token_type(token
) == TOKEN_IDENT
) {
217 struct token
*next
= token
->next
;
220 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
221 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
223 if (match_op(next
, '=')) {
224 struct expression
*expr
;
225 next
= constant_expression(next
->next
, &expr
);
226 lastval
= get_expression_value(expr
);
230 } else if (is_int_type(ctype
)) {
233 error_die(token
->pos
, "can't increment the last enum member");
236 sym
->value
= lastval
;
237 sym
->ctype
.base_type
= ctype
;
239 if (base_type
!= &bad_ctype
) {
240 if (ctype
->type
== SYM_NODE
)
241 ctype
= ctype
->ctype
.base_type
;
242 if (ctype
->type
== SYM_ENUM
)
243 ctype
= ctype
->ctype
.base_type
;
246 * - if all enum's are of the same type, then
247 * the base_type is that type (two first
249 * - if enums are of different types, they
250 * all have to be integer types, and the
251 * base type is "int_ctype".
252 * - otherwise the base_type is "bad_ctype".
256 } else if (ctype
== base_type
) {
258 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
259 base_type
= &int_ctype
;
261 base_type
= &bad_ctype
;
263 if (is_int_type(base_type
)) {
264 Num v
= {.y
= lastval
};
265 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
267 else if ((long long)lastval
>= 0)
271 upper_boundary(&upper
, &v
);
272 lower_boundary(&lower
, &v
);
275 if (!match_op(token
, ','))
280 base_type
= &bad_ctype
;
281 else if (!is_int_type(base_type
))
282 base_type
= base_type
;
283 else if (type_is_ok(base_type
, &upper
, &lower
))
284 base_type
= base_type
;
285 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
286 base_type
= &int_ctype
;
287 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
288 base_type
= &uint_ctype
;
289 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
290 base_type
= &long_ctype
;
291 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
292 base_type
= &ulong_ctype
;
293 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
294 base_type
= &llong_ctype
;
295 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
296 base_type
= &ullong_ctype
;
298 base_type
= &bad_ctype
;
299 parent
->ctype
.base_type
= base_type
;
300 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
304 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
306 return struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
309 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
313 if (!match_op(token
, '(')) {
314 warning(token
->pos
, "expected '(' after typeof");
317 if (lookup_type(token
->next
)) {
318 token
= typename(token
->next
, &sym
);
321 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
322 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
324 ctype
->modifiers
= 0;
325 ctype
->base_type
= typeof_sym
;
327 return expect(token
, ')', "after typeof");
330 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
332 if (attribute
== &packed_ident
||
333 attribute
== &__packed___ident
) {
334 ctype
->alignment
= 1;
337 if (attribute
== &aligned_ident
||
338 attribute
== &__aligned___ident
) {
339 int alignment
= max_alignment
;
341 alignment
= get_expression_value(expr
);
342 ctype
->alignment
= alignment
;
345 if (attribute
== &nocast_ident
) {
346 ctype
->modifiers
|= MOD_NOCAST
;
349 if (attribute
== &noderef_ident
) {
350 ctype
->modifiers
|= MOD_NODEREF
;
353 if (attribute
== &safe_ident
) {
354 ctype
->modifiers
|= MOD_SAFE
;
357 if (attribute
== &force_ident
) {
358 ctype
->modifiers
|= MOD_FORCE
;
361 if (attribute
== &bitwise_ident
) {
363 ctype
->modifiers
|= MOD_BITWISE
;
366 if (attribute
== &address_space_ident
) {
368 return "expected address space number";
369 ctype
->as
= get_expression_value(expr
);
372 if (attribute
== &context_ident
) {
373 if (expr
&& expr
->type
== EXPR_COMMA
) {
374 int input
= get_expression_value(expr
->left
);
375 int output
= get_expression_value(expr
->right
);
376 ctype
->in_context
= input
;
377 ctype
->out_context
= output
;
380 return "expected context input/output values";
382 if (attribute
== &mode_ident
||
383 attribute
== &__mode___ident
) {
384 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
385 struct ident
*ident
= expr
->symbol_name
;
388 * Match against __QI__/__HI__/__SI__/__DI__
390 * FIXME! This is broken - we don't actually get
391 * the type information updated properly at this
392 * stage for some reason.
394 if (ident
== &__QI___ident
||
395 ident
== &QI_ident
) {
396 ctype
->modifiers
|= MOD_CHAR
;
399 if (ident
== &__HI___ident
||
400 ident
== &HI_ident
) {
401 ctype
->modifiers
|= MOD_SHORT
;
404 if (ident
== &__SI___ident
||
405 ident
== &SI_ident
) {
409 if (ident
== &__DI___ident
||
410 ident
== &DI_ident
) {
411 ctype
->modifiers
|= MOD_LONGLONG
;
414 if (ident
== &__word___ident
||
415 ident
== &word_ident
) {
416 ctype
->modifiers
|= MOD_LONG
;
419 return "unknown mode attribute";
421 return "expected attribute mode symbol";
424 /* Throw away for now.. */
425 if (attribute
== &format_ident
||
426 attribute
== &__format___ident
||
427 attribute
== &__format_arg___ident
)
429 if (attribute
== §ion_ident
||
430 attribute
== &__section___ident
)
432 if (attribute
== &unused_ident
||
433 attribute
== &__unused___ident
)
435 if (attribute
== &const_ident
||
436 attribute
== &__const_ident
||
437 attribute
== &__const___ident
)
439 if (attribute
== &noreturn_ident
||
440 attribute
== &__noreturn___ident
)
442 if (attribute
== ®parm_ident
)
444 if (attribute
== &weak_ident
)
446 if (attribute
== &alias_ident
)
448 if (attribute
== &pure_ident
)
450 if (attribute
== &always_inline_ident
)
452 if (attribute
== &syscall_linkage_ident
)
454 if (attribute
== &visibility_ident
)
456 if (attribute
== &model_ident
||
457 attribute
== &__model___ident
)
460 return "unknown attribute";
463 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
465 ctype
->modifiers
= 0;
466 token
= expect(token
, '(', "after attribute");
467 token
= expect(token
, '(', "after attribute");
471 struct ident
*attribute_name
;
472 struct expression
*attribute_expr
;
474 if (eof_token(token
))
476 if (match_op(token
, ';'))
478 if (token_type(token
) != TOKEN_IDENT
)
480 attribute_name
= token
->ident
;
482 attribute_expr
= NULL
;
483 if (match_op(token
, '('))
484 token
= parens_expression(token
, &attribute_expr
, "in attribute");
485 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
487 warning(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
488 if (!match_op(token
, ','))
493 token
= expect(token
, ')', "after attribute");
494 token
= expect(token
, ')', "after attribute");
498 struct symbol
* ctype_integer(unsigned long spec
)
500 static struct symbol
*const integer_ctypes
[][3] = {
501 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
502 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
503 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
504 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
505 { &int_ctype
, &sint_ctype
, &uint_ctype
},
507 struct symbol
*const (*ctype
)[3];
510 ctype
= integer_ctypes
;
511 if (!(spec
& MOD_LONGLONG
)) {
513 if (!(spec
& MOD_LONG
)) {
515 if (!(spec
& MOD_SHORT
)) {
517 if (!(spec
& MOD_CHAR
))
523 sub
= ((spec
& MOD_UNSIGNED
)
525 : ((spec
& MOD_EXPLICITLY_SIGNED
)
529 return ctype
[0][sub
];
532 struct symbol
* ctype_fp(unsigned long spec
)
534 if (spec
& MOD_LONGLONG
)
535 return &ldouble_ctype
;
537 return &double_ctype
;
541 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
543 unsigned long mod
= thistype
->modifiers
;
546 unsigned long old
= ctype
->modifiers
;
547 unsigned long extra
= 0, dup
, conflict
;
549 if (mod
& old
& MOD_LONG
) {
550 extra
= MOD_LONGLONG
| MOD_LONG
;
554 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
556 warning(pos
, "Just how %sdo you want this type to be?",
557 modifier_string(dup
));
559 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
561 warning(pos
, "You cannot have both long and short modifiers.");
563 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
565 warning(pos
, "You cannot have both signed and unsigned modifiers.");
567 // Only one storage modifier allowed, except that "inline" doesn't count.
568 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
569 conflict
&= (conflict
- 1);
571 warning(pos
, "multiple storage classes");
573 ctype
->modifiers
= old
| mod
| extra
;
576 /* Context mask and value */
577 ctype
->in_context
+= thistype
->in_context
;
578 ctype
->out_context
+= thistype
->out_context
;
581 if (thistype
->alignment
& (thistype
->alignment
-1)) {
582 warning(pos
, "I don't like non-power-of-2 alignments");
583 thistype
->alignment
= 0;
585 if (thistype
->alignment
> ctype
->alignment
)
586 ctype
->alignment
= thistype
->alignment
;
589 ctype
->as
= thistype
->as
;
592 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
594 unsigned long banned
, wrong
;
595 unsigned long this_mod
= s
->ctype
.modifiers
;
596 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
597 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
599 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
600 banned
= BANNED_SIZE
| BANNED_SIGN
;
601 else if (this_mod
& MOD_SPECIALBITS
)
603 else if (s
->ctype
.base_type
== &fp_type
)
604 banned
= BANNED_SIGN
;
605 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
611 // vector_type <-- whatever that is
612 banned
= BANNED_SIZE
| BANNED_SIGN
;
615 wrong
= mod
& banned
;
617 warning(*pos
, "modifier %sis invalid in this context",
618 modifier_string (wrong
));
622 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
626 while ( (token
= next
) != NULL
) {
627 struct ctype thistype
;
629 struct symbol
*s
, *type
;
633 if (token_type(token
) != TOKEN_IDENT
)
635 ident
= token
->ident
;
637 s
= lookup_symbol(ident
, NS_TYPEDEF
);
641 mod
= thistype
.modifiers
;
642 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
644 if (mod
& MOD_SPECIALBITS
) {
645 if (mod
& MOD_STRUCTOF
)
646 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
647 else if (mod
& MOD_UNIONOF
)
648 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
649 else if (mod
& MOD_ENUMOF
)
650 next
= enum_specifier(next
, &thistype
);
651 else if (mod
& MOD_ATTRIBUTE
)
652 next
= attribute_specifier(next
, &thistype
);
653 else if (mod
& MOD_TYPEOF
)
654 next
= typeof_specifier(next
, &thistype
);
655 mod
= thistype
.modifiers
;
657 type
= thistype
.base_type
;
661 if (ctype
->base_type
)
663 /* User types only mix with qualifiers */
664 if (mod
& MOD_USERTYPE
) {
665 if (ctype
->modifiers
& MOD_SPECIFIER
)
668 ctype
->base_type
= type
;
671 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
672 apply_ctype(token
->pos
, &thistype
, ctype
);
675 /* Turn the "virtual types" into real types with real sizes etc */
676 if (!ctype
->base_type
) {
677 struct symbol
*base
= &incomplete_ctype
;
680 * If we have modifiers, we'll default to an integer
681 * type, and "ctype_integer()" will turn this into
684 if (ctype
->modifiers
& MOD_SPECIFIER
)
686 ctype
->base_type
= base
;
688 if (ctype
->base_type
== &int_type
) {
689 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
690 ctype
->modifiers
&= ~MOD_SPECIFIER
;
691 } else if (ctype
->base_type
== &fp_type
) {
692 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
693 ctype
->modifiers
&= ~MOD_SPECIFIER
;
695 if (ctype
->modifiers
& MOD_BITWISE
) {
697 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
698 if (!is_int_type(ctype
->base_type
)) {
699 warning(token
->pos
, "invalid modifier");
702 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
703 *type
= *ctype
->base_type
;
704 type
->ctype
.base_type
= ctype
->base_type
;
705 type
->type
= SYM_RESTRICT
;
706 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
707 ctype
->base_type
= type
;
712 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
714 struct expression
*expr
= NULL
;
716 token
= parse_expression(token
, &expr
);
717 sym
->array_size
= expr
;
721 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
722 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
724 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
727 if (token_type(token
) != TOKEN_IDENT
)
729 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
730 struct ctype thistype
= { 0, };
731 token
= attribute_specifier(token
->next
, &thistype
);
732 apply_ctype(token
->pos
, &thistype
, ctype
);
735 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
736 struct expression
*expr
;
737 token
= expect(token
->next
, '(', "after asm");
738 token
= parse_expression(token
->next
, &expr
);
739 token
= expect(token
, ')', "after asm");
747 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
749 struct ctype
*ctype
= &decl
->ctype
;
751 if (p
&& token_type(token
) == TOKEN_IDENT
) {
757 token
= handle_attributes(token
, ctype
);
759 if (token_type(token
) != TOKEN_SPECIAL
)
763 * This can be either a parameter list or a grouping.
764 * For the direct (non-abstract) case, we know if must be
765 * a parameter list if we already saw the identifier.
766 * For the abstract case, we know if must be a parameter
767 * list if it is empty or starts with a type.
769 if (token
->special
== '(') {
771 struct token
*next
= token
->next
;
772 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
775 struct symbol
*base_type
= ctype
->base_type
;
776 token
= declarator(next
, decl
, p
);
777 token
= expect(token
, ')', "in nested declarator");
778 while (ctype
->base_type
!= base_type
)
779 ctype
= &ctype
->base_type
->ctype
;
784 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
785 token
= parameter_type_list(next
, sym
);
786 token
= expect(token
, ')', "in function declarator");
789 if (token
->special
== '[') {
790 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
791 token
= abstract_array_declarator(token
->next
, array
);
792 token
= expect(token
, ']', "in abstract_array_declarator");
793 ctype
= &array
->ctype
;
801 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
803 unsigned long modifiers
;
804 struct symbol
*base_type
;
806 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
807 base_type
= ctype
->base_type
;
808 ctype
->modifiers
= modifiers
;
810 while (match_op(token
,'*')) {
811 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
812 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
813 ptr
->ctype
.as
= ctype
->as
;
814 ptr
->ctype
.in_context
+= ctype
->in_context
;
815 ptr
->ctype
.out_context
+= ctype
->out_context
;
816 ptr
->ctype
.base_type
= base_type
;
819 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
820 ctype
->base_type
= base_type
;
822 ctype
->in_context
= 0;
823 ctype
->out_context
= 0;
825 token
= declaration_specifiers(token
->next
, ctype
, 1);
826 modifiers
= ctype
->modifiers
;
831 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
833 token
= pointer(token
, &sym
->ctype
);
834 return direct_declarator(token
, sym
, p
);
837 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
839 struct ctype
*ctype
= &decl
->ctype
;
840 struct expression
*expr
;
841 struct symbol
*bitfield
;
844 if (!is_int_type(ctype
->base_type
)) {
845 warning(token
->pos
, "invalid bitfield specifier for type %s.",
846 show_typename(ctype
->base_type
));
847 // Parse this to recover gracefully.
848 return conditional_expression(token
->next
, &expr
);
851 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
852 token
= conditional_expression(token
->next
, &expr
);
853 width
= get_expression_value(expr
);
854 bitfield
->bit_size
= width
;
856 if (width
< 0 || width
> INT_MAX
) {
857 warning(token
->pos
, "invalid bitfield width, %lld.", width
);
859 } else if (decl
->ident
&& width
== 0) {
860 warning(token
->pos
, "invalid named zero-width bitfield `%s'",
861 show_ident(decl
->ident
));
863 } else if (decl
->ident
) {
864 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
865 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
866 if (width
== 1 && is_signed
) {
867 // Valid values are either {-1;0} or {0}, depending on integer
868 // representation. The latter makes for very efficient code...
869 warning(token
->pos
, "dubious one-bit signed bitfield");
871 if (Wdefault_bitfield_sign
&&
872 base_type
->type
!= SYM_ENUM
&&
873 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
875 // The sign of bitfields is unspecified by default.
876 warning (token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
879 bitfield
->bit_size
= width
;
883 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
885 while (!match_op(token
, '}')) {
886 struct ctype ctype
= {0, };
888 token
= declaration_specifiers(token
, &ctype
, 0);
890 struct ident
*ident
= NULL
;
891 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
893 token
= declarator(token
, decl
, &ident
);
895 if (match_op(token
, ':')) {
896 token
= handle_bitfield(token
, decl
);
897 token
= handle_attributes(token
, &decl
->ctype
);
899 add_symbol(list
, decl
);
900 if (!match_op(token
, ','))
904 if (!match_op(token
, ';')) {
905 warning(token
->pos
, "expected ; at end of declaration");
913 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
915 struct ident
*ident
= NULL
;
917 struct ctype ctype
= { 0, };
919 token
= declaration_specifiers(token
, &ctype
, 0);
920 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
923 token
= declarator(token
, sym
, &ident
);
928 struct token
*typename(struct token
*token
, struct symbol
**p
)
930 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
932 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
933 return declarator(token
, sym
, NULL
);
936 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
938 token
= parse_expression(token
, tree
);
939 return expect(token
, ';', "at end of statement");
942 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
943 struct expression_list
**inout
)
945 struct expression
*expr
;
947 /* Allow empty operands */
948 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
951 struct ident
*ident
= NULL
;
952 if (match_op(token
->next
, '[') &&
953 token_type(token
->next
->next
) == TOKEN_IDENT
&&
954 match_op(token
->next
->next
->next
, ']')) {
955 ident
= token
->next
->next
->ident
;
956 token
= token
->next
->next
->next
;
958 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
959 token
= primary_expression(token
->next
, &expr
);
960 add_expression(inout
, expr
);
961 token
= parens_expression(token
, &expr
, "in asm parameter");
962 add_expression(inout
, expr
);
963 } while (match_op(token
, ','));
967 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
968 struct expression_list
**clobbers
)
970 struct expression
*expr
;
973 token
= primary_expression(token
->next
, &expr
);
974 add_expression(clobbers
, expr
);
975 } while (match_op(token
, ','));
979 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
981 stmt
->type
= STMT_ASM
;
982 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
985 token
= expect(token
, '(', "after asm");
986 token
= parse_expression(token
, &stmt
->asm_string
);
987 if (match_op(token
, ':'))
988 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
989 if (match_op(token
, ':'))
990 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
991 if (match_op(token
, ':'))
992 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
993 token
= expect(token
, ')', "after asm");
994 return expect(token
, ';', "at end of asm-statement");
997 /* Make a statement out of an expression */
998 static struct statement
*make_statement(struct expression
*expr
)
1000 struct statement
*stmt
;
1004 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1005 stmt
->expression
= expr
;
1010 * All iterators have two symbols associated with them:
1011 * the "continue" and "break" symbols, which are targets
1012 * for continue and break statements respectively.
1014 * They are in a special name-space, but they follow
1015 * all the normal visibility rules, so nested iterators
1016 * automatically work right.
1018 static void start_iterator(struct statement
*stmt
)
1020 struct symbol
*cont
, *brk
;
1022 start_symbol_scope();
1023 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1024 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1025 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1026 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1028 stmt
->type
= STMT_ITERATOR
;
1029 stmt
->iterator_break
= brk
;
1030 stmt
->iterator_continue
= cont
;
1031 fn_local_symbol(brk
);
1032 fn_local_symbol(cont
);
1035 static void end_iterator(struct statement
*stmt
)
1040 static struct statement
*start_function(struct symbol
*sym
)
1043 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1045 start_function_scope();
1046 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1047 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1048 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1049 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1050 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1052 fn_local_symbol(ret
);
1054 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1060 static void end_function(struct symbol
*sym
)
1063 end_function_scope();
1067 * A "switch()" statement, like an iterator, has a
1068 * the "break" symbol associated with it. It works
1069 * exactly like the iterator break - it's the target
1070 * for any break-statements in scope, and means that
1071 * "break" handling doesn't even need to know whether
1072 * it's breaking out of an iterator or a switch.
1074 * In addition, the "case" symbol is a marker for the
1075 * case/default statements to find the switch statement
1076 * that they are associated with.
1078 static void start_switch(struct statement
*stmt
)
1080 struct symbol
*brk
, *switch_case
;
1082 start_symbol_scope();
1083 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1084 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1086 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1087 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1088 switch_case
->stmt
= stmt
;
1090 stmt
->type
= STMT_SWITCH
;
1091 stmt
->switch_break
= brk
;
1092 stmt
->switch_case
= switch_case
;
1094 fn_local_symbol(brk
);
1095 fn_local_symbol(switch_case
);
1098 static void end_switch(struct statement
*stmt
)
1100 if (!stmt
->switch_case
->symbol_list
)
1101 warning(stmt
->pos
, "switch with no cases");
1105 static void add_case_statement(struct statement
*stmt
)
1107 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1111 warning(stmt
->pos
, "not in switch scope");
1112 stmt
->type
= STMT_NONE
;
1115 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1116 add_symbol(&target
->symbol_list
, sym
);
1118 stmt
->case_label
= sym
;
1119 fn_local_symbol(sym
);
1122 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1124 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1127 error_die(token
->pos
, "internal error: return without a function target");
1128 stmt
->type
= STMT_RETURN
;
1129 stmt
->ret_target
= target
;
1130 return expression_statement(token
->next
, &stmt
->ret_value
);
1133 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1135 struct symbol_list
*syms
;
1136 struct expression
*e1
, *e2
, *e3
;
1137 struct statement
*iterator
;
1139 start_iterator(stmt
);
1140 token
= expect(token
->next
, '(', "after 'for'");
1144 /* C99 variable declaration? */
1145 if (lookup_type(token
)) {
1146 token
= external_declaration(token
, &syms
);
1148 token
= parse_expression(token
, &e1
);
1149 token
= expect(token
, ';', "in 'for'");
1151 token
= parse_expression(token
, &e2
);
1152 token
= expect(token
, ';', "in 'for'");
1153 token
= parse_expression(token
, &e3
);
1154 token
= expect(token
, ')', "in 'for'");
1155 token
= statement(token
, &iterator
);
1157 stmt
->iterator_syms
= syms
;
1158 stmt
->iterator_pre_statement
= make_statement(e1
);
1159 stmt
->iterator_pre_condition
= e2
;
1160 stmt
->iterator_post_statement
= make_statement(e3
);
1161 stmt
->iterator_post_condition
= e2
;
1162 stmt
->iterator_statement
= iterator
;
1168 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1170 struct expression
*expr
;
1171 struct statement
*iterator
;
1173 start_iterator(stmt
);
1174 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1175 token
= statement(token
, &iterator
);
1177 stmt
->iterator_pre_condition
= expr
;
1178 stmt
->iterator_post_condition
= expr
;
1179 stmt
->iterator_statement
= iterator
;
1185 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1187 struct expression
*expr
;
1188 struct statement
*iterator
;
1190 start_iterator(stmt
);
1191 token
= statement(token
->next
, &iterator
);
1192 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1193 token
= token
->next
;
1195 warning(token
->pos
, "expected 'while' after 'do'");
1196 token
= parens_expression(token
, &expr
, "after 'do-while'");
1198 stmt
->iterator_post_condition
= expr
;
1199 stmt
->iterator_statement
= iterator
;
1202 return expect(token
, ';', "after statement");
1205 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1207 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1210 if (token_type(token
) == TOKEN_IDENT
) {
1211 if (token
->ident
== &if_ident
) {
1212 stmt
->type
= STMT_IF
;
1213 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1214 token
= statement(token
, &stmt
->if_true
);
1215 if (token_type(token
) != TOKEN_IDENT
)
1217 if (token
->ident
!= &else_ident
)
1219 return statement(token
->next
, &stmt
->if_false
);
1222 if (token
->ident
== &return_ident
)
1223 return parse_return_statement(token
, stmt
);
1225 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1226 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1227 stmt
->type
= STMT_GOTO
;
1228 stmt
->goto_label
= target
;
1230 warning(stmt
->pos
, "break/continue not in iterator scope");
1231 return expect(token
->next
, ';', "at end of statement");
1233 if (token
->ident
== &default_ident
) {
1234 token
= token
->next
;
1235 goto default_statement
;
1237 if (token
->ident
== &case_ident
) {
1238 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1239 if (match_op(token
, SPECIAL_ELLIPSIS
))
1240 token
= parse_expression(token
->next
, &stmt
->case_to
);
1242 stmt
->type
= STMT_CASE
;
1243 token
= expect(token
, ':', "after default/case");
1244 add_case_statement(stmt
);
1245 return statement(token
, &stmt
->case_statement
);
1247 if (token
->ident
== &switch_ident
) {
1248 stmt
->type
= STMT_SWITCH
;
1250 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1251 token
= statement(token
, &stmt
->switch_statement
);
1255 if (token
->ident
== &for_ident
)
1256 return parse_for_statement(token
, stmt
);
1258 if (token
->ident
== &while_ident
)
1259 return parse_while_statement(token
, stmt
);
1261 if (token
->ident
== &do_ident
)
1262 return parse_do_statement(token
, stmt
);
1264 if (token
->ident
== &goto_ident
) {
1265 stmt
->type
= STMT_GOTO
;
1266 token
= token
->next
;
1267 if (match_op(token
, '*')) {
1268 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1269 add_statement(&function_computed_goto_list
, stmt
);
1270 } else if (token_type(token
) == TOKEN_IDENT
) {
1271 stmt
->goto_label
= label_symbol(token
);
1272 token
= token
->next
;
1274 warning(token
->pos
, "Expected identifier or goto expression");
1276 return expect(token
, ';', "at end of statement");
1278 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1279 return parse_asm(token
->next
, stmt
);
1281 if (token
->ident
== &__context___ident
) {
1282 stmt
->type
= STMT_CONTEXT
;
1283 token
= parse_expression(token
->next
, &stmt
->expression
);
1284 return expect(token
, ';', "at end of statement");
1286 if (token
->ident
== &__range___ident
) {
1287 stmt
->type
= STMT_RANGE
;
1288 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1289 token
= expect(token
, ',', "after range expression");
1290 token
= assignment_expression(token
, &stmt
->range_low
);
1291 token
= expect(token
, ',', "after low range");
1292 token
= assignment_expression(token
, &stmt
->range_high
);
1293 return expect(token
, ';', "after range statement");
1295 if (match_op(token
->next
, ':')) {
1296 stmt
->type
= STMT_LABEL
;
1297 stmt
->label_identifier
= label_symbol(token
);
1298 return statement(token
->next
->next
, &stmt
->label_statement
);
1302 if (match_op(token
, '{')) {
1303 stmt
->type
= STMT_COMPOUND
;
1304 start_symbol_scope();
1305 token
= compound_statement(token
->next
, stmt
);
1308 return expect(token
, '}', "at end of compound statement");
1311 stmt
->type
= STMT_EXPRESSION
;
1312 return expression_statement(token
, &stmt
->expression
);
1315 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1318 struct statement
* stmt
;
1319 if (eof_token(token
))
1321 if (match_op(token
, '}'))
1323 if (lookup_type(token
)) {
1324 if (warn_on_mixed
&& *list
)
1325 warning(token
->pos
, "mixing declarations and code");
1326 token
= external_declaration(token
, syms
);
1329 token
= statement(token
, &stmt
);
1330 add_statement(list
, stmt
);
1335 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1337 struct symbol_list
**list
= &fn
->arguments
;
1339 if (match_op(token
, ')')) {
1340 // No warning for "void oink ();"
1341 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1342 if (!match_op(token
->next
, ';'))
1343 warning(token
->pos
, "non-ANSI function declaration");
1350 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1352 warning(token
->pos
, "variadic functions must have one named argument");
1354 token
= token
->next
;
1358 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1359 token
= parameter_declaration(token
, &sym
);
1360 if (sym
->ctype
.base_type
== &void_ctype
) {
1361 /* Special case: (void) */
1362 if (!*list
&& !sym
->ident
)
1364 warning(token
->pos
, "void parameter");
1366 add_symbol(list
, sym
);
1367 if (!match_op(token
, ','))
1369 token
= token
->next
;
1375 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1377 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1381 static struct expression
*identifier_expression(struct token
*token
)
1383 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1384 expr
->expr_ident
= token
->ident
;
1388 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1390 int idx_from
, idx_to
;
1391 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1393 idx_from
= get_expression_value(from
);
1396 idx_to
= get_expression_value(to
);
1397 if (idx_to
< idx_from
|| idx_from
< 0)
1398 warning(from
->pos
, "nonsense array initializer index range");
1400 expr
->idx_from
= idx_from
;
1401 expr
->idx_to
= idx_to
;
1405 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1407 int expect_equal
= 0;
1408 struct token
*next
= token
->next
;
1409 struct expression
**tail
= ep
;
1414 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1415 struct expression
*expr
= identifier_expression(token
);
1416 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1417 token
= initializer(&expr
->ident_expression
, next
->next
);
1418 if (expr
->ident_expression
)
1423 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1424 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1425 struct expression
*expr
= identifier_expression(next
);
1427 tail
= &expr
->ident_expression
;
1430 } else if (match_op(token
, '[')) {
1431 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1432 token
= constant_expression(token
->next
, &from
);
1433 if (match_op(token
, SPECIAL_ELLIPSIS
))
1434 token
= constant_expression(token
->next
, &to
);
1435 expr
= index_expression(from
, to
);
1437 tail
= &expr
->idx_expression
;
1438 token
= expect(token
, ']', "at end of initializer index");
1445 if (nested
&& !expect_equal
) {
1446 if (!match_op(token
, '='))
1447 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1452 token
= expect(token
, '=', "at end of initializer index");
1454 token
= initializer(tail
, token
);
1460 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1462 struct expression
*expr
;
1465 token
= single_initializer(&expr
, token
);
1468 add_expression(list
, expr
);
1469 if (!match_op(token
, ','))
1471 token
= token
->next
;
1476 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1478 if (match_op(token
, '{')) {
1479 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1481 token
= initializer_list(&expr
->expr_list
, token
->next
);
1482 return expect(token
, '}', "at end of initializer");
1484 return assignment_expression(token
, tree
);
1487 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1490 warning(sym
->pos
, "no identifier for function argument");
1493 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1496 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1497 struct symbol_list
**list
)
1499 struct symbol_list
**old_symbol_list
;
1500 struct symbol
*base_type
= decl
->ctype
.base_type
;
1501 struct statement
*stmt
, **p
;
1504 old_symbol_list
= function_symbol_list
;
1505 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1506 function_symbol_list
= &decl
->inline_symbol_list
;
1507 p
= &base_type
->inline_stmt
;
1509 function_symbol_list
= &decl
->symbol_list
;
1510 p
= &base_type
->stmt
;
1512 function_computed_target_list
= NULL
;
1513 function_computed_goto_list
= NULL
;
1515 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1516 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1517 warning(decl
->pos
, "function with external linkage has definition");
1519 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1520 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1522 stmt
= start_function(decl
);
1525 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1526 declare_argument(arg
, base_type
);
1527 } END_FOR_EACH_PTR(arg
);
1529 token
= compound_statement(token
->next
, stmt
);
1532 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1533 add_symbol(list
, decl
);
1534 check_declaration(decl
);
1535 function_symbol_list
= old_symbol_list
;
1536 if (function_computed_goto_list
) {
1537 if (!function_computed_target_list
)
1538 warning(decl
->pos
, "function has computed goto but no targets?");
1540 struct statement
*stmt
;
1541 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1542 stmt
->target_list
= function_computed_target_list
;
1543 } END_FOR_EACH_PTR(stmt
);
1546 return expect(token
, '}', "at end of function");
1549 static void promote_k_r_types(struct symbol
*arg
)
1551 struct symbol
*base
= arg
->ctype
.base_type
;
1552 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1553 arg
->ctype
.base_type
= &int_ctype
;
1557 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1559 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1562 FOR_EACH_PTR(real_args
, arg
) {
1563 struct symbol
*type
;
1565 /* This is quadratic in the number of arguments. We _really_ don't care */
1566 FOR_EACH_PTR(argtypes
, type
) {
1567 if (type
->ident
== arg
->ident
)
1569 } END_FOR_EACH_PTR(type
);
1570 warning(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1574 /* "char" and "short" promote to "int" */
1575 promote_k_r_types(type
);
1577 arg
->ctype
= type
->ctype
;
1578 } END_FOR_EACH_PTR(arg
);
1580 FOR_EACH_PTR(argtypes
, arg
) {
1582 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1583 } END_FOR_EACH_PTR(arg
);
1587 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1588 struct symbol_list
**list
)
1590 struct symbol_list
*args
= NULL
;
1592 warning(token
->pos
, "non-ANSI function declaration");
1594 token
= external_declaration(token
, &args
);
1595 } while (lookup_type(token
));
1597 apply_k_r_types(args
, decl
);
1599 if (!match_op(token
, '{')) {
1600 warning(token
->pos
, "expected function body");
1603 return parse_function_body(token
, decl
, list
);
1607 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1609 struct ident
*ident
= NULL
;
1610 struct symbol
*decl
;
1611 struct ctype ctype
= { 0, };
1612 struct symbol
*base_type
;
1615 /* Top-level inline asm? */
1616 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1617 struct symbol_list
**old_symbol_list
;
1618 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1619 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1620 struct statement
*stmt
;
1622 anon
->ctype
.base_type
= fn
;
1623 old_symbol_list
= function_symbol_list
;
1624 function_symbol_list
= &anon
->symbol_list
;
1625 stmt
= start_function(anon
);
1626 token
= parse_asm(token
->next
, stmt
);
1628 function_symbol_list
= old_symbol_list
;
1629 add_symbol(list
, anon
);
1633 /* Parse declaration-specifiers, if any */
1634 token
= declaration_specifiers(token
, &ctype
, 0);
1635 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1636 decl
->ctype
= ctype
;
1637 token
= declarator(token
, decl
, &ident
);
1639 /* Just a type declaration? */
1641 return expect(token
, ';', "end of type declaration");
1643 /* type define declaration? */
1644 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1646 /* Typedef's don't have meaningful storage */
1648 ctype
.modifiers
&= ~MOD_STORAGE
;
1649 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1650 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1653 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1655 base_type
= decl
->ctype
.base_type
;
1656 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1657 /* K&R argument declaration? */
1658 if (lookup_type(token
))
1659 return parse_k_r_arguments(token
, decl
, list
);
1660 if (match_op(token
, '{'))
1661 return parse_function_body(token
, decl
, list
);
1663 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1664 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1665 } else if (!is_typedef
&& base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1666 warning(token
->pos
, "void declaration");
1670 if (!is_typedef
&& match_op(token
, '=')) {
1671 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1672 warning(decl
->pos
, "symbol with external linkage has initializer");
1673 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1675 token
= initializer(&decl
->initializer
, token
->next
);
1678 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1679 add_symbol(list
, decl
);
1680 fn_local_symbol(decl
);
1683 check_declaration(decl
);
1685 if (!match_op(token
, ','))
1688 token
= token
->next
;
1690 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1691 decl
->ctype
= ctype
;
1692 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1693 token
= declarator(token
, decl
, &ident
);
1695 warning(token
->pos
, "expected identifier name in type definition");
1699 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1701 /* Function declarations are automatically extern unless specifically static */
1702 base_type
= decl
->ctype
.base_type
;
1703 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1704 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1705 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1708 return expect(token
, ';', "at end of declaration");
1711 struct symbol_list
*translation_unit(struct token
*token
)
1713 while (!eof_token(token
))
1714 token
= external_declaration(token
, &translation_unit_used_list
);
1715 // They aren't needed any more
1716 clear_token_alloc();
1718 /* Evaluate the symbol list */
1719 evaluate_symbol_list(translation_unit_used_list
);
1720 return translation_unit_used_list
;