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
||
377 attribute
== &__bitwise___ident
) {
379 ctype
->modifiers
|= MOD_BITWISE
;
382 if (attribute
== &address_space_ident
) {
384 return "expected address space number";
385 ctype
->as
= get_expression_value(expr
);
388 if (attribute
== &context_ident
) {
389 if (expr
&& expr
->type
== EXPR_COMMA
) {
390 int input
= get_expression_value(expr
->left
);
391 int output
= get_expression_value(expr
->right
);
392 ctype
->in_context
= input
;
393 ctype
->out_context
= output
;
396 return "expected context input/output values";
398 if (attribute
== &mode_ident
||
399 attribute
== &__mode___ident
) {
400 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
401 struct ident
*ident
= expr
->symbol_name
;
404 * Match against __QI__/__HI__/__SI__/__DI__
406 * FIXME! This is broken - we don't actually get
407 * the type information updated properly at this
408 * stage for some reason.
410 if (ident
== &__QI___ident
||
411 ident
== &QI_ident
) {
412 ctype
->modifiers
|= MOD_CHAR
;
415 if (ident
== &__HI___ident
||
416 ident
== &HI_ident
) {
417 ctype
->modifiers
|= MOD_SHORT
;
420 if (ident
== &__SI___ident
||
421 ident
== &SI_ident
) {
425 if (ident
== &__DI___ident
||
426 ident
== &DI_ident
) {
427 ctype
->modifiers
|= MOD_LONGLONG
;
430 if (ident
== &__word___ident
||
431 ident
== &word_ident
) {
432 ctype
->modifiers
|= MOD_LONG
;
435 return "unknown mode attribute";
437 return "expected attribute mode symbol";
440 /* Throw away for now.. */
441 if (attribute
== &__transparent_union___ident
) {
442 if (Wtransparent_union
)
443 return "ignoring attribute __transparent_union__";
446 if (attribute
== ¬hrow_ident
||
447 attribute
== &__nothrow_ident
||
448 attribute
== &__nothrow___ident
)
450 if (attribute
== &__malloc___ident
)
452 if (attribute
== &nonnull_ident
||
453 attribute
== &__nonnull_ident
||
454 attribute
== &__nonnull___ident
)
456 if (attribute
== &format_ident
||
457 attribute
== &__format___ident
||
458 attribute
== &__format_arg___ident
)
460 if (attribute
== §ion_ident
||
461 attribute
== &__section___ident
)
463 if (attribute
== &unused_ident
||
464 attribute
== &__unused___ident
)
466 if (attribute
== &const_ident
||
467 attribute
== &__const_ident
||
468 attribute
== &__const___ident
)
470 if (attribute
== &noreturn_ident
||
471 attribute
== &__noreturn___ident
)
473 if (attribute
== ®parm_ident
)
475 if (attribute
== &weak_ident
||
476 attribute
== &__weak___ident
)
478 if (attribute
== &alias_ident
)
480 if (attribute
== &pure_ident
||
481 attribute
== &__pure___ident
)
483 if (attribute
== &always_inline_ident
)
485 if (attribute
== &syscall_linkage_ident
)
487 if (attribute
== &visibility_ident
)
489 if (attribute
== &deprecated_ident
||
490 attribute
== &__deprecated___ident
)
492 if (attribute
== &noinline_ident
)
494 if (attribute
== &__used___ident
)
496 if (attribute
== &warn_unused_result_ident
||
497 attribute
== &__warn_unused_result___ident
)
499 if (attribute
== &model_ident
||
500 attribute
== &__model___ident
)
503 return "unknown attribute";
506 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
508 ctype
->modifiers
= 0;
509 token
= expect(token
, '(', "after attribute");
510 token
= expect(token
, '(', "after attribute");
514 struct ident
*attribute_name
;
515 struct expression
*attribute_expr
;
517 if (eof_token(token
))
519 if (match_op(token
, ';'))
521 if (token_type(token
) != TOKEN_IDENT
)
523 attribute_name
= token
->ident
;
525 attribute_expr
= NULL
;
526 if (match_op(token
, '('))
527 token
= parens_expression(token
, &attribute_expr
, "in attribute");
528 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
530 warning(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
531 if (!match_op(token
, ','))
536 token
= expect(token
, ')', "after attribute");
537 token
= expect(token
, ')', "after attribute");
541 struct symbol
* ctype_integer(unsigned long spec
)
543 static struct symbol
*const integer_ctypes
[][3] = {
544 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
545 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
546 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
547 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
548 { &int_ctype
, &sint_ctype
, &uint_ctype
},
550 struct symbol
*const (*ctype
)[3];
553 ctype
= integer_ctypes
;
554 if (!(spec
& MOD_LONGLONG
)) {
556 if (!(spec
& MOD_LONG
)) {
558 if (!(spec
& MOD_SHORT
)) {
560 if (!(spec
& MOD_CHAR
))
566 sub
= ((spec
& MOD_UNSIGNED
)
568 : ((spec
& MOD_EXPLICITLY_SIGNED
)
572 return ctype
[0][sub
];
575 struct symbol
* ctype_fp(unsigned long spec
)
577 if (spec
& MOD_LONGLONG
)
578 return &ldouble_ctype
;
580 return &double_ctype
;
584 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
586 unsigned long mod
= thistype
->modifiers
;
589 unsigned long old
= ctype
->modifiers
;
590 unsigned long extra
= 0, dup
, conflict
;
592 if (mod
& old
& MOD_LONG
) {
593 extra
= MOD_LONGLONG
| MOD_LONG
;
597 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
599 warning(pos
, "Just how %sdo you want this type to be?",
600 modifier_string(dup
));
602 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
604 warning(pos
, "You cannot have both long and short modifiers.");
606 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
608 warning(pos
, "You cannot have both signed and unsigned modifiers.");
610 // Only one storage modifier allowed, except that "inline" doesn't count.
611 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
612 conflict
&= (conflict
- 1);
614 warning(pos
, "multiple storage classes");
616 ctype
->modifiers
= old
| mod
| extra
;
619 /* Context mask and value */
620 ctype
->in_context
+= thistype
->in_context
;
621 ctype
->out_context
+= thistype
->out_context
;
624 if (thistype
->alignment
& (thistype
->alignment
-1)) {
625 warning(pos
, "I don't like non-power-of-2 alignments");
626 thistype
->alignment
= 0;
628 if (thistype
->alignment
> ctype
->alignment
)
629 ctype
->alignment
= thistype
->alignment
;
632 ctype
->as
= thistype
->as
;
635 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
637 unsigned long banned
, wrong
;
638 unsigned long this_mod
= s
->ctype
.modifiers
;
639 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
640 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
642 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
643 banned
= BANNED_SIZE
| BANNED_SIGN
;
644 else if (this_mod
& MOD_SPECIALBITS
)
646 else if (s
->ctype
.base_type
== &fp_type
)
647 banned
= BANNED_SIGN
;
648 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
654 // vector_type <-- whatever that is
655 banned
= BANNED_SIZE
| BANNED_SIGN
;
658 wrong
= mod
& banned
;
660 warning(*pos
, "modifier %sis invalid in this context",
661 modifier_string (wrong
));
665 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
669 while ( (token
= next
) != NULL
) {
670 struct ctype thistype
;
672 struct symbol
*s
, *type
;
676 if (token_type(token
) != TOKEN_IDENT
)
678 ident
= token
->ident
;
680 s
= lookup_symbol(ident
, NS_TYPEDEF
);
684 mod
= thistype
.modifiers
;
685 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
687 if (mod
& MOD_SPECIALBITS
) {
688 if (mod
& MOD_STRUCTOF
)
689 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
690 else if (mod
& MOD_UNIONOF
)
691 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
692 else if (mod
& MOD_ENUMOF
)
693 next
= enum_specifier(next
, &thistype
);
694 else if (mod
& MOD_ATTRIBUTE
)
695 next
= attribute_specifier(next
, &thistype
);
696 else if (mod
& MOD_TYPEOF
)
697 next
= typeof_specifier(next
, &thistype
);
698 mod
= thistype
.modifiers
;
700 type
= thistype
.base_type
;
704 if (ctype
->base_type
)
706 /* User types only mix with qualifiers */
707 if (mod
& MOD_USERTYPE
) {
708 if (ctype
->modifiers
& MOD_SPECIFIER
)
711 ctype
->base_type
= type
;
714 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
715 apply_ctype(token
->pos
, &thistype
, ctype
);
718 /* Turn the "virtual types" into real types with real sizes etc */
719 if (!ctype
->base_type
) {
720 struct symbol
*base
= &incomplete_ctype
;
723 * If we have modifiers, we'll default to an integer
724 * type, and "ctype_integer()" will turn this into
727 if (ctype
->modifiers
& MOD_SPECIFIER
)
729 ctype
->base_type
= base
;
731 if (ctype
->base_type
== &int_type
) {
732 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
733 ctype
->modifiers
&= ~MOD_SPECIFIER
;
734 } else if (ctype
->base_type
== &fp_type
) {
735 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
736 ctype
->modifiers
&= ~MOD_SPECIFIER
;
738 if (ctype
->modifiers
& MOD_BITWISE
) {
740 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
741 if (!is_int_type(ctype
->base_type
)) {
742 warning(token
->pos
, "invalid modifier");
745 type
= alloc_symbol(token
->pos
, SYM_BASETYPE
);
746 *type
= *ctype
->base_type
;
747 type
->ctype
.base_type
= ctype
->base_type
;
748 type
->type
= SYM_RESTRICT
;
749 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
750 ctype
->base_type
= type
;
755 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
757 struct expression
*expr
= NULL
;
759 token
= parse_expression(token
, &expr
);
760 sym
->array_size
= expr
;
764 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
765 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
767 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
770 if (token_type(token
) != TOKEN_IDENT
)
772 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
773 struct ctype thistype
= { 0, };
774 token
= attribute_specifier(token
->next
, &thistype
);
775 apply_ctype(token
->pos
, &thistype
, ctype
);
778 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
)) {
779 struct expression
*expr
;
780 token
= expect(token
->next
, '(', "after asm");
781 token
= parse_expression(token
->next
, &expr
);
782 token
= expect(token
, ')', "after asm");
790 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
792 struct ctype
*ctype
= &decl
->ctype
;
794 if (p
&& token_type(token
) == TOKEN_IDENT
) {
800 token
= handle_attributes(token
, ctype
);
802 if (token_type(token
) != TOKEN_SPECIAL
)
806 * This can be either a parameter list or a grouping.
807 * For the direct (non-abstract) case, we know if must be
808 * a parameter list if we already saw the identifier.
809 * For the abstract case, we know if must be a parameter
810 * list if it is empty or starts with a type.
812 if (token
->special
== '(') {
814 struct token
*next
= token
->next
;
815 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
818 struct symbol
*base_type
= ctype
->base_type
;
819 token
= declarator(next
, decl
, p
);
820 token
= expect(token
, ')', "in nested declarator");
821 while (ctype
->base_type
!= base_type
)
822 ctype
= &ctype
->base_type
->ctype
;
827 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
828 token
= parameter_type_list(next
, sym
, p
);
829 token
= expect(token
, ')', "in function declarator");
832 if (token
->special
== '[') {
833 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
834 token
= abstract_array_declarator(token
->next
, array
);
835 token
= expect(token
, ']', "in abstract_array_declarator");
836 ctype
= &array
->ctype
;
844 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
846 unsigned long modifiers
;
847 struct symbol
*base_type
;
849 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
850 base_type
= ctype
->base_type
;
851 ctype
->modifiers
= modifiers
;
853 while (match_op(token
,'*')) {
854 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
855 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
856 ptr
->ctype
.as
= ctype
->as
;
857 ptr
->ctype
.in_context
+= ctype
->in_context
;
858 ptr
->ctype
.out_context
+= ctype
->out_context
;
859 ptr
->ctype
.base_type
= base_type
;
862 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
863 ctype
->base_type
= base_type
;
865 ctype
->in_context
= 0;
866 ctype
->out_context
= 0;
868 token
= declaration_specifiers(token
->next
, ctype
, 1);
869 modifiers
= ctype
->modifiers
;
874 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
876 token
= pointer(token
, &sym
->ctype
);
877 return direct_declarator(token
, sym
, p
);
880 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
882 struct ctype
*ctype
= &decl
->ctype
;
883 struct expression
*expr
;
884 struct symbol
*bitfield
;
887 if (!is_int_type(ctype
->base_type
)) {
888 warning(token
->pos
, "invalid bitfield specifier for type %s.",
889 show_typename(ctype
->base_type
));
890 // Parse this to recover gracefully.
891 return conditional_expression(token
->next
, &expr
);
894 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
895 token
= conditional_expression(token
->next
, &expr
);
896 width
= get_expression_value(expr
);
897 bitfield
->bit_size
= width
;
899 if (width
< 0 || width
> INT_MAX
) {
900 warning(token
->pos
, "invalid bitfield width, %lld.", width
);
902 } else if (decl
->ident
&& width
== 0) {
903 warning(token
->pos
, "invalid named zero-width bitfield `%s'",
904 show_ident(decl
->ident
));
906 } else if (decl
->ident
) {
907 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
908 int is_signed
= !(base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
909 if (width
== 1 && is_signed
) {
910 // Valid values are either {-1;0} or {0}, depending on integer
911 // representation. The latter makes for very efficient code...
912 warning(token
->pos
, "dubious one-bit signed bitfield");
914 if (Wdefault_bitfield_sign
&&
915 base_type
->type
!= SYM_ENUM
&&
916 !(base_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
918 // The sign of bitfields is unspecified by default.
919 warning (token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
922 bitfield
->bit_size
= width
;
926 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
928 while (!match_op(token
, '}')) {
929 struct ctype ctype
= {0, };
931 token
= declaration_specifiers(token
, &ctype
, 0);
933 struct ident
*ident
= NULL
;
934 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
936 token
= declarator(token
, decl
, &ident
);
938 if (match_op(token
, ':')) {
939 token
= handle_bitfield(token
, decl
);
940 token
= handle_attributes(token
, &decl
->ctype
);
942 add_symbol(list
, decl
);
943 if (!match_op(token
, ','))
947 if (!match_op(token
, ';')) {
948 warning(token
->pos
, "expected ; at end of declaration");
956 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
958 struct ident
*ident
= NULL
;
960 struct ctype ctype
= { 0, };
962 token
= declaration_specifiers(token
, &ctype
, 0);
963 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
966 token
= declarator(token
, sym
, &ident
);
971 struct token
*typename(struct token
*token
, struct symbol
**p
)
973 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
975 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
976 return declarator(token
, sym
, NULL
);
979 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
981 token
= parse_expression(token
, tree
);
982 return expect(token
, ';', "at end of statement");
985 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
986 struct expression_list
**inout
)
988 struct expression
*expr
;
990 /* Allow empty operands */
991 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
994 struct ident
*ident
= NULL
;
995 if (match_op(token
->next
, '[') &&
996 token_type(token
->next
->next
) == TOKEN_IDENT
&&
997 match_op(token
->next
->next
->next
, ']')) {
998 ident
= token
->next
->next
->ident
;
999 token
= token
->next
->next
->next
;
1001 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1002 token
= primary_expression(token
->next
, &expr
);
1003 add_expression(inout
, expr
);
1004 token
= parens_expression(token
, &expr
, "in asm parameter");
1005 add_expression(inout
, expr
);
1006 } while (match_op(token
, ','));
1010 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1011 struct expression_list
**clobbers
)
1013 struct expression
*expr
;
1016 token
= primary_expression(token
->next
, &expr
);
1017 add_expression(clobbers
, expr
);
1018 } while (match_op(token
, ','));
1022 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
1024 stmt
->type
= STMT_ASM
;
1025 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1026 token
= token
->next
;
1028 token
= expect(token
, '(', "after asm");
1029 token
= parse_expression(token
, &stmt
->asm_string
);
1030 if (match_op(token
, ':'))
1031 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1032 if (match_op(token
, ':'))
1033 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1034 if (match_op(token
, ':'))
1035 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1036 token
= expect(token
, ')', "after asm");
1037 return expect(token
, ';', "at end of asm-statement");
1040 /* Make a statement out of an expression */
1041 static struct statement
*make_statement(struct expression
*expr
)
1043 struct statement
*stmt
;
1047 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1048 stmt
->expression
= expr
;
1053 * All iterators have two symbols associated with them:
1054 * the "continue" and "break" symbols, which are targets
1055 * for continue and break statements respectively.
1057 * They are in a special name-space, but they follow
1058 * all the normal visibility rules, so nested iterators
1059 * automatically work right.
1061 static void start_iterator(struct statement
*stmt
)
1063 struct symbol
*cont
, *brk
;
1065 start_symbol_scope();
1066 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1067 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1068 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1069 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1071 stmt
->type
= STMT_ITERATOR
;
1072 stmt
->iterator_break
= brk
;
1073 stmt
->iterator_continue
= cont
;
1074 fn_local_symbol(brk
);
1075 fn_local_symbol(cont
);
1078 static void end_iterator(struct statement
*stmt
)
1083 static struct statement
*start_function(struct symbol
*sym
)
1086 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1088 start_function_scope();
1089 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1090 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1091 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1092 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1093 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1095 fn_local_symbol(ret
);
1097 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1103 static void end_function(struct symbol
*sym
)
1106 end_function_scope();
1110 * A "switch()" statement, like an iterator, has a
1111 * the "break" symbol associated with it. It works
1112 * exactly like the iterator break - it's the target
1113 * for any break-statements in scope, and means that
1114 * "break" handling doesn't even need to know whether
1115 * it's breaking out of an iterator or a switch.
1117 * In addition, the "case" symbol is a marker for the
1118 * case/default statements to find the switch statement
1119 * that they are associated with.
1121 static void start_switch(struct statement
*stmt
)
1123 struct symbol
*brk
, *switch_case
;
1125 start_symbol_scope();
1126 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1127 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1129 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1130 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1131 switch_case
->stmt
= stmt
;
1133 stmt
->type
= STMT_SWITCH
;
1134 stmt
->switch_break
= brk
;
1135 stmt
->switch_case
= switch_case
;
1137 fn_local_symbol(brk
);
1138 fn_local_symbol(switch_case
);
1141 static void end_switch(struct statement
*stmt
)
1143 if (!stmt
->switch_case
->symbol_list
)
1144 warning(stmt
->pos
, "switch with no cases");
1148 static void add_case_statement(struct statement
*stmt
)
1150 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1154 warning(stmt
->pos
, "not in switch scope");
1155 stmt
->type
= STMT_NONE
;
1158 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1159 add_symbol(&target
->symbol_list
, sym
);
1161 stmt
->case_label
= sym
;
1162 fn_local_symbol(sym
);
1165 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1167 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1170 error_die(token
->pos
, "internal error: return without a function target");
1171 stmt
->type
= STMT_RETURN
;
1172 stmt
->ret_target
= target
;
1173 return expression_statement(token
->next
, &stmt
->ret_value
);
1176 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1178 struct symbol_list
*syms
;
1179 struct expression
*e1
, *e2
, *e3
;
1180 struct statement
*iterator
;
1182 start_iterator(stmt
);
1183 token
= expect(token
->next
, '(', "after 'for'");
1187 /* C99 variable declaration? */
1188 if (lookup_type(token
)) {
1189 token
= external_declaration(token
, &syms
);
1191 token
= parse_expression(token
, &e1
);
1192 token
= expect(token
, ';', "in 'for'");
1194 token
= parse_expression(token
, &e2
);
1195 token
= expect(token
, ';', "in 'for'");
1196 token
= parse_expression(token
, &e3
);
1197 token
= expect(token
, ')', "in 'for'");
1198 token
= statement(token
, &iterator
);
1200 stmt
->iterator_syms
= syms
;
1201 stmt
->iterator_pre_statement
= make_statement(e1
);
1202 stmt
->iterator_pre_condition
= e2
;
1203 stmt
->iterator_post_statement
= make_statement(e3
);
1204 stmt
->iterator_post_condition
= e2
;
1205 stmt
->iterator_statement
= iterator
;
1211 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1213 struct expression
*expr
;
1214 struct statement
*iterator
;
1216 start_iterator(stmt
);
1217 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1218 token
= statement(token
, &iterator
);
1220 stmt
->iterator_pre_condition
= expr
;
1221 stmt
->iterator_post_condition
= expr
;
1222 stmt
->iterator_statement
= iterator
;
1228 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1230 struct expression
*expr
;
1231 struct statement
*iterator
;
1233 start_iterator(stmt
);
1234 token
= statement(token
->next
, &iterator
);
1235 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1236 token
= token
->next
;
1238 warning(token
->pos
, "expected 'while' after 'do'");
1239 token
= parens_expression(token
, &expr
, "after 'do-while'");
1241 stmt
->iterator_post_condition
= expr
;
1242 stmt
->iterator_statement
= iterator
;
1245 return expect(token
, ';', "after statement");
1248 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1250 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1253 if (token_type(token
) == TOKEN_IDENT
) {
1254 if (token
->ident
== &if_ident
) {
1255 stmt
->type
= STMT_IF
;
1256 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1257 token
= statement(token
, &stmt
->if_true
);
1258 if (token_type(token
) != TOKEN_IDENT
)
1260 if (token
->ident
!= &else_ident
)
1262 return statement(token
->next
, &stmt
->if_false
);
1265 if (token
->ident
== &return_ident
)
1266 return parse_return_statement(token
, stmt
);
1268 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1269 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1270 stmt
->type
= STMT_GOTO
;
1271 stmt
->goto_label
= target
;
1273 warning(stmt
->pos
, "break/continue not in iterator scope");
1274 return expect(token
->next
, ';', "at end of statement");
1276 if (token
->ident
== &default_ident
) {
1277 token
= token
->next
;
1278 goto default_statement
;
1280 if (token
->ident
== &case_ident
) {
1281 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1282 if (match_op(token
, SPECIAL_ELLIPSIS
))
1283 token
= parse_expression(token
->next
, &stmt
->case_to
);
1285 stmt
->type
= STMT_CASE
;
1286 token
= expect(token
, ':', "after default/case");
1287 add_case_statement(stmt
);
1288 return statement(token
, &stmt
->case_statement
);
1290 if (token
->ident
== &switch_ident
) {
1291 stmt
->type
= STMT_SWITCH
;
1293 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1294 token
= statement(token
, &stmt
->switch_statement
);
1298 if (token
->ident
== &for_ident
)
1299 return parse_for_statement(token
, stmt
);
1301 if (token
->ident
== &while_ident
)
1302 return parse_while_statement(token
, stmt
);
1304 if (token
->ident
== &do_ident
)
1305 return parse_do_statement(token
, stmt
);
1307 if (token
->ident
== &goto_ident
) {
1308 stmt
->type
= STMT_GOTO
;
1309 token
= token
->next
;
1310 if (match_op(token
, '*')) {
1311 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1312 add_statement(&function_computed_goto_list
, stmt
);
1313 } else if (token_type(token
) == TOKEN_IDENT
) {
1314 stmt
->goto_label
= label_symbol(token
);
1315 token
= token
->next
;
1317 warning(token
->pos
, "Expected identifier or goto expression");
1319 return expect(token
, ';', "at end of statement");
1321 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1322 return parse_asm(token
->next
, stmt
);
1324 if (token
->ident
== &__context___ident
) {
1325 stmt
->type
= STMT_CONTEXT
;
1326 token
= parse_expression(token
->next
, &stmt
->expression
);
1327 return expect(token
, ';', "at end of statement");
1329 if (token
->ident
== &__range___ident
) {
1330 stmt
->type
= STMT_RANGE
;
1331 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1332 token
= expect(token
, ',', "after range expression");
1333 token
= assignment_expression(token
, &stmt
->range_low
);
1334 token
= expect(token
, ',', "after low range");
1335 token
= assignment_expression(token
, &stmt
->range_high
);
1336 return expect(token
, ';', "after range statement");
1338 if (match_op(token
->next
, ':')) {
1339 stmt
->type
= STMT_LABEL
;
1340 stmt
->label_identifier
= label_symbol(token
);
1341 return statement(token
->next
->next
, &stmt
->label_statement
);
1345 if (match_op(token
, '{')) {
1346 stmt
->type
= STMT_COMPOUND
;
1347 start_symbol_scope();
1348 token
= compound_statement(token
->next
, stmt
);
1351 return expect(token
, '}', "at end of compound statement");
1354 stmt
->type
= STMT_EXPRESSION
;
1355 return expression_statement(token
, &stmt
->expression
);
1358 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
, struct symbol_list
**syms
)
1361 struct statement
* stmt
;
1362 if (eof_token(token
))
1364 if (match_op(token
, '}'))
1366 if (lookup_type(token
)) {
1367 if (warn_on_mixed
&& *list
)
1368 warning(token
->pos
, "mixing declarations and code");
1369 token
= external_declaration(token
, syms
);
1372 token
= statement(token
, &stmt
);
1373 add_statement(list
, stmt
);
1378 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1380 struct symbol_list
**list
= &fn
->arguments
;
1382 if (match_op(token
, ')')) {
1383 // No warning for "void oink ();"
1384 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1385 if (p
&& !match_op(token
->next
, ';'))
1386 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1393 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1395 warning(token
->pos
, "variadic functions must have one named argument");
1397 token
= token
->next
;
1401 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1402 token
= parameter_declaration(token
, &sym
);
1403 if (sym
->ctype
.base_type
== &void_ctype
) {
1404 /* Special case: (void) */
1405 if (!*list
&& !sym
->ident
)
1407 warning(token
->pos
, "void parameter");
1409 add_symbol(list
, sym
);
1410 if (!match_op(token
, ','))
1412 token
= token
->next
;
1418 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1420 token
= statement_list(token
, &stmt
->stmts
, &stmt
->syms
);
1424 static struct expression
*identifier_expression(struct token
*token
)
1426 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1427 expr
->expr_ident
= token
->ident
;
1431 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1433 int idx_from
, idx_to
;
1434 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1436 idx_from
= get_expression_value(from
);
1439 idx_to
= get_expression_value(to
);
1440 if (idx_to
< idx_from
|| idx_from
< 0)
1441 warning(from
->pos
, "nonsense array initializer index range");
1443 expr
->idx_from
= idx_from
;
1444 expr
->idx_to
= idx_to
;
1448 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1450 int expect_equal
= 0;
1451 struct token
*next
= token
->next
;
1452 struct expression
**tail
= ep
;
1457 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1458 struct expression
*expr
= identifier_expression(token
);
1459 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1460 token
= initializer(&expr
->ident_expression
, next
->next
);
1461 if (expr
->ident_expression
)
1466 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1467 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1468 struct expression
*expr
= identifier_expression(next
);
1470 tail
= &expr
->ident_expression
;
1473 } else if (match_op(token
, '[')) {
1474 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1475 token
= constant_expression(token
->next
, &from
);
1477 warning(token
->pos
, "Expected constant expression");
1480 if (match_op(token
, SPECIAL_ELLIPSIS
))
1481 token
= constant_expression(token
->next
, &to
);
1482 expr
= index_expression(from
, to
);
1484 tail
= &expr
->idx_expression
;
1485 token
= expect(token
, ']', "at end of initializer index");
1492 if (nested
&& !expect_equal
) {
1493 if (!match_op(token
, '='))
1494 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1499 token
= expect(token
, '=', "at end of initializer index");
1501 token
= initializer(tail
, token
);
1507 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1509 struct expression
*expr
;
1512 token
= single_initializer(&expr
, token
);
1515 add_expression(list
, expr
);
1516 if (!match_op(token
, ','))
1518 token
= token
->next
;
1523 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1525 if (match_op(token
, '{')) {
1526 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1528 token
= initializer_list(&expr
->expr_list
, token
->next
);
1529 return expect(token
, '}', "at end of initializer");
1531 return assignment_expression(token
, tree
);
1534 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1537 warning(sym
->pos
, "no identifier for function argument");
1540 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1543 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1544 struct symbol_list
**list
)
1546 struct symbol_list
**old_symbol_list
;
1547 struct symbol
*base_type
= decl
->ctype
.base_type
;
1548 struct statement
*stmt
, **p
;
1551 old_symbol_list
= function_symbol_list
;
1552 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1553 function_symbol_list
= &decl
->inline_symbol_list
;
1554 p
= &base_type
->inline_stmt
;
1556 function_symbol_list
= &decl
->symbol_list
;
1557 p
= &base_type
->stmt
;
1559 function_computed_target_list
= NULL
;
1560 function_computed_goto_list
= NULL
;
1562 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1563 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1564 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1566 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1567 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1569 stmt
= start_function(decl
);
1572 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1573 declare_argument(arg
, base_type
);
1574 } END_FOR_EACH_PTR(arg
);
1576 token
= compound_statement(token
->next
, stmt
);
1579 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1580 add_symbol(list
, decl
);
1581 check_declaration(decl
);
1582 function_symbol_list
= old_symbol_list
;
1583 if (function_computed_goto_list
) {
1584 if (!function_computed_target_list
)
1585 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1587 struct statement
*stmt
;
1588 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1589 stmt
->target_list
= function_computed_target_list
;
1590 } END_FOR_EACH_PTR(stmt
);
1593 return expect(token
, '}', "at end of function");
1596 static void promote_k_r_types(struct symbol
*arg
)
1598 struct symbol
*base
= arg
->ctype
.base_type
;
1599 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1600 arg
->ctype
.base_type
= &int_ctype
;
1604 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1606 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1609 FOR_EACH_PTR(real_args
, arg
) {
1610 struct symbol
*type
;
1612 /* This is quadratic in the number of arguments. We _really_ don't care */
1613 FOR_EACH_PTR(argtypes
, type
) {
1614 if (type
->ident
== arg
->ident
)
1616 } END_FOR_EACH_PTR(type
);
1617 warning(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1621 /* "char" and "short" promote to "int" */
1622 promote_k_r_types(type
);
1624 arg
->ctype
= type
->ctype
;
1625 } END_FOR_EACH_PTR(arg
);
1627 FOR_EACH_PTR(argtypes
, arg
) {
1629 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1630 } END_FOR_EACH_PTR(arg
);
1634 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1635 struct symbol_list
**list
)
1637 struct symbol_list
*args
= NULL
;
1639 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(decl
->ident
));
1641 token
= external_declaration(token
, &args
);
1642 } while (lookup_type(token
));
1644 apply_k_r_types(args
, decl
);
1646 if (!match_op(token
, '{')) {
1647 warning(token
->pos
, "expected function body");
1650 return parse_function_body(token
, decl
, list
);
1654 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1656 struct ident
*ident
= NULL
;
1657 struct symbol
*decl
;
1658 struct ctype ctype
= { 0, };
1659 struct symbol
*base_type
;
1662 /* Top-level inline asm? */
1663 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1664 struct symbol_list
**old_symbol_list
;
1665 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1666 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1667 struct statement
*stmt
;
1669 anon
->ctype
.base_type
= fn
;
1670 old_symbol_list
= function_symbol_list
;
1671 function_symbol_list
= &anon
->symbol_list
;
1672 stmt
= start_function(anon
);
1673 token
= parse_asm(token
->next
, stmt
);
1675 function_symbol_list
= old_symbol_list
;
1676 add_symbol(list
, anon
);
1680 /* Parse declaration-specifiers, if any */
1681 token
= declaration_specifiers(token
, &ctype
, 0);
1682 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1683 decl
->ctype
= ctype
;
1684 token
= declarator(token
, decl
, &ident
);
1686 /* Just a type declaration? */
1688 return expect(token
, ';', "end of type declaration");
1690 /* type define declaration? */
1691 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1693 /* Typedef's don't have meaningful storage */
1695 ctype
.modifiers
&= ~MOD_STORAGE
;
1696 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1697 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1700 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1702 base_type
= decl
->ctype
.base_type
;
1703 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1704 /* K&R argument declaration? */
1705 if (lookup_type(token
))
1706 return parse_k_r_arguments(token
, decl
, list
);
1707 if (match_op(token
, '{'))
1708 return parse_function_body(token
, decl
, list
);
1710 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1711 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1712 } else if (!is_typedef
&& base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
1713 warning(token
->pos
, "void declaration");
1717 if (!is_typedef
&& match_op(token
, '=')) {
1718 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1719 warning(decl
->pos
, "symbol with external linkage has initializer");
1720 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1722 token
= initializer(&decl
->initializer
, token
->next
);
1725 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1726 add_symbol(list
, decl
);
1727 fn_local_symbol(decl
);
1730 check_declaration(decl
);
1732 if (!match_op(token
, ','))
1735 token
= token
->next
;
1737 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1738 decl
->ctype
= ctype
;
1739 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1740 token
= declarator(token
, decl
, &ident
);
1742 warning(token
->pos
, "expected identifier name in type definition");
1746 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1748 /* Function declarations are automatically extern unless specifically static */
1749 base_type
= decl
->ctype
.base_type
;
1750 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1751 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1752 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1755 return expect(token
, ';', "at end of declaration");
1758 struct symbol_list
*translation_unit(struct token
*token
)
1760 while (!eof_token(token
))
1761 token
= external_declaration(token
, &translation_unit_used_list
);
1762 // They aren't needed any more
1763 clear_token_alloc();
1765 /* Evaluate the symbol list */
1766 evaluate_symbol_list(translation_unit_used_list
);
1767 return translation_unit_used_list
;