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
)
425 if (attribute
== §ion_ident
||
426 attribute
== &__section___ident
)
428 if (attribute
== &unused_ident
||
429 attribute
== &__unused___ident
)
431 if (attribute
== &const_ident
||
432 attribute
== &__const_ident
||
433 attribute
== &__const___ident
)
435 if (attribute
== &noreturn_ident
||
436 attribute
== &__noreturn___ident
)
438 if (attribute
== ®parm_ident
)
440 if (attribute
== &weak_ident
)
442 if (attribute
== &alias_ident
)
444 if (attribute
== &pure_ident
)
446 if (attribute
== &always_inline_ident
)
448 if (attribute
== &syscall_linkage_ident
)
450 if (attribute
== &visibility_ident
)
452 if (attribute
== &model_ident
||
453 attribute
== &__model___ident
)
456 return "unknown attribute";
459 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
461 ctype
->modifiers
= 0;
462 token
= expect(token
, '(', "after attribute");
463 token
= expect(token
, '(', "after attribute");
467 struct ident
*attribute_name
;
468 struct expression
*attribute_expr
;
470 if (eof_token(token
))
472 if (match_op(token
, ';'))
474 if (token_type(token
) != TOKEN_IDENT
)
476 attribute_name
= token
->ident
;
478 attribute_expr
= NULL
;
479 if (match_op(token
, '('))
480 token
= parens_expression(token
, &attribute_expr
, "in attribute");
481 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
483 warning(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
484 if (!match_op(token
, ','))
489 token
= expect(token
, ')', "after attribute");
490 token
= expect(token
, ')', "after attribute");
494 struct symbol
* ctype_integer(unsigned long spec
)
496 static struct symbol
*const integer_ctypes
[][3] = {
497 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
498 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
499 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
500 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
501 { &int_ctype
, &sint_ctype
, &uint_ctype
},
503 struct symbol
*const (*ctype
)[3];
506 ctype
= integer_ctypes
;
507 if (!(spec
& MOD_LONGLONG
)) {
509 if (!(spec
& MOD_LONG
)) {
511 if (!(spec
& MOD_SHORT
)) {
513 if (!(spec
& MOD_CHAR
))
519 sub
= ((spec
& MOD_UNSIGNED
)
521 : ((spec
& MOD_EXPLICITLY_SIGNED
)
525 return ctype
[0][sub
];
528 struct symbol
* ctype_fp(unsigned long spec
)
530 if (spec
& MOD_LONGLONG
)
531 return &ldouble_ctype
;
533 return &double_ctype
;
537 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
539 unsigned long mod
= thistype
->modifiers
;
542 unsigned long old
= ctype
->modifiers
;
543 unsigned long extra
= 0, dup
, conflict
;
545 if (mod
& old
& MOD_LONG
) {
546 extra
= MOD_LONGLONG
| MOD_LONG
;
550 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
552 warning(pos
, "Just how %sdo you want this type to be?",
553 modifier_string(dup
));
555 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
557 warning(pos
, "You cannot have both long and short modifiers.");
559 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
561 warning(pos
, "You cannot have both signed and unsigned modifiers.");
563 // Only one storage modifier allowed, except that "inline" doesn't count.
564 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
565 conflict
&= (conflict
- 1);
567 warning(pos
, "multiple storage classes");
569 ctype
->modifiers
= old
| mod
| extra
;
572 /* Context mask and value */
573 ctype
->in_context
+= thistype
->in_context
;
574 ctype
->out_context
+= thistype
->out_context
;
577 if (thistype
->alignment
& (thistype
->alignment
-1)) {
578 warning(pos
, "I don't like non-power-of-2 alignments");
579 thistype
->alignment
= 0;
581 if (thistype
->alignment
> ctype
->alignment
)
582 ctype
->alignment
= thistype
->alignment
;
585 ctype
->as
= thistype
->as
;
588 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
590 unsigned long banned
, wrong
;
591 unsigned long this_mod
= s
->ctype
.modifiers
;
592 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
593 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
595 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
596 banned
= BANNED_SIZE
| BANNED_SIGN
;
597 else if (this_mod
& MOD_SPECIALBITS
)
599 else if (s
->ctype
.base_type
== &fp_type
)
600 banned
= BANNED_SIGN
;
601 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
607 // vector_type <-- whatever that is
608 banned
= BANNED_SIZE
| BANNED_SIGN
;
611 wrong
= mod
& banned
;
613 warning(*pos
, "modifier %sis invalid in this context",
614 modifier_string (wrong
));
618 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
622 while ( (token
= next
) != NULL
) {
623 struct ctype thistype
;
625 struct symbol
*s
, *type
;
629 if (token_type(token
) != TOKEN_IDENT
)
631 ident
= token
->ident
;
633 s
= lookup_symbol(ident
, NS_TYPEDEF
);
637 mod
= thistype
.modifiers
;
638 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
640 if (mod
& MOD_SPECIALBITS
) {
641 if (mod
& MOD_STRUCTOF
)
642 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
643 else if (mod
& MOD_UNIONOF
)
644 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
645 else if (mod
& MOD_ENUMOF
)
646 next
= enum_specifier(next
, &thistype
);
647 else if (mod
& MOD_ATTRIBUTE
)
648 next
= attribute_specifier(next
, &thistype
);
649 else if (mod
& MOD_TYPEOF
)
650 next
= typeof_specifier(next
, &thistype
);
651 mod
= thistype
.modifiers
;
653 type
= thistype
.base_type
;
657 if (ctype
->base_type
)
659 /* User types only mix with qualifiers */
660 if (mod
& MOD_USERTYPE
) {
661 if (ctype
->modifiers
& MOD_SPECIFIER
)
664 ctype
->base_type
= type
;
667 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
668 apply_ctype(token
->pos
, &thistype
, ctype
);
671 /* Turn the "virtual types" into real types with real sizes etc */
672 if (!ctype
->base_type
) {
673 struct symbol
*base
= &incomplete_ctype
;
676 * If we have modifiers, we'll default to an integer
677 * type, and "ctype_integer()" will turn this into
680 if (ctype
->modifiers
& MOD_SPECIFIER
)
682 ctype
->base_type
= base
;
684 if (ctype
->base_type
== &int_type
) {
685 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
686 ctype
->modifiers
&= ~MOD_SPECIFIER
;
687 } else if (ctype
->base_type
== &fp_type
) {
688 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
689 ctype
->modifiers
&= ~MOD_SPECIFIER
;
691 if (ctype
->modifiers
& MOD_BITWISE
) {
693 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
694 if (!is_int_type(ctype
->base_type
)) {
695 warning(token
->pos
, "invalid modifier");
698 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
699 *type
= *ctype
->base_type
;
700 type
->ctype
.base_type
= ctype
->base_type
;
701 type
->type
= SYM_RESTRICT
;
702 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
703 ctype
->base_type
= type
;
708 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
710 struct expression
*expr
= NULL
;
712 token
= parse_expression(token
, &expr
);
713 sym
->array_size
= expr
;
717 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
718 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
720 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
723 if (token_type(token
) != TOKEN_IDENT
)
725 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
726 struct ctype thistype
= { 0, };
727 token
= attribute_specifier(token
->next
, &thistype
);
728 apply_ctype(token
->pos
, &thistype
, ctype
);
731 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
732 struct expression
*expr
;
733 token
= expect(token
->next
, '(', "after asm");
734 token
= parse_expression(token
->next
, &expr
);
735 token
= expect(token
, ')', "after asm");
743 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
745 struct ctype
*ctype
= &decl
->ctype
;
747 if (p
&& token_type(token
) == TOKEN_IDENT
) {
753 token
= handle_attributes(token
, ctype
);
755 if (token_type(token
) != TOKEN_SPECIAL
)
759 * This can be either a parameter list or a grouping.
760 * For the direct (non-abstract) case, we know if must be
761 * a parameter list if we already saw the identifier.
762 * For the abstract case, we know if must be a parameter
763 * list if it is empty or starts with a type.
765 if (token
->special
== '(') {
767 struct token
*next
= token
->next
;
768 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
771 struct symbol
*base_type
= ctype
->base_type
;
772 token
= declarator(next
, decl
, p
);
773 token
= expect(token
, ')', "in nested declarator");
774 while (ctype
->base_type
!= base_type
)
775 ctype
= &ctype
->base_type
->ctype
;
780 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
781 token
= parameter_type_list(next
, sym
);
782 token
= expect(token
, ')', "in function declarator");
785 if (token
->special
== '[') {
786 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
787 token
= abstract_array_declarator(token
->next
, array
);
788 token
= expect(token
, ']', "in abstract_array_declarator");
789 ctype
= &array
->ctype
;
797 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
799 unsigned long modifiers
;
800 struct symbol
*base_type
;
802 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
803 base_type
= ctype
->base_type
;
804 ctype
->modifiers
= modifiers
;
806 while (match_op(token
,'*')) {
807 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
808 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
809 ptr
->ctype
.as
= ctype
->as
;
810 ptr
->ctype
.in_context
+= ctype
->in_context
;
811 ptr
->ctype
.out_context
+= ctype
->out_context
;
812 ptr
->ctype
.base_type
= base_type
;
815 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
816 ctype
->base_type
= base_type
;
818 ctype
->in_context
= 0;
819 ctype
->out_context
= 0;
821 token
= declaration_specifiers(token
->next
, ctype
, 1);
822 modifiers
= ctype
->modifiers
;
827 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
829 token
= pointer(token
, &sym
->ctype
);
830 return direct_declarator(token
, sym
, p
);
833 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
835 struct ctype
*ctype
= &decl
->ctype
;
836 struct expression
*expr
;
837 struct symbol
*bitfield
;
840 if (!is_int_type(ctype
->base_type
)) {
841 warning(token
->pos
, "invalid bitfield specifier for type %s.",
842 show_typename(ctype
->base_type
));
843 // Parse this to recover gracefully.
844 return conditional_expression(token
->next
, &expr
);
847 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
848 token
= conditional_expression(token
->next
, &expr
);
849 width
= get_expression_value(expr
);
850 bitfield
->bit_size
= width
;
852 if (width
< 0 || width
> INT_MAX
) {
853 warning(token
->pos
, "invalid bitfield width, %lld.", width
);
855 } else if (decl
->ident
&& width
== 0) {
856 warning(token
->pos
, "invalid named zero-width bitfield `%s'",
857 show_ident(decl
->ident
));
859 } else if (decl
->ident
) {
860 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
861 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
862 if (width
== 1 && is_signed
) {
863 // Valid values are either {-1;0} or {0}, depending on integer
864 // representation. The latter makes for very efficient code...
865 warning(token
->pos
, "dubious one-bit signed bitfield");
867 if (Wdefault_bitfield_sign
&&
868 base_type
->type
!= SYM_ENUM
&&
869 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
871 // The sign of bitfields is unspecified by default.
872 warning (token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
875 bitfield
->bit_size
= width
;
879 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
881 while (!match_op(token
, '}')) {
882 struct ctype ctype
= {0, };
884 token
= declaration_specifiers(token
, &ctype
, 0);
886 struct ident
*ident
= NULL
;
887 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
889 token
= declarator(token
, decl
, &ident
);
891 if (match_op(token
, ':')) {
892 token
= handle_bitfield(token
, decl
);
893 token
= handle_attributes(token
, &decl
->ctype
);
895 add_symbol(list
, decl
);
896 if (!match_op(token
, ','))
900 if (!match_op(token
, ';')) {
901 warning(token
->pos
, "expected ; at end of declaration");
909 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
911 struct ident
*ident
= NULL
;
913 struct ctype ctype
= { 0, };
915 token
= declaration_specifiers(token
, &ctype
, 0);
916 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
919 token
= declarator(token
, sym
, &ident
);
924 struct token
*typename(struct token
*token
, struct symbol
**p
)
926 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
928 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
929 return declarator(token
, sym
, NULL
);
932 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
934 token
= parse_expression(token
, tree
);
935 return expect(token
, ';', "at end of statement");
938 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
939 struct expression_list
**inout
)
941 struct expression
*expr
;
943 /* Allow empty operands */
944 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
947 if (match_op(token
->next
, '[') &&
948 token_type(token
->next
->next
) == TOKEN_IDENT
&&
949 match_op(token
->next
->next
->next
, ']'))
950 token
= token
->next
->next
->next
;
951 token
= primary_expression(token
->next
, &expr
);
952 add_expression(inout
, expr
);
953 token
= parens_expression(token
, &expr
, "in asm parameter");
954 add_expression(inout
, expr
);
955 } while (match_op(token
, ','));
959 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
960 struct expression_list
**clobbers
)
962 struct expression
*expr
;
965 token
= primary_expression(token
->next
, &expr
);
966 add_expression(clobbers
, expr
);
967 } while (match_op(token
, ','));
971 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
973 stmt
->type
= STMT_ASM
;
974 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
977 token
= expect(token
, '(', "after asm");
978 token
= parse_expression(token
, &stmt
->asm_string
);
979 if (match_op(token
, ':'))
980 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
981 if (match_op(token
, ':'))
982 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
983 if (match_op(token
, ':'))
984 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
985 token
= expect(token
, ')', "after asm");
986 return expect(token
, ';', "at end of asm-statement");
989 /* Make a statement out of an expression */
990 static struct statement
*make_statement(struct expression
*expr
)
992 struct statement
*stmt
;
996 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
997 stmt
->expression
= expr
;
1002 * All iterators have two symbols associated with them:
1003 * the "continue" and "break" symbols, which are targets
1004 * for continue and break statements respectively.
1006 * They are in a special name-space, but they follow
1007 * all the normal visibility rules, so nested iterators
1008 * automatically work right.
1010 static void start_iterator(struct statement
*stmt
)
1012 struct symbol
*cont
, *brk
;
1014 start_symbol_scope();
1015 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1016 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1017 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1018 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1020 stmt
->type
= STMT_ITERATOR
;
1021 stmt
->iterator_break
= brk
;
1022 stmt
->iterator_continue
= cont
;
1023 fn_local_symbol(brk
);
1024 fn_local_symbol(cont
);
1027 static void end_iterator(struct statement
*stmt
)
1032 static struct statement
*start_function(struct symbol
*sym
)
1035 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1037 start_function_scope();
1038 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1039 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1040 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1041 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1042 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1044 fn_local_symbol(ret
);
1046 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1052 static void end_function(struct symbol
*sym
)
1055 end_function_scope();
1059 * A "switch()" statement, like an iterator, has a
1060 * the "break" symbol associated with it. It works
1061 * exactly like the iterator break - it's the target
1062 * for any break-statements in scope, and means that
1063 * "break" handling doesn't even need to know whether
1064 * it's breaking out of an iterator or a switch.
1066 * In addition, the "case" symbol is a marker for the
1067 * case/default statements to find the switch statement
1068 * that they are associated with.
1070 static void start_switch(struct statement
*stmt
)
1072 struct symbol
*brk
, *switch_case
;
1074 start_symbol_scope();
1075 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1076 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1078 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1079 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1080 switch_case
->stmt
= stmt
;
1082 stmt
->type
= STMT_SWITCH
;
1083 stmt
->switch_break
= brk
;
1084 stmt
->switch_case
= switch_case
;
1086 fn_local_symbol(brk
);
1087 fn_local_symbol(switch_case
);
1090 static void end_switch(struct statement
*stmt
)
1092 if (!stmt
->switch_case
->symbol_list
)
1093 warning(stmt
->pos
, "switch with no cases");
1097 static void add_case_statement(struct statement
*stmt
)
1099 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1103 warning(stmt
->pos
, "not in switch scope");
1104 stmt
->type
= STMT_NONE
;
1107 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1108 add_symbol(&target
->symbol_list
, sym
);
1110 stmt
->case_label
= sym
;
1111 fn_local_symbol(sym
);
1114 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1116 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1119 error_die(token
->pos
, "internal error: return without a function target");
1120 stmt
->type
= STMT_RETURN
;
1121 stmt
->ret_target
= target
;
1122 return expression_statement(token
->next
, &stmt
->ret_value
);
1125 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1127 struct symbol_list
*syms
;
1128 struct expression
*e1
, *e2
, *e3
;
1129 struct statement
*iterator
;
1131 start_iterator(stmt
);
1132 token
= expect(token
->next
, '(', "after 'for'");
1136 /* C99 variable declaration? */
1137 if (lookup_type(token
)) {
1138 token
= external_declaration(token
, &syms
);
1140 token
= parse_expression(token
, &e1
);
1141 token
= expect(token
, ';', "in 'for'");
1143 token
= parse_expression(token
, &e2
);
1144 token
= expect(token
, ';', "in 'for'");
1145 token
= parse_expression(token
, &e3
);
1146 token
= expect(token
, ')', "in 'for'");
1147 token
= statement(token
, &iterator
);
1149 stmt
->iterator_syms
= syms
;
1150 stmt
->iterator_pre_statement
= make_statement(e1
);
1151 stmt
->iterator_pre_condition
= e2
;
1152 stmt
->iterator_post_statement
= make_statement(e3
);
1153 stmt
->iterator_post_condition
= e2
;
1154 stmt
->iterator_statement
= iterator
;
1160 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1162 struct expression
*expr
;
1163 struct statement
*iterator
;
1165 start_iterator(stmt
);
1166 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1167 token
= statement(token
, &iterator
);
1169 stmt
->iterator_pre_condition
= expr
;
1170 stmt
->iterator_post_condition
= expr
;
1171 stmt
->iterator_statement
= iterator
;
1177 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1179 struct expression
*expr
;
1180 struct statement
*iterator
;
1182 start_iterator(stmt
);
1183 token
= statement(token
->next
, &iterator
);
1184 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1185 token
= token
->next
;
1187 warning(token
->pos
, "expected 'while' after 'do'");
1188 token
= parens_expression(token
, &expr
, "after 'do-while'");
1190 stmt
->iterator_post_condition
= expr
;
1191 stmt
->iterator_statement
= iterator
;
1194 return expect(token
, ';', "after statement");
1197 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1199 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1202 if (token_type(token
) == TOKEN_IDENT
) {
1203 if (token
->ident
== &if_ident
) {
1204 stmt
->type
= STMT_IF
;
1205 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1206 token
= statement(token
, &stmt
->if_true
);
1207 if (token_type(token
) != TOKEN_IDENT
)
1209 if (token
->ident
!= &else_ident
)
1211 return statement(token
->next
, &stmt
->if_false
);
1214 if (token
->ident
== &return_ident
)
1215 return parse_return_statement(token
, stmt
);
1217 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1218 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1219 stmt
->type
= STMT_GOTO
;
1220 stmt
->goto_label
= target
;
1222 warning(stmt
->pos
, "break/continue not in iterator scope");
1223 return expect(token
->next
, ';', "at end of statement");
1225 if (token
->ident
== &default_ident
) {
1226 token
= token
->next
;
1227 goto default_statement
;
1229 if (token
->ident
== &case_ident
) {
1230 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1231 if (match_op(token
, SPECIAL_ELLIPSIS
))
1232 token
= parse_expression(token
->next
, &stmt
->case_to
);
1234 stmt
->type
= STMT_CASE
;
1235 token
= expect(token
, ':', "after default/case");
1236 add_case_statement(stmt
);
1237 return statement(token
, &stmt
->case_statement
);
1239 if (token
->ident
== &switch_ident
) {
1240 stmt
->type
= STMT_SWITCH
;
1242 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1243 token
= statement(token
, &stmt
->switch_statement
);
1247 if (token
->ident
== &for_ident
)
1248 return parse_for_statement(token
, stmt
);
1250 if (token
->ident
== &while_ident
)
1251 return parse_while_statement(token
, stmt
);
1253 if (token
->ident
== &do_ident
)
1254 return parse_do_statement(token
, stmt
);
1256 if (token
->ident
== &goto_ident
) {
1257 stmt
->type
= STMT_GOTO
;
1258 token
= token
->next
;
1259 if (match_op(token
, '*')) {
1260 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1261 add_statement(&function_computed_goto_list
, stmt
);
1262 } else if (token_type(token
) == TOKEN_IDENT
) {
1263 stmt
->goto_label
= label_symbol(token
);
1264 token
= token
->next
;
1266 warning(token
->pos
, "Expected identifier or goto expression");
1268 return expect(token
, ';', "at end of statement");
1270 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1271 return parse_asm(token
->next
, stmt
);
1273 if (token
->ident
== &__context___ident
) {
1274 stmt
->type
= STMT_INTERNAL
;
1275 token
= parse_expression(token
->next
, &stmt
->expression
);
1276 return expect(token
, ';', "at end of statement");
1278 if (match_op(token
->next
, ':')) {
1279 stmt
->type
= STMT_LABEL
;
1280 stmt
->label_identifier
= label_symbol(token
);
1281 return statement(token
->next
->next
, &stmt
->label_statement
);
1285 if (match_op(token
, '{')) {
1286 stmt
->type
= STMT_COMPOUND
;
1287 start_symbol_scope();
1288 token
= compound_statement(token
->next
, stmt
);
1291 return expect(token
, '}', "at end of compound statement");
1294 stmt
->type
= STMT_EXPRESSION
;
1295 return expression_statement(token
, &stmt
->expression
);
1298 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1301 struct statement
* stmt
;
1302 if (eof_token(token
))
1304 if (match_op(token
, '}'))
1306 if (lookup_type(token
)) {
1307 if (warn_on_mixed
&& *list
)
1308 warning(token
->pos
, "mixing declarations and code");
1309 token
= external_declaration(token
, syms
);
1312 token
= statement(token
, &stmt
);
1313 add_statement(list
, stmt
);
1318 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1320 struct symbol_list
**list
= &fn
->arguments
;
1322 if (match_op(token
, ')')) {
1323 // No warning for "void oink ();"
1324 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1325 if (!match_op(token
->next
, ';'))
1326 warning(token
->pos
, "non-ANSI function declaration");
1333 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1335 warning(token
->pos
, "variadic functions must have one named argument");
1337 token
= token
->next
;
1341 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1342 token
= parameter_declaration(token
, &sym
);
1343 if (sym
->ctype
.base_type
== &void_ctype
) {
1344 /* Special case: (void) */
1345 if (!*list
&& !sym
->ident
)
1347 warning(token
->pos
, "void parameter");
1349 add_symbol(list
, sym
);
1350 if (!match_op(token
, ','))
1352 token
= token
->next
;
1358 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1360 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1364 static struct expression
*identifier_expression(struct token
*token
)
1366 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1367 expr
->expr_ident
= token
->ident
;
1371 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1373 int idx_from
, idx_to
;
1374 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1376 idx_from
= get_expression_value(from
);
1379 idx_to
= get_expression_value(to
);
1380 if (idx_to
< idx_from
|| idx_from
< 0)
1381 warning(from
->pos
, "nonsense array initializer index range");
1383 expr
->idx_from
= idx_from
;
1384 expr
->idx_to
= idx_to
;
1388 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1390 int expect_equal
= 0;
1391 struct token
*next
= token
->next
;
1392 struct expression
**tail
= ep
;
1397 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1398 struct expression
*expr
= identifier_expression(token
);
1399 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1400 token
= initializer(&expr
->ident_expression
, next
->next
);
1401 if (expr
->ident_expression
)
1406 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1407 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1408 struct expression
*expr
= identifier_expression(next
);
1410 tail
= &expr
->ident_expression
;
1413 } else if (match_op(token
, '[')) {
1414 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1415 token
= constant_expression(token
->next
, &from
);
1416 if (match_op(token
, SPECIAL_ELLIPSIS
))
1417 token
= constant_expression(token
->next
, &to
);
1418 expr
= index_expression(from
, to
);
1420 tail
= &expr
->idx_expression
;
1421 token
= expect(token
, ']', "at end of initializer index");
1428 if (nested
&& !expect_equal
) {
1429 if (!match_op(token
, '='))
1430 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1435 token
= expect(token
, '=', "at end of initializer index");
1437 token
= initializer(tail
, token
);
1443 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1445 struct expression
*expr
;
1448 token
= single_initializer(&expr
, token
);
1451 add_expression(list
, expr
);
1452 if (!match_op(token
, ','))
1454 token
= token
->next
;
1459 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1461 if (match_op(token
, '{')) {
1462 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1464 token
= initializer_list(&expr
->expr_list
, token
->next
);
1465 return expect(token
, '}', "at end of initializer");
1467 return assignment_expression(token
, tree
);
1470 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1473 warning(sym
->pos
, "no identifier for function argument");
1476 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1479 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1480 struct symbol_list
**list
)
1482 struct symbol_list
**old_symbol_list
;
1483 struct symbol
*base_type
= decl
->ctype
.base_type
;
1484 struct statement
*stmt
, **p
;
1487 old_symbol_list
= function_symbol_list
;
1488 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1489 function_symbol_list
= &decl
->inline_symbol_list
;
1490 p
= &base_type
->inline_stmt
;
1492 function_symbol_list
= &decl
->symbol_list
;
1493 p
= &base_type
->stmt
;
1495 function_computed_target_list
= NULL
;
1496 function_computed_goto_list
= NULL
;
1498 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1499 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1500 warning(decl
->pos
, "function with external linkage has definition");
1502 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1503 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1505 stmt
= start_function(decl
);
1508 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1509 declare_argument(arg
, base_type
);
1510 } END_FOR_EACH_PTR(arg
);
1512 token
= compound_statement(token
->next
, stmt
);
1515 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1516 add_symbol(list
, decl
);
1517 check_declaration(decl
);
1518 function_symbol_list
= old_symbol_list
;
1519 if (function_computed_goto_list
) {
1520 if (!function_computed_target_list
)
1521 warning(decl
->pos
, "function has computed goto but no targets?");
1523 struct statement
*stmt
;
1524 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1525 stmt
->target_list
= function_computed_target_list
;
1526 } END_FOR_EACH_PTR(stmt
);
1529 return expect(token
, '}', "at end of function");
1532 static void promote_k_r_types(struct symbol
*arg
)
1534 struct symbol
*base
= arg
->ctype
.base_type
;
1535 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1536 arg
->ctype
.base_type
= &int_ctype
;
1540 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1542 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1545 FOR_EACH_PTR(real_args
, arg
) {
1546 struct symbol
*type
;
1548 /* This is quadratic in the number of arguments. We _really_ don't care */
1549 FOR_EACH_PTR(argtypes
, type
) {
1550 if (type
->ident
== arg
->ident
)
1552 } END_FOR_EACH_PTR(type
);
1553 warning(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1557 /* "char" and "short" promote to "int" */
1558 promote_k_r_types(type
);
1560 arg
->ctype
= type
->ctype
;
1561 } END_FOR_EACH_PTR(arg
);
1563 FOR_EACH_PTR(argtypes
, arg
) {
1565 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1566 } END_FOR_EACH_PTR(arg
);
1570 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1571 struct symbol_list
**list
)
1573 struct symbol_list
*args
= NULL
;
1575 warning(token
->pos
, "non-ANSI function declaration");
1577 token
= external_declaration(token
, &args
);
1578 } while (lookup_type(token
));
1580 apply_k_r_types(args
, decl
);
1582 if (!match_op(token
, '{')) {
1583 warning(token
->pos
, "expected function body");
1586 return parse_function_body(token
, decl
, list
);
1590 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1592 struct ident
*ident
= NULL
;
1593 struct symbol
*decl
;
1594 struct ctype ctype
= { 0, };
1595 struct symbol
*base_type
;
1598 /* Top-level inline asm? */
1599 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1600 struct symbol_list
**old_symbol_list
;
1601 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1602 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1603 struct statement
*stmt
;
1605 anon
->ctype
.base_type
= fn
;
1606 old_symbol_list
= function_symbol_list
;
1607 function_symbol_list
= &anon
->symbol_list
;
1608 stmt
= start_function(anon
);
1609 token
= parse_asm(token
->next
, stmt
);
1611 function_symbol_list
= old_symbol_list
;
1612 add_symbol(list
, anon
);
1616 /* Parse declaration-specifiers, if any */
1617 token
= declaration_specifiers(token
, &ctype
, 0);
1618 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1619 decl
->ctype
= ctype
;
1620 token
= declarator(token
, decl
, &ident
);
1622 /* Just a type declaration? */
1624 return expect(token
, ';', "end of type declaration");
1626 /* type define declaration? */
1627 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1629 /* Typedef's don't have meaningful storage */
1631 ctype
.modifiers
&= ~MOD_STORAGE
;
1632 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1633 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1636 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1638 base_type
= decl
->ctype
.base_type
;
1639 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1640 /* K&R argument declaration? */
1641 if (lookup_type(token
))
1642 return parse_k_r_arguments(token
, decl
, list
);
1643 if (match_op(token
, '{'))
1644 return parse_function_body(token
, decl
, list
);
1646 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1647 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1648 } else if (!is_typedef
&& base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1649 warning(token
->pos
, "void declaration");
1653 if (!is_typedef
&& match_op(token
, '=')) {
1654 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1655 warning(decl
->pos
, "symbol with external linkage has initializer");
1656 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1658 token
= initializer(&decl
->initializer
, token
->next
);
1661 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1662 add_symbol(list
, decl
);
1663 fn_local_symbol(decl
);
1666 check_declaration(decl
);
1668 if (!match_op(token
, ','))
1671 token
= token
->next
;
1673 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1674 decl
->ctype
= ctype
;
1675 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1676 token
= declarator(token
, decl
, &ident
);
1678 warning(token
->pos
, "expected identifier name in type definition");
1682 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1684 /* Function declarations are automatically extern unless specifically static */
1685 base_type
= decl
->ctype
.base_type
;
1686 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1687 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1688 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1691 return expect(token
, ';', "at end of declaration");
1694 struct symbol_list
*translation_unit(struct token
*token
)
1696 while (!eof_token(token
))
1697 token
= external_declaration(token
, &translation_unit_used_list
);
1698 // They aren't needed any more
1699 clear_token_alloc();
1701 /* Evaluate the symbol list */
1702 evaluate_symbol_list(translation_unit_used_list
);
1703 return translation_unit_used_list
;