2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
9 * Licensed under the Open Software License version 1.1
25 #include "expression.h"
28 static struct symbol_list
**function_symbol_list
;
30 // Add a symbol to the list of function-local symbols
31 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
33 static struct token
*statement(struct token
*token
, struct statement
**tree
);
34 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
36 struct statement
*alloc_statement(struct position pos
, int type
)
38 struct statement
*stmt
= __alloc_statement(0);
44 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
46 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
48 struct symbol
*sym
= alloc_symbol(pos
, type
);
50 sym
->ctype
.base_type
= ctype
->base_type
;
51 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
53 ctype
->base_type
= sym
;
54 ctype
->modifiers
&= MOD_STORAGE
;
58 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
60 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
62 sym
= alloc_symbol(token
->pos
, type
);
63 sym
->ident
= token
->ident
;
64 bind_symbol(sym
, token
->ident
, ns
);
65 if (type
== SYM_LABEL
)
72 * NOTE! NS_LABEL is not just a different namespace,
73 * it also ends up using function scope instead of the
74 * regular symbol scope.
76 struct symbol
*label_symbol(struct token
*token
)
78 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
81 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
82 struct token
*token
, struct ctype
*ctype
,
83 struct token
*(*parse
)(struct token
*, struct symbol
*))
88 if (token_type(token
) == TOKEN_IDENT
) {
89 sym
= lookup_or_create_symbol(ns
, type
, token
);
91 ctype
->base_type
= sym
;
92 if (match_op(token
, '{')) {
93 token
= parse(token
->next
, sym
);
94 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
99 // private struct/union/enum type
100 if (!match_op(token
, '{')) {
101 warn(token
->pos
, "expected declaration");
102 ctype
->base_type
= &bad_type
;
106 sym
= alloc_symbol(token
->pos
, type
);
107 token
= parse(token
->next
, sym
);
108 ctype
->base_type
= sym
;
109 return expect(token
, '}', "at end of specifier");
112 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
114 return struct_declaration_list(token
, &sym
->symbol_list
);
117 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
119 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
122 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
125 while (token_type(token
) == TOKEN_IDENT
) {
126 struct token
*next
= token
->next
;
129 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
130 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
131 sym
->ctype
.base_type
= parent
;
133 if (match_op(next
, '=')) {
134 struct expression
*expr
;
135 next
= constant_expression(next
->next
, &expr
);
136 nextval
= get_expression_value(expr
);
138 sym
->value
= nextval
;
141 if (!match_op(token
, ','))
144 nextval
= nextval
+ 1;
149 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
151 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
154 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
158 if (!match_op(token
, '(')) {
159 warn(token
->pos
, "expected '(' after typeof");
162 if (lookup_type(token
->next
)) {
163 token
= typename(token
->next
, &sym
);
166 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
167 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
169 ctype
->modifiers
= 0;
170 ctype
->base_type
= typeof_sym
;
172 return expect(token
, ')', "after typeof");
175 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
177 if (match_string_ident(attribute
, "packed") ||
178 match_string_ident(attribute
, "__packed__")) {
179 ctype
->alignment
= 1;
182 if (match_string_ident(attribute
, "aligned") ||
183 match_string_ident(attribute
, "__aligned__")) {
184 int alignment
= MAX_ALIGNMENT
;
186 alignment
= get_expression_value(expr
);
187 ctype
->alignment
= alignment
;
190 if (match_string_ident(attribute
, "nocast")) {
191 ctype
->modifiers
|= MOD_NOCAST
;
194 if (match_string_ident(attribute
, "noderef")) {
195 ctype
->modifiers
|= MOD_NODEREF
;
198 if (match_string_ident(attribute
, "safe")) {
199 ctype
->modifiers
|= MOD_SAFE
;
202 if (match_string_ident(attribute
, "address_space")) {
204 return "expected address space number";
205 ctype
->as
= get_expression_value(expr
);
208 if (match_string_ident(attribute
, "context")) {
209 if (expr
&& expr
->type
== EXPR_COMMA
) {
210 int mask
= get_expression_value(expr
->left
);
211 int value
= get_expression_value(expr
->right
);
213 return "nonsense attribute types";
214 ctype
->contextmask
|= mask
;
215 ctype
->context
|= value
;
218 return "expected context mask and value";
220 if (match_string_ident(attribute
, "mode") ||
221 match_string_ident(attribute
, "__mode__")) {
222 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
223 struct ident
*ident
= expr
->symbol_name
;
226 * Match against __HI__/__SI__/__DI__
228 * FIXME! This is broken - we don't actually get
229 * the type information updated properly at this
230 * stage for some reason.
232 if (match_string_ident(ident
, "__HI__")) {
233 ctype
->modifiers
|= MOD_SHORT
;
234 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
237 if (match_string_ident(ident
, "__SI__")) {
241 if (match_string_ident(ident
, "__DI__")) {
242 ctype
->modifiers
|= MOD_LONGLONG
;
243 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
246 return "unknown mode attribute";
248 return "expected attribute mode symbol";
251 /* Throw away for now.. */
252 if (match_string_ident(attribute
, "format") ||
253 match_string_ident(attribute
, "__format__"))
255 if (match_string_ident(attribute
, "section") ||
256 match_string_ident(attribute
, "__section__"))
258 if (match_string_ident(attribute
, "unused") ||
259 match_string_ident(attribute
, "__unused__"))
261 if (match_string_ident(attribute
, "const") ||
262 match_string_ident(attribute
, "__const__"))
264 if (match_string_ident(attribute
, "noreturn"))
266 if (match_string_ident(attribute
, "regparm"))
268 if (match_string_ident(attribute
, "weak"))
271 return "unknown attribute";
274 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
276 ctype
->modifiers
= 0;
277 token
= expect(token
, '(', "after attribute");
278 token
= expect(token
, '(', "after attribute");
282 struct ident
*attribute_name
;
283 struct expression
*attribute_expr
;
285 if (eof_token(token
))
287 if (match_op(token
, ';'))
289 if (token_type(token
) != TOKEN_IDENT
)
291 attribute_name
= token
->ident
;
293 attribute_expr
= NULL
;
294 if (match_op(token
, '('))
295 token
= parens_expression(token
, &attribute_expr
, "in attribute");
296 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
298 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
299 if (!match_op(token
, ','))
304 token
= expect(token
, ')', "after attribute");
305 token
= expect(token
, ')', "after attribute");
309 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
310 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
312 struct symbol
* ctype_integer(unsigned int spec
)
314 static struct symbol
*const integer_ctypes
[][2] = {
315 { &llong_ctype
, &ullong_ctype
},
316 { &long_ctype
, &ulong_ctype
},
317 { &short_ctype
, &ushort_ctype
},
318 { &char_ctype
, &uchar_ctype
},
319 { &int_ctype
, &uint_ctype
},
321 struct symbol
*const (*ctype
)[2];
323 ctype
= integer_ctypes
;
324 if (!(spec
& MOD_LONGLONG
)) {
326 if (!(spec
& MOD_LONG
)) {
328 if (!(spec
& MOD_SHORT
)) {
330 if (!(spec
& MOD_CHAR
))
335 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
338 struct symbol
* ctype_fp(unsigned int spec
)
340 if (spec
& MOD_LONGLONG
)
341 return &ldouble_ctype
;
343 return &double_ctype
;
347 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
349 unsigned long mod
= thistype
->modifiers
;
352 unsigned long old
= ctype
->modifiers
;
353 unsigned long extra
= 0, dup
;
355 if (mod
& old
& MOD_LONG
) {
356 extra
= MOD_LONGLONG
| MOD_LONG
;
360 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
362 warn(pos
, "Just how %sdo you want this type to be?",
363 modifier_string(dup
));
364 ctype
->modifiers
= old
| mod
| extra
;
367 /* Context mask and value */
368 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
369 warn(pos
, "inconsistend attribute types");
370 thistype
->context
= 0;
371 thistype
->contextmask
= 0;
373 ctype
->context
|= thistype
->context
;
374 ctype
->contextmask
|= thistype
->contextmask
;
377 if (thistype
->alignment
& (thistype
->alignment
-1)) {
378 warn(pos
, "I don't like non-power-of-2 alignments");
379 thistype
->alignment
= 0;
381 if (thistype
->alignment
> ctype
->alignment
)
382 ctype
->alignment
= thistype
->alignment
;
385 ctype
->as
= thistype
->as
;
389 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
393 while ( (token
= next
) != NULL
) {
394 struct ctype thistype
;
396 struct symbol
*s
, *type
;
400 if (token_type(token
) != TOKEN_IDENT
)
402 ident
= token
->ident
;
404 s
= lookup_symbol(ident
, NS_TYPEDEF
);
408 mod
= thistype
.modifiers
;
409 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
411 if (mod
& MOD_SPECIALBITS
) {
412 if (mod
& MOD_STRUCTOF
)
413 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
414 else if (mod
& MOD_UNIONOF
)
415 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
416 else if (mod
& MOD_ENUMOF
)
417 next
= enum_specifier(next
, &thistype
);
418 else if (mod
& MOD_ATTRIBUTE
)
419 next
= attribute_specifier(next
, &thistype
);
420 else if (mod
& MOD_TYPEOF
)
421 next
= typeof_specifier(next
, &thistype
);
422 mod
= thistype
.modifiers
;
424 type
= thistype
.base_type
;
428 if (ctype
->base_type
)
430 ctype
->base_type
= type
;
433 apply_ctype(token
->pos
, &thistype
, ctype
);
436 /* Turn the "virtual types" into real types with real sizes etc */
437 if (!ctype
->base_type
&& (ctype
->modifiers
& MOD_SPECIFIER
))
438 ctype
->base_type
= &int_type
;
440 if (ctype
->base_type
== &int_type
) {
441 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
442 ctype
->modifiers
&= ~MOD_SPECIFIER
;
445 if (ctype
->base_type
== &fp_type
) {
446 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
447 ctype
->modifiers
&= ~MOD_SPECIFIER
;
453 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
455 struct expression
*expr
= NULL
;
457 token
= parse_expression(token
, &expr
);
458 sym
->array_size
= expr
;
462 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
463 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
465 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
467 struct ctype
*ctype
= &(*tree
)->ctype
;
469 if (p
&& token_type(token
) == TOKEN_IDENT
) {
475 if (match_ident(token
, &__attribute___ident
) || match_ident(token
, &__attribute_ident
)) {
476 struct ctype thistype
= { 0, };
477 token
= attribute_specifier(token
->next
, &thistype
);
478 apply_ctype(token
->pos
, &thistype
, ctype
);
481 if (token_type(token
) != TOKEN_SPECIAL
)
485 * This can be either a parameter list or a grouping.
486 * For the direct (non-abstract) case, we know if must be
487 * a paramter list if we already saw the identifier.
488 * For the abstract case, we know if must be a parameter
489 * list if it is empty or starts with a type.
491 if (token
->special
== '(') {
493 struct token
*next
= token
->next
;
494 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
497 struct symbol
*base_type
= ctype
->base_type
;
498 token
= declarator(next
, tree
, p
);
499 token
= expect(token
, ')', "in nested declarator");
500 while (ctype
->base_type
!= base_type
)
501 ctype
= &ctype
->base_type
->ctype
;
506 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
507 token
= parameter_type_list(next
, sym
);
508 token
= expect(token
, ')', "in function declarator");
511 if (token
->special
== '[') {
512 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
513 token
= abstract_array_declarator(token
->next
, array
);
514 token
= expect(token
, ']', "in abstract_array_declarator");
515 ctype
= &array
->ctype
;
518 if (token
->special
== ':') {
519 struct symbol
*bitfield
;
520 struct expression
*expr
;
521 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
522 token
= conditional_expression(token
->next
, &expr
);
523 bitfield
->fieldwidth
= get_expression_value(expr
);
534 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
536 unsigned long modifiers
;
537 struct symbol
*base_type
;
539 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
540 base_type
= ctype
->base_type
;
541 ctype
->modifiers
= modifiers
;
543 while (match_op(token
,'*')) {
544 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
545 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
546 ptr
->ctype
.as
= ctype
->as
;
547 ptr
->ctype
.context
= ctype
->context
;
548 ptr
->ctype
.contextmask
= ctype
->contextmask
;
549 ptr
->ctype
.base_type
= base_type
;
552 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
553 ctype
->base_type
= base_type
;
556 ctype
->contextmask
= 0;
558 token
= declaration_specifiers(token
->next
, ctype
, 1);
563 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
565 token
= pointer(token
, &(*tree
)->ctype
);
566 return direct_declarator(token
, tree
, p
);
569 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
571 while (!match_op(token
, '}')) {
572 struct ctype ctype
= {0, };
574 token
= declaration_specifiers(token
, &ctype
, 0);
576 struct ident
*ident
= NULL
;
577 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
579 token
= pointer(token
, &decl
->ctype
);
580 token
= direct_declarator(token
, &decl
, &ident
);
581 if (match_op(token
, ':')) {
582 struct expression
*expr
;
583 token
= parse_expression(token
->next
, &expr
);
585 add_symbol(list
, decl
);
586 if (!match_op(token
, ','))
590 if (!match_op(token
, ';'))
597 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
599 struct ident
*ident
= NULL
;
601 struct ctype ctype
= { 0, };
603 token
= declaration_specifiers(token
, &ctype
, 0);
604 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
607 token
= pointer(token
, &sym
->ctype
);
608 token
= direct_declarator(token
, tree
, &ident
);
612 struct token
*typename(struct token
*token
, struct symbol
**p
)
614 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
616 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
617 return declarator(token
, &sym
, NULL
);
620 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
622 token
= parse_expression(token
, tree
);
623 return expect(token
, ';', "at end of statement");
626 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
628 struct expression
*expr
;
630 /* Allow empty operands */
631 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
634 token
= primary_expression(token
->next
, &expr
);
635 token
= parens_expression(token
, &expr
, "in asm parameter");
636 } while (match_op(token
, ','));
640 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
642 struct expression
*expr
;
645 token
= primary_expression(token
->next
, &expr
);
646 } while (match_op(token
, ','));
650 /* Make a statement out of an expression */
651 static struct statement
*make_statement(struct expression
*expr
)
653 struct statement
*stmt
;
657 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
658 stmt
->expression
= expr
;
663 * All iterators have two symbols associated with them:
664 * the "continue" and "break" symbols, which are targets
665 * for continue and break statements respectively.
667 * They are in a special name-space, but they follow
668 * all the normal visibility rules, so nested iterators
669 * automatically work right.
671 static void start_iterator(struct statement
*stmt
)
673 struct symbol
*cont
, *brk
;
675 start_symbol_scope();
676 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
677 cont
->ident
= &continue_ident
;
678 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
679 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
680 brk
->ident
= &break_ident
;
681 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
683 stmt
->type
= STMT_ITERATOR
;
684 stmt
->iterator_break
= brk
;
685 stmt
->iterator_continue
= cont
;
686 fn_local_symbol(brk
);
687 fn_local_symbol(cont
);
690 static void end_iterator(struct statement
*stmt
)
695 static struct statement
*start_function(struct symbol
*sym
)
698 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
700 start_function_scope();
701 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
702 ret
->ident
= &return_ident
;
703 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
704 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
705 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
706 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
709 fn_local_symbol(ret
);
713 static void end_function(struct symbol
*sym
)
715 end_function_scope();
719 * A "switch()" statement, like an iterator, has a
720 * the "break" symbol associated with it. It works
721 * exactly like the iterator break - it's the target
722 * for any break-statements in scope, and means that
723 * "break" handling doesn't even need to know whether
724 * it's breaking out of an iterator or a switch.
726 * In addition, the "case" symbol is a marker for the
727 * case/default statements to find the switch statement
728 * that they are associated with.
730 static void start_switch(struct statement
*stmt
)
732 struct symbol
*brk
, *switch_case
;
734 start_symbol_scope();
735 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
736 brk
->ident
= &break_ident
;
737 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
739 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
740 switch_case
->ident
= &case_ident
;
741 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
742 switch_case
->stmt
= stmt
;
744 stmt
->type
= STMT_SWITCH
;
745 stmt
->switch_break
= brk
;
746 stmt
->switch_case
= switch_case
;
748 fn_local_symbol(brk
);
749 fn_local_symbol(switch_case
);
752 static void end_switch(struct statement
*stmt
)
754 if (!stmt
->switch_case
->symbol_list
)
755 warn(stmt
->pos
, "switch with no cases");
759 static void add_case_statement(struct statement
*stmt
)
761 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
765 warn(stmt
->pos
, "not in switch scope");
768 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
769 add_symbol(&target
->symbol_list
, sym
);
771 stmt
->case_label
= sym
;
772 fn_local_symbol(sym
);
775 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
777 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
780 error(token
->pos
, "internal error: return without a function target");
781 stmt
->type
= STMT_RETURN
;
782 stmt
->ret_target
= target
;
783 return expression_statement(token
->next
, &stmt
->ret_value
);
786 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
788 struct symbol_list
*syms
;
789 struct expression
*e1
, *e2
, *e3
;
790 struct statement
*iterator
;
792 start_iterator(stmt
);
793 token
= expect(token
->next
, '(', "after 'for'");
797 /* C99 variable declaration? */
798 if (lookup_type(token
)) {
799 token
= external_declaration(token
, &syms
);
801 token
= parse_expression(token
, &e1
);
802 token
= expect(token
, ';', "in 'for'");
804 token
= parse_expression(token
, &e2
);
805 token
= expect(token
, ';', "in 'for'");
806 token
= parse_expression(token
, &e3
);
807 token
= expect(token
, ')', "in 'for'");
808 token
= statement(token
, &iterator
);
810 stmt
->iterator_syms
= syms
;
811 stmt
->iterator_pre_statement
= make_statement(e1
);
812 stmt
->iterator_pre_condition
= e2
;
813 stmt
->iterator_post_statement
= make_statement(e3
);
814 stmt
->iterator_post_condition
= e2
;
815 stmt
->iterator_statement
= iterator
;
821 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
823 struct expression
*expr
;
824 struct statement
*iterator
;
826 start_iterator(stmt
);
827 token
= parens_expression(token
->next
, &expr
, "after 'while'");
828 token
= statement(token
, &iterator
);
830 stmt
->iterator_pre_condition
= expr
;
831 stmt
->iterator_post_condition
= expr
;
832 stmt
->iterator_statement
= iterator
;
838 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
840 struct expression
*expr
;
841 struct statement
*iterator
;
843 start_iterator(stmt
);
844 token
= statement(token
->next
, &iterator
);
845 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
848 warn(token
->pos
, "expected 'while' after 'do'");
849 token
= parens_expression(token
, &expr
, "after 'do-while'");
851 stmt
->iterator_post_condition
= expr
;
852 stmt
->iterator_statement
= iterator
;
855 return expect(token
, ';', "after statement");
858 static struct token
*statement(struct token
*token
, struct statement
**tree
)
860 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
863 if (token_type(token
) == TOKEN_IDENT
) {
864 if (token
->ident
== &if_ident
) {
865 stmt
->type
= STMT_IF
;
866 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
867 token
= statement(token
, &stmt
->if_true
);
868 if (token_type(token
) != TOKEN_IDENT
)
870 if (token
->ident
!= &else_ident
)
872 return statement(token
->next
, &stmt
->if_false
);
875 if (token
->ident
== &return_ident
)
876 return parse_return_statement(token
, stmt
);
878 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
879 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
880 stmt
->type
= STMT_GOTO
;
881 stmt
->goto_label
= target
;
883 warn(stmt
->pos
, "break/continue not in iterator scope");
884 return expect(token
->next
, ';', "at end of statement");
886 if (token
->ident
== &default_ident
) {
888 goto default_statement
;
890 if (token
->ident
== &case_ident
) {
891 token
= parse_expression(token
->next
, &stmt
->case_expression
);
892 if (match_op(token
, SPECIAL_ELLIPSIS
))
893 token
= parse_expression(token
->next
, &stmt
->case_to
);
895 stmt
->type
= STMT_CASE
;
896 token
= expect(token
, ':', "after default/case");
897 add_case_statement(stmt
);
898 return statement(token
, &stmt
->case_statement
);
900 if (token
->ident
== &switch_ident
) {
901 stmt
->type
= STMT_SWITCH
;
903 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
904 token
= statement(token
, &stmt
->switch_statement
);
908 if (token
->ident
== &for_ident
)
909 return parse_for_statement(token
, stmt
);
911 if (token
->ident
== &while_ident
)
912 return parse_while_statement(token
, stmt
);
914 if (token
->ident
== &do_ident
)
915 return parse_do_statement(token
, stmt
);
917 if (token
->ident
== &goto_ident
) {
918 stmt
->type
= STMT_GOTO
;
920 if (match_op(token
, '*')) {
921 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
922 } else if (token_type(token
) == TOKEN_IDENT
) {
923 stmt
->goto_label
= label_symbol(token
);
926 warn(token
->pos
, "Expected identifier or goto expression");
928 return expect(token
, ';', "at end of statement");
930 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm___ident
|| token
->ident
== &__asm_ident
) {
931 struct expression
*expr
;
932 stmt
->type
= STMT_ASM
;
934 if (token_type(token
) == TOKEN_IDENT
) {
935 if (token
->ident
== &__volatile___ident
|| token
->ident
== &volatile_ident
)
938 token
= expect(token
, '(', "after asm");
939 token
= parse_expression(token
->next
, &expr
);
940 if (match_op(token
, ':'))
941 token
= parse_asm_operands(token
, stmt
);
942 if (match_op(token
, ':'))
943 token
= parse_asm_operands(token
, stmt
);
944 if (match_op(token
, ':'))
945 token
= parse_asm_clobbers(token
, stmt
);
946 token
= expect(token
, ')', "after asm");
947 return expect(token
, ';', "at end of asm-statement");
949 if (match_op(token
->next
, ':')) {
950 stmt
->type
= STMT_LABEL
;
951 stmt
->label_identifier
= label_symbol(token
);
952 return statement(token
->next
->next
, &stmt
->label_statement
);
956 if (match_op(token
, '{')) {
957 stmt
->type
= STMT_COMPOUND
;
958 start_symbol_scope();
959 token
= compound_statement(token
->next
, stmt
);
962 return expect(token
, '}', "at end of compound statement");
965 stmt
->type
= STMT_EXPRESSION
;
966 return expression_statement(token
, &stmt
->expression
);
969 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
972 struct statement
* stmt
;
973 if (eof_token(token
))
975 if (match_op(token
, '}'))
977 token
= statement(token
, &stmt
);
978 add_statement(list
, stmt
);
983 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
985 struct symbol_list
**list
= &fn
->arguments
;
987 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
989 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
995 if (!lookup_type(token
)) {
996 warn(token
->pos
, "non-ANSI parameter list");
999 token
= parameter_declaration(token
, &sym
);
1000 /* Special case: (void) */
1001 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1003 add_symbol(list
, sym
);
1004 if (!match_op(token
, ','))
1006 token
= token
->next
;
1012 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1014 while (!eof_token(token
)) {
1015 if (!lookup_type(token
))
1017 token
= external_declaration(token
, &stmt
->syms
);
1019 token
= statement_list(token
, &stmt
->stmts
);
1023 static struct expression
*identifier_expression(struct token
*token
)
1025 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1026 expr
->expr_ident
= token
->ident
;
1030 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1032 int idx_from
, idx_to
;
1033 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1035 idx_from
= get_expression_value(from
);
1038 idx_to
= get_expression_value(to
);
1039 if (idx_to
< idx_from
|| idx_from
< 0)
1040 warn(from
->pos
, "nonsense array initializer index range");
1042 expr
->idx_from
= idx_from
;
1043 expr
->idx_to
= idx_to
;
1047 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1050 struct token
*next
= token
->next
;
1051 struct expression
*expr
;
1053 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1054 add_expression(list
, identifier_expression(next
));
1055 token
= next
->next
->next
;
1056 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1057 add_expression(list
, identifier_expression(token
));
1059 } else if (match_op(token
, '[')) {
1060 struct expression
*from
= NULL
, *to
= NULL
;
1061 token
= constant_expression(token
->next
, &from
);
1062 if (match_op(token
, SPECIAL_ELLIPSIS
))
1063 token
= constant_expression(token
->next
, &to
);
1064 add_expression(list
, index_expression(from
, to
));
1065 token
= expect(token
, ']', "at end of initializer index");
1066 token
= expect(token
, '=', "at end of initializer index");
1070 token
= initializer(&expr
, token
);
1073 add_expression(list
, expr
);
1074 if (!match_op(token
, ','))
1076 token
= token
->next
;
1081 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1083 if (match_op(token
, '{')) {
1084 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1086 token
= initializer_list(&expr
->expr_list
, token
->next
);
1087 return expect(token
, '}', "at end of initializer");
1089 return assignment_expression(token
, tree
);
1092 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1095 warn(sym
->pos
, "no identifier for function argument");
1098 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1101 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1102 struct symbol_list
**list
)
1104 struct symbol
*base_type
= decl
->ctype
.base_type
;
1105 struct statement
*stmt
;
1108 function_symbol_list
= &decl
->symbol_list
;
1109 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1110 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1111 warn(decl
->pos
, "function with external linkage has definition");
1113 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1114 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1116 stmt
= start_function(decl
);
1118 base_type
->stmt
= stmt
;
1119 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1120 declare_argument(arg
, base_type
);
1123 token
= compound_statement(token
->next
, stmt
);
1126 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1127 add_symbol(list
, decl
);
1128 check_declaration(decl
);
1129 function_symbol_list
= NULL
;
1130 return expect(token
, '}', "at end of function");
1133 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1135 struct ident
*ident
= NULL
;
1136 struct symbol
*decl
;
1137 struct ctype ctype
= { 0, };
1138 struct symbol
*base_type
;
1141 /* Parse declaration-specifiers, if any */
1142 token
= declaration_specifiers(token
, &ctype
, 0);
1143 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1144 decl
->ctype
= ctype
;
1145 token
= pointer(token
, &decl
->ctype
);
1146 token
= declarator(token
, &decl
, &ident
);
1148 /* Just a type declaration? */
1150 return expect(token
, ';', "end of type declaration");
1152 decl
->ident
= ident
;
1154 /* type define declaration? */
1155 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1157 /* Typedef's don't have meaningful storage */
1159 ctype
.modifiers
&= ~MOD_STORAGE
;
1160 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1163 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1165 base_type
= decl
->ctype
.base_type
;
1166 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1167 if (match_op(token
, '{'))
1168 return parse_function_body(token
, decl
, list
);
1170 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1171 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1175 if (token_type(token
) == TOKEN_IDENT
) {
1176 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1177 struct expression
*expr
;
1179 token
= expect(token
->next
, '(', "after asm");
1180 token
= parse_expression(token
->next
, &expr
);
1181 token
= expect(token
, ')', "after asm");
1184 if (!is_typedef
&& match_op(token
, '=')) {
1185 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1186 warn(decl
->pos
, "symbol with external linkage has initializer");
1187 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1189 token
= initializer(&decl
->initializer
, token
->next
);
1192 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1193 add_symbol(list
, decl
);
1194 if (function_symbol_list
)
1195 fn_local_symbol(decl
);
1198 check_declaration(decl
);
1200 if (!match_op(token
, ','))
1204 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1205 decl
->ctype
= ctype
;
1206 token
= pointer(token
, &decl
->ctype
);
1207 token
= declarator(token
->next
, &decl
, &ident
);
1209 warn(token
->pos
, "expected identifier name in type definition");
1213 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1215 /* Function declarations are automatically extern unless specifically static */
1216 base_type
= decl
->ctype
.base_type
;
1217 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1218 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1219 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1222 return expect(token
, ';', "at end of declaration");
1225 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1227 while (!eof_token(token
))
1228 token
= external_declaration(token
, list
);
1229 // They aren't needed any more
1230 clear_token_alloc();