2 typedef int __builtin_va_list
;
5 * Stupid C parser, version 1e-6.
7 * Let's see how hard this is to do.
23 void show_statement(struct statement
*stmt
)
26 printf("\t<nostatement>");
32 show_expression(stmt
->expression
);
38 show_symbol_list(stmt
->syms
, "\n\t");
41 show_statement_list(stmt
->stmts
, ";\n");
46 show_expression(stmt
->expression
);
50 show_expression(stmt
->if_conditional
);
52 show_statement(stmt
->if_true
);
55 show_statement(stmt
->if_false
);
60 show_expression(stmt
->switch_expression
);
62 show_statement(stmt
->switch_statement
);
66 if (!stmt
->case_expression
)
70 show_expression(stmt
->case_expression
);
73 show_expression(stmt
->case_to
);
77 show_statement(stmt
->case_statement
);
89 static void show_one_statement(struct statement
*stmt
, void *sep
, int flags
)
92 if (!(flags
& ITERATE_LAST
))
93 printf("%s", (const char *)sep
);
96 void show_statement_list(struct statement_list
*stmt
, const char *sep
)
98 statement_iterate(stmt
, show_one_statement
, (void *)sep
);
101 void show_expression(struct expression
*expr
)
107 switch (expr
->type
) {
109 show_expression(expr
->left
);
110 printf(" %s ", show_special(expr
->op
));
111 show_expression(expr
->right
);
114 printf("%s<", show_special(expr
->op
));
115 show_expression(expr
->unop
);
119 show_expression(expr
->unop
);
120 printf(" %s ", show_special(expr
->op
));
123 printf("%s", show_token(expr
->token
));
127 printf("<nosymbol>");
130 printf("<%s:", show_token(expr
->symbol
->token
));
131 show_type(expr
->symbol
);
135 show_expression(expr
->deref
);
136 printf("%s", show_special(expr
->op
));
137 printf("%s", show_token(expr
->member
));
141 show_type(expr
->cast_type
);
143 show_expression(expr
->cast_expression
);
151 static struct expression
*alloc_expression(struct token
*token
, int type
)
153 struct expression
*expr
= __alloc_expression(0);
159 struct statement
*alloc_statement(struct token
* token
, int type
)
161 struct statement
*stmt
= __alloc_statement(0);
166 static int match_oplist(int op
, ...)
172 int nextop
= va_arg(args
, int);
180 int lookup_type(struct token
*token
)
182 if (token
->type
== TOKEN_IDENT
)
183 return lookup_symbol(token
->ident
, NS_TYPEDEF
) != NULL
;
187 static struct token
*comma_expression(struct token
*, struct expression
**);
188 static struct token
*compound_statement(struct token
*, struct statement
*);
189 static struct token
*initializer(struct token
*token
, struct ctype
*type
);
191 static struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
193 token
= expect(token
, '(', where
);
194 if (match_op(token
, '{')) {
195 struct expression
*e
= alloc_expression(token
, EXPR_STATEMENT
);
196 struct statement
*stmt
= alloc_statement(token
, STMT_COMPOUND
);
199 start_symbol_scope();
200 token
= compound_statement(token
->next
, stmt
);
202 token
= expect(token
, '}', "at end of statement expression");
204 token
= parse_expression(token
, expr
);
205 return expect(token
, ')', where
);
208 static struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
210 struct expression
*expr
= NULL
;
212 switch (token
->type
) {
216 expr
= alloc_expression(token
, EXPR_CONSTANT
);
221 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
);
223 warn(token
, "undefined identifier '%s'", token
->ident
->name
);
224 expr
= alloc_expression(token
, EXPR_SYMBOL
);
231 expr
= alloc_expression(token
, EXPR_CONSTANT
);
234 } while (token
->type
== TOKEN_STRING
);
238 if (token
->special
== '(') {
239 expr
= alloc_expression(token
, EXPR_PREOP
);
241 token
= parens_expression(token
, &expr
->unop
, "in expression");
251 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
)
253 struct expression
*expr
= NULL
;
255 token
= primary_expression(token
, &expr
);
256 while (expr
&& token
->type
== TOKEN_SPECIAL
) {
257 switch (token
->special
) {
258 case '[': { /* Array dereference */
259 struct expression
*array_expr
= alloc_expression(token
, EXPR_BINOP
);
260 array_expr
->op
= '[';
261 array_expr
->left
= expr
;
262 token
= parse_expression(token
->next
, &array_expr
->right
);
263 token
= expect(token
, ']', "at end of array dereference");
267 case SPECIAL_INCREMENT
: /* Post-increment */
268 case SPECIAL_DECREMENT
: { /* Post-decrement */
269 struct expression
*post
= alloc_expression(token
, EXPR_POSTOP
);
270 post
->op
= token
->special
;
276 case '.': /* Structure member dereference */
277 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
278 struct expression
*deref
= alloc_expression(token
, EXPR_DEREF
);
279 deref
->op
= token
->special
;
282 if (token
->type
!= TOKEN_IDENT
) {
283 warn(token
, "Expected member name");
286 deref
->member
= token
;
292 case '(': { /* Function call */
293 struct expression
*call
= alloc_expression(token
, EXPR_BINOP
);
296 token
= comma_expression(token
->next
, &call
->right
);
297 token
= expect(token
, ')', "in function call");
311 static struct token
*typename(struct token
*, struct symbol
**);
313 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
314 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
316 if (token
->type
== TOKEN_IDENT
&&
317 (token
->ident
== &sizeof_ident
||
318 token
->ident
== &__alignof___ident
)) {
319 struct expression
*sizeof_ex
= alloc_expression(token
, EXPR_SIZEOF
);
321 tree
= &sizeof_ex
->unop
;
323 if (!match_op(token
, '(') || !lookup_type(token
->next
))
324 return unary_expression(token
, &sizeof_ex
->cast_expression
);
325 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
326 return expect(token
, ')', "at end of sizeof type-name");
329 if (token
->type
== TOKEN_SPECIAL
) {
330 if (match_oplist(token
->special
,
331 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
332 '&', '*', '+', '-', '~', '!', 0)) {
333 struct expression
*unary
= alloc_expression(token
, EXPR_PREOP
);
334 unary
->op
= token
->special
;
336 return cast_expression(token
->next
, &unary
->unop
);
340 return postfix_expression(token
, tree
);
344 * Ambiguity: a '(' can be either a cast-expression or
345 * a primary-expression depending on whether it is followed
348 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
350 if (match_op(token
, '(')) {
351 struct token
*next
= token
->next
;
352 if (lookup_type(next
)) {
353 struct expression
*cast
= alloc_expression(next
, EXPR_CAST
);
355 token
= typename(next
, &cast
->cast_type
);
356 token
= expect(token
, ')', "at end of cast operator");
357 if (match_op(token
, '{'))
358 return initializer(token
, &cast
->cast_type
->ctype
);
359 token
= cast_expression(token
, &cast
->cast_expression
);
364 return unary_expression(token
, tree
);
367 /* Generic left-to-right binop parsing */
368 static struct token
*lr_binop_expression(struct token
*token
, struct expression
**tree
,
369 struct token
*(*inner
)(struct token
*, struct expression
**), ...)
371 struct expression
*left
= NULL
;
372 struct token
* next
= inner(token
, &left
);
375 while (next
->type
== TOKEN_SPECIAL
) {
376 struct expression
*top
, *right
= NULL
;
377 int op
= next
->special
;
380 va_start(args
, inner
);
382 int nextop
= va_arg(args
, int);
389 top
= alloc_expression(next
, EXPR_BINOP
);
390 next
= inner(next
->next
, &right
);
392 warn(next
, "No right hand side of '%s'-expression", show_special(op
));
406 static struct token
*multiplicative_expression(struct token
*token
, struct expression
**tree
)
408 return lr_binop_expression(token
, tree
, cast_expression
, '*', '/', '%', 0);
411 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
413 return lr_binop_expression(token
, tree
, multiplicative_expression
, '+', '-', 0);
416 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
418 return lr_binop_expression(token
, tree
, additive_expression
, SPECIAL_LEFTSHIFT
, SPECIAL_RIGHTSHIFT
, 0);
421 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
423 return lr_binop_expression(token
, tree
, shift_expression
, '<', '>', SPECIAL_LTE
, SPECIAL_GTE
, 0);
426 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
428 return lr_binop_expression(token
, tree
, relational_expression
, SPECIAL_EQUAL
, SPECIAL_NOTEQUAL
, 0);
431 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
433 return lr_binop_expression(token
, tree
, equality_expression
, '&', 0);
436 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
438 return lr_binop_expression(token
, tree
, bitwise_and_expression
, '^', 0);
441 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
443 return lr_binop_expression(token
, tree
, bitwise_xor_expression
, '|', 0);
446 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
448 return lr_binop_expression(token
, tree
, bitwise_or_expression
, SPECIAL_LOGICAL_AND
, 0);
451 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
453 return lr_binop_expression(token
, tree
, logical_and_expression
, SPECIAL_LOGICAL_OR
, 0);
456 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
458 token
= logical_or_expression(token
, tree
);
459 if (match_op(token
, '?')) {
460 struct expression
*expr
= alloc_expression(token
, EXPR_CONDITIONAL
);
461 expr
->op
= token
->special
;
464 token
= parse_expression(token
->next
, &expr
->cond_true
);
465 token
= expect(token
, ':', "in conditional expression");
466 token
= conditional_expression(token
, &expr
->cond_false
);
471 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
473 token
= conditional_expression(token
, tree
);
474 if (token
->type
== TOKEN_SPECIAL
) {
475 static const int assignments
[] = {
476 '=', SPECIAL_ADD_ASSIGN
, SPECIAL_MINUS_ASSIGN
,
477 SPECIAL_TIMES_ASSIGN
, SPECIAL_DIV_ASSIGN
,
478 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
479 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
480 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
481 int i
, op
= token
->special
;
482 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
483 if (assignments
[i
] == op
) {
484 struct expression
* expr
= alloc_expression(token
, EXPR_BINOP
);
488 return assignment_expression(token
->next
, &expr
->right
);
494 struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
496 return lr_binop_expression(token
, tree
, assignment_expression
, ',', 0);
499 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
501 return comma_expression(token
,tree
);
504 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
506 static void force_default_type(struct ctype
*type
)
508 if (!type
->base_type
)
509 type
->base_type
= &int_type
;
512 static struct symbol
*indirect(struct symbol
*base
, int type
)
514 struct symbol
*sym
= alloc_symbol(NULL
, type
);
516 sym
->ctype
.base_type
= base
;
517 sym
->ctype
.modifiers
= 0;
518 force_default_type(&sym
->ctype
);
522 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
524 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
526 sym
= alloc_symbol(token
, type
);
527 bind_symbol(sym
, token
->ident
, ns
);
532 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
533 struct token
*token
, struct ctype
*ctype
,
534 struct token
*(*parse
)(struct token
*, struct symbol
*))
538 if (token
->type
== TOKEN_IDENT
) {
539 sym
= lookup_or_create_symbol(ns
, type
, token
);
541 ctype
->base_type
= sym
;
542 if (match_op(token
, '{')) {
543 token
= parse(token
->next
, sym
);
544 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
549 // private struct/union/enum type
550 if (!match_op(token
, '{')) {
551 warn(token
, "expected declaration");
552 ctype
->base_type
= &bad_type
;
556 sym
= alloc_symbol(token
, type
);
557 token
= parse(token
->next
, sym
);
558 ctype
->base_type
= sym
->ctype
.base_type
;
559 ctype
->modifiers
= sym
->ctype
.modifiers
;
560 return expect(token
, '}', "at end of specifier");
563 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
565 return struct_declaration_list(token
, &sym
->symbol_list
);
568 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
570 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
574 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*sym
)
576 return skip_to(token
, '}');
579 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
581 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
584 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
587 struct expression
*expr
;
589 if (match_op(token
, '(') && lookup_type(token
->next
)) {
590 token
= typename(token
->next
, &sym
);
592 return expect(token
, ')', "after typeof");
594 token
= parse_expression(token
, &expr
);
596 ctype
->base_type
= &bad_type
;
600 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
604 token
= expect(token
, '(', "after attribute");
605 token
= expect(token
, '(', "after attribute");
608 if (eof_token(token
))
610 if (match_op(token
, ';'))
612 if (match_op(token
, ')')) {
617 if (match_op(token
, '('))
622 token
= expect(token
, ')', "after attribute");
623 token
= expect(token
, ')', "after attribute");
627 #define MOD_SPECIALBITS (SYM_STRUCTOF | SYM_UNIONOF | SYM_ENUMOF | SYM_ATTRIBUTE | SYM_TYPEOF)
629 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
)
633 while ( (token
= next
) != NULL
) {
634 struct ctype thistype
;
636 struct symbol
*s
, *type
;
639 if (token
->type
!= TOKEN_IDENT
)
641 ident
= token
->ident
;
643 s
= lookup_symbol(ident
, NS_TYPEDEF
);
647 if (thistype
.modifiers
& MOD_SPECIALBITS
) {
648 unsigned long mod
= thistype
.modifiers
;
649 thistype
.modifiers
= mod
& ~MOD_SPECIALBITS
;
650 if (mod
& SYM_STRUCTOF
)
651 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
652 else if (mod
& SYM_UNIONOF
)
653 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
654 else if (mod
& SYM_ENUMOF
)
655 next
= enum_specifier(next
, &thistype
);
656 else if (mod
& SYM_ATTRIBUTE
)
657 next
= attribute_specifier(next
, &thistype
);
658 else if (mod
& SYM_TYPEOF
)
659 next
= typeof_specifier(next
, &thistype
);
662 type
= s
->ctype
.base_type
;
664 if (type
!= ctype
->base_type
) {
665 if (ctype
->base_type
) {
666 warn(token
, "Strange mix of types");
669 ctype
->base_type
= type
;
672 if (thistype
.modifiers
) {
673 unsigned long old
= ctype
->modifiers
;
674 unsigned long extra
= 0, dup
;
676 if (thistype
.modifiers
& old
& SYM_LONG
) {
677 extra
= SYM_LONGLONG
| SYM_LONG
;
678 thistype
.modifiers
&= ~SYM_LONG
;
681 dup
= (thistype
.modifiers
& old
) | (extra
& old
) | (extra
& thistype
.modifiers
);
683 warn(token
, "Just how %s do you want this type to be?",
684 modifier_string(dup
));
685 ctype
->modifiers
= old
| thistype
.modifiers
| extra
;
691 static int constant_value(struct expression
*expr
)
696 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
**tree
)
698 struct expression
*expr
;
699 struct symbol
*sym
= *tree
;
700 token
= parse_expression(token
, &expr
);
701 sym
->size
= constant_value(expr
);
705 static struct token
*abstract_function_declarator(struct token
*, struct symbol_list
**);
706 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct token
**p
);
708 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct token
**p
)
710 if (p
&& token
->type
== TOKEN_IDENT
) {
711 if (lookup_symbol(token
->ident
, NS_TYPEDEF
)) {
712 warn(token
, "unexpected type/qualifier");
720 if (match_ident(token
, &__attribute___ident
) || match_ident(token
, &__attribute_ident
)) {
721 struct ctype ctype
= { 0, };
722 token
= attribute_specifier(token
->next
, &ctype
);
725 if (token
->type
!= TOKEN_SPECIAL
)
729 * This can be either a parameter list or a grouping.
730 * For the direct (non-abstract) case, we know if must be
731 * a paramter list if we already saw the identifier.
732 * For the abstract case, we know if must be a parameter
733 * list if it is empty or starts with a type.
735 if (token
->special
== '(') {
736 struct token
*next
= token
->next
;
737 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
740 token
= declarator(next
, tree
, p
);
741 token
= expect(token
, ')', "in nested declarator");
745 *tree
= indirect(*tree
, SYM_FN
);
746 token
= abstract_function_declarator(next
, &(*tree
)->arguments
);
747 token
= expect(token
, ')', "in function declarator");
750 if (token
->special
== '[') {
751 *tree
= indirect(*tree
, SYM_ARRAY
);
752 token
= abstract_array_declarator(token
->next
, tree
);
753 token
= expect(token
, ']', "in abstract_array_declarator");
756 if (token
->special
== ':') {
757 struct expression
*expr
;
758 *tree
= indirect(*tree
, SYM_BITFIELD
);
759 token
= conditional_expression(token
->next
, &expr
);
771 static struct token
*pointer(struct token
*token
, struct symbol
**tree
, struct ctype
*ctype
)
773 force_default_type(ctype
);
774 (*tree
)->ctype
= *ctype
;
775 while (match_op(token
,'*')) {
777 sym
= alloc_symbol(token
, SYM_PTR
);
778 sym
->ctype
.base_type
= *tree
;
780 token
= declaration_specifiers(token
->next
, &sym
->ctype
);
785 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct token
**p
)
787 token
= pointer(token
, tree
, &(*tree
)->ctype
);
788 return direct_declarator(token
, tree
, p
);
791 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
794 struct ctype ctype
= {0, };
796 token
= declaration_specifiers(token
, &ctype
);
798 struct symbol
*spec
= alloc_symbol(token
, SYM_TYPE
);
799 struct ctype
*base_type
= &ctype
;
800 struct symbol
*declaration
= spec
;
801 struct token
*ident
= NULL
;
802 token
= pointer(token
, &declaration
, base_type
);
803 token
= direct_declarator(token
, &declaration
, &ident
);
804 if (match_op(token
, ':')) {
805 struct expression
*expr
;
806 token
= parse_expression(token
->next
, &expr
);
808 add_symbol(list
, declaration
);
809 if (!match_op(token
, ','))
813 if (!match_op(token
, ';'))
820 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
822 struct token
*ident
= NULL
;
823 struct ctype ctype
= { 0, }, *base_type
;
825 token
= declaration_specifiers(token
, &ctype
);
826 *tree
= alloc_symbol(token
, SYM_TYPE
);
828 token
= pointer(token
, tree
, base_type
);
829 token
= direct_declarator(token
, tree
, &ident
);
833 static struct token
*typename(struct token
*token
, struct symbol
**p
)
835 struct ctype ctype
= { 0, };
836 struct symbol
*sym
= alloc_symbol(token
, SYM_TYPE
);
838 token
= declaration_specifiers(token
, &ctype
);
839 return declarator(token
, &sym
, NULL
);
842 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
844 token
= parse_expression(token
, tree
);
845 return expect(token
, ';', "at end of statement");
848 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
850 struct expression
*expr
;
852 /* Allow empty operands */
853 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
856 token
= primary_expression(token
->next
, &expr
);
857 token
= parens_expression(token
, &expr
, "in asm parameter");
858 } while (match_op(token
, ','));
862 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
864 struct expression
*expr
;
867 token
= primary_expression(token
->next
, &expr
);
868 } while (match_op(token
, ','));
872 struct token
*statement(struct token
*token
, struct statement
**tree
)
874 struct statement
*stmt
= alloc_statement(token
, STMT_NONE
);
877 if (token
->type
== TOKEN_IDENT
) {
878 if (token
->ident
== &if_ident
) {
879 stmt
->type
= STMT_IF
;
880 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
881 token
= statement(token
, &stmt
->if_true
);
882 if (token
->type
!= TOKEN_IDENT
)
884 if (token
->ident
!= &else_ident
)
886 return statement(token
->next
, &stmt
->if_false
);
888 if (token
->ident
== &return_ident
) {
889 stmt
->type
= STMT_RETURN
;
890 return expression_statement(token
->next
, &stmt
->expression
);
892 if (token
->ident
== &break_ident
) {
893 stmt
->type
= STMT_BREAK
;
894 return expect(token
->next
, ';', "at end of statement");
896 if (token
->ident
== &continue_ident
) {
897 stmt
->type
= STMT_CONTINUE
;
898 return expect(token
->next
, ';', "at end of statement");
900 if (token
->ident
== &default_ident
) {
902 goto default_statement
;
904 if (token
->ident
== &case_ident
) {
905 token
= parse_expression(token
->next
, &stmt
->case_expression
);
906 if (match_op(token
, SPECIAL_ELLIPSIS
))
907 token
= parse_expression(token
->next
, &stmt
->case_to
);
909 stmt
->type
= STMT_CASE
;
910 token
= expect(token
, ':', "after default/case");
911 return statement(token
, &stmt
->case_statement
);
913 if (token
->ident
== &switch_ident
) {
914 stmt
->type
= STMT_SWITCH
;
915 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
916 return statement(token
, &stmt
->switch_statement
);
918 if (token
->ident
== &for_ident
) {
919 stmt
->type
= STMT_FOR
;
920 token
= expect(token
->next
, '(', "after 'for'");
921 token
= parse_expression(token
, &stmt
->e1
);
922 token
= expect(token
, ';', "in 'for'");
923 token
= parse_expression(token
, &stmt
->e2
);
924 token
= expect(token
, ';', "in 'for'");
925 token
= parse_expression(token
, &stmt
->e3
);
926 token
= expect(token
, ')', "in 'for'");
927 return statement(token
, &stmt
->iterate
);
929 if (token
->ident
== &while_ident
) {
930 stmt
->type
= STMT_WHILE
;
931 token
= parens_expression(token
->next
, &stmt
->e1
, "after 'while'");
932 return statement(token
, &stmt
->iterate
);
934 if (token
->ident
== &do_ident
) {
935 stmt
->type
= STMT_DO
;
936 token
= statement(token
->next
, &stmt
->iterate
);
937 if (token
->type
== TOKEN_IDENT
&& token
->ident
== &while_ident
)
940 warn(token
, "expected 'while' after 'do'");
941 token
= parens_expression(token
, &stmt
->e1
, "after 'do-while'");
942 return expect(token
, ';', "after statement");
944 if (token
->ident
== &goto_ident
) {
945 stmt
->type
= STMT_GOTO
;
947 if (token
->type
== TOKEN_IDENT
) {
948 stmt
->goto_label
= token
;
951 warn(token
, "invalid label");
952 return expect(token
, ';', "at end of statement");
954 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm___ident
|| token
->ident
== &__asm_ident
) {
955 struct expression
*expr
;
956 stmt
->type
= STMT_ASM
;
958 if (token
->type
== TOKEN_IDENT
) {
959 if (token
->ident
== &__volatile___ident
|| token
->ident
== &volatile_ident
)
962 token
= expect(token
, '(', "after asm");
963 token
= parse_expression(token
->next
, &expr
);
964 if (match_op(token
, ':'))
965 token
= parse_asm_operands(token
, stmt
);
966 if (match_op(token
, ':'))
967 token
= parse_asm_operands(token
, stmt
);
968 if (match_op(token
, ':'))
969 token
= parse_asm_clobbers(token
, stmt
);
970 token
= expect(token
, ')', "after asm");
971 return expect(token
, ';', "at end of asm-statement");
973 if (match_op(token
->next
, ':')) {
974 stmt
->type
= STMT_LABEL
;
975 stmt
->label_identifier
= token
;
976 return statement(token
->next
->next
, &stmt
->label_statement
);
980 if (match_op(token
, '{')) {
981 stmt
->type
= STMT_COMPOUND
;
982 start_symbol_scope();
983 token
= compound_statement(token
->next
, stmt
);
986 return expect(token
, '}', "at end of compound statement");
989 stmt
->type
= STMT_EXPRESSION
;
990 return expression_statement(token
, &stmt
->expression
);
993 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
996 struct statement
* stmt
;
997 if (eof_token(token
))
999 if (match_op(token
, '}'))
1001 token
= statement(token
, &stmt
);
1002 add_statement(list
, stmt
);
1007 static struct token
*parameter_type_list(struct token
*token
, struct symbol_list
**list
)
1010 struct symbol
*sym
= alloc_symbol(token
, SYM_TYPE
);
1012 token
= parameter_declaration(token
, &sym
);
1013 add_symbol(list
, sym
);
1014 if (!match_op(token
, ','))
1016 token
= token
->next
;
1018 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1019 /* FIXME: mark the function */
1020 token
= token
->next
;
1027 static struct token
*abstract_function_declarator(struct token
*token
, struct symbol_list
**list
)
1029 return parameter_type_list(token
, list
);
1032 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
1034 static struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1036 while (!eof_token(token
)) {
1037 if (!lookup_type(token
))
1039 token
= external_declaration(token
, &stmt
->syms
);
1041 token
= statement_list(token
, &stmt
->stmts
);
1045 static struct token
*initializer_list(struct token
*token
, struct ctype
*type
)
1048 token
= initializer(token
, type
);
1049 if (!match_op(token
, ','))
1051 token
= token
->next
;
1056 static struct token
*initializer(struct token
*token
, struct ctype
*type
)
1058 struct expression
*expr
;
1059 if (match_op(token
, '{')) {
1060 token
= initializer_list(token
->next
, type
);
1061 return expect(token
, '}', "at end of initializer");
1063 return assignment_expression(token
, &expr
);
1066 static void declare_argument(struct symbol
*sym
, void *data
, int flags
)
1069 warn(sym
->token
, "no identifier for function argument");
1072 bind_symbol(sym
, sym
->ident
->ident
, NS_SYMBOL
);
1075 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1077 struct token
*ident
= NULL
;
1078 struct symbol
*decl
;
1079 struct ctype ctype
= { 0, };
1081 /* Parse declaration-specifiers, if any */
1082 token
= declaration_specifiers(token
, &ctype
);
1084 decl
= alloc_symbol(token
, SYM_TYPE
);
1085 token
= pointer(token
, &decl
, &ctype
);
1086 token
= declarator(token
, &decl
, &ident
);
1088 /* Just a type declaration? */
1090 return expect(token
, ';', "end of type declaration");
1092 /* type define declaration? */
1093 if (ctype
.modifiers
& SYM_TYPEDEF
) {
1094 ctype
.modifiers
&= ~SYM_TYPEDEF
;
1095 bind_symbol(indirect(decl
, SYM_TYPEDEF
), ident
->ident
, NS_TYPEDEF
);
1096 return expect(token
, ';', "end of typedef");
1099 decl
->ident
= ident
;
1100 add_symbol(list
, decl
);
1101 bind_symbol(decl
, ident
->ident
, NS_SYMBOL
);
1103 if (decl
->type
== SYM_FN
&& match_op(token
, '{')) {
1104 decl
->stmt
= alloc_statement(token
, STMT_COMPOUND
);
1105 start_symbol_scope();
1106 symbol_iterate(decl
->arguments
, declare_argument
, NULL
);
1107 token
= compound_statement(token
->next
, decl
->stmt
);
1109 return expect(token
, '}', "at end of function");
1113 if (match_op(token
, '='))
1114 token
= initializer(token
->next
, &decl
->ctype
);
1115 if (!match_op(token
, ','))
1119 decl
= alloc_symbol(token
, SYM_TYPE
);
1120 token
= pointer(token
, &decl
, &ctype
);
1121 token
= declarator(token
->next
, &decl
, &ident
);
1123 warn(token
, "expected identifier name in type definition");
1127 add_symbol(list
, decl
);
1128 bind_symbol(decl
, ident
->ident
, NS_SYMBOL
);
1130 return expect(token
, ';', "at end of declaration");
1133 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1135 while (!eof_token(token
))
1136 token
= external_declaration(token
, list
);