2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
24 #include "expression.h"
26 static struct token
*statement(struct token
*token
, struct statement
**tree
);
27 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
29 struct statement
*alloc_statement(struct position pos
, int type
)
31 struct statement
*stmt
= __alloc_statement(0);
37 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
39 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
41 struct symbol
*sym
= alloc_symbol(pos
, type
);
43 sym
->ctype
.base_type
= ctype
->base_type
;
44 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
46 ctype
->base_type
= sym
;
47 ctype
->modifiers
&= MOD_STORAGE
;
51 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
53 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
55 sym
= alloc_symbol(token
->pos
, type
);
56 sym
->ident
= token
->ident
;
57 bind_symbol(sym
, token
->ident
, ns
);
63 * NOTE! NS_LABEL is not just a different namespace,
64 * it also ends up using function scope instead of the
65 * regular symbol scope.
67 struct symbol
*label_symbol(struct token
*token
)
69 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
72 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
73 struct token
*token
, struct ctype
*ctype
,
74 struct token
*(*parse
)(struct token
*, struct symbol
*))
79 if (token_type(token
) == TOKEN_IDENT
) {
80 sym
= lookup_or_create_symbol(ns
, type
, token
);
82 ctype
->base_type
= sym
;
83 if (match_op(token
, '{')) {
84 token
= parse(token
->next
, sym
);
85 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
90 // private struct/union/enum type
91 if (!match_op(token
, '{')) {
92 warn(token
->pos
, "expected declaration");
93 ctype
->base_type
= &bad_type
;
97 sym
= alloc_symbol(token
->pos
, type
);
98 token
= parse(token
->next
, sym
);
99 ctype
->base_type
= sym
;
100 return expect(token
, '}', "at end of specifier");
103 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
105 return struct_declaration_list(token
, &sym
->symbol_list
);
108 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
110 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
113 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
116 while (token_type(token
) == TOKEN_IDENT
) {
117 struct token
*next
= token
->next
;
120 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
121 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
122 sym
->ctype
.base_type
= parent
;
124 if (match_op(next
, '=')) {
125 struct expression
*expr
;
126 next
= constant_expression(next
->next
, &expr
);
127 nextval
= get_expression_value(expr
);
129 sym
->value
= nextval
;
132 if (!match_op(token
, ','))
135 nextval
= nextval
+ 1;
140 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
142 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
145 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
149 if (!match_op(token
, '(')) {
150 warn(token
->pos
, "expected '(' after typeof");
153 if (lookup_type(token
->next
)) {
154 token
= typename(token
->next
, &sym
);
157 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
158 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
160 ctype
->modifiers
= 0;
161 ctype
->base_type
= typeof_sym
;
163 return expect(token
, ')', "after typeof");
166 static void handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
168 if (match_string_ident(attribute
, "packed")) {
169 ctype
->alignment
= 1;
172 if (match_string_ident(attribute
, "aligned")) {
173 ctype
->alignment
= get_expression_value(expr
);
176 if (match_string_ident(attribute
, "nocast")) {
177 ctype
->modifiers
|= MOD_NOCAST
;
180 if (match_string_ident(attribute
, "noderef")) {
181 ctype
->modifiers
|= MOD_NODEREF
;
184 if (match_string_ident(attribute
, "address_space")) {
185 ctype
->as
= get_expression_value(expr
);
188 if (match_string_ident(attribute
, "context")) {
189 if (expr
->type
== EXPR_COMMA
) {
190 int mask
= get_expression_value(expr
->left
);
191 int value
= get_expression_value(expr
->right
);
193 warn(expr
->pos
, "nonsense attribute types");
196 ctype
->contextmask
|= mask
;
197 ctype
->context
|= value
;
204 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
206 ctype
->modifiers
= 0;
207 token
= expect(token
, '(', "after attribute");
208 token
= expect(token
, '(', "after attribute");
211 struct ident
*attribute_name
;
212 struct expression
*attribute_expr
;
214 if (eof_token(token
))
216 if (match_op(token
, ';'))
218 if (token_type(token
) != TOKEN_IDENT
)
220 attribute_name
= token
->ident
;
222 attribute_expr
= NULL
;
223 if (match_op(token
, '('))
224 token
= parens_expression(token
, &attribute_expr
, "in attribute");
225 handle_attribute(ctype
, attribute_name
, attribute_expr
);
226 if (!match_op(token
, ','))
231 token
= expect(token
, ')', "after attribute");
232 token
= expect(token
, ')', "after attribute");
236 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
237 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
239 struct symbol
* ctype_integer(unsigned int spec
)
241 static struct symbol
*const integer_ctypes
[][2] = {
242 { &llong_ctype
, &ullong_ctype
},
243 { &long_ctype
, &ulong_ctype
},
244 { &short_ctype
, &ushort_ctype
},
245 { &char_ctype
, &uchar_ctype
},
246 { &int_ctype
, &uint_ctype
},
248 struct symbol
*const (*ctype
)[2];
250 ctype
= integer_ctypes
;
251 if (!(spec
& MOD_LONGLONG
)) {
253 if (!(spec
& MOD_LONG
)) {
255 if (!(spec
& MOD_SHORT
)) {
257 if (!(spec
& MOD_CHAR
))
262 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
265 struct symbol
* ctype_fp(unsigned int spec
)
267 if (spec
& MOD_LONGLONG
)
268 return &ldouble_ctype
;
270 return &double_ctype
;
274 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
276 unsigned long mod
= thistype
->modifiers
;
279 unsigned long old
= ctype
->modifiers
;
280 unsigned long extra
= 0, dup
;
282 if (mod
& old
& MOD_LONG
) {
283 extra
= MOD_LONGLONG
| MOD_LONG
;
287 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
289 warn(pos
, "Just how %sdo you want this type to be?",
290 modifier_string(dup
));
291 ctype
->modifiers
= old
| mod
| extra
;
294 /* Context mask and value */
295 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
296 warn(pos
, "inconsistend attribute types");
297 thistype
->context
= 0;
298 thistype
->contextmask
= 0;
300 ctype
->context
|= thistype
->context
;
301 ctype
->contextmask
|= thistype
->contextmask
;
304 if (thistype
->alignment
& (thistype
->alignment
-1)) {
305 warn(pos
, "I don't like non-power-of-2 alignments");
306 thistype
->alignment
= 0;
308 if (thistype
->alignment
> ctype
->alignment
)
309 ctype
->alignment
= thistype
->alignment
;
312 ctype
->as
= thistype
->as
;
316 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
320 while ( (token
= next
) != NULL
) {
321 struct ctype thistype
;
323 struct symbol
*s
, *type
;
327 if (token_type(token
) != TOKEN_IDENT
)
329 ident
= token
->ident
;
331 s
= lookup_symbol(ident
, NS_TYPEDEF
);
335 mod
= thistype
.modifiers
;
336 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
338 if (mod
& MOD_SPECIALBITS
) {
339 if (mod
& MOD_STRUCTOF
)
340 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
341 else if (mod
& MOD_UNIONOF
)
342 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
343 else if (mod
& MOD_ENUMOF
)
344 next
= enum_specifier(next
, &thistype
);
345 else if (mod
& MOD_ATTRIBUTE
)
346 next
= attribute_specifier(next
, &thistype
);
347 else if (mod
& MOD_TYPEOF
)
348 next
= typeof_specifier(next
, &thistype
);
349 mod
= thistype
.modifiers
;
351 type
= thistype
.base_type
;
355 if (type
!= ctype
->base_type
) {
356 if (ctype
->base_type
) {
357 warn(token
->pos
, "Strange mix of types");
360 ctype
->base_type
= type
;
364 apply_ctype(token
->pos
, &thistype
, ctype
);
367 /* Turn the "virtual types" into real types with real sizes etc */
368 if (!ctype
->base_type
&& (ctype
->modifiers
& MOD_SPECIFIER
))
369 ctype
->base_type
= &int_type
;
371 if (ctype
->base_type
== &int_type
) {
372 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
373 ctype
->modifiers
&= ~MOD_SPECIFIER
;
376 if (ctype
->base_type
== &fp_type
) {
377 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
378 ctype
->modifiers
&= ~MOD_SPECIFIER
;
384 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
386 struct expression
*expr
= NULL
;
388 token
= parse_expression(token
, &expr
);
390 sym
->array_size
= get_expression_value(expr
);
392 sym
->array_size
= -1;
396 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
397 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
399 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
401 struct ctype
*ctype
= &(*tree
)->ctype
;
403 if (p
&& token_type(token
) == TOKEN_IDENT
) {
409 if (match_ident(token
, &__attribute___ident
) || match_ident(token
, &__attribute_ident
)) {
410 struct ctype thistype
= { 0, };
411 token
= attribute_specifier(token
->next
, &thistype
);
412 apply_ctype(token
->pos
, &thistype
, ctype
);
415 if (token_type(token
) != TOKEN_SPECIAL
)
419 * This can be either a parameter list or a grouping.
420 * For the direct (non-abstract) case, we know if must be
421 * a paramter list if we already saw the identifier.
422 * For the abstract case, we know if must be a parameter
423 * list if it is empty or starts with a type.
425 if (token
->special
== '(') {
427 struct token
*next
= token
->next
;
428 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
431 struct symbol
*base_type
= ctype
->base_type
;
432 token
= declarator(next
, tree
, p
);
433 token
= expect(token
, ')', "in nested declarator");
434 while (ctype
->base_type
!= base_type
)
435 ctype
= &ctype
->base_type
->ctype
;
440 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
441 token
= parameter_type_list(next
, sym
);
442 token
= expect(token
, ')', "in function declarator");
445 if (token
->special
== '[') {
446 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
447 token
= abstract_array_declarator(token
->next
, array
);
448 token
= expect(token
, ']', "in abstract_array_declarator");
449 ctype
= &array
->ctype
;
452 if (token
->special
== ':') {
453 struct symbol
*bitfield
;
454 struct expression
*expr
;
455 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
456 token
= conditional_expression(token
->next
, &expr
);
457 bitfield
->fieldwidth
= get_expression_value(expr
);
468 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
470 unsigned long modifiers
;
471 struct symbol
*base_type
;
473 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
474 base_type
= ctype
->base_type
;
475 ctype
->modifiers
= modifiers
;
477 while (match_op(token
,'*')) {
478 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
479 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
480 ptr
->ctype
.as
= ctype
->as
;
481 ptr
->ctype
.context
= ctype
->context
;
482 ptr
->ctype
.contextmask
= ctype
->contextmask
;
483 ptr
->ctype
.base_type
= base_type
;
486 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
487 ctype
->base_type
= base_type
;
490 ctype
->contextmask
= 0;
492 token
= declaration_specifiers(token
->next
, ctype
, 1);
497 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
499 token
= pointer(token
, &(*tree
)->ctype
);
500 return direct_declarator(token
, tree
, p
);
503 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
505 while (!match_op(token
, '}')) {
506 struct ctype ctype
= {0, };
508 token
= declaration_specifiers(token
, &ctype
, 0);
510 struct ident
*ident
= NULL
;
511 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
513 token
= pointer(token
, &decl
->ctype
);
514 token
= direct_declarator(token
, &decl
, &ident
);
515 if (match_op(token
, ':')) {
516 struct expression
*expr
;
517 token
= parse_expression(token
->next
, &expr
);
519 add_symbol(list
, decl
);
520 if (!match_op(token
, ','))
524 if (!match_op(token
, ';'))
531 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
533 struct ident
*ident
= NULL
;
535 struct ctype ctype
= { 0, };
537 token
= declaration_specifiers(token
, &ctype
, 0);
538 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
541 token
= pointer(token
, &sym
->ctype
);
542 token
= direct_declarator(token
, tree
, &ident
);
546 struct token
*typename(struct token
*token
, struct symbol
**p
)
548 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
550 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
551 return declarator(token
, &sym
, NULL
);
554 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
556 token
= parse_expression(token
, tree
);
557 return expect(token
, ';', "at end of statement");
560 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
562 struct expression
*expr
;
564 /* Allow empty operands */
565 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
568 token
= primary_expression(token
->next
, &expr
);
569 token
= parens_expression(token
, &expr
, "in asm parameter");
570 } while (match_op(token
, ','));
574 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
576 struct expression
*expr
;
579 token
= primary_expression(token
->next
, &expr
);
580 } while (match_op(token
, ','));
584 /* Make a statement out of an expression */
585 static struct statement
*make_statement(struct expression
*expr
)
587 struct statement
*stmt
;
591 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
592 stmt
->expression
= expr
;
597 * All iterators have two symbols associated with them:
598 * the "continue" and "break" symbols, which are targets
599 * for continue and break statements respectively.
601 * They are in a special name-space, but they follow
602 * all the normal visibility rules, so nested iterators
603 * automatically work right.
605 static void start_iterator(struct statement
*stmt
)
607 struct symbol
*cont
, *brk
;
609 start_symbol_scope();
610 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
611 cont
->ident
= &continue_ident
;
612 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
613 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
614 brk
->ident
= &break_ident
;
615 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
617 stmt
->type
= STMT_ITERATOR
;
618 stmt
->iterator_break
= brk
;
619 stmt
->iterator_continue
= cont
;
622 static void end_iterator(struct statement
*stmt
)
628 * A "switch()" statement, like an iterator, has a
629 * the "break" symbol associated with it. It works
630 * exactly like the iterator break - it's the target
631 * for any break-statements in scope, and means that
632 * "break" handling doesn't even need to know whether
633 * it's breaking out of an iterator or a switch.
635 * In addition, the "case" symbol is a marker for the
636 * case/default statements to find the switch statement
637 * that they are associated with.
639 static void start_switch(struct statement
*stmt
)
641 struct symbol
*brk
, *switch_case
;
643 start_symbol_scope();
644 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
645 brk
->ident
= &break_ident
;
646 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
648 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
649 switch_case
->ident
= &case_ident
;
650 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
651 switch_case
->stmt
= stmt
;
653 stmt
->type
= STMT_SWITCH
;
654 stmt
->switch_break
= brk
;
655 stmt
->switch_case
= switch_case
;
658 static void end_switch(struct statement
*stmt
)
660 if (!stmt
->switch_case
->symbol_list
)
661 warn(stmt
->pos
, "switch with no cases");
665 static void add_case_statement(struct statement
*stmt
)
667 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
671 warn(stmt
->pos
, "not in switch scope");
674 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
675 add_symbol(&target
->symbol_list
, sym
);
677 stmt
->case_label
= sym
;
680 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
682 struct symbol_list
*syms
;
683 struct expression
*e1
, *e2
, *e3
;
684 struct statement
*iterator
;
686 start_iterator(stmt
);
687 token
= expect(token
->next
, '(', "after 'for'");
691 /* C99 variable declaration? */
692 if (lookup_type(token
)) {
693 token
= external_declaration(token
, &syms
);
695 token
= parse_expression(token
, &e1
);
696 token
= expect(token
, ';', "in 'for'");
698 token
= parse_expression(token
, &e2
);
699 token
= expect(token
, ';', "in 'for'");
700 token
= parse_expression(token
, &e3
);
701 token
= expect(token
, ')', "in 'for'");
702 token
= statement(token
, &iterator
);
704 stmt
->iterator_syms
= syms
;
705 stmt
->iterator_pre_statement
= make_statement(e1
);
706 stmt
->iterator_pre_condition
= e2
;
707 stmt
->iterator_post_statement
= make_statement(e3
);
708 stmt
->iterator_post_condition
= e2
;
709 stmt
->iterator_statement
= iterator
;
715 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
717 struct expression
*expr
;
718 struct statement
*iterator
;
720 start_iterator(stmt
);
721 token
= parens_expression(token
->next
, &expr
, "after 'while'");
722 token
= statement(token
, &iterator
);
724 stmt
->iterator_pre_condition
= expr
;
725 stmt
->iterator_post_condition
= expr
;
726 stmt
->iterator_statement
= iterator
;
732 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
734 struct expression
*expr
;
735 struct statement
*iterator
;
737 start_iterator(stmt
);
738 token
= statement(token
->next
, &iterator
);
739 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
742 warn(token
->pos
, "expected 'while' after 'do'");
743 token
= parens_expression(token
, &expr
, "after 'do-while'");
745 stmt
->iterator_post_condition
= expr
;
746 stmt
->iterator_statement
= iterator
;
749 return expect(token
, ';', "after statement");
752 static struct token
*statement(struct token
*token
, struct statement
**tree
)
754 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
757 if (token_type(token
) == TOKEN_IDENT
) {
758 if (token
->ident
== &if_ident
) {
759 stmt
->type
= STMT_IF
;
760 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
761 token
= statement(token
, &stmt
->if_true
);
762 if (token_type(token
) != TOKEN_IDENT
)
764 if (token
->ident
!= &else_ident
)
766 return statement(token
->next
, &stmt
->if_false
);
768 if (token
->ident
== &return_ident
) {
769 stmt
->type
= STMT_RETURN
;
770 return expression_statement(token
->next
, &stmt
->expression
);
772 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
773 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
774 stmt
->type
= STMT_GOTO
;
775 stmt
->goto_label
= target
;
777 warn(stmt
->pos
, "break/continue not in iterator scope");
778 return expect(token
->next
, ';', "at end of statement");
780 if (token
->ident
== &default_ident
) {
782 goto default_statement
;
784 if (token
->ident
== &case_ident
) {
785 token
= parse_expression(token
->next
, &stmt
->case_expression
);
786 if (match_op(token
, SPECIAL_ELLIPSIS
))
787 token
= parse_expression(token
->next
, &stmt
->case_to
);
789 stmt
->type
= STMT_CASE
;
790 token
= expect(token
, ':', "after default/case");
791 add_case_statement(stmt
);
792 return statement(token
, &stmt
->case_statement
);
794 if (token
->ident
== &switch_ident
) {
795 stmt
->type
= STMT_SWITCH
;
797 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
798 token
= statement(token
, &stmt
->switch_statement
);
802 if (token
->ident
== &for_ident
)
803 return parse_for_statement(token
, stmt
);
805 if (token
->ident
== &while_ident
)
806 return parse_while_statement(token
, stmt
);
808 if (token
->ident
== &do_ident
)
809 return parse_do_statement(token
, stmt
);
811 if (token
->ident
== &goto_ident
) {
812 stmt
->type
= STMT_GOTO
;
814 if (match_op(token
, '*')) {
815 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
816 } else if (token_type(token
) == TOKEN_IDENT
) {
817 stmt
->goto_label
= label_symbol(token
);
820 warn(token
->pos
, "Expected identifier or goto expression");
822 return expect(token
, ';', "at end of statement");
824 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm___ident
|| token
->ident
== &__asm_ident
) {
825 struct expression
*expr
;
826 stmt
->type
= STMT_ASM
;
828 if (token_type(token
) == TOKEN_IDENT
) {
829 if (token
->ident
== &__volatile___ident
|| token
->ident
== &volatile_ident
)
832 token
= expect(token
, '(', "after asm");
833 token
= parse_expression(token
->next
, &expr
);
834 if (match_op(token
, ':'))
835 token
= parse_asm_operands(token
, stmt
);
836 if (match_op(token
, ':'))
837 token
= parse_asm_operands(token
, stmt
);
838 if (match_op(token
, ':'))
839 token
= parse_asm_clobbers(token
, stmt
);
840 token
= expect(token
, ')', "after asm");
841 return expect(token
, ';', "at end of asm-statement");
843 if (match_op(token
->next
, ':')) {
844 stmt
->type
= STMT_LABEL
;
845 stmt
->label_identifier
= label_symbol(token
);
846 return statement(token
->next
->next
, &stmt
->label_statement
);
850 if (match_op(token
, '{')) {
851 stmt
->type
= STMT_COMPOUND
;
852 start_symbol_scope();
853 token
= compound_statement(token
->next
, stmt
);
856 return expect(token
, '}', "at end of compound statement");
859 stmt
->type
= STMT_EXPRESSION
;
860 return expression_statement(token
, &stmt
->expression
);
863 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
866 struct statement
* stmt
;
867 if (eof_token(token
))
869 if (match_op(token
, '}'))
871 token
= statement(token
, &stmt
);
872 add_statement(list
, stmt
);
877 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
879 struct symbol_list
**list
= &fn
->arguments
;
881 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
883 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
889 if (!lookup_type(token
)) {
890 warn(token
->pos
, "non-ANSI parameter list");
893 token
= parameter_declaration(token
, &sym
);
894 /* Special case: (void) */
895 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
897 add_symbol(list
, sym
);
898 if (!match_op(token
, ','))
906 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
908 while (!eof_token(token
)) {
909 if (!lookup_type(token
))
911 token
= external_declaration(token
, &stmt
->syms
);
913 token
= statement_list(token
, &stmt
->stmts
);
917 static struct expression
*identifier_expression(struct token
*token
)
919 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
920 expr
->expr_ident
= token
->ident
;
924 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
926 int idx_from
, idx_to
;
927 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
929 idx_from
= get_expression_value(from
);
932 idx_to
= get_expression_value(to
);
933 if (idx_to
< idx_from
|| idx_from
< 0)
934 warn(from
->pos
, "nonsense array initializer index range");
936 expr
->idx_from
= idx_from
;
937 expr
->idx_to
= idx_to
;
941 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
944 struct token
*next
= token
->next
;
945 struct expression
*expr
;
947 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
948 add_expression(list
, identifier_expression(next
));
949 token
= next
->next
->next
;
950 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
951 add_expression(list
, identifier_expression(token
));
953 } else if (match_op(token
, '[')) {
954 struct expression
*from
= NULL
, *to
= NULL
;
955 token
= constant_expression(token
->next
, &from
);
956 if (match_op(token
, SPECIAL_ELLIPSIS
))
957 token
= constant_expression(token
->next
, &to
);
958 add_expression(list
, index_expression(from
, to
));
959 token
= expect(token
, ']', "at end of initializer index");
960 token
= expect(token
, '=', "at end of initializer index");
964 token
= initializer(&expr
, token
);
967 add_expression(list
, expr
);
968 if (!match_op(token
, ','))
975 struct token
*initializer(struct expression
**tree
, struct token
*token
)
977 if (match_op(token
, '{')) {
978 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
980 token
= initializer_list(&expr
->expr_list
, token
->next
);
981 return expect(token
, '}', "at end of initializer");
983 return assignment_expression(token
, tree
);
986 static void declare_argument(struct symbol
*sym
, void *data
, int flags
)
988 struct symbol
*decl
= data
;
991 warn(decl
->pos
, "no identifier for function argument");
994 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
997 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
999 struct ident
*ident
= NULL
;
1000 struct symbol
*decl
;
1001 struct ctype ctype
= { 0, };
1002 struct symbol
*base_type
;
1005 /* Parse declaration-specifiers, if any */
1006 token
= declaration_specifiers(token
, &ctype
, 0);
1007 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1008 decl
->ctype
= ctype
;
1009 token
= pointer(token
, &decl
->ctype
);
1010 token
= declarator(token
, &decl
, &ident
);
1012 /* Just a type declaration? */
1014 return expect(token
, ';', "end of type declaration");
1016 decl
->ident
= ident
;
1018 /* type define declaration? */
1019 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1020 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1022 base_type
= decl
->ctype
.base_type
;
1023 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1024 if (match_op(token
, '{')) {
1025 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1026 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1027 warn(decl
->pos
, "function with external linkage has definition");
1029 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1030 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1031 base_type
->stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
1032 start_function_scope();
1033 symbol_iterate(base_type
->arguments
, declare_argument
, decl
);
1034 token
= compound_statement(token
->next
, base_type
->stmt
);
1035 end_function_scope();
1036 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1037 add_symbol(list
, decl
);
1038 check_declaration(decl
);
1039 return expect(token
, '}', "at end of function");
1041 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1042 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1046 if (!is_typedef
&& match_op(token
, '=')) {
1047 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1048 warn(decl
->pos
, "symbol with external linkage has initializer");
1049 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1051 token
= initializer(&decl
->initializer
, token
->next
);
1053 if (!is_typedef
&& !(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
)))
1054 add_symbol(list
, decl
);
1055 check_declaration(decl
);
1057 if (!match_op(token
, ','))
1061 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1062 decl
->ctype
= ctype
;
1063 token
= pointer(token
, &decl
->ctype
);
1064 token
= declarator(token
->next
, &decl
, &ident
);
1066 warn(token
->pos
, "expected identifier name in type definition");
1070 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1072 /* Function declarations are automatically extern unless specifically static */
1073 base_type
= decl
->ctype
.base_type
;
1074 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1075 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1076 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1079 return expect(token
, ';', "at end of declaration");
1082 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1084 while (!eof_token(token
))
1085 token
= external_declaration(token
, list
);
1086 // They aren't needed any more
1087 clear_token_alloc();