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");
141 // private struct/union/enum type
142 if (!match_op(token
, '{')) {
143 warning(token
->pos
, "expected declaration");
144 ctype
->base_type
= &bad_ctype
;
148 sym
= alloc_symbol(token
->pos
, type
);
149 token
= parse(token
->next
, sym
);
150 ctype
->base_type
= sym
;
151 return expect(token
, '}', "at end of specifier");
154 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
156 return struct_declaration_list(token
, &sym
->symbol_list
);
159 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
161 return struct_union_enum_specifier(type
, token
, ctype
, parse_struct_declaration
);
166 unsigned long long y
;
169 static void upper_boundary(Num
*n
, Num
*v
)
181 static void lower_boundary(Num
*n
, Num
*v
)
193 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
195 int shift
= type
->bit_size
;
196 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
200 if (upper
->x
== 0 && upper
->y
>> shift
)
202 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
207 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
209 unsigned long long lastval
= 0;
210 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
211 Num upper
= {-1, 0}, lower
= {1, 0};
213 while (token_type(token
) == TOKEN_IDENT
) {
214 struct token
*next
= token
->next
;
217 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
218 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
220 if (match_op(next
, '=')) {
221 struct expression
*expr
;
222 next
= constant_expression(next
->next
, &expr
);
223 lastval
= get_expression_value(expr
);
227 } else if (is_int_type(ctype
)) {
230 error_die(token
->pos
, "can't increment the last enum member");
233 sym
->value
= lastval
;
234 sym
->ctype
.base_type
= ctype
;
236 if (base_type
!= &bad_ctype
) {
237 if (ctype
->type
== SYM_NODE
)
238 ctype
= ctype
->ctype
.base_type
;
239 if (ctype
->type
== SYM_ENUM
)
240 ctype
= ctype
->ctype
.base_type
;
243 * - if all enum's are of the same type, then
244 * the base_type is that type (two first
246 * - if enums are of different types, they
247 * all have to be integer types, and the
248 * base type is "int_ctype".
249 * - otherwise the base_type is "bad_ctype".
253 } else if (ctype
== base_type
) {
255 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
256 base_type
= &int_ctype
;
258 base_type
= &bad_ctype
;
260 if (is_int_type(base_type
)) {
261 Num v
= {.y
= lastval
};
262 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
264 else if ((long long)lastval
>= 0)
268 upper_boundary(&upper
, &v
);
269 lower_boundary(&lower
, &v
);
272 if (!match_op(token
, ','))
277 base_type
= &bad_ctype
;
278 else if (!is_int_type(base_type
))
279 base_type
= base_type
;
280 else if (type_is_ok(base_type
, &upper
, &lower
))
281 base_type
= base_type
;
282 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
283 base_type
= &int_ctype
;
284 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
285 base_type
= &uint_ctype
;
286 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
287 base_type
= &long_ctype
;
288 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
289 base_type
= &ulong_ctype
;
290 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
291 base_type
= &llong_ctype
;
292 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
293 base_type
= &ullong_ctype
;
295 base_type
= &bad_ctype
;
296 parent
->ctype
.base_type
= base_type
;
297 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
301 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
303 return struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
306 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
310 if (!match_op(token
, '(')) {
311 warning(token
->pos
, "expected '(' after typeof");
314 if (lookup_type(token
->next
)) {
315 token
= typename(token
->next
, &sym
);
318 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
319 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
321 ctype
->modifiers
= 0;
322 ctype
->base_type
= typeof_sym
;
324 return expect(token
, ')', "after typeof");
327 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
329 if (attribute
== &packed_ident
||
330 attribute
== &__packed___ident
) {
331 ctype
->alignment
= 1;
334 if (attribute
== &aligned_ident
||
335 attribute
== &__aligned___ident
) {
336 int alignment
= max_alignment
;
338 alignment
= get_expression_value(expr
);
339 ctype
->alignment
= alignment
;
342 if (attribute
== &nocast_ident
) {
343 ctype
->modifiers
|= MOD_NOCAST
;
346 if (attribute
== &noderef_ident
) {
347 ctype
->modifiers
|= MOD_NODEREF
;
350 if (attribute
== &safe_ident
) {
351 ctype
->modifiers
|= MOD_SAFE
;
354 if (attribute
== &force_ident
) {
355 ctype
->modifiers
|= MOD_FORCE
;
358 if (attribute
== &bitwise_ident
) {
360 ctype
->modifiers
|= MOD_BITWISE
;
363 if (attribute
== &address_space_ident
) {
365 return "expected address space number";
366 ctype
->as
= get_expression_value(expr
);
369 if (attribute
== &context_ident
) {
370 if (expr
&& expr
->type
== EXPR_COMMA
) {
371 int input
= get_expression_value(expr
->left
);
372 int output
= get_expression_value(expr
->right
);
373 ctype
->in_context
= input
;
374 ctype
->out_context
= output
;
377 return "expected context input/output values";
379 if (attribute
== &mode_ident
||
380 attribute
== &__mode___ident
) {
381 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
382 struct ident
*ident
= expr
->symbol_name
;
385 * Match against __QI__/__HI__/__SI__/__DI__
387 * FIXME! This is broken - we don't actually get
388 * the type information updated properly at this
389 * stage for some reason.
391 if (ident
== &__QI___ident
||
392 ident
== &QI_ident
) {
393 ctype
->modifiers
|= MOD_CHAR
;
396 if (ident
== &__HI___ident
||
397 ident
== &HI_ident
) {
398 ctype
->modifiers
|= MOD_SHORT
;
401 if (ident
== &__SI___ident
||
402 ident
== &SI_ident
) {
406 if (ident
== &__DI___ident
||
407 ident
== &DI_ident
) {
408 ctype
->modifiers
|= MOD_LONGLONG
;
411 if (ident
== &__word___ident
||
412 ident
== &word_ident
) {
413 ctype
->modifiers
|= MOD_LONG
;
416 return "unknown mode attribute";
418 return "expected attribute mode symbol";
421 /* Throw away for now.. */
422 if (attribute
== &format_ident
||
423 attribute
== &__format___ident
||
424 attribute
== &__format_arg___ident
)
426 if (attribute
== §ion_ident
||
427 attribute
== &__section___ident
)
429 if (attribute
== &unused_ident
||
430 attribute
== &__unused___ident
)
432 if (attribute
== &const_ident
||
433 attribute
== &__const_ident
||
434 attribute
== &__const___ident
)
436 if (attribute
== &noreturn_ident
||
437 attribute
== &__noreturn___ident
)
439 if (attribute
== ®parm_ident
)
441 if (attribute
== &weak_ident
)
443 if (attribute
== &alias_ident
)
445 if (attribute
== &pure_ident
)
447 if (attribute
== &always_inline_ident
)
449 if (attribute
== &syscall_linkage_ident
)
451 if (attribute
== &visibility_ident
)
453 if (attribute
== &model_ident
||
454 attribute
== &__model___ident
)
457 return "unknown attribute";
460 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
462 ctype
->modifiers
= 0;
463 token
= expect(token
, '(', "after attribute");
464 token
= expect(token
, '(', "after attribute");
468 struct ident
*attribute_name
;
469 struct expression
*attribute_expr
;
471 if (eof_token(token
))
473 if (match_op(token
, ';'))
475 if (token_type(token
) != TOKEN_IDENT
)
477 attribute_name
= token
->ident
;
479 attribute_expr
= NULL
;
480 if (match_op(token
, '('))
481 token
= parens_expression(token
, &attribute_expr
, "in attribute");
482 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
484 warning(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
485 if (!match_op(token
, ','))
490 token
= expect(token
, ')', "after attribute");
491 token
= expect(token
, ')', "after attribute");
495 struct symbol
* ctype_integer(unsigned long spec
)
497 static struct symbol
*const integer_ctypes
[][3] = {
498 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
499 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
500 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
501 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
502 { &int_ctype
, &sint_ctype
, &uint_ctype
},
504 struct symbol
*const (*ctype
)[3];
507 ctype
= integer_ctypes
;
508 if (!(spec
& MOD_LONGLONG
)) {
510 if (!(spec
& MOD_LONG
)) {
512 if (!(spec
& MOD_SHORT
)) {
514 if (!(spec
& MOD_CHAR
))
520 sub
= ((spec
& MOD_UNSIGNED
)
522 : ((spec
& MOD_EXPLICITLY_SIGNED
)
526 return ctype
[0][sub
];
529 struct symbol
* ctype_fp(unsigned long spec
)
531 if (spec
& MOD_LONGLONG
)
532 return &ldouble_ctype
;
534 return &double_ctype
;
538 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
540 unsigned long mod
= thistype
->modifiers
;
543 unsigned long old
= ctype
->modifiers
;
544 unsigned long extra
= 0, dup
, conflict
;
546 if (mod
& old
& MOD_LONG
) {
547 extra
= MOD_LONGLONG
| MOD_LONG
;
551 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
553 warning(pos
, "Just how %sdo you want this type to be?",
554 modifier_string(dup
));
556 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
558 warning(pos
, "You cannot have both long and short modifiers.");
560 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
562 warning(pos
, "You cannot have both signed and unsigned modifiers.");
564 // Only one storage modifier allowed, except that "inline" doesn't count.
565 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
566 conflict
&= (conflict
- 1);
568 warning(pos
, "multiple storage classes");
570 ctype
->modifiers
= old
| mod
| extra
;
573 /* Context mask and value */
574 ctype
->in_context
+= thistype
->in_context
;
575 ctype
->out_context
+= thistype
->out_context
;
578 if (thistype
->alignment
& (thistype
->alignment
-1)) {
579 warning(pos
, "I don't like non-power-of-2 alignments");
580 thistype
->alignment
= 0;
582 if (thistype
->alignment
> ctype
->alignment
)
583 ctype
->alignment
= thistype
->alignment
;
586 ctype
->as
= thistype
->as
;
589 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
591 unsigned long banned
, wrong
;
592 unsigned long this_mod
= s
->ctype
.modifiers
;
593 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
594 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
596 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
597 banned
= BANNED_SIZE
| BANNED_SIGN
;
598 else if (this_mod
& MOD_SPECIALBITS
)
600 else if (s
->ctype
.base_type
== &fp_type
)
601 banned
= BANNED_SIGN
;
602 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
608 // vector_type <-- whatever that is
609 banned
= BANNED_SIZE
| BANNED_SIGN
;
612 wrong
= mod
& banned
;
614 warning(*pos
, "modifier %sis invalid in this context",
615 modifier_string (wrong
));
619 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
623 while ( (token
= next
) != NULL
) {
624 struct ctype thistype
;
626 struct symbol
*s
, *type
;
630 if (token_type(token
) != TOKEN_IDENT
)
632 ident
= token
->ident
;
634 s
= lookup_symbol(ident
, NS_TYPEDEF
);
638 mod
= thistype
.modifiers
;
639 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
641 if (mod
& MOD_SPECIALBITS
) {
642 if (mod
& MOD_STRUCTOF
)
643 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
644 else if (mod
& MOD_UNIONOF
)
645 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
646 else if (mod
& MOD_ENUMOF
)
647 next
= enum_specifier(next
, &thistype
);
648 else if (mod
& MOD_ATTRIBUTE
)
649 next
= attribute_specifier(next
, &thistype
);
650 else if (mod
& MOD_TYPEOF
)
651 next
= typeof_specifier(next
, &thistype
);
652 mod
= thistype
.modifiers
;
654 type
= thistype
.base_type
;
658 if (ctype
->base_type
)
660 /* User types only mix with qualifiers */
661 if (mod
& MOD_USERTYPE
) {
662 if (ctype
->modifiers
& MOD_SPECIFIER
)
665 ctype
->base_type
= type
;
668 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
669 apply_ctype(token
->pos
, &thistype
, ctype
);
672 /* Turn the "virtual types" into real types with real sizes etc */
673 if (!ctype
->base_type
) {
674 struct symbol
*base
= &incomplete_ctype
;
677 * If we have modifiers, we'll default to an integer
678 * type, and "ctype_integer()" will turn this into
681 if (ctype
->modifiers
& MOD_SPECIFIER
)
683 ctype
->base_type
= base
;
685 if (ctype
->base_type
== &int_type
) {
686 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
687 ctype
->modifiers
&= ~MOD_SPECIFIER
;
688 } else if (ctype
->base_type
== &fp_type
) {
689 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
690 ctype
->modifiers
&= ~MOD_SPECIFIER
;
692 if (ctype
->modifiers
& MOD_BITWISE
) {
694 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
695 if (!is_int_type(ctype
->base_type
)) {
696 warning(token
->pos
, "invalid modifier");
699 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
700 *type
= *ctype
->base_type
;
701 type
->ctype
.base_type
= ctype
->base_type
;
702 type
->type
= SYM_RESTRICT
;
703 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
704 ctype
->base_type
= type
;
709 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
711 struct expression
*expr
= NULL
;
713 token
= parse_expression(token
, &expr
);
714 sym
->array_size
= expr
;
718 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
719 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
721 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
724 if (token_type(token
) != TOKEN_IDENT
)
726 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
727 struct ctype thistype
= { 0, };
728 token
= attribute_specifier(token
->next
, &thistype
);
729 apply_ctype(token
->pos
, &thistype
, ctype
);
732 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
733 struct expression
*expr
;
734 token
= expect(token
->next
, '(', "after asm");
735 token
= parse_expression(token
->next
, &expr
);
736 token
= expect(token
, ')', "after asm");
744 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
746 struct ctype
*ctype
= &decl
->ctype
;
748 if (p
&& token_type(token
) == TOKEN_IDENT
) {
754 token
= handle_attributes(token
, ctype
);
756 if (token_type(token
) != TOKEN_SPECIAL
)
760 * This can be either a parameter list or a grouping.
761 * For the direct (non-abstract) case, we know if must be
762 * a parameter list if we already saw the identifier.
763 * For the abstract case, we know if must be a parameter
764 * list if it is empty or starts with a type.
766 if (token
->special
== '(') {
768 struct token
*next
= token
->next
;
769 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
772 struct symbol
*base_type
= ctype
->base_type
;
773 token
= declarator(next
, decl
, p
);
774 token
= expect(token
, ')', "in nested declarator");
775 while (ctype
->base_type
!= base_type
)
776 ctype
= &ctype
->base_type
->ctype
;
781 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
782 token
= parameter_type_list(next
, sym
);
783 token
= expect(token
, ')', "in function declarator");
786 if (token
->special
== '[') {
787 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
788 token
= abstract_array_declarator(token
->next
, array
);
789 token
= expect(token
, ']', "in abstract_array_declarator");
790 ctype
= &array
->ctype
;
798 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
800 unsigned long modifiers
;
801 struct symbol
*base_type
;
803 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
804 base_type
= ctype
->base_type
;
805 ctype
->modifiers
= modifiers
;
807 while (match_op(token
,'*')) {
808 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
809 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
810 ptr
->ctype
.as
= ctype
->as
;
811 ptr
->ctype
.in_context
+= ctype
->in_context
;
812 ptr
->ctype
.out_context
+= ctype
->out_context
;
813 ptr
->ctype
.base_type
= base_type
;
816 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
817 ctype
->base_type
= base_type
;
819 ctype
->in_context
= 0;
820 ctype
->out_context
= 0;
822 token
= declaration_specifiers(token
->next
, ctype
, 1);
823 modifiers
= ctype
->modifiers
;
828 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
830 token
= pointer(token
, &sym
->ctype
);
831 return direct_declarator(token
, sym
, p
);
834 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
836 struct ctype
*ctype
= &decl
->ctype
;
837 struct expression
*expr
;
838 struct symbol
*bitfield
;
841 if (!is_int_type(ctype
->base_type
)) {
842 warning(token
->pos
, "invalid bitfield specifier for type %s.",
843 show_typename(ctype
->base_type
));
844 // Parse this to recover gracefully.
845 return conditional_expression(token
->next
, &expr
);
848 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
849 token
= conditional_expression(token
->next
, &expr
);
850 width
= get_expression_value(expr
);
851 bitfield
->bit_size
= width
;
853 if (width
< 0 || width
> INT_MAX
) {
854 warning(token
->pos
, "invalid bitfield width, %lld.", width
);
856 } else if (decl
->ident
&& width
== 0) {
857 warning(token
->pos
, "invalid named zero-width bitfield `%s'",
858 show_ident(decl
->ident
));
860 } else if (decl
->ident
) {
861 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
862 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
863 if (width
== 1 && is_signed
) {
864 // Valid values are either {-1;0} or {0}, depending on integer
865 // representation. The latter makes for very efficient code...
866 warning(token
->pos
, "dubious one-bit signed bitfield");
868 if (Wdefault_bitfield_sign
&&
869 base_type
->type
!= SYM_ENUM
&&
870 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
872 // The sign of bitfields is unspecified by default.
873 warning (token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
876 bitfield
->bit_size
= width
;
880 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
882 while (!match_op(token
, '}')) {
883 struct ctype ctype
= {0, };
885 token
= declaration_specifiers(token
, &ctype
, 0);
887 struct ident
*ident
= NULL
;
888 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
890 token
= declarator(token
, decl
, &ident
);
892 if (match_op(token
, ':')) {
893 token
= handle_bitfield(token
, decl
);
894 token
= handle_attributes(token
, &decl
->ctype
);
896 add_symbol(list
, decl
);
897 if (!match_op(token
, ','))
901 if (!match_op(token
, ';')) {
902 warning(token
->pos
, "expected ; at end of declaration");
910 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
912 struct ident
*ident
= NULL
;
914 struct ctype ctype
= { 0, };
916 token
= declaration_specifiers(token
, &ctype
, 0);
917 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
920 token
= declarator(token
, sym
, &ident
);
925 struct token
*typename(struct token
*token
, struct symbol
**p
)
927 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
929 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
930 return declarator(token
, sym
, NULL
);
933 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
935 token
= parse_expression(token
, tree
);
936 return expect(token
, ';', "at end of statement");
939 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
940 struct expression_list
**inout
)
942 struct expression
*expr
;
944 /* Allow empty operands */
945 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
948 struct ident
*ident
= NULL
;
949 if (match_op(token
->next
, '[') &&
950 token_type(token
->next
->next
) == TOKEN_IDENT
&&
951 match_op(token
->next
->next
->next
, ']')) {
952 ident
= token
->next
->next
->ident
;
953 token
= token
->next
->next
->next
;
955 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
956 token
= primary_expression(token
->next
, &expr
);
957 add_expression(inout
, expr
);
958 token
= parens_expression(token
, &expr
, "in asm parameter");
959 add_expression(inout
, expr
);
960 } while (match_op(token
, ','));
964 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
965 struct expression_list
**clobbers
)
967 struct expression
*expr
;
970 token
= primary_expression(token
->next
, &expr
);
971 add_expression(clobbers
, expr
);
972 } while (match_op(token
, ','));
976 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
978 stmt
->type
= STMT_ASM
;
979 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
982 token
= expect(token
, '(', "after asm");
983 token
= parse_expression(token
, &stmt
->asm_string
);
984 if (match_op(token
, ':'))
985 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
986 if (match_op(token
, ':'))
987 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
988 if (match_op(token
, ':'))
989 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
990 token
= expect(token
, ')', "after asm");
991 return expect(token
, ';', "at end of asm-statement");
994 /* Make a statement out of an expression */
995 static struct statement
*make_statement(struct expression
*expr
)
997 struct statement
*stmt
;
1001 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1002 stmt
->expression
= expr
;
1007 * All iterators have two symbols associated with them:
1008 * the "continue" and "break" symbols, which are targets
1009 * for continue and break statements respectively.
1011 * They are in a special name-space, but they follow
1012 * all the normal visibility rules, so nested iterators
1013 * automatically work right.
1015 static void start_iterator(struct statement
*stmt
)
1017 struct symbol
*cont
, *brk
;
1019 start_symbol_scope();
1020 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1021 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1022 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1023 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1025 stmt
->type
= STMT_ITERATOR
;
1026 stmt
->iterator_break
= brk
;
1027 stmt
->iterator_continue
= cont
;
1028 fn_local_symbol(brk
);
1029 fn_local_symbol(cont
);
1032 static void end_iterator(struct statement
*stmt
)
1037 static struct statement
*start_function(struct symbol
*sym
)
1040 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1042 start_function_scope();
1043 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1044 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1045 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1046 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1047 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1049 fn_local_symbol(ret
);
1051 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1057 static void end_function(struct symbol
*sym
)
1060 end_function_scope();
1064 * A "switch()" statement, like an iterator, has a
1065 * the "break" symbol associated with it. It works
1066 * exactly like the iterator break - it's the target
1067 * for any break-statements in scope, and means that
1068 * "break" handling doesn't even need to know whether
1069 * it's breaking out of an iterator or a switch.
1071 * In addition, the "case" symbol is a marker for the
1072 * case/default statements to find the switch statement
1073 * that they are associated with.
1075 static void start_switch(struct statement
*stmt
)
1077 struct symbol
*brk
, *switch_case
;
1079 start_symbol_scope();
1080 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1081 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1083 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1084 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1085 switch_case
->stmt
= stmt
;
1087 stmt
->type
= STMT_SWITCH
;
1088 stmt
->switch_break
= brk
;
1089 stmt
->switch_case
= switch_case
;
1091 fn_local_symbol(brk
);
1092 fn_local_symbol(switch_case
);
1095 static void end_switch(struct statement
*stmt
)
1097 if (!stmt
->switch_case
->symbol_list
)
1098 warning(stmt
->pos
, "switch with no cases");
1102 static void add_case_statement(struct statement
*stmt
)
1104 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1108 warning(stmt
->pos
, "not in switch scope");
1109 stmt
->type
= STMT_NONE
;
1112 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1113 add_symbol(&target
->symbol_list
, sym
);
1115 stmt
->case_label
= sym
;
1116 fn_local_symbol(sym
);
1119 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1121 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1124 error_die(token
->pos
, "internal error: return without a function target");
1125 stmt
->type
= STMT_RETURN
;
1126 stmt
->ret_target
= target
;
1127 return expression_statement(token
->next
, &stmt
->ret_value
);
1130 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1132 struct symbol_list
*syms
;
1133 struct expression
*e1
, *e2
, *e3
;
1134 struct statement
*iterator
;
1136 start_iterator(stmt
);
1137 token
= expect(token
->next
, '(', "after 'for'");
1141 /* C99 variable declaration? */
1142 if (lookup_type(token
)) {
1143 token
= external_declaration(token
, &syms
);
1145 token
= parse_expression(token
, &e1
);
1146 token
= expect(token
, ';', "in 'for'");
1148 token
= parse_expression(token
, &e2
);
1149 token
= expect(token
, ';', "in 'for'");
1150 token
= parse_expression(token
, &e3
);
1151 token
= expect(token
, ')', "in 'for'");
1152 token
= statement(token
, &iterator
);
1154 stmt
->iterator_syms
= syms
;
1155 stmt
->iterator_pre_statement
= make_statement(e1
);
1156 stmt
->iterator_pre_condition
= e2
;
1157 stmt
->iterator_post_statement
= make_statement(e3
);
1158 stmt
->iterator_post_condition
= e2
;
1159 stmt
->iterator_statement
= iterator
;
1165 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1167 struct expression
*expr
;
1168 struct statement
*iterator
;
1170 start_iterator(stmt
);
1171 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1172 token
= statement(token
, &iterator
);
1174 stmt
->iterator_pre_condition
= expr
;
1175 stmt
->iterator_post_condition
= expr
;
1176 stmt
->iterator_statement
= iterator
;
1182 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1184 struct expression
*expr
;
1185 struct statement
*iterator
;
1187 start_iterator(stmt
);
1188 token
= statement(token
->next
, &iterator
);
1189 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1190 token
= token
->next
;
1192 warning(token
->pos
, "expected 'while' after 'do'");
1193 token
= parens_expression(token
, &expr
, "after 'do-while'");
1195 stmt
->iterator_post_condition
= expr
;
1196 stmt
->iterator_statement
= iterator
;
1199 return expect(token
, ';', "after statement");
1202 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1204 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1207 if (token_type(token
) == TOKEN_IDENT
) {
1208 if (token
->ident
== &if_ident
) {
1209 stmt
->type
= STMT_IF
;
1210 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1211 token
= statement(token
, &stmt
->if_true
);
1212 if (token_type(token
) != TOKEN_IDENT
)
1214 if (token
->ident
!= &else_ident
)
1216 return statement(token
->next
, &stmt
->if_false
);
1219 if (token
->ident
== &return_ident
)
1220 return parse_return_statement(token
, stmt
);
1222 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1223 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1224 stmt
->type
= STMT_GOTO
;
1225 stmt
->goto_label
= target
;
1227 warning(stmt
->pos
, "break/continue not in iterator scope");
1228 return expect(token
->next
, ';', "at end of statement");
1230 if (token
->ident
== &default_ident
) {
1231 token
= token
->next
;
1232 goto default_statement
;
1234 if (token
->ident
== &case_ident
) {
1235 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1236 if (match_op(token
, SPECIAL_ELLIPSIS
))
1237 token
= parse_expression(token
->next
, &stmt
->case_to
);
1239 stmt
->type
= STMT_CASE
;
1240 token
= expect(token
, ':', "after default/case");
1241 add_case_statement(stmt
);
1242 return statement(token
, &stmt
->case_statement
);
1244 if (token
->ident
== &switch_ident
) {
1245 stmt
->type
= STMT_SWITCH
;
1247 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1248 token
= statement(token
, &stmt
->switch_statement
);
1252 if (token
->ident
== &for_ident
)
1253 return parse_for_statement(token
, stmt
);
1255 if (token
->ident
== &while_ident
)
1256 return parse_while_statement(token
, stmt
);
1258 if (token
->ident
== &do_ident
)
1259 return parse_do_statement(token
, stmt
);
1261 if (token
->ident
== &goto_ident
) {
1262 stmt
->type
= STMT_GOTO
;
1263 token
= token
->next
;
1264 if (match_op(token
, '*')) {
1265 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1266 add_statement(&function_computed_goto_list
, stmt
);
1267 } else if (token_type(token
) == TOKEN_IDENT
) {
1268 stmt
->goto_label
= label_symbol(token
);
1269 token
= token
->next
;
1271 warning(token
->pos
, "Expected identifier or goto expression");
1273 return expect(token
, ';', "at end of statement");
1275 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1276 return parse_asm(token
->next
, stmt
);
1278 if (token
->ident
== &__context___ident
) {
1279 stmt
->type
= STMT_INTERNAL
;
1280 token
= parse_expression(token
->next
, &stmt
->expression
);
1281 return expect(token
, ';', "at end of statement");
1283 if (match_op(token
->next
, ':')) {
1284 stmt
->type
= STMT_LABEL
;
1285 stmt
->label_identifier
= label_symbol(token
);
1286 return statement(token
->next
->next
, &stmt
->label_statement
);
1290 if (match_op(token
, '{')) {
1291 stmt
->type
= STMT_COMPOUND
;
1292 start_symbol_scope();
1293 token
= compound_statement(token
->next
, stmt
);
1296 return expect(token
, '}', "at end of compound statement");
1299 stmt
->type
= STMT_EXPRESSION
;
1300 return expression_statement(token
, &stmt
->expression
);
1303 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1306 struct statement
* stmt
;
1307 if (eof_token(token
))
1309 if (match_op(token
, '}'))
1311 if (lookup_type(token
)) {
1312 if (warn_on_mixed
&& *list
)
1313 warning(token
->pos
, "mixing declarations and code");
1314 token
= external_declaration(token
, syms
);
1317 token
= statement(token
, &stmt
);
1318 add_statement(list
, stmt
);
1323 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1325 struct symbol_list
**list
= &fn
->arguments
;
1327 if (match_op(token
, ')')) {
1328 // No warning for "void oink ();"
1329 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1330 if (!match_op(token
->next
, ';'))
1331 warning(token
->pos
, "non-ANSI function declaration");
1338 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1340 warning(token
->pos
, "variadic functions must have one named argument");
1342 token
= token
->next
;
1346 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1347 token
= parameter_declaration(token
, &sym
);
1348 if (sym
->ctype
.base_type
== &void_ctype
) {
1349 /* Special case: (void) */
1350 if (!*list
&& !sym
->ident
)
1352 warning(token
->pos
, "void parameter");
1354 add_symbol(list
, sym
);
1355 if (!match_op(token
, ','))
1357 token
= token
->next
;
1363 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1365 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1369 static struct expression
*identifier_expression(struct token
*token
)
1371 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1372 expr
->expr_ident
= token
->ident
;
1376 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1378 int idx_from
, idx_to
;
1379 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1381 idx_from
= get_expression_value(from
);
1384 idx_to
= get_expression_value(to
);
1385 if (idx_to
< idx_from
|| idx_from
< 0)
1386 warning(from
->pos
, "nonsense array initializer index range");
1388 expr
->idx_from
= idx_from
;
1389 expr
->idx_to
= idx_to
;
1393 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1395 int expect_equal
= 0;
1396 struct token
*next
= token
->next
;
1397 struct expression
**tail
= ep
;
1402 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1403 struct expression
*expr
= identifier_expression(token
);
1404 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1405 token
= initializer(&expr
->ident_expression
, next
->next
);
1406 if (expr
->ident_expression
)
1411 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1412 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1413 struct expression
*expr
= identifier_expression(next
);
1415 tail
= &expr
->ident_expression
;
1418 } else if (match_op(token
, '[')) {
1419 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1420 token
= constant_expression(token
->next
, &from
);
1421 if (match_op(token
, SPECIAL_ELLIPSIS
))
1422 token
= constant_expression(token
->next
, &to
);
1423 expr
= index_expression(from
, to
);
1425 tail
= &expr
->idx_expression
;
1426 token
= expect(token
, ']', "at end of initializer index");
1433 if (nested
&& !expect_equal
) {
1434 if (!match_op(token
, '='))
1435 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1440 token
= expect(token
, '=', "at end of initializer index");
1442 token
= initializer(tail
, token
);
1448 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1450 struct expression
*expr
;
1453 token
= single_initializer(&expr
, token
);
1456 add_expression(list
, expr
);
1457 if (!match_op(token
, ','))
1459 token
= token
->next
;
1464 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1466 if (match_op(token
, '{')) {
1467 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1469 token
= initializer_list(&expr
->expr_list
, token
->next
);
1470 return expect(token
, '}', "at end of initializer");
1472 return assignment_expression(token
, tree
);
1475 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1478 warning(sym
->pos
, "no identifier for function argument");
1481 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1484 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1485 struct symbol_list
**list
)
1487 struct symbol_list
**old_symbol_list
;
1488 struct symbol
*base_type
= decl
->ctype
.base_type
;
1489 struct statement
*stmt
, **p
;
1492 old_symbol_list
= function_symbol_list
;
1493 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1494 function_symbol_list
= &decl
->inline_symbol_list
;
1495 p
= &base_type
->inline_stmt
;
1497 function_symbol_list
= &decl
->symbol_list
;
1498 p
= &base_type
->stmt
;
1500 function_computed_target_list
= NULL
;
1501 function_computed_goto_list
= NULL
;
1503 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1504 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1505 warning(decl
->pos
, "function with external linkage has definition");
1507 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1508 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1510 stmt
= start_function(decl
);
1513 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1514 declare_argument(arg
, base_type
);
1515 } END_FOR_EACH_PTR(arg
);
1517 token
= compound_statement(token
->next
, stmt
);
1520 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1521 add_symbol(list
, decl
);
1522 check_declaration(decl
);
1523 function_symbol_list
= old_symbol_list
;
1524 if (function_computed_goto_list
) {
1525 if (!function_computed_target_list
)
1526 warning(decl
->pos
, "function has computed goto but no targets?");
1528 struct statement
*stmt
;
1529 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1530 stmt
->target_list
= function_computed_target_list
;
1531 } END_FOR_EACH_PTR(stmt
);
1534 return expect(token
, '}', "at end of function");
1537 static void promote_k_r_types(struct symbol
*arg
)
1539 struct symbol
*base
= arg
->ctype
.base_type
;
1540 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1541 arg
->ctype
.base_type
= &int_ctype
;
1545 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1547 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1550 FOR_EACH_PTR(real_args
, arg
) {
1551 struct symbol
*type
;
1553 /* This is quadratic in the number of arguments. We _really_ don't care */
1554 FOR_EACH_PTR(argtypes
, type
) {
1555 if (type
->ident
== arg
->ident
)
1557 } END_FOR_EACH_PTR(type
);
1558 warning(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1562 /* "char" and "short" promote to "int" */
1563 promote_k_r_types(type
);
1565 arg
->ctype
= type
->ctype
;
1566 } END_FOR_EACH_PTR(arg
);
1568 FOR_EACH_PTR(argtypes
, arg
) {
1570 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1571 } END_FOR_EACH_PTR(arg
);
1575 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1576 struct symbol_list
**list
)
1578 struct symbol_list
*args
= NULL
;
1580 warning(token
->pos
, "non-ANSI function declaration");
1582 token
= external_declaration(token
, &args
);
1583 } while (lookup_type(token
));
1585 apply_k_r_types(args
, decl
);
1587 if (!match_op(token
, '{')) {
1588 warning(token
->pos
, "expected function body");
1591 return parse_function_body(token
, decl
, list
);
1595 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1597 struct ident
*ident
= NULL
;
1598 struct symbol
*decl
;
1599 struct ctype ctype
= { 0, };
1600 struct symbol
*base_type
;
1603 /* Top-level inline asm? */
1604 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1605 struct symbol_list
**old_symbol_list
;
1606 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1607 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1608 struct statement
*stmt
;
1610 anon
->ctype
.base_type
= fn
;
1611 old_symbol_list
= function_symbol_list
;
1612 function_symbol_list
= &anon
->symbol_list
;
1613 stmt
= start_function(anon
);
1614 token
= parse_asm(token
->next
, stmt
);
1616 function_symbol_list
= old_symbol_list
;
1617 add_symbol(list
, anon
);
1621 /* Parse declaration-specifiers, if any */
1622 token
= declaration_specifiers(token
, &ctype
, 0);
1623 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1624 decl
->ctype
= ctype
;
1625 token
= declarator(token
, decl
, &ident
);
1627 /* Just a type declaration? */
1629 return expect(token
, ';', "end of type declaration");
1631 /* type define declaration? */
1632 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1634 /* Typedef's don't have meaningful storage */
1636 ctype
.modifiers
&= ~MOD_STORAGE
;
1637 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1638 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1641 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1643 base_type
= decl
->ctype
.base_type
;
1644 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1645 /* K&R argument declaration? */
1646 if (lookup_type(token
))
1647 return parse_k_r_arguments(token
, decl
, list
);
1648 if (match_op(token
, '{'))
1649 return parse_function_body(token
, decl
, list
);
1651 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1652 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1653 } else if (!is_typedef
&& base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1654 warning(token
->pos
, "void declaration");
1658 if (!is_typedef
&& match_op(token
, '=')) {
1659 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1660 warning(decl
->pos
, "symbol with external linkage has initializer");
1661 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1663 token
= initializer(&decl
->initializer
, token
->next
);
1666 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1667 add_symbol(list
, decl
);
1668 fn_local_symbol(decl
);
1671 check_declaration(decl
);
1673 if (!match_op(token
, ','))
1676 token
= token
->next
;
1678 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1679 decl
->ctype
= ctype
;
1680 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1681 token
= declarator(token
, decl
, &ident
);
1683 warning(token
->pos
, "expected identifier name in type definition");
1687 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1689 /* Function declarations are automatically extern unless specifically static */
1690 base_type
= decl
->ctype
.base_type
;
1691 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1692 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1693 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1696 return expect(token
, ';', "at end of declaration");
1699 struct symbol_list
*translation_unit(struct token
*token
)
1701 while (!eof_token(token
))
1702 token
= external_declaration(token
, &translation_unit_used_list
);
1703 // They aren't needed any more
1704 clear_token_alloc();
1706 /* Evaluate the symbol list */
1707 evaluate_symbol_list(translation_unit_used_list
);
1708 return translation_unit_used_list
;