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
26 #include "expression.h"
29 #define warn_on_mixed (1)
31 static struct symbol_list
**function_symbol_list
;
32 struct symbol_list
*function_computed_target_list
;
33 struct statement_list
*function_computed_goto_list
;
35 // Add a symbol to the list of function-local symbols
36 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
38 static struct token
*statement(struct token
*token
, struct statement
**tree
);
39 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
41 static int match_idents(struct token
*token
, ...)
45 if (token_type(token
) != TOKEN_IDENT
)
48 va_start(args
, token
);
50 struct ident
* next
= va_arg(args
, struct ident
*);
53 if (token
->ident
== next
)
59 struct statement
*alloc_statement(struct position pos
, int type
)
61 struct statement
*stmt
= __alloc_statement(0);
67 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
69 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
71 struct symbol
*sym
= alloc_symbol(pos
, type
);
73 sym
->ctype
.base_type
= ctype
->base_type
;
74 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
76 ctype
->base_type
= sym
;
77 ctype
->modifiers
&= MOD_STORAGE
;
81 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
83 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
85 sym
= alloc_symbol(token
->pos
, type
);
86 bind_symbol(sym
, token
->ident
, ns
);
87 if (type
== SYM_LABEL
)
94 * NOTE! NS_LABEL is not just a different namespace,
95 * it also ends up using function scope instead of the
96 * regular symbol scope.
98 struct symbol
*label_symbol(struct token
*token
)
100 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
103 struct token
*struct_union_enum_specifier(enum type type
,
104 struct token
*token
, struct ctype
*ctype
,
105 struct token
*(*parse
)(struct token
*, struct symbol
*))
109 ctype
->modifiers
= 0;
110 if (token_type(token
) == TOKEN_IDENT
) {
111 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
113 (sym
->scope
!= block_scope
&&
114 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
115 // Either a new symbol, or else an out-of-scope
116 // symbol being redefined.
117 sym
= alloc_symbol(token
->pos
, type
);
118 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
120 if (sym
->type
!= type
)
121 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
123 ctype
->base_type
= sym
;
124 if (match_op(token
, '{')) {
125 // The following test is actually wrong for empty
126 // structs, but (1) they are not C99, (2) gcc does
127 // the same thing, and (3) it's easier.
128 if (sym
->symbol_list
)
129 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
130 token
= parse(token
->next
, sym
);
131 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
136 // private struct/union/enum type
137 if (!match_op(token
, '{')) {
138 warning(token
->pos
, "expected declaration");
139 ctype
->base_type
= &bad_ctype
;
143 sym
= alloc_symbol(token
->pos
, type
);
144 token
= parse(token
->next
, sym
);
145 ctype
->base_type
= sym
;
146 return expect(token
, '}', "at end of specifier");
149 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
151 return struct_declaration_list(token
, &sym
->symbol_list
);
154 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
156 return struct_union_enum_specifier(type
, token
, ctype
, parse_struct_declaration
);
161 unsigned long long y
;
164 static void upper_boundary(Num
*n
, Num
*v
)
176 static void lower_boundary(Num
*n
, Num
*v
)
188 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
190 int shift
= type
->bit_size
;
191 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
195 if (upper
->x
== 0 && upper
->y
>> shift
)
197 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
202 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
204 unsigned long long lastval
= 0;
205 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
206 Num upper
= {-1, 0}, lower
= {1, 0};
208 while (token_type(token
) == TOKEN_IDENT
) {
209 struct token
*next
= token
->next
;
212 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
213 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
215 if (match_op(next
, '=')) {
216 struct expression
*expr
;
217 next
= constant_expression(next
->next
, &expr
);
218 lastval
= get_expression_value(expr
);
222 } else if (is_int_type(ctype
)) {
225 error_die(token
->pos
, "can't increment the last enum member");
228 sym
->value
= lastval
;
229 sym
->ctype
.base_type
= ctype
;
231 if (base_type
!= &bad_ctype
) {
232 if (ctype
->type
== SYM_NODE
)
233 ctype
= ctype
->ctype
.base_type
;
234 if (ctype
->type
== SYM_ENUM
)
235 ctype
= ctype
->ctype
.base_type
;
238 * - if all enum's are of the same type, then
239 * the base_type is that type (two first
241 * - if enums are of different types, they
242 * all have to be integer types, and the
243 * base type is "int_ctype".
244 * - otherwise the base_type is "bad_ctype".
248 } else if (ctype
== base_type
) {
250 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
251 base_type
= &int_ctype
;
253 base_type
= &bad_ctype
;
255 if (is_int_type(base_type
)) {
256 Num v
= {.y
= lastval
};
257 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
259 else if ((long long)lastval
>= 0)
263 upper_boundary(&upper
, &v
);
264 lower_boundary(&lower
, &v
);
267 if (!match_op(token
, ','))
272 base_type
= &bad_ctype
;
273 else if (!is_int_type(base_type
))
274 base_type
= base_type
;
275 else if (type_is_ok(base_type
, &upper
, &lower
))
276 base_type
= base_type
;
277 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
278 base_type
= &int_ctype
;
279 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
280 base_type
= &uint_ctype
;
281 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
282 base_type
= &long_ctype
;
283 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
284 base_type
= &ulong_ctype
;
285 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
286 base_type
= &llong_ctype
;
287 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
288 base_type
= &ullong_ctype
;
290 base_type
= &bad_ctype
;
291 parent
->ctype
.base_type
= base_type
;
292 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
296 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
298 return struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
301 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
305 if (!match_op(token
, '(')) {
306 warning(token
->pos
, "expected '(' after typeof");
309 if (lookup_type(token
->next
)) {
310 token
= typename(token
->next
, &sym
);
313 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
314 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
316 ctype
->modifiers
= 0;
317 ctype
->base_type
= typeof_sym
;
319 return expect(token
, ')', "after typeof");
322 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
324 if (attribute
== &packed_ident
||
325 attribute
== &__packed___ident
) {
326 ctype
->alignment
= 1;
329 if (attribute
== &aligned_ident
||
330 attribute
== &__aligned___ident
) {
331 int alignment
= max_alignment
;
333 alignment
= get_expression_value(expr
);
334 ctype
->alignment
= alignment
;
337 if (attribute
== &nocast_ident
) {
338 ctype
->modifiers
|= MOD_NOCAST
;
341 if (attribute
== &noderef_ident
) {
342 ctype
->modifiers
|= MOD_NODEREF
;
345 if (attribute
== &safe_ident
) {
346 ctype
->modifiers
|= MOD_SAFE
;
349 if (attribute
== &force_ident
) {
350 ctype
->modifiers
|= MOD_FORCE
;
353 if (attribute
== &bitwise_ident
) {
355 ctype
->modifiers
|= MOD_BITWISE
;
358 if (attribute
== &address_space_ident
) {
360 return "expected address space number";
361 ctype
->as
= get_expression_value(expr
);
364 if (attribute
== &context_ident
) {
365 if (expr
&& expr
->type
== EXPR_COMMA
) {
366 int input
= get_expression_value(expr
->left
);
367 int output
= get_expression_value(expr
->right
);
368 ctype
->in_context
= input
;
369 ctype
->out_context
= output
;
372 return "expected context input/output values";
374 if (attribute
== &mode_ident
||
375 attribute
== &__mode___ident
) {
376 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
377 struct ident
*ident
= expr
->symbol_name
;
380 * Match against __QI__/__HI__/__SI__/__DI__
382 * FIXME! This is broken - we don't actually get
383 * the type information updated properly at this
384 * stage for some reason.
386 if (ident
== &__QI___ident
||
387 ident
== &QI_ident
) {
388 ctype
->modifiers
|= MOD_CHAR
;
389 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
392 if (ident
== &__HI___ident
||
393 ident
== &HI_ident
) {
394 ctype
->modifiers
|= MOD_SHORT
;
395 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
398 if (ident
== &__SI___ident
||
399 ident
== &SI_ident
) {
403 if (ident
== &__DI___ident
||
404 ident
== &DI_ident
) {
405 ctype
->modifiers
|= MOD_LONGLONG
;
406 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
409 if (ident
== &__word___ident
||
410 ident
== &word_ident
) {
411 ctype
->modifiers
|= MOD_LONG
;
412 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
415 return "unknown mode attribute";
417 return "expected attribute mode symbol";
420 /* Throw away for now.. */
421 if (attribute
== &format_ident
||
422 attribute
== &__format___ident
)
424 if (attribute
== §ion_ident
||
425 attribute
== &__section___ident
)
427 if (attribute
== &unused_ident
||
428 attribute
== &__unused___ident
)
430 if (attribute
== &const_ident
||
431 attribute
== &__const_ident
||
432 attribute
== &__const___ident
)
434 if (attribute
== &noreturn_ident
||
435 attribute
== &__noreturn___ident
)
437 if (attribute
== ®parm_ident
)
439 if (attribute
== &weak_ident
)
441 if (attribute
== &alias_ident
)
443 if (attribute
== &pure_ident
)
445 if (attribute
== &always_inline_ident
)
447 if (attribute
== &syscall_linkage_ident
)
449 if (attribute
== &visibility_ident
)
451 if (attribute
== &model_ident
||
452 attribute
== &__model___ident
)
455 return "unknown attribute";
458 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
460 ctype
->modifiers
= 0;
461 token
= expect(token
, '(', "after attribute");
462 token
= expect(token
, '(', "after attribute");
466 struct ident
*attribute_name
;
467 struct expression
*attribute_expr
;
469 if (eof_token(token
))
471 if (match_op(token
, ';'))
473 if (token_type(token
) != TOKEN_IDENT
)
475 attribute_name
= token
->ident
;
477 attribute_expr
= NULL
;
478 if (match_op(token
, '('))
479 token
= parens_expression(token
, &attribute_expr
, "in attribute");
480 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
482 warning(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
483 if (!match_op(token
, ','))
488 token
= expect(token
, ')', "after attribute");
489 token
= expect(token
, ')', "after attribute");
493 struct symbol
* ctype_integer(unsigned long spec
)
495 static struct symbol
*const integer_ctypes
[][3] = {
496 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
497 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
498 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
499 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
500 { &int_ctype
, &sint_ctype
, &uint_ctype
},
502 struct symbol
*const (*ctype
)[3];
505 ctype
= integer_ctypes
;
506 if (!(spec
& MOD_LONGLONG
)) {
508 if (!(spec
& MOD_LONG
)) {
510 if (!(spec
& MOD_SHORT
)) {
512 if (!(spec
& MOD_CHAR
))
518 sub
= ((spec
& MOD_UNSIGNED
)
520 : ((spec
& MOD_EXPLICITLY_SIGNED
)
524 return ctype
[0][sub
];
527 struct symbol
* ctype_fp(unsigned long spec
)
529 if (spec
& MOD_LONGLONG
)
530 return &ldouble_ctype
;
532 return &double_ctype
;
536 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
538 unsigned long mod
= thistype
->modifiers
;
541 unsigned long old
= ctype
->modifiers
;
542 unsigned long extra
= 0, dup
, conflict
;
544 if (mod
& old
& MOD_LONG
) {
545 extra
= MOD_LONGLONG
| MOD_LONG
;
549 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
551 warning(pos
, "Just how %sdo you want this type to be?",
552 modifier_string(dup
));
554 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
556 warning(pos
, "You cannot have both long and short modifiers.");
558 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
560 warning(pos
, "You cannot have both signed and unsigned modifiers.");
562 // Only one storage modifier allowed, except that "inline" doesn't count.
563 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
564 conflict
&= (conflict
- 1);
566 warning(pos
, "multiple storage classes");
568 ctype
->modifiers
= old
| mod
| extra
;
571 /* Context mask and value */
572 ctype
->in_context
+= thistype
->in_context
;
573 ctype
->out_context
+= thistype
->out_context
;
576 if (thistype
->alignment
& (thistype
->alignment
-1)) {
577 warning(pos
, "I don't like non-power-of-2 alignments");
578 thistype
->alignment
= 0;
580 if (thistype
->alignment
> ctype
->alignment
)
581 ctype
->alignment
= thistype
->alignment
;
584 ctype
->as
= thistype
->as
;
587 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
589 unsigned long banned
, wrong
;
590 unsigned long this_mod
= s
->ctype
.modifiers
;
591 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
592 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
594 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
595 banned
= BANNED_SIZE
| BANNED_SIGN
;
596 else if (this_mod
& MOD_SPECIALBITS
)
598 else if (s
->ctype
.base_type
== &fp_type
)
599 banned
= BANNED_SIGN
;
600 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
606 // vector_type <-- whatever that is
607 banned
= BANNED_SIZE
| BANNED_SIGN
;
610 wrong
= mod
& banned
;
612 warning(*pos
, "modifier %sis invalid in this context",
613 modifier_string (wrong
));
617 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
621 while ( (token
= next
) != NULL
) {
622 struct ctype thistype
;
624 struct symbol
*s
, *type
;
628 if (token_type(token
) != TOKEN_IDENT
)
630 ident
= token
->ident
;
632 s
= lookup_symbol(ident
, NS_TYPEDEF
);
636 mod
= thistype
.modifiers
;
637 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
639 if (mod
& MOD_SPECIALBITS
) {
640 if (mod
& MOD_STRUCTOF
)
641 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
642 else if (mod
& MOD_UNIONOF
)
643 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
644 else if (mod
& MOD_ENUMOF
)
645 next
= enum_specifier(next
, &thistype
);
646 else if (mod
& MOD_ATTRIBUTE
)
647 next
= attribute_specifier(next
, &thistype
);
648 else if (mod
& MOD_TYPEOF
)
649 next
= typeof_specifier(next
, &thistype
);
650 mod
= thistype
.modifiers
;
652 type
= thistype
.base_type
;
656 if (ctype
->base_type
)
658 /* User types only mix with qualifiers */
659 if (mod
& MOD_USERTYPE
) {
660 if (ctype
->modifiers
& MOD_SPECIFIER
)
663 ctype
->base_type
= type
;
666 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
667 apply_ctype(token
->pos
, &thistype
, ctype
);
670 /* Turn the "virtual types" into real types with real sizes etc */
671 if (!ctype
->base_type
) {
672 struct symbol
*base
= &incomplete_ctype
;
675 * If we have modifiers, we'll default to an integer
676 * type, and "ctype_integer()" will turn this into
679 if (ctype
->modifiers
& MOD_SPECIFIER
)
681 ctype
->base_type
= base
;
683 if (ctype
->base_type
== &int_type
) {
684 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
685 ctype
->modifiers
&= ~MOD_SPECIFIER
;
686 } else if (ctype
->base_type
== &fp_type
) {
687 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
688 ctype
->modifiers
&= ~MOD_SPECIFIER
;
690 if (ctype
->modifiers
& MOD_BITWISE
) {
692 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
693 if (!is_int_type(ctype
->base_type
)) {
694 warning(token
->pos
, "invalid modifier");
697 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
698 *type
= *ctype
->base_type
;
699 type
->ctype
.base_type
= ctype
->base_type
;
700 type
->type
= SYM_RESTRICT
;
701 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
702 ctype
->base_type
= type
;
707 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
709 struct expression
*expr
= NULL
;
711 token
= parse_expression(token
, &expr
);
712 sym
->array_size
= expr
;
716 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
717 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
719 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
722 if (token_type(token
) != TOKEN_IDENT
)
724 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
725 struct ctype thistype
= { 0, };
726 token
= attribute_specifier(token
->next
, &thistype
);
727 apply_ctype(token
->pos
, &thistype
, ctype
);
730 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
731 struct expression
*expr
;
732 token
= expect(token
->next
, '(', "after asm");
733 token
= parse_expression(token
->next
, &expr
);
734 token
= expect(token
, ')', "after asm");
742 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
744 struct ctype
*ctype
= &decl
->ctype
;
746 if (p
&& token_type(token
) == TOKEN_IDENT
) {
752 token
= handle_attributes(token
, ctype
);
754 if (token_type(token
) != TOKEN_SPECIAL
)
758 * This can be either a parameter list or a grouping.
759 * For the direct (non-abstract) case, we know if must be
760 * a parameter list if we already saw the identifier.
761 * For the abstract case, we know if must be a parameter
762 * list if it is empty or starts with a type.
764 if (token
->special
== '(') {
766 struct token
*next
= token
->next
;
767 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
770 struct symbol
*base_type
= ctype
->base_type
;
771 token
= declarator(next
, decl
, p
);
772 token
= expect(token
, ')', "in nested declarator");
773 while (ctype
->base_type
!= base_type
)
774 ctype
= &ctype
->base_type
->ctype
;
779 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
780 token
= parameter_type_list(next
, sym
);
781 token
= expect(token
, ')', "in function declarator");
784 if (token
->special
== '[') {
785 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
786 token
= abstract_array_declarator(token
->next
, array
);
787 token
= expect(token
, ']', "in abstract_array_declarator");
788 ctype
= &array
->ctype
;
796 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
798 unsigned long modifiers
;
799 struct symbol
*base_type
;
801 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
802 base_type
= ctype
->base_type
;
803 ctype
->modifiers
= modifiers
;
805 while (match_op(token
,'*')) {
806 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
807 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
808 ptr
->ctype
.as
= ctype
->as
;
809 ptr
->ctype
.in_context
+= ctype
->in_context
;
810 ptr
->ctype
.out_context
+= ctype
->out_context
;
811 ptr
->ctype
.base_type
= base_type
;
814 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
815 ctype
->base_type
= base_type
;
817 ctype
->in_context
= 0;
818 ctype
->out_context
= 0;
820 token
= declaration_specifiers(token
->next
, ctype
, 1);
821 modifiers
= ctype
->modifiers
;
826 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
828 token
= pointer(token
, &sym
->ctype
);
829 return direct_declarator(token
, sym
, p
);
832 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
834 struct ctype
*ctype
= &decl
->ctype
;
835 struct expression
*expr
;
836 struct symbol
*bitfield
;
839 if (!is_int_type(ctype
->base_type
)) {
840 warning(token
->pos
, "invalid bitfield specifier for type %s.",
841 show_typename(ctype
->base_type
));
842 // Parse this to recover gracefully.
843 return conditional_expression(token
->next
, &expr
);
846 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
847 token
= conditional_expression(token
->next
, &expr
);
848 width
= get_expression_value(expr
);
849 bitfield
->bit_size
= width
;
851 if (width
< 0 || width
> INT_MAX
) {
852 warning(token
->pos
, "invalid bitfield width, %lld.", width
);
854 } else if (decl
->ident
&& width
== 0) {
855 warning(token
->pos
, "invalid named zero-width bitfield `%s'",
856 show_ident(decl
->ident
));
858 } else if (decl
->ident
) {
859 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
860 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
861 if (width
== 1 && is_signed
) {
862 // Valid values are either {-1;0} or {0}, depending on integer
863 // representation. The latter makes for very efficient code...
864 warning(token
->pos
, "dubious one-bit signed bitfield");
866 if (Wdefault_bitfield_sign
&&
867 base_type
->type
!= SYM_ENUM
&&
868 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
870 // The sign of bitfields is unspecified by default.
871 warning (token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
874 bitfield
->bit_size
= width
;
878 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
880 while (!match_op(token
, '}')) {
881 struct ctype ctype
= {0, };
883 token
= declaration_specifiers(token
, &ctype
, 0);
885 struct ident
*ident
= NULL
;
886 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
888 token
= declarator(token
, decl
, &ident
);
890 if (match_op(token
, ':')) {
891 token
= handle_bitfield(token
, decl
);
892 token
= handle_attributes(token
, &decl
->ctype
);
894 add_symbol(list
, decl
);
895 if (!match_op(token
, ','))
899 if (!match_op(token
, ';')) {
900 warning(token
->pos
, "expected ; at end of declaration");
908 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
910 struct ident
*ident
= NULL
;
912 struct ctype ctype
= { 0, };
914 token
= declaration_specifiers(token
, &ctype
, 0);
915 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
918 token
= declarator(token
, sym
, &ident
);
923 struct token
*typename(struct token
*token
, struct symbol
**p
)
925 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
927 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
928 return declarator(token
, sym
, NULL
);
931 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
933 token
= parse_expression(token
, tree
);
934 return expect(token
, ';', "at end of statement");
937 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
939 struct expression
*expr
;
941 /* Allow empty operands */
942 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
945 if (match_op(token
->next
, '[') &&
946 token_type(token
->next
->next
) == TOKEN_IDENT
&&
947 match_op(token
->next
->next
->next
, ']'))
948 token
= token
->next
->next
->next
;
949 token
= primary_expression(token
->next
, &expr
);
950 token
= parens_expression(token
, &expr
, "in asm parameter");
951 } while (match_op(token
, ','));
955 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
957 struct expression
*expr
;
960 token
= primary_expression(token
->next
, &expr
);
961 } while (match_op(token
, ','));
965 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
967 struct expression
*expr
;
969 stmt
->type
= STMT_ASM
;
970 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
973 token
= expect(token
, '(', "after asm");
974 token
= parse_expression(token
->next
, &expr
);
975 if (match_op(token
, ':'))
976 token
= parse_asm_operands(token
, stmt
);
977 if (match_op(token
, ':'))
978 token
= parse_asm_operands(token
, stmt
);
979 if (match_op(token
, ':'))
980 token
= parse_asm_clobbers(token
, stmt
);
981 token
= expect(token
, ')', "after asm");
982 return expect(token
, ';', "at end of asm-statement");
985 /* Make a statement out of an expression */
986 static struct statement
*make_statement(struct expression
*expr
)
988 struct statement
*stmt
;
992 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
993 stmt
->expression
= expr
;
998 * All iterators have two symbols associated with them:
999 * the "continue" and "break" symbols, which are targets
1000 * for continue and break statements respectively.
1002 * They are in a special name-space, but they follow
1003 * all the normal visibility rules, so nested iterators
1004 * automatically work right.
1006 static void start_iterator(struct statement
*stmt
)
1008 struct symbol
*cont
, *brk
;
1010 start_symbol_scope();
1011 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1012 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1013 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1014 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1016 stmt
->type
= STMT_ITERATOR
;
1017 stmt
->iterator_break
= brk
;
1018 stmt
->iterator_continue
= cont
;
1019 fn_local_symbol(brk
);
1020 fn_local_symbol(cont
);
1023 static void end_iterator(struct statement
*stmt
)
1028 static struct statement
*start_function(struct symbol
*sym
)
1031 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1033 start_function_scope();
1034 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1035 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1036 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1037 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1038 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1040 fn_local_symbol(ret
);
1042 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1048 static void end_function(struct symbol
*sym
)
1051 end_function_scope();
1055 * A "switch()" statement, like an iterator, has a
1056 * the "break" symbol associated with it. It works
1057 * exactly like the iterator break - it's the target
1058 * for any break-statements in scope, and means that
1059 * "break" handling doesn't even need to know whether
1060 * it's breaking out of an iterator or a switch.
1062 * In addition, the "case" symbol is a marker for the
1063 * case/default statements to find the switch statement
1064 * that they are associated with.
1066 static void start_switch(struct statement
*stmt
)
1068 struct symbol
*brk
, *switch_case
;
1070 start_symbol_scope();
1071 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1072 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1074 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1075 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1076 switch_case
->stmt
= stmt
;
1078 stmt
->type
= STMT_SWITCH
;
1079 stmt
->switch_break
= brk
;
1080 stmt
->switch_case
= switch_case
;
1082 fn_local_symbol(brk
);
1083 fn_local_symbol(switch_case
);
1086 static void end_switch(struct statement
*stmt
)
1088 if (!stmt
->switch_case
->symbol_list
)
1089 warning(stmt
->pos
, "switch with no cases");
1093 static void add_case_statement(struct statement
*stmt
)
1095 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1099 warning(stmt
->pos
, "not in switch scope");
1100 stmt
->type
= STMT_NONE
;
1103 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1104 add_symbol(&target
->symbol_list
, sym
);
1106 stmt
->case_label
= sym
;
1107 fn_local_symbol(sym
);
1110 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1112 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1115 error_die(token
->pos
, "internal error: return without a function target");
1116 stmt
->type
= STMT_RETURN
;
1117 stmt
->ret_target
= target
;
1118 return expression_statement(token
->next
, &stmt
->ret_value
);
1121 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1123 struct symbol_list
*syms
;
1124 struct expression
*e1
, *e2
, *e3
;
1125 struct statement
*iterator
;
1127 start_iterator(stmt
);
1128 token
= expect(token
->next
, '(', "after 'for'");
1132 /* C99 variable declaration? */
1133 if (lookup_type(token
)) {
1134 token
= external_declaration(token
, &syms
);
1136 token
= parse_expression(token
, &e1
);
1137 token
= expect(token
, ';', "in 'for'");
1139 token
= parse_expression(token
, &e2
);
1140 token
= expect(token
, ';', "in 'for'");
1141 token
= parse_expression(token
, &e3
);
1142 token
= expect(token
, ')', "in 'for'");
1143 token
= statement(token
, &iterator
);
1145 stmt
->iterator_syms
= syms
;
1146 stmt
->iterator_pre_statement
= make_statement(e1
);
1147 stmt
->iterator_pre_condition
= e2
;
1148 stmt
->iterator_post_statement
= make_statement(e3
);
1149 stmt
->iterator_post_condition
= e2
;
1150 stmt
->iterator_statement
= iterator
;
1156 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1158 struct expression
*expr
;
1159 struct statement
*iterator
;
1161 start_iterator(stmt
);
1162 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1163 token
= statement(token
, &iterator
);
1165 stmt
->iterator_pre_condition
= expr
;
1166 stmt
->iterator_post_condition
= expr
;
1167 stmt
->iterator_statement
= iterator
;
1173 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1175 struct expression
*expr
;
1176 struct statement
*iterator
;
1178 start_iterator(stmt
);
1179 token
= statement(token
->next
, &iterator
);
1180 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1181 token
= token
->next
;
1183 warning(token
->pos
, "expected 'while' after 'do'");
1184 token
= parens_expression(token
, &expr
, "after 'do-while'");
1186 stmt
->iterator_post_condition
= expr
;
1187 stmt
->iterator_statement
= iterator
;
1190 return expect(token
, ';', "after statement");
1193 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1195 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1198 if (token_type(token
) == TOKEN_IDENT
) {
1199 if (token
->ident
== &if_ident
) {
1200 stmt
->type
= STMT_IF
;
1201 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1202 token
= statement(token
, &stmt
->if_true
);
1203 if (token_type(token
) != TOKEN_IDENT
)
1205 if (token
->ident
!= &else_ident
)
1207 return statement(token
->next
, &stmt
->if_false
);
1210 if (token
->ident
== &return_ident
)
1211 return parse_return_statement(token
, stmt
);
1213 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1214 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1215 stmt
->type
= STMT_GOTO
;
1216 stmt
->goto_label
= target
;
1218 warning(stmt
->pos
, "break/continue not in iterator scope");
1219 return expect(token
->next
, ';', "at end of statement");
1221 if (token
->ident
== &default_ident
) {
1222 token
= token
->next
;
1223 goto default_statement
;
1225 if (token
->ident
== &case_ident
) {
1226 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1227 if (match_op(token
, SPECIAL_ELLIPSIS
))
1228 token
= parse_expression(token
->next
, &stmt
->case_to
);
1230 stmt
->type
= STMT_CASE
;
1231 token
= expect(token
, ':', "after default/case");
1232 add_case_statement(stmt
);
1233 return statement(token
, &stmt
->case_statement
);
1235 if (token
->ident
== &switch_ident
) {
1236 stmt
->type
= STMT_SWITCH
;
1238 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1239 token
= statement(token
, &stmt
->switch_statement
);
1243 if (token
->ident
== &for_ident
)
1244 return parse_for_statement(token
, stmt
);
1246 if (token
->ident
== &while_ident
)
1247 return parse_while_statement(token
, stmt
);
1249 if (token
->ident
== &do_ident
)
1250 return parse_do_statement(token
, stmt
);
1252 if (token
->ident
== &goto_ident
) {
1253 stmt
->type
= STMT_GOTO
;
1254 token
= token
->next
;
1255 if (match_op(token
, '*')) {
1256 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1257 add_statement(&function_computed_goto_list
, stmt
);
1258 } else if (token_type(token
) == TOKEN_IDENT
) {
1259 stmt
->goto_label
= label_symbol(token
);
1260 token
= token
->next
;
1262 warning(token
->pos
, "Expected identifier or goto expression");
1264 return expect(token
, ';', "at end of statement");
1266 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1267 return parse_asm(token
->next
, stmt
);
1269 if (token
->ident
== &__context___ident
) {
1270 stmt
->type
= STMT_INTERNAL
;
1271 token
= parse_expression(token
->next
, &stmt
->expression
);
1272 return expect(token
, ';', "at end of statement");
1274 if (match_op(token
->next
, ':')) {
1275 stmt
->type
= STMT_LABEL
;
1276 stmt
->label_identifier
= label_symbol(token
);
1277 return statement(token
->next
->next
, &stmt
->label_statement
);
1281 if (match_op(token
, '{')) {
1282 stmt
->type
= STMT_COMPOUND
;
1283 start_symbol_scope();
1284 token
= compound_statement(token
->next
, stmt
);
1287 return expect(token
, '}', "at end of compound statement");
1290 stmt
->type
= STMT_EXPRESSION
;
1291 return expression_statement(token
, &stmt
->expression
);
1294 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1297 struct statement
* stmt
;
1298 if (eof_token(token
))
1300 if (match_op(token
, '}'))
1302 if (lookup_type(token
)) {
1303 if (warn_on_mixed
&& *list
)
1304 warning(token
->pos
, "mixing declarations and code");
1305 token
= external_declaration(token
, syms
);
1308 token
= statement(token
, &stmt
);
1309 add_statement(list
, stmt
);
1314 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1316 struct symbol_list
**list
= &fn
->arguments
;
1318 if (match_op(token
, ')')) {
1319 // No warning for "void oink ();"
1320 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1321 if (!match_op(token
->next
, ';'))
1322 warning(token
->pos
, "non-ANSI function declaration");
1329 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1331 warning(token
->pos
, "variadic functions must have one named argument");
1333 token
= token
->next
;
1337 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1338 token
= parameter_declaration(token
, &sym
);
1339 if (sym
->ctype
.base_type
== &void_ctype
) {
1340 /* Special case: (void) */
1341 if (!*list
&& !sym
->ident
)
1343 warning(token
->pos
, "void parameter");
1345 add_symbol(list
, sym
);
1346 if (!match_op(token
, ','))
1348 token
= token
->next
;
1354 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1356 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1360 static struct expression
*identifier_expression(struct token
*token
)
1362 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1363 expr
->expr_ident
= token
->ident
;
1367 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1369 int idx_from
, idx_to
;
1370 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1372 idx_from
= get_expression_value(from
);
1375 idx_to
= get_expression_value(to
);
1376 if (idx_to
< idx_from
|| idx_from
< 0)
1377 warning(from
->pos
, "nonsense array initializer index range");
1379 expr
->idx_from
= idx_from
;
1380 expr
->idx_to
= idx_to
;
1384 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1386 int expect_equal
= 0;
1387 struct token
*next
= token
->next
;
1388 struct expression
**tail
= ep
;
1393 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1394 struct expression
*expr
= identifier_expression(token
);
1395 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1396 token
= initializer(&expr
->ident_expression
, next
->next
);
1397 if (expr
->ident_expression
)
1402 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1403 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1404 struct expression
*expr
= identifier_expression(next
);
1406 tail
= &expr
->ident_expression
;
1409 } else if (match_op(token
, '[')) {
1410 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1411 token
= constant_expression(token
->next
, &from
);
1412 if (match_op(token
, SPECIAL_ELLIPSIS
))
1413 token
= constant_expression(token
->next
, &to
);
1414 expr
= index_expression(from
, to
);
1416 tail
= &expr
->idx_expression
;
1417 token
= expect(token
, ']', "at end of initializer index");
1424 if (nested
&& !expect_equal
) {
1425 if (!match_op(token
, '='))
1426 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1431 token
= expect(token
, '=', "at end of initializer index");
1433 token
= initializer(tail
, token
);
1439 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1441 struct expression
*expr
;
1444 token
= single_initializer(&expr
, token
);
1447 add_expression(list
, expr
);
1448 if (!match_op(token
, ','))
1450 token
= token
->next
;
1455 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1457 if (match_op(token
, '{')) {
1458 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1460 token
= initializer_list(&expr
->expr_list
, token
->next
);
1461 return expect(token
, '}', "at end of initializer");
1463 return assignment_expression(token
, tree
);
1466 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1469 warning(sym
->pos
, "no identifier for function argument");
1472 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1475 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1476 struct symbol_list
**list
)
1478 struct symbol
*base_type
= decl
->ctype
.base_type
;
1479 struct statement
*stmt
, **p
;
1482 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1483 function_symbol_list
= &decl
->inline_symbol_list
;
1484 p
= &base_type
->inline_stmt
;
1486 function_symbol_list
= &decl
->symbol_list
;
1487 p
= &base_type
->stmt
;
1489 function_computed_target_list
= NULL
;
1490 function_computed_goto_list
= NULL
;
1492 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1493 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1494 warning(decl
->pos
, "function with external linkage has definition");
1496 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1497 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1499 stmt
= start_function(decl
);
1502 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1503 declare_argument(arg
, base_type
);
1504 } END_FOR_EACH_PTR(arg
);
1506 token
= compound_statement(token
->next
, stmt
);
1509 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1510 add_symbol(list
, decl
);
1511 check_declaration(decl
);
1512 function_symbol_list
= NULL
;
1513 if (function_computed_goto_list
) {
1514 if (!function_computed_target_list
)
1515 warning(decl
->pos
, "function has computed goto but no targets?");
1517 struct statement
*stmt
;
1518 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1519 stmt
->target_list
= function_computed_target_list
;
1520 } END_FOR_EACH_PTR(stmt
);
1523 return expect(token
, '}', "at end of function");
1526 static void promote_k_r_types(struct symbol
*arg
)
1528 struct symbol
*base
= arg
->ctype
.base_type
;
1529 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1530 arg
->ctype
.base_type
= &int_ctype
;
1534 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1536 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1539 FOR_EACH_PTR(real_args
, arg
) {
1540 struct symbol
*type
;
1542 /* This is quadratic in the number of arguments. We _really_ don't care */
1543 FOR_EACH_PTR(argtypes
, type
) {
1544 if (type
->ident
== arg
->ident
)
1546 } END_FOR_EACH_PTR(type
);
1547 warning(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1551 /* "char" and "short" promote to "int" */
1552 promote_k_r_types(type
);
1554 arg
->ctype
= type
->ctype
;
1555 } END_FOR_EACH_PTR(arg
);
1557 FOR_EACH_PTR(argtypes
, arg
) {
1559 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1560 } END_FOR_EACH_PTR(arg
);
1564 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1565 struct symbol_list
**list
)
1567 struct symbol_list
*args
= NULL
;
1569 warning(token
->pos
, "non-ANSI function declaration");
1571 token
= external_declaration(token
, &args
);
1572 } while (lookup_type(token
));
1574 apply_k_r_types(args
, decl
);
1576 if (!match_op(token
, '{')) {
1577 warning(token
->pos
, "expected function body");
1580 return parse_function_body(token
, decl
, list
);
1584 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1586 struct ident
*ident
= NULL
;
1587 struct symbol
*decl
;
1588 struct ctype ctype
= { 0, };
1589 struct symbol
*base_type
;
1592 /* Top-level inline asm? */
1593 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1594 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1595 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1596 struct statement
*stmt
;
1598 anon
->ctype
.base_type
= fn
;
1599 function_symbol_list
= &anon
->symbol_list
;
1600 stmt
= start_function(anon
);
1601 token
= parse_asm(token
->next
, stmt
);
1603 function_symbol_list
= NULL
;
1604 add_symbol(list
, anon
);
1608 /* Parse declaration-specifiers, if any */
1609 token
= declaration_specifiers(token
, &ctype
, 0);
1610 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1611 decl
->ctype
= ctype
;
1612 token
= declarator(token
, decl
, &ident
);
1614 /* Just a type declaration? */
1616 return expect(token
, ';', "end of type declaration");
1618 /* type define declaration? */
1619 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1621 /* Typedef's don't have meaningful storage */
1623 ctype
.modifiers
&= ~MOD_STORAGE
;
1624 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1625 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1628 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1630 base_type
= decl
->ctype
.base_type
;
1631 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1632 /* K&R argument declaration? */
1633 if (lookup_type(token
))
1634 return parse_k_r_arguments(token
, decl
, list
);
1635 if (match_op(token
, '{'))
1636 return parse_function_body(token
, decl
, list
);
1638 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1639 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1640 } else if (!is_typedef
&& base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1641 warning(token
->pos
, "void declaration");
1645 if (!is_typedef
&& match_op(token
, '=')) {
1646 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1647 warning(decl
->pos
, "symbol with external linkage has initializer");
1648 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1650 token
= initializer(&decl
->initializer
, token
->next
);
1653 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1654 add_symbol(list
, decl
);
1655 if (function_symbol_list
)
1656 fn_local_symbol(decl
);
1659 check_declaration(decl
);
1661 if (!match_op(token
, ','))
1664 token
= token
->next
;
1666 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1667 decl
->ctype
= ctype
;
1668 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1669 token
= declarator(token
, decl
, &ident
);
1671 warning(token
->pos
, "expected identifier name in type definition");
1675 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1677 /* Function declarations are automatically extern unless specifically static */
1678 base_type
= decl
->ctype
.base_type
;
1679 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1680 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1681 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1684 return expect(token
, ';', "at end of declaration");
1687 struct symbol_list
*translation_unit(struct token
*token
)
1689 while (!eof_token(token
))
1690 token
= external_declaration(token
, &translation_unit_used_list
);
1691 // They aren't needed any more
1692 clear_token_alloc();
1694 /* Evaluate the symbol list */
1695 evaluate_symbol_list(translation_unit_used_list
);
1696 return translation_unit_used_list
;