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 static struct token
*struct_union_enum_specifier(enum type type
,
109 struct token
*token
, struct ctype
*ctype
,
110 struct token
*(*parse
)(struct token
*, struct symbol
*))
114 ctype
->modifiers
= 0;
115 if (token_type(token
) == TOKEN_IDENT
) {
116 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
118 (sym
->scope
!= block_scope
&&
119 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
120 // Either a new symbol, or else an out-of-scope
121 // symbol being redefined.
122 sym
= alloc_symbol(token
->pos
, type
);
123 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
125 if (sym
->type
!= type
)
126 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
128 ctype
->base_type
= sym
;
129 if (match_op(token
, '{')) {
130 // The following test is actually wrong for empty
131 // structs, but (1) they are not C99, (2) gcc does
132 // the same thing, and (3) it's easier.
133 if (sym
->symbol_list
)
134 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
135 token
= parse(token
->next
, sym
);
136 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
138 // Mark the structure as needing re-examination
144 // private struct/union/enum type
145 if (!match_op(token
, '{')) {
146 warning(token
->pos
, "expected declaration");
147 ctype
->base_type
= &bad_ctype
;
151 sym
= alloc_symbol(token
->pos
, type
);
152 token
= parse(token
->next
, sym
);
153 ctype
->base_type
= sym
;
154 return expect(token
, '}', "at end of specifier");
157 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
159 return struct_declaration_list(token
, &sym
->symbol_list
);
162 static struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
164 return struct_union_enum_specifier(type
, token
, ctype
, parse_struct_declaration
);
169 unsigned long long y
;
172 static void upper_boundary(Num
*n
, Num
*v
)
184 static void lower_boundary(Num
*n
, Num
*v
)
196 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
198 int shift
= type
->bit_size
;
199 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
203 if (upper
->x
== 0 && upper
->y
>> shift
)
205 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
210 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
212 unsigned long long lastval
= 0;
213 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
214 Num upper
= {-1, 0}, lower
= {1, 0};
216 parent
->examined
= 1;
217 parent
->ctype
.base_type
= &int_ctype
;
218 while (token_type(token
) == TOKEN_IDENT
) {
219 struct expression
*expr
= NULL
;
220 struct token
*next
= token
->next
;
223 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
224 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
225 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
227 if (match_op(next
, '=')) {
228 next
= constant_expression(next
->next
, &expr
);
229 lastval
= get_expression_value(expr
);
231 if (expr
&& expr
->ctype
)
235 } else if (is_int_type(ctype
)) {
238 error_die(token
->pos
, "can't increment the last enum member");
242 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
243 expr
->value
= lastval
;
246 sym
->initializer
= expr
;
247 sym
->ctype
.base_type
= parent
;
249 if (base_type
!= &bad_ctype
) {
250 if (ctype
->type
== SYM_NODE
)
251 ctype
= ctype
->ctype
.base_type
;
252 if (ctype
->type
== SYM_ENUM
) {
256 ctype
= ctype
->ctype
.base_type
;
260 * - if all enum's are of the same type, then
261 * the base_type is that type (two first
263 * - if enums are of different types, they
264 * all have to be integer types, and the
265 * base type is "int_ctype".
266 * - otherwise the base_type is "bad_ctype".
270 } else if (ctype
== base_type
) {
272 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
273 base_type
= &int_ctype
;
275 base_type
= &bad_ctype
;
277 if (is_int_type(base_type
)) {
278 Num v
= {.y
= lastval
};
279 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
281 else if ((long long)lastval
>= 0)
285 upper_boundary(&upper
, &v
);
286 lower_boundary(&lower
, &v
);
289 if (!match_op(token
, ','))
294 base_type
= &bad_ctype
;
295 else if (!is_int_type(base_type
))
296 base_type
= base_type
;
297 else if (type_is_ok(base_type
, &upper
, &lower
))
298 base_type
= base_type
;
299 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
300 base_type
= &int_ctype
;
301 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
302 base_type
= &uint_ctype
;
303 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
304 base_type
= &long_ctype
;
305 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
306 base_type
= &ulong_ctype
;
307 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
308 base_type
= &llong_ctype
;
309 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
310 base_type
= &ullong_ctype
;
312 base_type
= &bad_ctype
;
313 parent
->ctype
.base_type
= base_type
;
314 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
315 parent
->examined
= 0;
319 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
321 return struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
324 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
328 if (!match_op(token
, '(')) {
329 warning(token
->pos
, "expected '(' after typeof");
332 if (lookup_type(token
->next
)) {
333 token
= typename(token
->next
, &sym
);
336 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
337 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
339 ctype
->modifiers
= 0;
340 ctype
->base_type
= typeof_sym
;
342 return expect(token
, ')', "after typeof");
345 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
347 if (attribute
== &packed_ident
||
348 attribute
== &__packed___ident
) {
349 ctype
->alignment
= 1;
352 if (attribute
== &aligned_ident
||
353 attribute
== &__aligned___ident
) {
354 int alignment
= max_alignment
;
356 alignment
= get_expression_value(expr
);
357 ctype
->alignment
= alignment
;
360 if (attribute
== &nocast_ident
) {
361 ctype
->modifiers
|= MOD_NOCAST
;
364 if (attribute
== &noderef_ident
) {
365 ctype
->modifiers
|= MOD_NODEREF
;
368 if (attribute
== &safe_ident
) {
369 ctype
->modifiers
|= MOD_SAFE
;
372 if (attribute
== &force_ident
) {
373 ctype
->modifiers
|= MOD_FORCE
;
376 if (attribute
== &bitwise_ident
) {
378 ctype
->modifiers
|= MOD_BITWISE
;
381 if (attribute
== &address_space_ident
) {
383 return "expected address space number";
384 ctype
->as
= get_expression_value(expr
);
387 if (attribute
== &context_ident
) {
388 if (expr
&& expr
->type
== EXPR_COMMA
) {
389 int input
= get_expression_value(expr
->left
);
390 int output
= get_expression_value(expr
->right
);
391 ctype
->in_context
= input
;
392 ctype
->out_context
= output
;
395 return "expected context input/output values";
397 if (attribute
== &mode_ident
||
398 attribute
== &__mode___ident
) {
399 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
400 struct ident
*ident
= expr
->symbol_name
;
403 * Match against __QI__/__HI__/__SI__/__DI__
405 * FIXME! This is broken - we don't actually get
406 * the type information updated properly at this
407 * stage for some reason.
409 if (ident
== &__QI___ident
||
410 ident
== &QI_ident
) {
411 ctype
->modifiers
|= MOD_CHAR
;
414 if (ident
== &__HI___ident
||
415 ident
== &HI_ident
) {
416 ctype
->modifiers
|= MOD_SHORT
;
419 if (ident
== &__SI___ident
||
420 ident
== &SI_ident
) {
424 if (ident
== &__DI___ident
||
425 ident
== &DI_ident
) {
426 ctype
->modifiers
|= MOD_LONGLONG
;
429 if (ident
== &__word___ident
||
430 ident
== &word_ident
) {
431 ctype
->modifiers
|= MOD_LONG
;
434 return "unknown mode attribute";
436 return "expected attribute mode symbol";
439 /* Throw away for now.. */
440 if (attribute
== &format_ident
||
441 attribute
== &__format___ident
||
442 attribute
== &__format_arg___ident
)
444 if (attribute
== §ion_ident
||
445 attribute
== &__section___ident
)
447 if (attribute
== &unused_ident
||
448 attribute
== &__unused___ident
)
450 if (attribute
== &const_ident
||
451 attribute
== &__const_ident
||
452 attribute
== &__const___ident
)
454 if (attribute
== &noreturn_ident
||
455 attribute
== &__noreturn___ident
)
457 if (attribute
== ®parm_ident
)
459 if (attribute
== &weak_ident
)
461 if (attribute
== &alias_ident
)
463 if (attribute
== &pure_ident
)
465 if (attribute
== &always_inline_ident
)
467 if (attribute
== &syscall_linkage_ident
)
469 if (attribute
== &visibility_ident
)
471 if (attribute
== &model_ident
||
472 attribute
== &__model___ident
)
475 return "unknown attribute";
478 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
480 ctype
->modifiers
= 0;
481 token
= expect(token
, '(', "after attribute");
482 token
= expect(token
, '(', "after attribute");
486 struct ident
*attribute_name
;
487 struct expression
*attribute_expr
;
489 if (eof_token(token
))
491 if (match_op(token
, ';'))
493 if (token_type(token
) != TOKEN_IDENT
)
495 attribute_name
= token
->ident
;
497 attribute_expr
= NULL
;
498 if (match_op(token
, '('))
499 token
= parens_expression(token
, &attribute_expr
, "in attribute");
500 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
502 warning(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
503 if (!match_op(token
, ','))
508 token
= expect(token
, ')', "after attribute");
509 token
= expect(token
, ')', "after attribute");
513 struct symbol
* ctype_integer(unsigned long spec
)
515 static struct symbol
*const integer_ctypes
[][3] = {
516 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
517 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
518 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
519 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
520 { &int_ctype
, &sint_ctype
, &uint_ctype
},
522 struct symbol
*const (*ctype
)[3];
525 ctype
= integer_ctypes
;
526 if (!(spec
& MOD_LONGLONG
)) {
528 if (!(spec
& MOD_LONG
)) {
530 if (!(spec
& MOD_SHORT
)) {
532 if (!(spec
& MOD_CHAR
))
538 sub
= ((spec
& MOD_UNSIGNED
)
540 : ((spec
& MOD_EXPLICITLY_SIGNED
)
544 return ctype
[0][sub
];
547 struct symbol
* ctype_fp(unsigned long spec
)
549 if (spec
& MOD_LONGLONG
)
550 return &ldouble_ctype
;
552 return &double_ctype
;
556 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
558 unsigned long mod
= thistype
->modifiers
;
561 unsigned long old
= ctype
->modifiers
;
562 unsigned long extra
= 0, dup
, conflict
;
564 if (mod
& old
& MOD_LONG
) {
565 extra
= MOD_LONGLONG
| MOD_LONG
;
569 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
571 warning(pos
, "Just how %sdo you want this type to be?",
572 modifier_string(dup
));
574 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
576 warning(pos
, "You cannot have both long and short modifiers.");
578 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
580 warning(pos
, "You cannot have both signed and unsigned modifiers.");
582 // Only one storage modifier allowed, except that "inline" doesn't count.
583 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
584 conflict
&= (conflict
- 1);
586 warning(pos
, "multiple storage classes");
588 ctype
->modifiers
= old
| mod
| extra
;
591 /* Context mask and value */
592 ctype
->in_context
+= thistype
->in_context
;
593 ctype
->out_context
+= thistype
->out_context
;
596 if (thistype
->alignment
& (thistype
->alignment
-1)) {
597 warning(pos
, "I don't like non-power-of-2 alignments");
598 thistype
->alignment
= 0;
600 if (thistype
->alignment
> ctype
->alignment
)
601 ctype
->alignment
= thistype
->alignment
;
604 ctype
->as
= thistype
->as
;
607 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
609 unsigned long banned
, wrong
;
610 unsigned long this_mod
= s
->ctype
.modifiers
;
611 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
612 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
614 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
615 banned
= BANNED_SIZE
| BANNED_SIGN
;
616 else if (this_mod
& MOD_SPECIALBITS
)
618 else if (s
->ctype
.base_type
== &fp_type
)
619 banned
= BANNED_SIGN
;
620 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
626 // vector_type <-- whatever that is
627 banned
= BANNED_SIZE
| BANNED_SIGN
;
630 wrong
= mod
& banned
;
632 warning(*pos
, "modifier %sis invalid in this context",
633 modifier_string (wrong
));
637 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
641 while ( (token
= next
) != NULL
) {
642 struct ctype thistype
;
644 struct symbol
*s
, *type
;
648 if (token_type(token
) != TOKEN_IDENT
)
650 ident
= token
->ident
;
652 s
= lookup_symbol(ident
, NS_TYPEDEF
);
656 mod
= thistype
.modifiers
;
657 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
659 if (mod
& MOD_SPECIALBITS
) {
660 if (mod
& MOD_STRUCTOF
)
661 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
662 else if (mod
& MOD_UNIONOF
)
663 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
664 else if (mod
& MOD_ENUMOF
)
665 next
= enum_specifier(next
, &thistype
);
666 else if (mod
& MOD_ATTRIBUTE
)
667 next
= attribute_specifier(next
, &thistype
);
668 else if (mod
& MOD_TYPEOF
)
669 next
= typeof_specifier(next
, &thistype
);
670 mod
= thistype
.modifiers
;
672 type
= thistype
.base_type
;
676 if (ctype
->base_type
)
678 /* User types only mix with qualifiers */
679 if (mod
& MOD_USERTYPE
) {
680 if (ctype
->modifiers
& MOD_SPECIFIER
)
683 ctype
->base_type
= type
;
686 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
687 apply_ctype(token
->pos
, &thistype
, ctype
);
690 /* Turn the "virtual types" into real types with real sizes etc */
691 if (!ctype
->base_type
) {
692 struct symbol
*base
= &incomplete_ctype
;
695 * If we have modifiers, we'll default to an integer
696 * type, and "ctype_integer()" will turn this into
699 if (ctype
->modifiers
& MOD_SPECIFIER
)
701 ctype
->base_type
= base
;
703 if (ctype
->base_type
== &int_type
) {
704 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
705 ctype
->modifiers
&= ~MOD_SPECIFIER
;
706 } else if (ctype
->base_type
== &fp_type
) {
707 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
708 ctype
->modifiers
&= ~MOD_SPECIFIER
;
710 if (ctype
->modifiers
& MOD_BITWISE
) {
712 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
713 if (!is_int_type(ctype
->base_type
)) {
714 warning(token
->pos
, "invalid modifier");
717 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
718 *type
= *ctype
->base_type
;
719 type
->ctype
.base_type
= ctype
->base_type
;
720 type
->type
= SYM_RESTRICT
;
721 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
722 ctype
->base_type
= type
;
727 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
729 struct expression
*expr
= NULL
;
731 token
= parse_expression(token
, &expr
);
732 sym
->array_size
= expr
;
736 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
737 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
739 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
742 if (token_type(token
) != TOKEN_IDENT
)
744 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
745 struct ctype thistype
= { 0, };
746 token
= attribute_specifier(token
->next
, &thistype
);
747 apply_ctype(token
->pos
, &thistype
, ctype
);
750 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
751 struct expression
*expr
;
752 token
= expect(token
->next
, '(', "after asm");
753 token
= parse_expression(token
->next
, &expr
);
754 token
= expect(token
, ')', "after asm");
762 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
764 struct ctype
*ctype
= &decl
->ctype
;
766 if (p
&& token_type(token
) == TOKEN_IDENT
) {
772 token
= handle_attributes(token
, ctype
);
774 if (token_type(token
) != TOKEN_SPECIAL
)
778 * This can be either a parameter list or a grouping.
779 * For the direct (non-abstract) case, we know if must be
780 * a parameter list if we already saw the identifier.
781 * For the abstract case, we know if must be a parameter
782 * list if it is empty or starts with a type.
784 if (token
->special
== '(') {
786 struct token
*next
= token
->next
;
787 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
790 struct symbol
*base_type
= ctype
->base_type
;
791 token
= declarator(next
, decl
, p
);
792 token
= expect(token
, ')', "in nested declarator");
793 while (ctype
->base_type
!= base_type
)
794 ctype
= &ctype
->base_type
->ctype
;
799 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
800 token
= parameter_type_list(next
, sym
, p
);
801 token
= expect(token
, ')', "in function declarator");
804 if (token
->special
== '[') {
805 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
806 token
= abstract_array_declarator(token
->next
, array
);
807 token
= expect(token
, ']', "in abstract_array_declarator");
808 ctype
= &array
->ctype
;
816 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
818 unsigned long modifiers
;
819 struct symbol
*base_type
;
821 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
822 base_type
= ctype
->base_type
;
823 ctype
->modifiers
= modifiers
;
825 while (match_op(token
,'*')) {
826 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
827 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
828 ptr
->ctype
.as
= ctype
->as
;
829 ptr
->ctype
.in_context
+= ctype
->in_context
;
830 ptr
->ctype
.out_context
+= ctype
->out_context
;
831 ptr
->ctype
.base_type
= base_type
;
834 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
835 ctype
->base_type
= base_type
;
837 ctype
->in_context
= 0;
838 ctype
->out_context
= 0;
840 token
= declaration_specifiers(token
->next
, ctype
, 1);
841 modifiers
= ctype
->modifiers
;
846 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
848 token
= pointer(token
, &sym
->ctype
);
849 return direct_declarator(token
, sym
, p
);
852 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
854 struct ctype
*ctype
= &decl
->ctype
;
855 struct expression
*expr
;
856 struct symbol
*bitfield
;
859 if (!is_int_type(ctype
->base_type
)) {
860 warning(token
->pos
, "invalid bitfield specifier for type %s.",
861 show_typename(ctype
->base_type
));
862 // Parse this to recover gracefully.
863 return conditional_expression(token
->next
, &expr
);
866 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
867 token
= conditional_expression(token
->next
, &expr
);
868 width
= get_expression_value(expr
);
869 bitfield
->bit_size
= width
;
871 if (width
< 0 || width
> INT_MAX
) {
872 warning(token
->pos
, "invalid bitfield width, %lld.", width
);
874 } else if (decl
->ident
&& width
== 0) {
875 warning(token
->pos
, "invalid named zero-width bitfield `%s'",
876 show_ident(decl
->ident
));
878 } else if (decl
->ident
) {
879 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
880 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
881 if (width
== 1 && is_signed
) {
882 // Valid values are either {-1;0} or {0}, depending on integer
883 // representation. The latter makes for very efficient code...
884 warning(token
->pos
, "dubious one-bit signed bitfield");
886 if (Wdefault_bitfield_sign
&&
887 base_type
->type
!= SYM_ENUM
&&
888 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
890 // The sign of bitfields is unspecified by default.
891 warning (token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
894 bitfield
->bit_size
= width
;
898 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
900 while (!match_op(token
, '}')) {
901 struct ctype ctype
= {0, };
903 token
= declaration_specifiers(token
, &ctype
, 0);
905 struct ident
*ident
= NULL
;
906 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
908 token
= declarator(token
, decl
, &ident
);
910 if (match_op(token
, ':')) {
911 token
= handle_bitfield(token
, decl
);
912 token
= handle_attributes(token
, &decl
->ctype
);
914 add_symbol(list
, decl
);
915 if (!match_op(token
, ','))
919 if (!match_op(token
, ';')) {
920 warning(token
->pos
, "expected ; at end of declaration");
928 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
930 struct ident
*ident
= NULL
;
932 struct ctype ctype
= { 0, };
934 token
= declaration_specifiers(token
, &ctype
, 0);
935 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
938 token
= declarator(token
, sym
, &ident
);
943 struct token
*typename(struct token
*token
, struct symbol
**p
)
945 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
947 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
948 return declarator(token
, sym
, NULL
);
951 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
953 token
= parse_expression(token
, tree
);
954 return expect(token
, ';', "at end of statement");
957 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
958 struct expression_list
**inout
)
960 struct expression
*expr
;
962 /* Allow empty operands */
963 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
966 struct ident
*ident
= NULL
;
967 if (match_op(token
->next
, '[') &&
968 token_type(token
->next
->next
) == TOKEN_IDENT
&&
969 match_op(token
->next
->next
->next
, ']')) {
970 ident
= token
->next
->next
->ident
;
971 token
= token
->next
->next
->next
;
973 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
974 token
= primary_expression(token
->next
, &expr
);
975 add_expression(inout
, expr
);
976 token
= parens_expression(token
, &expr
, "in asm parameter");
977 add_expression(inout
, expr
);
978 } while (match_op(token
, ','));
982 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
983 struct expression_list
**clobbers
)
985 struct expression
*expr
;
988 token
= primary_expression(token
->next
, &expr
);
989 add_expression(clobbers
, expr
);
990 } while (match_op(token
, ','));
994 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
996 stmt
->type
= STMT_ASM
;
997 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1000 token
= expect(token
, '(', "after asm");
1001 token
= parse_expression(token
, &stmt
->asm_string
);
1002 if (match_op(token
, ':'))
1003 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1004 if (match_op(token
, ':'))
1005 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1006 if (match_op(token
, ':'))
1007 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1008 token
= expect(token
, ')', "after asm");
1009 return expect(token
, ';', "at end of asm-statement");
1012 /* Make a statement out of an expression */
1013 static struct statement
*make_statement(struct expression
*expr
)
1015 struct statement
*stmt
;
1019 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1020 stmt
->expression
= expr
;
1025 * All iterators have two symbols associated with them:
1026 * the "continue" and "break" symbols, which are targets
1027 * for continue and break statements respectively.
1029 * They are in a special name-space, but they follow
1030 * all the normal visibility rules, so nested iterators
1031 * automatically work right.
1033 static void start_iterator(struct statement
*stmt
)
1035 struct symbol
*cont
, *brk
;
1037 start_symbol_scope();
1038 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1039 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1040 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1041 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1043 stmt
->type
= STMT_ITERATOR
;
1044 stmt
->iterator_break
= brk
;
1045 stmt
->iterator_continue
= cont
;
1046 fn_local_symbol(brk
);
1047 fn_local_symbol(cont
);
1050 static void end_iterator(struct statement
*stmt
)
1055 static struct statement
*start_function(struct symbol
*sym
)
1058 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1060 start_function_scope();
1061 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1062 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1063 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1064 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1065 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1067 fn_local_symbol(ret
);
1069 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1075 static void end_function(struct symbol
*sym
)
1078 end_function_scope();
1082 * A "switch()" statement, like an iterator, has a
1083 * the "break" symbol associated with it. It works
1084 * exactly like the iterator break - it's the target
1085 * for any break-statements in scope, and means that
1086 * "break" handling doesn't even need to know whether
1087 * it's breaking out of an iterator or a switch.
1089 * In addition, the "case" symbol is a marker for the
1090 * case/default statements to find the switch statement
1091 * that they are associated with.
1093 static void start_switch(struct statement
*stmt
)
1095 struct symbol
*brk
, *switch_case
;
1097 start_symbol_scope();
1098 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1099 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1101 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1102 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1103 switch_case
->stmt
= stmt
;
1105 stmt
->type
= STMT_SWITCH
;
1106 stmt
->switch_break
= brk
;
1107 stmt
->switch_case
= switch_case
;
1109 fn_local_symbol(brk
);
1110 fn_local_symbol(switch_case
);
1113 static void end_switch(struct statement
*stmt
)
1115 if (!stmt
->switch_case
->symbol_list
)
1116 warning(stmt
->pos
, "switch with no cases");
1120 static void add_case_statement(struct statement
*stmt
)
1122 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1126 warning(stmt
->pos
, "not in switch scope");
1127 stmt
->type
= STMT_NONE
;
1130 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1131 add_symbol(&target
->symbol_list
, sym
);
1133 stmt
->case_label
= sym
;
1134 fn_local_symbol(sym
);
1137 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1139 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1142 error_die(token
->pos
, "internal error: return without a function target");
1143 stmt
->type
= STMT_RETURN
;
1144 stmt
->ret_target
= target
;
1145 return expression_statement(token
->next
, &stmt
->ret_value
);
1148 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1150 struct symbol_list
*syms
;
1151 struct expression
*e1
, *e2
, *e3
;
1152 struct statement
*iterator
;
1154 start_iterator(stmt
);
1155 token
= expect(token
->next
, '(', "after 'for'");
1159 /* C99 variable declaration? */
1160 if (lookup_type(token
)) {
1161 token
= external_declaration(token
, &syms
);
1163 token
= parse_expression(token
, &e1
);
1164 token
= expect(token
, ';', "in 'for'");
1166 token
= parse_expression(token
, &e2
);
1167 token
= expect(token
, ';', "in 'for'");
1168 token
= parse_expression(token
, &e3
);
1169 token
= expect(token
, ')', "in 'for'");
1170 token
= statement(token
, &iterator
);
1172 stmt
->iterator_syms
= syms
;
1173 stmt
->iterator_pre_statement
= make_statement(e1
);
1174 stmt
->iterator_pre_condition
= e2
;
1175 stmt
->iterator_post_statement
= make_statement(e3
);
1176 stmt
->iterator_post_condition
= e2
;
1177 stmt
->iterator_statement
= iterator
;
1183 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1185 struct expression
*expr
;
1186 struct statement
*iterator
;
1188 start_iterator(stmt
);
1189 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1190 token
= statement(token
, &iterator
);
1192 stmt
->iterator_pre_condition
= expr
;
1193 stmt
->iterator_post_condition
= expr
;
1194 stmt
->iterator_statement
= iterator
;
1200 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1202 struct expression
*expr
;
1203 struct statement
*iterator
;
1205 start_iterator(stmt
);
1206 token
= statement(token
->next
, &iterator
);
1207 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1208 token
= token
->next
;
1210 warning(token
->pos
, "expected 'while' after 'do'");
1211 token
= parens_expression(token
, &expr
, "after 'do-while'");
1213 stmt
->iterator_post_condition
= expr
;
1214 stmt
->iterator_statement
= iterator
;
1217 return expect(token
, ';', "after statement");
1220 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1222 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1225 if (token_type(token
) == TOKEN_IDENT
) {
1226 if (token
->ident
== &if_ident
) {
1227 stmt
->type
= STMT_IF
;
1228 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1229 token
= statement(token
, &stmt
->if_true
);
1230 if (token_type(token
) != TOKEN_IDENT
)
1232 if (token
->ident
!= &else_ident
)
1234 return statement(token
->next
, &stmt
->if_false
);
1237 if (token
->ident
== &return_ident
)
1238 return parse_return_statement(token
, stmt
);
1240 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1241 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1242 stmt
->type
= STMT_GOTO
;
1243 stmt
->goto_label
= target
;
1245 warning(stmt
->pos
, "break/continue not in iterator scope");
1246 return expect(token
->next
, ';', "at end of statement");
1248 if (token
->ident
== &default_ident
) {
1249 token
= token
->next
;
1250 goto default_statement
;
1252 if (token
->ident
== &case_ident
) {
1253 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1254 if (match_op(token
, SPECIAL_ELLIPSIS
))
1255 token
= parse_expression(token
->next
, &stmt
->case_to
);
1257 stmt
->type
= STMT_CASE
;
1258 token
= expect(token
, ':', "after default/case");
1259 add_case_statement(stmt
);
1260 return statement(token
, &stmt
->case_statement
);
1262 if (token
->ident
== &switch_ident
) {
1263 stmt
->type
= STMT_SWITCH
;
1265 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1266 token
= statement(token
, &stmt
->switch_statement
);
1270 if (token
->ident
== &for_ident
)
1271 return parse_for_statement(token
, stmt
);
1273 if (token
->ident
== &while_ident
)
1274 return parse_while_statement(token
, stmt
);
1276 if (token
->ident
== &do_ident
)
1277 return parse_do_statement(token
, stmt
);
1279 if (token
->ident
== &goto_ident
) {
1280 stmt
->type
= STMT_GOTO
;
1281 token
= token
->next
;
1282 if (match_op(token
, '*')) {
1283 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1284 add_statement(&function_computed_goto_list
, stmt
);
1285 } else if (token_type(token
) == TOKEN_IDENT
) {
1286 stmt
->goto_label
= label_symbol(token
);
1287 token
= token
->next
;
1289 warning(token
->pos
, "Expected identifier or goto expression");
1291 return expect(token
, ';', "at end of statement");
1293 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1294 return parse_asm(token
->next
, stmt
);
1296 if (token
->ident
== &__context___ident
) {
1297 stmt
->type
= STMT_CONTEXT
;
1298 token
= parse_expression(token
->next
, &stmt
->expression
);
1299 return expect(token
, ';', "at end of statement");
1301 if (token
->ident
== &__range___ident
) {
1302 stmt
->type
= STMT_RANGE
;
1303 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1304 token
= expect(token
, ',', "after range expression");
1305 token
= assignment_expression(token
, &stmt
->range_low
);
1306 token
= expect(token
, ',', "after low range");
1307 token
= assignment_expression(token
, &stmt
->range_high
);
1308 return expect(token
, ';', "after range statement");
1310 if (match_op(token
->next
, ':')) {
1311 stmt
->type
= STMT_LABEL
;
1312 stmt
->label_identifier
= label_symbol(token
);
1313 return statement(token
->next
->next
, &stmt
->label_statement
);
1317 if (match_op(token
, '{')) {
1318 stmt
->type
= STMT_COMPOUND
;
1319 start_symbol_scope();
1320 token
= compound_statement(token
->next
, stmt
);
1323 return expect(token
, '}', "at end of compound statement");
1326 stmt
->type
= STMT_EXPRESSION
;
1327 return expression_statement(token
, &stmt
->expression
);
1330 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1333 struct statement
* stmt
;
1334 if (eof_token(token
))
1336 if (match_op(token
, '}'))
1338 if (lookup_type(token
)) {
1339 if (warn_on_mixed
&& *list
)
1340 warning(token
->pos
, "mixing declarations and code");
1341 token
= external_declaration(token
, syms
);
1344 token
= statement(token
, &stmt
);
1345 add_statement(list
, stmt
);
1350 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1352 struct symbol_list
**list
= &fn
->arguments
;
1354 if (match_op(token
, ')')) {
1355 // No warning for "void oink ();"
1356 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1357 if (*p
&& !match_op(token
->next
, ';'))
1358 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1365 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1367 warning(token
->pos
, "variadic functions must have one named argument");
1369 token
= token
->next
;
1373 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1374 token
= parameter_declaration(token
, &sym
);
1375 if (sym
->ctype
.base_type
== &void_ctype
) {
1376 /* Special case: (void) */
1377 if (!*list
&& !sym
->ident
)
1379 warning(token
->pos
, "void parameter");
1381 add_symbol(list
, sym
);
1382 if (!match_op(token
, ','))
1384 token
= token
->next
;
1390 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1392 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1396 static struct expression
*identifier_expression(struct token
*token
)
1398 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1399 expr
->expr_ident
= token
->ident
;
1403 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1405 int idx_from
, idx_to
;
1406 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1408 idx_from
= get_expression_value(from
);
1411 idx_to
= get_expression_value(to
);
1412 if (idx_to
< idx_from
|| idx_from
< 0)
1413 warning(from
->pos
, "nonsense array initializer index range");
1415 expr
->idx_from
= idx_from
;
1416 expr
->idx_to
= idx_to
;
1420 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1422 int expect_equal
= 0;
1423 struct token
*next
= token
->next
;
1424 struct expression
**tail
= ep
;
1429 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1430 struct expression
*expr
= identifier_expression(token
);
1431 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1432 token
= initializer(&expr
->ident_expression
, next
->next
);
1433 if (expr
->ident_expression
)
1438 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1439 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1440 struct expression
*expr
= identifier_expression(next
);
1442 tail
= &expr
->ident_expression
;
1445 } else if (match_op(token
, '[')) {
1446 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1447 token
= constant_expression(token
->next
, &from
);
1448 if (match_op(token
, SPECIAL_ELLIPSIS
))
1449 token
= constant_expression(token
->next
, &to
);
1450 expr
= index_expression(from
, to
);
1452 tail
= &expr
->idx_expression
;
1453 token
= expect(token
, ']', "at end of initializer index");
1460 if (nested
&& !expect_equal
) {
1461 if (!match_op(token
, '='))
1462 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1467 token
= expect(token
, '=', "at end of initializer index");
1469 token
= initializer(tail
, token
);
1475 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1477 struct expression
*expr
;
1480 token
= single_initializer(&expr
, token
);
1483 add_expression(list
, expr
);
1484 if (!match_op(token
, ','))
1486 token
= token
->next
;
1491 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1493 if (match_op(token
, '{')) {
1494 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1496 token
= initializer_list(&expr
->expr_list
, token
->next
);
1497 return expect(token
, '}', "at end of initializer");
1499 return assignment_expression(token
, tree
);
1502 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1505 warning(sym
->pos
, "no identifier for function argument");
1508 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1511 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1512 struct symbol_list
**list
)
1514 struct symbol_list
**old_symbol_list
;
1515 struct symbol
*base_type
= decl
->ctype
.base_type
;
1516 struct statement
*stmt
, **p
;
1519 old_symbol_list
= function_symbol_list
;
1520 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1521 function_symbol_list
= &decl
->inline_symbol_list
;
1522 p
= &base_type
->inline_stmt
;
1524 function_symbol_list
= &decl
->symbol_list
;
1525 p
= &base_type
->stmt
;
1527 function_computed_target_list
= NULL
;
1528 function_computed_goto_list
= NULL
;
1530 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1531 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1532 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1534 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1535 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1537 stmt
= start_function(decl
);
1540 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1541 declare_argument(arg
, base_type
);
1542 } END_FOR_EACH_PTR(arg
);
1544 token
= compound_statement(token
->next
, stmt
);
1547 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1548 add_symbol(list
, decl
);
1549 check_declaration(decl
);
1550 function_symbol_list
= old_symbol_list
;
1551 if (function_computed_goto_list
) {
1552 if (!function_computed_target_list
)
1553 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1555 struct statement
*stmt
;
1556 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1557 stmt
->target_list
= function_computed_target_list
;
1558 } END_FOR_EACH_PTR(stmt
);
1561 return expect(token
, '}', "at end of function");
1564 static void promote_k_r_types(struct symbol
*arg
)
1566 struct symbol
*base
= arg
->ctype
.base_type
;
1567 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1568 arg
->ctype
.base_type
= &int_ctype
;
1572 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1574 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1577 FOR_EACH_PTR(real_args
, arg
) {
1578 struct symbol
*type
;
1580 /* This is quadratic in the number of arguments. We _really_ don't care */
1581 FOR_EACH_PTR(argtypes
, type
) {
1582 if (type
->ident
== arg
->ident
)
1584 } END_FOR_EACH_PTR(type
);
1585 warning(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1589 /* "char" and "short" promote to "int" */
1590 promote_k_r_types(type
);
1592 arg
->ctype
= type
->ctype
;
1593 } END_FOR_EACH_PTR(arg
);
1595 FOR_EACH_PTR(argtypes
, arg
) {
1597 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1598 } END_FOR_EACH_PTR(arg
);
1602 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1603 struct symbol_list
**list
)
1605 struct symbol_list
*args
= NULL
;
1607 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(decl
->ident
));
1609 token
= external_declaration(token
, &args
);
1610 } while (lookup_type(token
));
1612 apply_k_r_types(args
, decl
);
1614 if (!match_op(token
, '{')) {
1615 warning(token
->pos
, "expected function body");
1618 return parse_function_body(token
, decl
, list
);
1622 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1624 struct ident
*ident
= NULL
;
1625 struct symbol
*decl
;
1626 struct ctype ctype
= { 0, };
1627 struct symbol
*base_type
;
1630 /* Top-level inline asm? */
1631 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1632 struct symbol_list
**old_symbol_list
;
1633 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1634 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1635 struct statement
*stmt
;
1637 anon
->ctype
.base_type
= fn
;
1638 old_symbol_list
= function_symbol_list
;
1639 function_symbol_list
= &anon
->symbol_list
;
1640 stmt
= start_function(anon
);
1641 token
= parse_asm(token
->next
, stmt
);
1643 function_symbol_list
= old_symbol_list
;
1644 add_symbol(list
, anon
);
1648 /* Parse declaration-specifiers, if any */
1649 token
= declaration_specifiers(token
, &ctype
, 0);
1650 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1651 decl
->ctype
= ctype
;
1652 token
= declarator(token
, decl
, &ident
);
1654 /* Just a type declaration? */
1656 return expect(token
, ';', "end of type declaration");
1658 /* type define declaration? */
1659 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1661 /* Typedef's don't have meaningful storage */
1663 ctype
.modifiers
&= ~MOD_STORAGE
;
1664 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1665 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1668 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1670 base_type
= decl
->ctype
.base_type
;
1671 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1672 /* K&R argument declaration? */
1673 if (lookup_type(token
))
1674 return parse_k_r_arguments(token
, decl
, list
);
1675 if (match_op(token
, '{'))
1676 return parse_function_body(token
, decl
, list
);
1678 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1679 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1680 } else if (!is_typedef
&& base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1681 warning(token
->pos
, "void declaration");
1685 if (!is_typedef
&& match_op(token
, '=')) {
1686 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1687 warning(decl
->pos
, "symbol with external linkage has initializer");
1688 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1690 token
= initializer(&decl
->initializer
, token
->next
);
1693 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1694 add_symbol(list
, decl
);
1695 fn_local_symbol(decl
);
1698 check_declaration(decl
);
1700 if (!match_op(token
, ','))
1703 token
= token
->next
;
1705 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1706 decl
->ctype
= ctype
;
1707 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1708 token
= declarator(token
, decl
, &ident
);
1710 warning(token
->pos
, "expected identifier name in type definition");
1714 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1716 /* Function declarations are automatically extern unless specifically static */
1717 base_type
= decl
->ctype
.base_type
;
1718 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1719 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1720 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1723 return expect(token
, ';', "at end of declaration");
1726 struct symbol_list
*translation_unit(struct token
*token
)
1728 while (!eof_token(token
))
1729 token
= external_declaration(token
, &translation_unit_used_list
);
1730 // They aren't needed any more
1731 clear_token_alloc();
1733 /* Evaluate the symbol list */
1734 evaluate_symbol_list(translation_unit_used_list
);
1735 return translation_unit_used_list
;