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
8 * Copyright (C) 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
28 #include "expression.h"
31 #define warn_on_mixed (1)
33 static struct symbol_list
**function_symbol_list
;
34 struct symbol_list
*function_computed_target_list
;
35 struct statement_list
*function_computed_goto_list
;
37 static struct token
*statement(struct token
*token
, struct statement
**tree
);
38 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
);
40 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
);
41 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
);
42 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
);
43 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
);
44 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
);
46 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
47 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
48 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
49 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
50 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
51 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
52 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
53 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
54 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
55 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
56 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
57 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
58 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
);
59 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
62 static struct symbol_op modifier_op
= {
66 static struct symbol_op qualifier_op
= {
70 static struct symbol_op typeof_op
= {
72 .declarator
= typeof_specifier
,
75 static struct symbol_op attribute_op
= {
77 .declarator
= attribute_specifier
,
80 static struct symbol_op struct_op
= {
82 .declarator
= struct_specifier
,
85 static struct symbol_op union_op
= {
87 .declarator
= union_specifier
,
90 static struct symbol_op enum_op
= {
92 .declarator
= enum_specifier
,
97 static struct symbol_op if_op
= {
98 .statement
= parse_if_statement
,
101 static struct symbol_op return_op
= {
102 .statement
= parse_return_statement
,
105 static struct symbol_op loop_iter_op
= {
106 .statement
= parse_loop_iterator
,
109 static struct symbol_op default_op
= {
110 .statement
= parse_default_statement
,
113 static struct symbol_op case_op
= {
114 .statement
= parse_case_statement
,
117 static struct symbol_op switch_op
= {
118 .statement
= parse_switch_statement
,
121 static struct symbol_op for_op
= {
122 .statement
= parse_for_statement
,
125 static struct symbol_op while_op
= {
126 .statement
= parse_while_statement
,
129 static struct symbol_op do_op
= {
130 .statement
= parse_do_statement
,
133 static struct symbol_op goto_op
= {
134 .statement
= parse_goto_statement
,
137 static struct symbol_op context_op
= {
138 .statement
= parse_context_statement
,
141 static struct symbol_op range_op
= {
142 .statement
= parse_range_statement
,
145 static struct symbol_op asm_op
= {
146 .statement
= parse_asm
,
147 .toplevel
= toplevel_asm_declaration
,
150 static struct init_keyword
{
153 unsigned long modifiers
;
154 struct symbol_op
*op
;
155 } keyword_table
[] = {
156 /* Type qualifiers */
157 { "const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
158 { "__const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
159 { "__const__", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
160 { "volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
161 { "__volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
162 { "__volatile__", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
165 { "typedef", NS_TYPEDEF
, MOD_TYPEDEF
, .op
= &modifier_op
},
168 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
169 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
170 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
172 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
173 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
175 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
176 { "union", NS_TYPEDEF
, .op
= &union_op
},
177 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
179 { "inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
180 { "__inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
181 { "__inline__", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
183 /* Ignored for now.. */
184 { "restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
185 { "__restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
188 { "if", NS_KEYWORD
, .op
= &if_op
},
189 { "return", NS_KEYWORD
, .op
= &return_op
},
190 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
191 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
192 { "default", NS_KEYWORD
, .op
= &default_op
},
193 { "case", NS_KEYWORD
, .op
= &case_op
},
194 { "switch", NS_KEYWORD
, .op
= &switch_op
},
195 { "for", NS_KEYWORD
, .op
= &for_op
},
196 { "while", NS_KEYWORD
, .op
= &while_op
},
197 { "do", NS_KEYWORD
, .op
= &do_op
},
198 { "goto", NS_KEYWORD
, .op
= &goto_op
},
199 { "__context__",NS_KEYWORD
, .op
= &context_op
},
200 { "__range__", NS_KEYWORD
, .op
= &range_op
},
201 { "asm", NS_KEYWORD
, .op
= &asm_op
},
202 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
203 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
207 void init_parser(int stream
)
210 for (i
= 0; i
< sizeof keyword_table
/sizeof keyword_table
[0]; i
++) {
211 struct init_keyword
*ptr
= keyword_table
+ i
;
212 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
213 sym
->ident
->keyword
= 1;
214 sym
->ctype
.modifiers
= ptr
->modifiers
;
219 // Add a symbol to the list of function-local symbols
220 static void fn_local_symbol(struct symbol
*sym
)
222 if (function_symbol_list
)
223 add_symbol(function_symbol_list
, sym
);
226 static int match_idents(struct token
*token
, ...)
230 if (token_type(token
) != TOKEN_IDENT
)
233 va_start(args
, token
);
235 struct ident
* next
= va_arg(args
, struct ident
*);
238 if (token
->ident
== next
)
244 struct statement
*alloc_statement(struct position pos
, int type
)
246 struct statement
*stmt
= __alloc_statement(0);
252 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
254 static int apply_modifiers(struct position pos
, struct ctype
*ctype
)
258 while ((base
= ctype
->base_type
)) {
259 switch (base
->type
) {
265 ctype
= &base
->ctype
;
271 /* Turn the "virtual types" into real types with real sizes etc */
272 if (ctype
->base_type
== &int_type
) {
273 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
274 ctype
->modifiers
&= ~MOD_SPECIFIER
;
275 } else if (ctype
->base_type
== &fp_type
) {
276 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
277 ctype
->modifiers
&= ~MOD_SPECIFIER
;
280 if (ctype
->modifiers
& MOD_BITWISE
) {
282 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
283 if (!is_int_type(ctype
->base_type
)) {
284 sparse_error(pos
, "invalid modifier");
287 type
= alloc_symbol(pos
, SYM_BASETYPE
);
288 *type
= *ctype
->base_type
;
289 type
->ctype
.base_type
= ctype
->base_type
;
290 type
->type
= SYM_RESTRICT
;
291 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
292 ctype
->base_type
= type
;
298 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
300 struct symbol
*sym
= alloc_symbol(pos
, type
);
302 sym
->ctype
.base_type
= ctype
->base_type
;
303 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
305 ctype
->base_type
= sym
;
306 ctype
->modifiers
&= MOD_STORAGE
;
310 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
312 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
314 sym
= alloc_symbol(token
->pos
, type
);
315 bind_symbol(sym
, token
->ident
, ns
);
316 if (type
== SYM_LABEL
)
317 fn_local_symbol(sym
);
323 * NOTE! NS_LABEL is not just a different namespace,
324 * it also ends up using function scope instead of the
325 * regular symbol scope.
327 struct symbol
*label_symbol(struct token
*token
)
329 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
332 static struct token
*struct_union_enum_specifier(enum type type
,
333 struct token
*token
, struct ctype
*ctype
,
334 struct token
*(*parse
)(struct token
*, struct symbol
*))
337 struct position
*repos
;
339 ctype
->modifiers
= 0;
340 token
= handle_attributes(token
, ctype
);
341 if (token_type(token
) == TOKEN_IDENT
) {
342 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
344 (sym
->scope
!= block_scope
&&
345 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
346 // Either a new symbol, or else an out-of-scope
347 // symbol being redefined.
348 sym
= alloc_symbol(token
->pos
, type
);
349 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
351 if (sym
->type
!= type
)
352 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
353 ctype
->base_type
= sym
;
356 if (match_op(token
, '{')) {
357 // The following test is actually wrong for empty
358 // structs, but (1) they are not C99, (2) gcc does
359 // the same thing, and (3) it's easier.
360 if (sym
->symbol_list
)
361 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
363 token
= parse(token
->next
, sym
);
364 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
366 // Mark the structure as needing re-examination
372 // private struct/union/enum type
373 if (!match_op(token
, '{')) {
374 sparse_error(token
->pos
, "expected declaration");
375 ctype
->base_type
= &bad_ctype
;
379 sym
= alloc_symbol(token
->pos
, type
);
380 token
= parse(token
->next
, sym
);
381 ctype
->base_type
= sym
;
382 return expect(token
, '}', "at end of specifier");
385 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
387 return struct_declaration_list(token
, &sym
->symbol_list
);
390 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
)
392 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctype
, parse_struct_declaration
);
395 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
)
397 return struct_union_enum_specifier(SYM_UNION
, token
, ctype
, parse_struct_declaration
);
403 unsigned long long y
;
406 static void upper_boundary(Num
*n
, Num
*v
)
418 static void lower_boundary(Num
*n
, Num
*v
)
430 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
432 int shift
= type
->bit_size
;
433 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
437 if (upper
->x
== 0 && upper
->y
>> shift
)
439 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
444 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
446 if (s1
->bit_size
< s2
->bit_size
) {
448 } else if (s1
->bit_size
== s2
->bit_size
) {
449 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
452 if (s1
->bit_size
< bits_in_int
)
457 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
461 FOR_EACH_PTR(list
, sym
) {
462 struct expression
*expr
= sym
->initializer
;
463 struct symbol
*ctype
;
464 if (expr
->type
!= EXPR_VALUE
)
467 if (ctype
->bit_size
== base_type
->bit_size
)
469 cast_value(expr
, base_type
, expr
, ctype
);
470 } END_FOR_EACH_PTR(sym
);
473 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
475 unsigned long long lastval
= 0;
476 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
477 Num upper
= {-1, 0}, lower
= {1, 0};
478 struct symbol_list
*entries
= NULL
;
480 parent
->examined
= 1;
481 parent
->ctype
.base_type
= &int_ctype
;
482 while (token_type(token
) == TOKEN_IDENT
) {
483 struct expression
*expr
= NULL
;
484 struct token
*next
= token
->next
;
487 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
488 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
489 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
491 if (match_op(next
, '=')) {
492 next
= constant_expression(next
->next
, &expr
);
493 lastval
= get_expression_value(expr
);
495 if (expr
&& expr
->ctype
)
499 } else if (is_int_type(ctype
)) {
502 error_die(token
->pos
, "can't increment the last enum member");
506 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
507 expr
->value
= lastval
;
511 sym
->initializer
= expr
;
512 sym
->ctype
.base_type
= parent
;
513 add_ptr_list(&entries
, sym
);
515 if (base_type
!= &bad_ctype
) {
516 if (ctype
->type
== SYM_NODE
)
517 ctype
= ctype
->ctype
.base_type
;
518 if (ctype
->type
== SYM_ENUM
) {
522 ctype
= ctype
->ctype
.base_type
;
526 * - if all enum's are of the same type, then
527 * the base_type is that type (two first
529 * - if enums are of different types, they
530 * all have to be integer types, and the
531 * base type is at least "int_ctype".
532 * - otherwise the base_type is "bad_ctype".
536 } else if (ctype
== base_type
) {
538 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
539 base_type
= bigger_enum_type(base_type
, ctype
);
541 base_type
= &bad_ctype
;
542 parent
->ctype
.base_type
= base_type
;
544 if (is_int_type(base_type
)) {
545 Num v
= {.y
= lastval
};
546 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
548 else if ((long long)lastval
>= 0)
552 upper_boundary(&upper
, &v
);
553 lower_boundary(&lower
, &v
);
556 if (!match_op(token
, ','))
561 sparse_error(token
->pos
, "bad enum definition");
562 base_type
= &bad_ctype
;
564 else if (!is_int_type(base_type
))
565 base_type
= base_type
;
566 else if (type_is_ok(base_type
, &upper
, &lower
))
567 base_type
= base_type
;
568 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
569 base_type
= &int_ctype
;
570 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
571 base_type
= &uint_ctype
;
572 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
573 base_type
= &long_ctype
;
574 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
575 base_type
= &ulong_ctype
;
576 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
577 base_type
= &llong_ctype
;
578 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
579 base_type
= &ullong_ctype
;
581 base_type
= &bad_ctype
;
582 parent
->ctype
.base_type
= base_type
;
583 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
584 parent
->examined
= 0;
586 cast_enum_list(entries
, base_type
);
587 free_ptr_list(&entries
);
592 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
594 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
596 ctype
= &ctype
->base_type
->ctype
;
597 if (!ctype
->base_type
)
598 ctype
->base_type
= &incomplete_ctype
;
603 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
607 if (!match_op(token
, '(')) {
608 sparse_error(token
->pos
, "expected '(' after typeof");
611 if (lookup_type(token
->next
)) {
612 token
= typename(token
->next
, &sym
);
615 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
616 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
618 ctype
->modifiers
= 0;
619 ctype
->base_type
= typeof_sym
;
621 return expect(token
, ')', "after typeof");
624 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
626 if (attribute
== &packed_ident
||
627 attribute
== &__packed___ident
) {
628 ctype
->alignment
= 1;
631 if (attribute
== &aligned_ident
||
632 attribute
== &__aligned___ident
) {
633 int alignment
= max_alignment
;
635 alignment
= get_expression_value(expr
);
636 ctype
->alignment
= alignment
;
639 if (attribute
== &nocast_ident
) {
640 ctype
->modifiers
|= MOD_NOCAST
;
643 if (attribute
== &noderef_ident
) {
644 ctype
->modifiers
|= MOD_NODEREF
;
647 if (attribute
== &safe_ident
) {
648 ctype
->modifiers
|= MOD_SAFE
;
651 if (attribute
== &force_ident
) {
652 ctype
->modifiers
|= MOD_FORCE
;
655 if (attribute
== &bitwise_ident
||
656 attribute
== &__bitwise___ident
) {
658 ctype
->modifiers
|= MOD_BITWISE
;
661 if (attribute
== &address_space_ident
) {
663 return "expected address space number";
664 ctype
->as
= get_expression_value(expr
);
667 if (attribute
== &context_ident
) {
668 if (expr
&& expr
->type
== EXPR_COMMA
) {
669 struct context
*context
= alloc_context();
670 if(expr
->left
->type
== EXPR_COMMA
) {
671 context
->context
= expr
->left
->left
;
672 context
->in
= get_expression_value(
675 context
->context
= NULL
;
676 context
->in
= get_expression_value(expr
->left
);
678 context
->out
= get_expression_value(expr
->right
);
679 add_ptr_list(&ctype
->contexts
, context
);
682 return "expected context input/output values";
684 if (attribute
== &mode_ident
||
685 attribute
== &__mode___ident
) {
686 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
687 struct ident
*ident
= expr
->symbol_name
;
690 * Match against __QI__/__HI__/__SI__/__DI__
692 * FIXME! This is broken - we don't actually get
693 * the type information updated properly at this
694 * stage for some reason.
696 if (ident
== &__QI___ident
||
697 ident
== &QI_ident
) {
698 ctype
->modifiers
|= MOD_CHAR
;
701 if (ident
== &__HI___ident
||
702 ident
== &HI_ident
) {
703 ctype
->modifiers
|= MOD_SHORT
;
706 if (ident
== &__SI___ident
||
707 ident
== &SI_ident
) {
711 if (ident
== &__DI___ident
||
712 ident
== &DI_ident
) {
713 ctype
->modifiers
|= MOD_LONGLONG
;
716 if (ident
== &__word___ident
||
717 ident
== &word_ident
) {
718 ctype
->modifiers
|= MOD_LONG
;
721 return "unknown mode attribute";
723 return "expected attribute mode symbol";
726 /* Throw away for now.. */
727 if (attribute
== &__transparent_union___ident
) {
728 if (Wtransparent_union
)
729 return "ignoring attribute __transparent_union__";
732 if (attribute
== ¬hrow_ident
||
733 attribute
== &__nothrow_ident
||
734 attribute
== &__nothrow___ident
)
736 if (attribute
== &__malloc___ident
)
738 if (attribute
== &nonnull_ident
||
739 attribute
== &__nonnull_ident
||
740 attribute
== &__nonnull___ident
)
742 if (attribute
== &format_ident
||
743 attribute
== &__format___ident
||
744 attribute
== &__format_arg___ident
)
746 if (attribute
== §ion_ident
||
747 attribute
== &__section___ident
)
749 if (attribute
== &unused_ident
||
750 attribute
== &__unused___ident
)
752 if (attribute
== &const_ident
||
753 attribute
== &__const_ident
||
754 attribute
== &__const___ident
)
756 if (attribute
== &noreturn_ident
||
757 attribute
== &__noreturn___ident
)
759 if (attribute
== &no_instrument_function_ident
||
760 attribute
== &__no_instrument_function___ident
)
762 if (attribute
== &sentinel_ident
||
763 attribute
== &__sentinel___ident
)
765 if (attribute
== ®parm_ident
)
767 if (attribute
== &weak_ident
||
768 attribute
== &__weak___ident
)
770 if (attribute
== &alias_ident
||
771 attribute
== &__alias___ident
)
773 if (attribute
== &pure_ident
||
774 attribute
== &__pure___ident
)
776 if (attribute
== &always_inline_ident
)
778 if (attribute
== &syscall_linkage_ident
)
780 if (attribute
== &visibility_ident
||
781 attribute
== &__visibility___ident
)
783 if (attribute
== &deprecated_ident
||
784 attribute
== &__deprecated___ident
)
786 if (attribute
== &noinline_ident
)
788 if (attribute
== &__used___ident
)
790 if (attribute
== &warn_unused_result_ident
||
791 attribute
== &__warn_unused_result___ident
)
793 if (attribute
== &model_ident
||
794 attribute
== &__model___ident
)
797 return "unknown attribute";
800 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
802 ctype
->modifiers
= 0;
803 token
= expect(token
, '(', "after attribute");
804 token
= expect(token
, '(', "after attribute");
807 const char *error_str
;
808 struct ident
*attribute_name
;
809 struct expression
*attribute_expr
;
811 if (eof_token(token
))
813 if (match_op(token
, ';'))
815 if (token_type(token
) != TOKEN_IDENT
)
817 attribute_name
= token
->ident
;
819 attribute_expr
= NULL
;
820 if (match_op(token
, '('))
821 token
= parens_expression(token
, &attribute_expr
, "in attribute");
822 error_str
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
824 sparse_error(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error_str
);
825 if (!match_op(token
, ','))
830 token
= expect(token
, ')', "after attribute");
831 token
= expect(token
, ')', "after attribute");
835 struct symbol
* ctype_integer(unsigned long spec
)
837 static struct symbol
*const integer_ctypes
[][3] = {
838 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
839 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
840 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
841 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
842 { &int_ctype
, &sint_ctype
, &uint_ctype
},
844 struct symbol
*const (*ctype
)[3];
847 ctype
= integer_ctypes
;
848 if (!(spec
& MOD_LONGLONG
)) {
850 if (!(spec
& MOD_LONG
)) {
852 if (!(spec
& MOD_SHORT
)) {
854 if (!(spec
& MOD_CHAR
))
860 sub
= ((spec
& MOD_UNSIGNED
)
862 : ((spec
& MOD_EXPLICITLY_SIGNED
)
866 return ctype
[0][sub
];
869 struct symbol
* ctype_fp(unsigned long spec
)
871 if (spec
& MOD_LONGLONG
)
872 return &ldouble_ctype
;
874 return &double_ctype
;
878 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
880 unsigned long mod
= thistype
->modifiers
;
883 unsigned long old
= ctype
->modifiers
;
884 unsigned long extra
= 0, dup
, conflict
;
886 if (mod
& old
& MOD_LONG
) {
887 extra
= MOD_LONGLONG
| MOD_LONG
;
891 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
893 sparse_error(pos
, "Just how %sdo you want this type to be?",
894 modifier_string(dup
));
896 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
898 sparse_error(pos
, "You cannot have both long and short modifiers.");
900 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
902 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
904 // Only one storage modifier allowed, except that "inline" doesn't count.
905 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
906 conflict
&= (conflict
- 1);
908 sparse_error(pos
, "multiple storage classes");
910 ctype
->modifiers
= old
| mod
| extra
;
914 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
915 (struct ptr_list
**)&ctype
->contexts
);
918 if (thistype
->alignment
& (thistype
->alignment
-1)) {
919 warning(pos
, "I don't like non-power-of-2 alignments");
920 thistype
->alignment
= 0;
922 if (thistype
->alignment
> ctype
->alignment
)
923 ctype
->alignment
= thistype
->alignment
;
927 ctype
->as
= thistype
->as
;
930 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
932 unsigned long banned
, wrong
;
933 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
934 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
936 if (s
->type
== SYM_KEYWORD
)
937 banned
= s
->op
->type
== KW_SPECIFIER
? (BANNED_SIZE
| BANNED_SIGN
) : 0;
938 else if (s
->ctype
.base_type
== &fp_type
)
939 banned
= BANNED_SIGN
;
940 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
946 // vector_type <-- whatever that is
947 banned
= BANNED_SIZE
| BANNED_SIGN
;
950 wrong
= mod
& banned
;
952 sparse_error(*pos
, "modifier %sis invalid in this context",
953 modifier_string (wrong
));
956 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
960 while ( (token
= next
) != NULL
) {
961 struct ctype thistype
;
963 struct symbol
*s
, *type
;
967 if (token_type(token
) != TOKEN_IDENT
)
969 ident
= token
->ident
;
971 s
= lookup_symbol(ident
, NS_TYPEDEF
);
975 mod
= thistype
.modifiers
;
977 if (s
->type
!= SYM_KEYWORD
)
979 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
982 if (s
->type
== SYM_KEYWORD
&& s
->op
->declarator
) {
983 next
= s
->op
->declarator(next
, &thistype
);
984 mod
= thistype
.modifiers
;
986 type
= thistype
.base_type
;
990 if (ctype
->base_type
)
992 /* User types only mix with qualifiers */
993 if (mod
& MOD_USERTYPE
) {
994 if (ctype
->modifiers
& MOD_SPECIFIER
)
997 ctype
->base_type
= type
;
1000 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
1001 apply_ctype(token
->pos
, &thistype
, ctype
);
1004 if (!ctype
->base_type
) {
1005 struct symbol
*base
= &incomplete_ctype
;
1008 * If we have modifiers, we'll default to an integer
1009 * type, and "ctype_integer()" will turn this into
1012 if (ctype
->modifiers
& MOD_SPECIFIER
)
1014 ctype
->base_type
= base
;
1019 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1021 struct expression
*expr
= NULL
;
1023 token
= parse_expression(token
, &expr
);
1024 sym
->array_size
= expr
;
1028 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
1029 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
1031 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
1034 if (token_type(token
) != TOKEN_IDENT
)
1036 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
1037 struct ctype thistype
= { 0, };
1038 token
= attribute_specifier(token
->next
, &thistype
);
1039 apply_ctype(token
->pos
, &thistype
, ctype
);
1042 if (match_idents(token
, &asm_ident
, &__asm_ident
, &__asm___ident
, NULL
)) {
1043 struct expression
*expr
;
1044 token
= expect(token
->next
, '(', "after asm");
1045 token
= parse_expression(token
->next
, &expr
);
1046 token
= expect(token
, ')', "after asm");
1054 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
1056 struct ctype
*ctype
= &decl
->ctype
;
1058 if (p
&& token_type(token
) == TOKEN_IDENT
) {
1060 token
= token
->next
;
1064 token
= handle_attributes(token
, ctype
);
1066 if (token_type(token
) != TOKEN_SPECIAL
)
1070 * This can be either a parameter list or a grouping.
1071 * For the direct (non-abstract) case, we know if must be
1072 * a parameter list if we already saw the identifier.
1073 * For the abstract case, we know if must be a parameter
1074 * list if it is empty or starts with a type.
1076 if (token
->special
== '(') {
1078 struct token
*next
= token
->next
;
1079 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
1082 struct symbol
*base_type
= ctype
->base_type
;
1083 token
= declarator(next
, decl
, p
);
1084 token
= expect(token
, ')', "in nested declarator");
1085 while (ctype
->base_type
!= base_type
)
1086 ctype
= &ctype
->base_type
->ctype
;
1091 sym
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1092 token
= parameter_type_list(next
, sym
, p
);
1093 token
= expect(token
, ')', "in function declarator");
1096 if (token
->special
== '[') {
1097 struct symbol
*array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1098 token
= abstract_array_declarator(token
->next
, array
);
1099 token
= expect(token
, ']', "in abstract_array_declarator");
1100 ctype
= &array
->ctype
;
1108 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
1110 unsigned long modifiers
;
1111 struct symbol
*base_type
;
1113 modifiers
= ctype
->modifiers
& ~MOD_TYPEDEF
;
1114 base_type
= ctype
->base_type
;
1115 ctype
->modifiers
= modifiers
;
1117 while (match_op(token
,'*')) {
1118 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1119 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
1120 ptr
->ctype
.as
= ctype
->as
;
1121 concat_ptr_list((struct ptr_list
*)ctype
->contexts
,
1122 (struct ptr_list
**)&ptr
->ctype
.contexts
);
1123 ptr
->ctype
.base_type
= base_type
;
1126 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
1127 ctype
->base_type
= base_type
;
1129 free_ptr_list(&ctype
->contexts
);
1131 token
= declaration_specifiers(token
->next
, ctype
, 1);
1132 modifiers
= ctype
->modifiers
;
1137 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
1139 token
= pointer(token
, &sym
->ctype
);
1140 return direct_declarator(token
, sym
, p
);
1143 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
1145 struct ctype
*ctype
= &decl
->ctype
;
1146 struct expression
*expr
;
1147 struct symbol
*bitfield
;
1150 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1151 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1152 show_typename(ctype
->base_type
));
1153 // Parse this to recover gracefully.
1154 return conditional_expression(token
->next
, &expr
);
1157 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1158 token
= conditional_expression(token
->next
, &expr
);
1159 width
= get_expression_value(expr
);
1160 bitfield
->bit_size
= width
;
1162 if (width
< 0 || width
> INT_MAX
) {
1163 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1165 } else if (decl
->ident
&& width
== 0) {
1166 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1167 show_ident(decl
->ident
));
1169 } else if (decl
->ident
) {
1170 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1171 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1172 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1173 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1174 // Valid values are either {-1;0} or {0}, depending on integer
1175 // representation. The latter makes for very efficient code...
1176 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1178 if (Wdefault_bitfield_sign
&&
1179 bitfield_type
->type
!= SYM_ENUM
&&
1180 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1182 // The sign of bitfields is unspecified by default.
1183 sparse_error(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1186 bitfield
->bit_size
= width
;
1190 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1192 struct ctype ctype
= {0, };
1194 token
= declaration_specifiers(token
, &ctype
, 0);
1196 struct ident
*ident
= NULL
;
1197 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1198 decl
->ctype
= ctype
;
1199 token
= declarator(token
, decl
, &ident
);
1200 decl
->ident
= ident
;
1201 if (match_op(token
, ':')) {
1202 token
= handle_bitfield(token
, decl
);
1203 token
= handle_attributes(token
, &decl
->ctype
);
1205 apply_modifiers(token
->pos
, &decl
->ctype
);
1206 add_symbol(list
, decl
);
1207 if (!match_op(token
, ','))
1209 token
= token
->next
;
1214 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1216 while (!match_op(token
, '}')) {
1217 if (!match_op(token
, ';'))
1218 token
= declaration_list(token
, list
);
1219 if (!match_op(token
, ';')) {
1220 sparse_error(token
->pos
, "expected ; at end of declaration");
1223 token
= token
->next
;
1228 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
1230 struct ident
*ident
= NULL
;
1232 struct ctype ctype
= { 0, };
1234 token
= declaration_specifiers(token
, &ctype
, 0);
1235 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1238 token
= declarator(token
, sym
, &ident
);
1240 apply_modifiers(token
->pos
, &sym
->ctype
);
1244 struct token
*typename(struct token
*token
, struct symbol
**p
)
1246 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1248 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
1249 token
= declarator(token
, sym
, NULL
);
1250 apply_modifiers(token
->pos
, &sym
->ctype
);
1254 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1256 token
= parse_expression(token
, tree
);
1257 return expect(token
, ';', "at end of statement");
1260 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1261 struct expression_list
**inout
)
1263 struct expression
*expr
;
1265 /* Allow empty operands */
1266 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1269 struct ident
*ident
= NULL
;
1270 if (match_op(token
->next
, '[') &&
1271 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1272 match_op(token
->next
->next
->next
, ']')) {
1273 ident
= token
->next
->next
->ident
;
1274 token
= token
->next
->next
->next
;
1276 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1277 token
= primary_expression(token
->next
, &expr
);
1278 add_expression(inout
, expr
);
1279 token
= parens_expression(token
, &expr
, "in asm parameter");
1280 add_expression(inout
, expr
);
1281 } while (match_op(token
, ','));
1285 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1286 struct expression_list
**clobbers
)
1288 struct expression
*expr
;
1291 token
= primary_expression(token
->next
, &expr
);
1292 add_expression(clobbers
, expr
);
1293 } while (match_op(token
, ','));
1297 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
1299 token
= token
->next
;
1300 stmt
->type
= STMT_ASM
;
1301 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1302 token
= token
->next
;
1304 token
= expect(token
, '(', "after asm");
1305 token
= parse_expression(token
, &stmt
->asm_string
);
1306 if (match_op(token
, ':'))
1307 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1308 if (match_op(token
, ':'))
1309 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1310 if (match_op(token
, ':'))
1311 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1312 token
= expect(token
, ')', "after asm");
1313 return expect(token
, ';', "at end of asm-statement");
1316 /* Make a statement out of an expression */
1317 static struct statement
*make_statement(struct expression
*expr
)
1319 struct statement
*stmt
;
1323 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1324 stmt
->expression
= expr
;
1329 * All iterators have two symbols associated with them:
1330 * the "continue" and "break" symbols, which are targets
1331 * for continue and break statements respectively.
1333 * They are in a special name-space, but they follow
1334 * all the normal visibility rules, so nested iterators
1335 * automatically work right.
1337 static void start_iterator(struct statement
*stmt
)
1339 struct symbol
*cont
, *brk
;
1341 start_symbol_scope();
1342 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1343 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1344 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1345 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1347 stmt
->type
= STMT_ITERATOR
;
1348 stmt
->iterator_break
= brk
;
1349 stmt
->iterator_continue
= cont
;
1350 fn_local_symbol(brk
);
1351 fn_local_symbol(cont
);
1354 static void end_iterator(struct statement
*stmt
)
1359 static struct statement
*start_function(struct symbol
*sym
)
1362 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1364 start_function_scope();
1365 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1366 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1367 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1368 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1369 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1371 fn_local_symbol(ret
);
1373 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1379 static void end_function(struct symbol
*sym
)
1382 end_function_scope();
1386 * A "switch()" statement, like an iterator, has a
1387 * the "break" symbol associated with it. It works
1388 * exactly like the iterator break - it's the target
1389 * for any break-statements in scope, and means that
1390 * "break" handling doesn't even need to know whether
1391 * it's breaking out of an iterator or a switch.
1393 * In addition, the "case" symbol is a marker for the
1394 * case/default statements to find the switch statement
1395 * that they are associated with.
1397 static void start_switch(struct statement
*stmt
)
1399 struct symbol
*brk
, *switch_case
;
1401 start_symbol_scope();
1402 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1403 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1405 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1406 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1407 switch_case
->stmt
= stmt
;
1409 stmt
->type
= STMT_SWITCH
;
1410 stmt
->switch_break
= brk
;
1411 stmt
->switch_case
= switch_case
;
1413 fn_local_symbol(brk
);
1414 fn_local_symbol(switch_case
);
1417 static void end_switch(struct statement
*stmt
)
1419 if (!stmt
->switch_case
->symbol_list
)
1420 warning(stmt
->pos
, "switch with no cases");
1424 static void add_case_statement(struct statement
*stmt
)
1426 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1430 sparse_error(stmt
->pos
, "not in switch scope");
1431 stmt
->type
= STMT_NONE
;
1434 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1435 add_symbol(&target
->symbol_list
, sym
);
1437 stmt
->case_label
= sym
;
1438 fn_local_symbol(sym
);
1441 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1443 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1446 error_die(token
->pos
, "internal error: return without a function target");
1447 stmt
->type
= STMT_RETURN
;
1448 stmt
->ret_target
= target
;
1449 return expression_statement(token
->next
, &stmt
->ret_value
);
1452 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1454 struct symbol_list
*syms
;
1455 struct expression
*e1
, *e2
, *e3
;
1456 struct statement
*iterator
;
1458 start_iterator(stmt
);
1459 token
= expect(token
->next
, '(', "after 'for'");
1463 /* C99 variable declaration? */
1464 if (lookup_type(token
)) {
1465 token
= external_declaration(token
, &syms
);
1467 token
= parse_expression(token
, &e1
);
1468 token
= expect(token
, ';', "in 'for'");
1470 token
= parse_expression(token
, &e2
);
1471 token
= expect(token
, ';', "in 'for'");
1472 token
= parse_expression(token
, &e3
);
1473 token
= expect(token
, ')', "in 'for'");
1474 token
= statement(token
, &iterator
);
1476 stmt
->iterator_syms
= syms
;
1477 stmt
->iterator_pre_statement
= make_statement(e1
);
1478 stmt
->iterator_pre_condition
= e2
;
1479 stmt
->iterator_post_statement
= make_statement(e3
);
1480 stmt
->iterator_post_condition
= NULL
;
1481 stmt
->iterator_statement
= iterator
;
1487 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1489 struct expression
*expr
;
1490 struct statement
*iterator
;
1492 start_iterator(stmt
);
1493 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1494 token
= statement(token
, &iterator
);
1496 stmt
->iterator_pre_condition
= expr
;
1497 stmt
->iterator_post_condition
= NULL
;
1498 stmt
->iterator_statement
= iterator
;
1504 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1506 struct expression
*expr
;
1507 struct statement
*iterator
;
1509 start_iterator(stmt
);
1510 token
= statement(token
->next
, &iterator
);
1511 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1512 token
= token
->next
;
1514 sparse_error(token
->pos
, "expected 'while' after 'do'");
1515 token
= parens_expression(token
, &expr
, "after 'do-while'");
1517 stmt
->iterator_post_condition
= expr
;
1518 stmt
->iterator_statement
= iterator
;
1521 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
1522 warning(iterator
->pos
, "do-while statement is not a compound statement");
1524 return expect(token
, ';', "after statement");
1527 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
1529 stmt
->type
= STMT_IF
;
1530 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1531 token
= statement(token
, &stmt
->if_true
);
1532 if (token_type(token
) != TOKEN_IDENT
)
1534 if (token
->ident
!= &else_ident
)
1536 return statement(token
->next
, &stmt
->if_false
);
1539 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
1541 stmt
->type
= STMT_CASE
;
1542 token
= expect(token
, ':', "after default/case");
1543 add_case_statement(stmt
);
1544 return statement(token
, &stmt
->case_statement
);
1547 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
1549 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1550 if (match_op(token
, SPECIAL_ELLIPSIS
))
1551 token
= parse_expression(token
->next
, &stmt
->case_to
);
1552 return case_statement(token
, stmt
);
1555 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
1557 return case_statement(token
->next
, stmt
);
1560 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
1562 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1563 stmt
->type
= STMT_GOTO
;
1564 stmt
->goto_label
= target
;
1566 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1567 return expect(token
->next
, ';', "at end of statement");
1570 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
1572 stmt
->type
= STMT_SWITCH
;
1574 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1575 token
= statement(token
, &stmt
->switch_statement
);
1580 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
1582 stmt
->type
= STMT_GOTO
;
1583 token
= token
->next
;
1584 if (match_op(token
, '*')) {
1585 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1586 add_statement(&function_computed_goto_list
, stmt
);
1587 } else if (token_type(token
) == TOKEN_IDENT
) {
1588 stmt
->goto_label
= label_symbol(token
);
1589 token
= token
->next
;
1591 sparse_error(token
->pos
, "Expected identifier or goto expression");
1593 return expect(token
, ';', "at end of statement");
1596 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
1598 stmt
->type
= STMT_CONTEXT
;
1599 token
= parse_expression(token
->next
, &stmt
->expression
);
1600 if(stmt
->expression
->type
== EXPR_PREOP
1601 && stmt
->expression
->op
== '('
1602 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
1603 struct expression
*expr
;
1604 expr
= stmt
->expression
->unop
;
1605 stmt
->context
= expr
->left
;
1606 stmt
->expression
= expr
->right
;
1608 return expect(token
, ';', "at end of statement");
1611 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
1613 stmt
->type
= STMT_RANGE
;
1614 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1615 token
= expect(token
, ',', "after range expression");
1616 token
= assignment_expression(token
, &stmt
->range_low
);
1617 token
= expect(token
, ',', "after low range");
1618 token
= assignment_expression(token
, &stmt
->range_high
);
1619 return expect(token
, ';', "after range statement");
1622 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1624 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1627 if (token_type(token
) == TOKEN_IDENT
) {
1628 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1629 if (s
&& s
->op
->statement
)
1630 return s
->op
->statement(token
, stmt
);
1632 if (match_op(token
->next
, ':')) {
1633 stmt
->type
= STMT_LABEL
;
1634 stmt
->label_identifier
= label_symbol(token
);
1635 return statement(token
->next
->next
, &stmt
->label_statement
);
1639 if (match_op(token
, '{')) {
1640 stmt
->type
= STMT_COMPOUND
;
1641 start_symbol_scope();
1642 token
= compound_statement(token
->next
, stmt
);
1645 return expect(token
, '}', "at end of compound statement");
1648 stmt
->type
= STMT_EXPRESSION
;
1649 return expression_statement(token
, &stmt
->expression
);
1652 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1654 int seen_statement
= 0;
1656 struct statement
* stmt
;
1657 if (eof_token(token
))
1659 if (match_op(token
, '}'))
1661 if (lookup_type(token
)) {
1662 if (seen_statement
) {
1663 warning(token
->pos
, "mixing declarations and code");
1666 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1667 token
= external_declaration(token
, &stmt
->declaration
);
1669 seen_statement
= warn_on_mixed
;
1670 token
= statement(token
, &stmt
);
1672 add_statement(list
, stmt
);
1677 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1679 struct symbol_list
**list
= &fn
->arguments
;
1681 if (match_op(token
, ')')) {
1682 // No warning for "void oink ();"
1683 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1684 if (p
&& !match_op(token
->next
, ';'))
1685 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1692 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1694 warning(token
->pos
, "variadic functions must have one named argument");
1696 token
= token
->next
;
1700 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1701 token
= parameter_declaration(token
, &sym
);
1702 if (sym
->ctype
.base_type
== &void_ctype
) {
1703 /* Special case: (void) */
1704 if (!*list
&& !sym
->ident
)
1706 warning(token
->pos
, "void parameter");
1708 add_symbol(list
, sym
);
1709 if (!match_op(token
, ','))
1711 token
= token
->next
;
1717 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1719 token
= statement_list(token
, &stmt
->stmts
);
1723 static struct expression
*identifier_expression(struct token
*token
)
1725 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1726 expr
->expr_ident
= token
->ident
;
1730 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1732 int idx_from
, idx_to
;
1733 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1735 idx_from
= get_expression_value(from
);
1738 idx_to
= get_expression_value(to
);
1739 if (idx_to
< idx_from
|| idx_from
< 0)
1740 warning(from
->pos
, "nonsense array initializer index range");
1742 expr
->idx_from
= idx_from
;
1743 expr
->idx_to
= idx_to
;
1747 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1749 int expect_equal
= 0;
1750 struct token
*next
= token
->next
;
1751 struct expression
**tail
= ep
;
1756 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1757 struct expression
*expr
= identifier_expression(token
);
1758 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1759 token
= initializer(&expr
->ident_expression
, next
->next
);
1760 if (expr
->ident_expression
)
1765 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1766 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1767 struct expression
*expr
= identifier_expression(next
);
1769 tail
= &expr
->ident_expression
;
1772 } else if (match_op(token
, '[')) {
1773 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1774 token
= constant_expression(token
->next
, &from
);
1776 sparse_error(token
->pos
, "Expected constant expression");
1779 if (match_op(token
, SPECIAL_ELLIPSIS
))
1780 token
= constant_expression(token
->next
, &to
);
1781 expr
= index_expression(from
, to
);
1783 tail
= &expr
->idx_expression
;
1784 token
= expect(token
, ']', "at end of initializer index");
1791 if (nested
&& !expect_equal
) {
1792 if (!match_op(token
, '='))
1793 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1798 token
= expect(token
, '=', "at end of initializer index");
1800 token
= initializer(tail
, token
);
1806 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1808 struct expression
*expr
;
1811 token
= single_initializer(&expr
, token
);
1814 add_expression(list
, expr
);
1815 if (!match_op(token
, ','))
1817 token
= token
->next
;
1822 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1824 if (match_op(token
, '{')) {
1825 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1827 token
= initializer_list(&expr
->expr_list
, token
->next
);
1828 return expect(token
, '}', "at end of initializer");
1830 return assignment_expression(token
, tree
);
1833 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1836 sparse_error(sym
->pos
, "no identifier for function argument");
1839 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1842 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1843 struct symbol_list
**list
)
1845 struct symbol_list
**old_symbol_list
;
1846 struct symbol
*base_type
= decl
->ctype
.base_type
;
1847 struct statement
*stmt
, **p
;
1850 old_symbol_list
= function_symbol_list
;
1851 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1852 function_symbol_list
= &decl
->inline_symbol_list
;
1853 p
= &base_type
->inline_stmt
;
1855 function_symbol_list
= &decl
->symbol_list
;
1856 p
= &base_type
->stmt
;
1858 function_computed_target_list
= NULL
;
1859 function_computed_goto_list
= NULL
;
1861 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1862 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1863 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1865 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1866 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1868 stmt
= start_function(decl
);
1871 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1872 declare_argument(arg
, base_type
);
1873 } END_FOR_EACH_PTR(arg
);
1875 token
= compound_statement(token
->next
, stmt
);
1878 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1879 add_symbol(list
, decl
);
1880 check_declaration(decl
);
1881 function_symbol_list
= old_symbol_list
;
1882 if (function_computed_goto_list
) {
1883 if (!function_computed_target_list
)
1884 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1886 struct statement
*stmt
;
1887 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1888 stmt
->target_list
= function_computed_target_list
;
1889 } END_FOR_EACH_PTR(stmt
);
1892 return expect(token
, '}', "at end of function");
1895 static void promote_k_r_types(struct symbol
*arg
)
1897 struct symbol
*base
= arg
->ctype
.base_type
;
1898 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1899 arg
->ctype
.base_type
= &int_ctype
;
1903 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1905 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1908 FOR_EACH_PTR(real_args
, arg
) {
1909 struct symbol
*type
;
1911 /* This is quadratic in the number of arguments. We _really_ don't care */
1912 FOR_EACH_PTR(argtypes
, type
) {
1913 if (type
->ident
== arg
->ident
)
1915 } END_FOR_EACH_PTR(type
);
1916 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1920 /* "char" and "short" promote to "int" */
1921 promote_k_r_types(type
);
1923 arg
->ctype
= type
->ctype
;
1924 } END_FOR_EACH_PTR(arg
);
1926 FOR_EACH_PTR(argtypes
, arg
) {
1928 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1929 } END_FOR_EACH_PTR(arg
);
1933 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
1934 struct symbol_list
**list
)
1936 struct symbol_list
*args
= NULL
;
1938 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
1940 token
= declaration_list(token
, &args
);
1941 if (!match_op(token
, ';')) {
1942 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
1945 token
= token
->next
;
1946 } while (lookup_type(token
));
1948 apply_k_r_types(args
, decl
);
1950 if (!match_op(token
, '{')) {
1951 sparse_error(token
->pos
, "expected function body");
1954 return parse_function_body(token
, decl
, list
);
1957 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
1959 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1960 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1961 struct statement
*stmt
;
1963 anon
->ctype
.base_type
= fn
;
1964 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1967 token
= parse_asm(token
, stmt
);
1969 add_symbol(list
, anon
);
1973 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1975 struct ident
*ident
= NULL
;
1976 struct symbol
*decl
;
1977 struct ctype ctype
= { 0, };
1978 struct symbol
*base_type
;
1981 /* Top-level inline asm? */
1982 if (token_type(token
) == TOKEN_IDENT
) {
1983 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1984 if (s
&& s
->op
->toplevel
)
1985 return s
->op
->toplevel(token
, list
);
1988 /* Parse declaration-specifiers, if any */
1989 token
= declaration_specifiers(token
, &ctype
, 0);
1990 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1991 decl
->ctype
= ctype
;
1992 token
= declarator(token
, decl
, &ident
);
1993 apply_modifiers(token
->pos
, &decl
->ctype
);
1995 /* Just a type declaration? */
1997 return expect(token
, ';', "end of type declaration");
1999 /* type define declaration? */
2000 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
2002 /* Typedef's don't have meaningful storage */
2004 ctype
.modifiers
&= ~MOD_STORAGE
;
2005 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
2006 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2009 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2011 base_type
= decl
->ctype
.base_type
;
2014 if (base_type
&& !base_type
->ident
)
2015 base_type
->ident
= ident
;
2016 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2017 /* K&R argument declaration? */
2018 if (lookup_type(token
))
2019 return parse_k_r_arguments(token
, decl
, list
);
2020 if (match_op(token
, '{'))
2021 return parse_function_body(token
, decl
, list
);
2023 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2024 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2025 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2026 sparse_error(token
->pos
, "void declaration");
2030 if (!is_typedef
&& match_op(token
, '=')) {
2031 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2032 warning(decl
->pos
, "symbol with external linkage has initializer");
2033 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2035 token
= initializer(&decl
->initializer
, token
->next
);
2038 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2039 add_symbol(list
, decl
);
2040 fn_local_symbol(decl
);
2043 check_declaration(decl
);
2045 if (!match_op(token
, ','))
2048 token
= token
->next
;
2050 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2051 decl
->ctype
= ctype
;
2052 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
2053 token
= declarator(token
, decl
, &ident
);
2054 apply_modifiers(token
->pos
, &decl
->ctype
);
2056 sparse_error(token
->pos
, "expected identifier name in type definition");
2060 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2062 /* Function declarations are automatically extern unless specifically static */
2063 base_type
= decl
->ctype
.base_type
;
2064 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2065 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2066 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2069 return expect(token
, ';', "at end of declaration");