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 struct statement
*alloc_statement(struct position pos
, int type
)
28 struct statement
*stmt
= __alloc_statement(0);
34 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
36 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
38 struct symbol
*sym
= alloc_symbol(pos
, type
);
40 sym
->ctype
.base_type
= ctype
->base_type
;
41 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
43 ctype
->base_type
= sym
;
44 ctype
->modifiers
&= MOD_STORAGE
;
48 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
50 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
52 sym
= alloc_symbol(token
->pos
, type
);
53 sym
->ident
= token
->ident
;
54 bind_symbol(sym
, token
->ident
, ns
);
60 * NOTE! NS_LABEL is not just a different namespace,
61 * it also ends up using function scope instead of the
62 * regular symbol scope.
64 static struct symbol
*label_symbol(struct token
*token
)
66 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
69 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
70 struct token
*token
, struct ctype
*ctype
,
71 struct token
*(*parse
)(struct token
*, struct symbol
*))
76 if (token_type(token
) == TOKEN_IDENT
) {
77 sym
= lookup_or_create_symbol(ns
, type
, token
);
79 ctype
->base_type
= sym
;
80 if (match_op(token
, '{')) {
81 token
= parse(token
->next
, sym
);
82 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
87 // private struct/union/enum type
88 if (!match_op(token
, '{')) {
89 warn(token
->pos
, "expected declaration");
90 ctype
->base_type
= &bad_type
;
94 sym
= alloc_symbol(token
->pos
, type
);
95 token
= parse(token
->next
, sym
);
96 ctype
->base_type
= sym
;
97 return expect(token
, '}', "at end of specifier");
100 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
102 return struct_declaration_list(token
, &sym
->symbol_list
);
105 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
107 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
110 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
113 while (token_type(token
) == TOKEN_IDENT
) {
114 struct token
*next
= token
->next
;
117 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
118 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
119 sym
->ctype
.base_type
= parent
;
121 if (match_op(next
, '=')) {
122 struct expression
*expr
;
123 next
= constant_expression(next
->next
, &expr
);
124 nextval
= get_expression_value(expr
);
126 sym
->value
= nextval
;
129 if (!match_op(token
, ','))
132 nextval
= nextval
+ 1;
137 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
139 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
142 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
146 if (!match_op(token
, '(')) {
147 warn(token
->pos
, "expected '(' after typeof");
150 if (lookup_type(token
->next
)) {
151 token
= typename(token
->next
, &sym
);
154 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
155 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
157 ctype
->modifiers
= 0;
158 ctype
->base_type
= typeof_sym
;
160 return expect(token
, ')', "after typeof");
163 static void handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
165 if (match_string_ident(attribute
, "packed")) {
166 ctype
->alignment
= 1;
169 if (match_string_ident(attribute
, "aligned")) {
170 ctype
->alignment
= get_expression_value(expr
);
173 if (match_string_ident(attribute
, "nocast")) {
174 ctype
->modifiers
|= MOD_NOCAST
;
177 if (match_string_ident(attribute
, "noderef")) {
178 ctype
->modifiers
|= MOD_NODEREF
;
181 if (match_string_ident(attribute
, "address_space")) {
182 ctype
->as
= get_expression_value(expr
);
185 if (match_string_ident(attribute
, "context")) {
186 if (expr
->type
== EXPR_COMMA
) {
187 int mask
= get_expression_value(expr
->left
);
188 int value
= get_expression_value(expr
->right
);
190 warn(expr
->pos
, "nonsense attribute types");
193 ctype
->contextmask
|= mask
;
194 ctype
->context
|= value
;
201 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
203 ctype
->modifiers
= 0;
204 token
= expect(token
, '(', "after attribute");
205 token
= expect(token
, '(', "after attribute");
208 struct ident
*attribute_name
;
209 struct expression
*attribute_expr
;
211 if (eof_token(token
))
213 if (match_op(token
, ';'))
215 if (token_type(token
) != TOKEN_IDENT
)
217 attribute_name
= token
->ident
;
219 attribute_expr
= NULL
;
220 if (match_op(token
, '('))
221 token
= parens_expression(token
, &attribute_expr
, "in attribute");
222 handle_attribute(ctype
, attribute_name
, attribute_expr
);
223 if (!match_op(token
, ','))
228 token
= expect(token
, ')', "after attribute");
229 token
= expect(token
, ')', "after attribute");
233 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
234 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
236 struct symbol
* ctype_integer(unsigned int spec
)
238 static struct symbol
*const integer_ctypes
[][2] = {
239 { &llong_ctype
, &ullong_ctype
},
240 { &long_ctype
, &ulong_ctype
},
241 { &short_ctype
, &ushort_ctype
},
242 { &char_ctype
, &uchar_ctype
},
243 { &int_ctype
, &uint_ctype
},
245 struct symbol
*const (*ctype
)[2];
247 ctype
= integer_ctypes
;
248 if (!(spec
& MOD_LONGLONG
)) {
250 if (!(spec
& MOD_LONG
)) {
252 if (!(spec
& MOD_SHORT
)) {
254 if (!(spec
& MOD_CHAR
))
259 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
262 struct symbol
* ctype_fp(unsigned int spec
)
264 if (spec
& MOD_LONGLONG
)
265 return &ldouble_ctype
;
267 return &double_ctype
;
271 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
273 unsigned long mod
= thistype
->modifiers
;
276 unsigned long old
= ctype
->modifiers
;
277 unsigned long extra
= 0, dup
;
279 if (mod
& old
& MOD_LONG
) {
280 extra
= MOD_LONGLONG
| MOD_LONG
;
284 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
286 warn(pos
, "Just how %sdo you want this type to be?",
287 modifier_string(dup
));
288 ctype
->modifiers
= old
| mod
| extra
;
291 /* Context mask and value */
292 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
293 warn(pos
, "inconsistend attribute types");
294 thistype
->context
= 0;
295 thistype
->contextmask
= 0;
297 ctype
->context
|= thistype
->context
;
298 ctype
->contextmask
|= thistype
->contextmask
;
301 if (thistype
->alignment
& (thistype
->alignment
-1)) {
302 warn(pos
, "I don't like non-power-of-2 alignments");
303 thistype
->alignment
= 0;
305 if (thistype
->alignment
> ctype
->alignment
)
306 ctype
->alignment
= thistype
->alignment
;
309 ctype
->as
= thistype
->as
;
313 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
317 while ( (token
= next
) != NULL
) {
318 struct ctype thistype
;
320 struct symbol
*s
, *type
;
324 if (token_type(token
) != TOKEN_IDENT
)
326 ident
= token
->ident
;
328 s
= lookup_symbol(ident
, NS_TYPEDEF
);
332 mod
= thistype
.modifiers
;
333 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
335 if (mod
& MOD_SPECIALBITS
) {
336 if (mod
& MOD_STRUCTOF
)
337 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
338 else if (mod
& MOD_UNIONOF
)
339 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
340 else if (mod
& MOD_ENUMOF
)
341 next
= enum_specifier(next
, &thistype
);
342 else if (mod
& MOD_ATTRIBUTE
)
343 next
= attribute_specifier(next
, &thistype
);
344 else if (mod
& MOD_TYPEOF
)
345 next
= typeof_specifier(next
, &thistype
);
346 mod
= thistype
.modifiers
;
348 type
= thistype
.base_type
;
352 if (type
!= ctype
->base_type
) {
353 if (ctype
->base_type
) {
354 warn(token
->pos
, "Strange mix of types");
357 ctype
->base_type
= type
;
361 apply_ctype(token
->pos
, &thistype
, ctype
);
364 /* Turn the "virtual types" into real types with real sizes etc */
365 if (!ctype
->base_type
&& (ctype
->modifiers
& MOD_SPECIFIER
))
366 ctype
->base_type
= &int_type
;
368 if (ctype
->base_type
== &int_type
) {
369 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
370 ctype
->modifiers
&= ~MOD_SPECIFIER
;
373 if (ctype
->base_type
== &fp_type
) {
374 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
375 ctype
->modifiers
&= ~MOD_SPECIFIER
;
381 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
383 struct expression
*expr
= NULL
;
385 token
= parse_expression(token
, &expr
);
387 sym
->array_size
= get_expression_value(expr
);
389 sym
->array_size
= -1;
393 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
394 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
396 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
398 struct ctype
*ctype
= &(*tree
)->ctype
;
400 if (p
&& token_type(token
) == TOKEN_IDENT
) {
406 if (match_ident(token
, &__attribute___ident
) || match_ident(token
, &__attribute_ident
)) {
407 struct ctype thistype
= { 0, };
408 token
= attribute_specifier(token
->next
, &thistype
);
409 apply_ctype(token
->pos
, &thistype
, ctype
);
412 if (token_type(token
) != TOKEN_SPECIAL
)
416 * This can be either a parameter list or a grouping.
417 * For the direct (non-abstract) case, we know if must be
418 * a paramter list if we already saw the identifier.
419 * For the abstract case, we know if must be a parameter
420 * list if it is empty or starts with a type.
422 if (token
->special
== '(') {
424 struct token
*next
= token
->next
;
425 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
428 struct symbol
*base_type
= ctype
->base_type
;
429 token
= declarator(next
, tree
, p
);
430 token
= expect(token
, ')', "in nested declarator");
431 while (ctype
->base_type
!= base_type
)
432 ctype
= &ctype
->base_type
->ctype
;
437 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
438 token
= parameter_type_list(next
, sym
);
439 token
= expect(token
, ')', "in function declarator");
442 if (token
->special
== '[') {
443 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
444 token
= abstract_array_declarator(token
->next
, array
);
445 token
= expect(token
, ']', "in abstract_array_declarator");
446 ctype
= &array
->ctype
;
449 if (token
->special
== ':') {
450 struct symbol
*bitfield
;
451 struct expression
*expr
;
452 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
453 token
= conditional_expression(token
->next
, &expr
);
454 bitfield
->fieldwidth
= get_expression_value(expr
);
465 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
467 unsigned long modifiers
;
468 struct symbol
*base_type
;
470 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
471 base_type
= ctype
->base_type
;
472 ctype
->modifiers
= modifiers
;
474 while (match_op(token
,'*')) {
475 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
476 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
477 ptr
->ctype
.as
= ctype
->as
;
478 ptr
->ctype
.context
= ctype
->context
;
479 ptr
->ctype
.contextmask
= ctype
->contextmask
;
480 ptr
->ctype
.base_type
= base_type
;
483 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
484 ctype
->base_type
= base_type
;
487 ctype
->contextmask
= 0;
489 token
= declaration_specifiers(token
->next
, ctype
, 1);
494 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
496 token
= pointer(token
, &(*tree
)->ctype
);
497 return direct_declarator(token
, tree
, p
);
500 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
502 while (!match_op(token
, '}')) {
503 struct ctype ctype
= {0, };
505 token
= declaration_specifiers(token
, &ctype
, 0);
507 struct ident
*ident
= NULL
;
508 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
510 token
= pointer(token
, &decl
->ctype
);
511 token
= direct_declarator(token
, &decl
, &ident
);
512 if (match_op(token
, ':')) {
513 struct expression
*expr
;
514 token
= parse_expression(token
->next
, &expr
);
516 add_symbol(list
, decl
);
517 if (!match_op(token
, ','))
521 if (!match_op(token
, ';'))
528 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
530 struct ident
*ident
= NULL
;
532 struct ctype ctype
= { 0, };
534 token
= declaration_specifiers(token
, &ctype
, 0);
535 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
538 token
= pointer(token
, &sym
->ctype
);
539 token
= direct_declarator(token
, tree
, &ident
);
543 struct token
*typename(struct token
*token
, struct symbol
**p
)
545 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
547 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
548 return declarator(token
, &sym
, NULL
);
551 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
553 token
= parse_expression(token
, tree
);
554 return expect(token
, ';', "at end of statement");
557 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
559 struct expression
*expr
;
561 /* Allow empty operands */
562 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
565 token
= primary_expression(token
->next
, &expr
);
566 token
= parens_expression(token
, &expr
, "in asm parameter");
567 } while (match_op(token
, ','));
571 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
573 struct expression
*expr
;
576 token
= primary_expression(token
->next
, &expr
);
577 } while (match_op(token
, ','));
581 /* Make a statement out of an expression */
582 static struct statement
*make_statement(struct expression
*expr
)
584 struct statement
*stmt
;
588 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
589 stmt
->expression
= expr
;
594 * All iterators have two symbols associated with them:
595 * the "continue" and "break" symbols, which are targets
596 * for continue and break statements respectively.
598 * They are in a special name-space, but they follow
599 * all the normal visibility rules, so nested iterators
600 * automatically work right.
602 static void start_iterator(struct statement
*stmt
)
604 struct symbol
*cont
, *brk
;
606 start_symbol_scope();
607 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
608 cont
->ident
= &continue_ident
;
609 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
610 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
611 brk
->ident
= &break_ident
;
612 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
614 stmt
->type
= STMT_ITERATOR
;
615 stmt
->iterator_break
= brk
;
616 stmt
->iterator_continue
= cont
;
619 static void end_iterator(struct statement
*stmt
)
625 * A "switch()" statement, like an iterator, has a
626 * the "break" symbol associated with it. It works
627 * exactly like the iterator break - it's the target
628 * for any break-statements in scope, and means that
629 * "break" handling doesn't even need to know whether
630 * it's breaking out of an iterator or a switch.
632 * In addition, the "case" symbol is a marker for the
633 * case/default statements to find the switch statement
634 * that they are associated with.
636 static void start_switch(struct statement
*stmt
)
638 struct symbol
*brk
, *switch_case
;
640 start_symbol_scope();
641 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
642 brk
->ident
= &break_ident
;
643 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
645 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
646 switch_case
->ident
= &case_ident
;
647 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
648 switch_case
->stmt
= stmt
;
650 stmt
->type
= STMT_SWITCH
;
651 stmt
->switch_break
= brk
;
652 stmt
->switch_case
= switch_case
;
655 static void end_switch(struct statement
*stmt
)
657 if (!stmt
->switch_case
->symbol_list
)
658 warn(stmt
->pos
, "switch with no cases");
662 static void add_case_statement(struct statement
*stmt
)
664 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
668 warn(stmt
->pos
, "not in switch scope");
671 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
672 add_symbol(&target
->symbol_list
, sym
);
674 stmt
->case_label
= sym
;
677 struct token
*statement(struct token
*token
, struct statement
**tree
)
679 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
682 if (token_type(token
) == TOKEN_IDENT
) {
683 if (token
->ident
== &if_ident
) {
684 stmt
->type
= STMT_IF
;
685 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
686 token
= statement(token
, &stmt
->if_true
);
687 if (token_type(token
) != TOKEN_IDENT
)
689 if (token
->ident
!= &else_ident
)
691 return statement(token
->next
, &stmt
->if_false
);
693 if (token
->ident
== &return_ident
) {
694 stmt
->type
= STMT_RETURN
;
695 return expression_statement(token
->next
, &stmt
->expression
);
697 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
698 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
699 stmt
->type
= STMT_GOTO
;
700 stmt
->goto_label
= target
;
702 warn(stmt
->pos
, "break/continue not in iterator scope");
703 return expect(token
->next
, ';', "at end of statement");
705 if (token
->ident
== &default_ident
) {
707 goto default_statement
;
709 if (token
->ident
== &case_ident
) {
710 token
= parse_expression(token
->next
, &stmt
->case_expression
);
711 if (match_op(token
, SPECIAL_ELLIPSIS
))
712 token
= parse_expression(token
->next
, &stmt
->case_to
);
714 stmt
->type
= STMT_CASE
;
715 token
= expect(token
, ':', "after default/case");
716 add_case_statement(stmt
);
717 return statement(token
, &stmt
->case_statement
);
719 if (token
->ident
== &switch_ident
) {
720 stmt
->type
= STMT_SWITCH
;
722 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
723 token
= statement(token
, &stmt
->switch_statement
);
727 if (token
->ident
== &for_ident
) {
728 struct expression
*e1
, *e2
, *e3
;
729 struct statement
*iterator
;
731 start_iterator(stmt
);
732 token
= expect(token
->next
, '(', "after 'for'");
733 token
= parse_expression(token
, &e1
);
734 token
= expect(token
, ';', "in 'for'");
735 token
= parse_expression(token
, &e2
);
736 token
= expect(token
, ';', "in 'for'");
737 token
= parse_expression(token
, &e3
);
738 token
= expect(token
, ')', "in 'for'");
739 token
= statement(token
, &iterator
);
741 stmt
->iterator_pre_statement
= make_statement(e1
);
742 stmt
->iterator_pre_condition
= e2
;
743 stmt
->iterator_post_statement
= make_statement(e3
);
744 stmt
->iterator_post_condition
= e2
;
745 stmt
->iterator_statement
= iterator
;
750 if (token
->ident
== &while_ident
) {
751 struct expression
*expr
;
752 struct statement
*iterator
;
754 start_iterator(stmt
);
755 token
= parens_expression(token
->next
, &expr
, "after 'while'");
756 token
= statement(token
, &iterator
);
758 stmt
->iterator_pre_condition
= expr
;
759 stmt
->iterator_post_condition
= expr
;
760 stmt
->iterator_statement
= iterator
;
765 if (token
->ident
== &do_ident
) {
766 struct expression
*expr
;
767 struct statement
*iterator
;
769 start_iterator(stmt
);
770 token
= statement(token
->next
, &iterator
);
771 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
774 warn(token
->pos
, "expected 'while' after 'do'");
775 token
= parens_expression(token
, &expr
, "after 'do-while'");
777 stmt
->iterator_post_condition
= expr
;
778 stmt
->iterator_statement
= iterator
;
781 return expect(token
, ';', "after statement");
783 if (token
->ident
== &goto_ident
) {
784 stmt
->type
= STMT_GOTO
;
786 if (token_type(token
) == TOKEN_IDENT
) {
787 stmt
->goto_label
= label_symbol(token
);
790 warn(token
->pos
, "invalid label");
791 return expect(token
, ';', "at end of statement");
793 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm___ident
|| token
->ident
== &__asm_ident
) {
794 struct expression
*expr
;
795 stmt
->type
= STMT_ASM
;
797 if (token_type(token
) == TOKEN_IDENT
) {
798 if (token
->ident
== &__volatile___ident
|| token
->ident
== &volatile_ident
)
801 token
= expect(token
, '(', "after asm");
802 token
= parse_expression(token
->next
, &expr
);
803 if (match_op(token
, ':'))
804 token
= parse_asm_operands(token
, stmt
);
805 if (match_op(token
, ':'))
806 token
= parse_asm_operands(token
, stmt
);
807 if (match_op(token
, ':'))
808 token
= parse_asm_clobbers(token
, stmt
);
809 token
= expect(token
, ')', "after asm");
810 return expect(token
, ';', "at end of asm-statement");
812 if (match_op(token
->next
, ':')) {
813 stmt
->type
= STMT_LABEL
;
814 stmt
->label_identifier
= label_symbol(token
);
815 return statement(token
->next
->next
, &stmt
->label_statement
);
819 if (match_op(token
, '{')) {
820 stmt
->type
= STMT_COMPOUND
;
821 start_symbol_scope();
822 token
= compound_statement(token
->next
, stmt
);
825 return expect(token
, '}', "at end of compound statement");
828 stmt
->type
= STMT_EXPRESSION
;
829 return expression_statement(token
, &stmt
->expression
);
832 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
835 struct statement
* stmt
;
836 if (eof_token(token
))
838 if (match_op(token
, '}'))
840 token
= statement(token
, &stmt
);
841 add_statement(list
, stmt
);
846 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
848 struct symbol_list
**list
= &fn
->arguments
;
850 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
852 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
858 token
= parameter_declaration(token
, &sym
);
859 /* Special case: (void) */
860 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
862 add_symbol(list
, sym
);
863 if (!match_op(token
, ','))
871 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
873 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
875 while (!eof_token(token
)) {
876 if (!lookup_type(token
))
878 token
= external_declaration(token
, &stmt
->syms
);
880 token
= statement_list(token
, &stmt
->stmts
);
884 static struct expression
*identifier_expression(struct token
*token
)
886 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
887 expr
->expr_ident
= token
->ident
;
891 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
893 int idx_from
, idx_to
;
894 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
896 idx_from
= get_expression_value(from
);
899 idx_to
= get_expression_value(to
);
900 if (idx_to
< idx_from
|| idx_from
< 0)
901 warn(from
->pos
, "nonsense array initializer index range");
903 expr
->idx_from
= idx_from
;
904 expr
->idx_to
= idx_to
;
908 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
911 struct token
*next
= token
->next
;
912 struct expression
*expr
;
914 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
915 add_expression(list
, identifier_expression(next
));
916 token
= next
->next
->next
;
917 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
918 add_expression(list
, identifier_expression(token
));
920 } else if (match_op(token
, '[')) {
921 struct expression
*from
= NULL
, *to
= NULL
;
922 token
= constant_expression(token
->next
, &from
);
923 if (match_op(token
, SPECIAL_ELLIPSIS
))
924 token
= constant_expression(token
->next
, &to
);
925 add_expression(list
, index_expression(from
, to
));
926 token
= expect(token
, ']', "at end of initializer index");
927 token
= expect(token
, '=', "at end of initializer index");
931 token
= initializer(&expr
, token
);
934 add_expression(list
, expr
);
935 if (!match_op(token
, ','))
942 struct token
*initializer(struct expression
**tree
, struct token
*token
)
944 if (match_op(token
, '{')) {
945 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
947 token
= initializer_list(&expr
->expr_list
, token
->next
);
948 return expect(token
, '}', "at end of initializer");
950 return assignment_expression(token
, tree
);
953 static void declare_argument(struct symbol
*sym
, void *data
, int flags
)
955 struct symbol
*decl
= data
;
958 warn(decl
->pos
, "no identifier for function argument");
961 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
964 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
966 struct ident
*ident
= NULL
;
968 struct ctype ctype
= { 0, };
969 struct symbol
*base_type
;
972 /* Parse declaration-specifiers, if any */
973 token
= declaration_specifiers(token
, &ctype
, 0);
974 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
976 token
= pointer(token
, &decl
->ctype
);
977 token
= declarator(token
, &decl
, &ident
);
979 /* Just a type declaration? */
981 return expect(token
, ';', "end of type declaration");
985 /* type define declaration? */
986 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
987 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
989 base_type
= decl
->ctype
.base_type
;
990 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
991 if (match_op(token
, '{')) {
992 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
993 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
994 warn(decl
->pos
, "function with external linkage has definition");
996 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
997 decl
->ctype
.modifiers
|= MOD_EXTERN
;
998 base_type
->stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
999 start_function_scope();
1000 symbol_iterate(base_type
->arguments
, declare_argument
, decl
);
1001 token
= compound_statement(token
->next
, base_type
->stmt
);
1002 end_function_scope();
1003 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1004 add_symbol(list
, decl
);
1005 check_declaration(decl
);
1006 return expect(token
, '}', "at end of function");
1008 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1009 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1013 if (!is_typedef
&& match_op(token
, '=')) {
1014 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1015 warn(decl
->pos
, "symbol with external linkage has initializer");
1016 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1018 token
= initializer(&decl
->initializer
, token
->next
);
1020 if (!is_typedef
&& !(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
)))
1021 add_symbol(list
, decl
);
1022 check_declaration(decl
);
1024 if (!match_op(token
, ','))
1028 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1029 decl
->ctype
= ctype
;
1030 token
= pointer(token
, &decl
->ctype
);
1031 token
= declarator(token
->next
, &decl
, &ident
);
1033 warn(token
->pos
, "expected identifier name in type definition");
1037 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1039 /* Function declarations are automatically extern unless specifically static */
1040 base_type
= decl
->ctype
.base_type
;
1041 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1042 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1043 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1046 return expect(token
, ';', "at end of declaration");
1049 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1051 while (!eof_token(token
))
1052 token
= external_declaration(token
, list
);
1053 // They aren't needed any more
1054 clear_token_alloc();