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 static int match_idents(struct token
*token
, ...)
40 if (token_type(token
) != TOKEN_IDENT
)
43 va_start(args
, token
);
45 struct ident
* next
= va_arg(args
, struct ident
*);
48 if (token
->ident
== next
)
54 struct statement
*alloc_statement(struct position pos
, int type
)
56 struct statement
*stmt
= __alloc_statement(0);
62 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
64 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
66 struct symbol
*sym
= alloc_symbol(pos
, type
);
68 sym
->ctype
.base_type
= ctype
->base_type
;
69 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
71 ctype
->base_type
= sym
;
72 ctype
->modifiers
&= MOD_STORAGE
;
76 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
78 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
80 sym
= alloc_symbol(token
->pos
, type
);
81 sym
->ident
= token
->ident
;
82 bind_symbol(sym
, token
->ident
, ns
);
83 if (type
== SYM_LABEL
)
90 * NOTE! NS_LABEL is not just a different namespace,
91 * it also ends up using function scope instead of the
92 * regular symbol scope.
94 struct symbol
*label_symbol(struct token
*token
)
96 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
99 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
100 struct token
*token
, struct ctype
*ctype
,
101 struct token
*(*parse
)(struct token
*, struct symbol
*))
105 ctype
->modifiers
= 0;
106 if (token_type(token
) == TOKEN_IDENT
) {
107 sym
= lookup_or_create_symbol(ns
, type
, token
);
109 ctype
->base_type
= sym
;
110 if (match_op(token
, '{')) {
111 token
= parse(token
->next
, sym
);
112 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
117 // private struct/union/enum type
118 if (!match_op(token
, '{')) {
119 warn(token
->pos
, "expected declaration");
120 ctype
->base_type
= &bad_type
;
124 sym
= alloc_symbol(token
->pos
, type
);
125 token
= parse(token
->next
, sym
);
126 ctype
->base_type
= sym
;
127 return expect(token
, '}', "at end of specifier");
130 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
132 return struct_declaration_list(token
, &sym
->symbol_list
);
135 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
137 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
140 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
143 while (token_type(token
) == TOKEN_IDENT
) {
144 struct token
*next
= token
->next
;
147 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
148 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
149 sym
->ctype
.base_type
= parent
;
151 if (match_op(next
, '=')) {
152 struct expression
*expr
;
153 next
= constant_expression(next
->next
, &expr
);
154 nextval
= get_expression_value(expr
);
156 sym
->value
= nextval
;
159 if (!match_op(token
, ','))
162 nextval
= nextval
+ 1;
167 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
169 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
172 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
176 if (!match_op(token
, '(')) {
177 warn(token
->pos
, "expected '(' after typeof");
180 if (lookup_type(token
->next
)) {
181 token
= typename(token
->next
, &sym
);
184 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
185 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
187 ctype
->modifiers
= 0;
188 ctype
->base_type
= typeof_sym
;
190 return expect(token
, ')', "after typeof");
193 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
195 if (match_string_ident(attribute
, "packed") ||
196 match_string_ident(attribute
, "__packed__")) {
197 ctype
->alignment
= 1;
200 if (match_string_ident(attribute
, "aligned") ||
201 match_string_ident(attribute
, "__aligned__")) {
202 int alignment
= max_alignment
;
204 alignment
= get_expression_value(expr
);
205 ctype
->alignment
= alignment
;
208 if (match_string_ident(attribute
, "nocast")) {
209 ctype
->modifiers
|= MOD_NOCAST
;
212 if (match_string_ident(attribute
, "noderef")) {
213 ctype
->modifiers
|= MOD_NODEREF
;
216 if (match_string_ident(attribute
, "safe")) {
217 ctype
->modifiers
|= MOD_SAFE
;
220 if (match_string_ident(attribute
, "address_space")) {
222 return "expected address space number";
223 ctype
->as
= get_expression_value(expr
);
226 if (match_string_ident(attribute
, "context")) {
227 if (expr
&& expr
->type
== EXPR_COMMA
) {
228 int mask
= get_expression_value(expr
->left
);
229 int value
= get_expression_value(expr
->right
);
231 return "nonsense attribute types";
232 ctype
->contextmask
|= mask
;
233 ctype
->context
|= value
;
236 return "expected context mask and value";
238 if (match_string_ident(attribute
, "mode") ||
239 match_string_ident(attribute
, "__mode__")) {
240 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
241 struct ident
*ident
= expr
->symbol_name
;
244 * Match against __QI__/__HI__/__SI__/__DI__
246 * FIXME! This is broken - we don't actually get
247 * the type information updated properly at this
248 * stage for some reason.
250 if (match_string_ident(ident
, "__QI__")) {
251 ctype
->modifiers
|= MOD_SHORT
;
252 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
255 if (match_string_ident(ident
, "__HI__")) {
256 ctype
->modifiers
|= MOD_SHORT
;
257 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
260 if (match_string_ident(ident
, "__SI__")) {
264 if (match_string_ident(ident
, "__DI__")) {
265 ctype
->modifiers
|= MOD_LONGLONG
;
266 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
269 return "unknown mode attribute";
271 return "expected attribute mode symbol";
274 /* Throw away for now.. */
275 if (match_string_ident(attribute
, "format") ||
276 match_string_ident(attribute
, "__format__"))
278 if (match_string_ident(attribute
, "section") ||
279 match_string_ident(attribute
, "__section__"))
281 if (match_string_ident(attribute
, "unused") ||
282 match_string_ident(attribute
, "__unused__"))
284 if (match_string_ident(attribute
, "const") ||
285 match_string_ident(attribute
, "__const__"))
287 if (match_string_ident(attribute
, "noreturn"))
289 if (match_string_ident(attribute
, "regparm"))
291 if (match_string_ident(attribute
, "weak"))
293 if (match_string_ident(attribute
, "alias"))
295 if (match_string_ident(attribute
, "pure"))
297 if (match_string_ident(attribute
, "always_inline"))
300 return "unknown attribute";
303 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
305 ctype
->modifiers
= 0;
306 token
= expect(token
, '(', "after attribute");
307 token
= expect(token
, '(', "after attribute");
311 struct ident
*attribute_name
;
312 struct expression
*attribute_expr
;
314 if (eof_token(token
))
316 if (match_op(token
, ';'))
318 if (token_type(token
) != TOKEN_IDENT
)
320 attribute_name
= token
->ident
;
322 attribute_expr
= NULL
;
323 if (match_op(token
, '('))
324 token
= parens_expression(token
, &attribute_expr
, "in attribute");
325 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
327 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
328 if (!match_op(token
, ','))
333 token
= expect(token
, ')', "after attribute");
334 token
= expect(token
, ')', "after attribute");
338 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
339 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
341 struct symbol
* ctype_integer(unsigned int spec
)
343 static struct symbol
*const integer_ctypes
[][2] = {
344 { &llong_ctype
, &ullong_ctype
},
345 { &long_ctype
, &ulong_ctype
},
346 { &short_ctype
, &ushort_ctype
},
347 { &char_ctype
, &uchar_ctype
},
348 { &int_ctype
, &uint_ctype
},
350 struct symbol
*const (*ctype
)[2];
352 ctype
= integer_ctypes
;
353 if (!(spec
& MOD_LONGLONG
)) {
355 if (!(spec
& MOD_LONG
)) {
357 if (!(spec
& MOD_SHORT
)) {
359 if (!(spec
& MOD_CHAR
))
364 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
367 struct symbol
* ctype_fp(unsigned int spec
)
369 if (spec
& MOD_LONGLONG
)
370 return &ldouble_ctype
;
372 return &double_ctype
;
376 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
378 unsigned long mod
= thistype
->modifiers
;
381 unsigned long old
= ctype
->modifiers
;
382 unsigned long extra
= 0, dup
;
384 if (mod
& old
& MOD_LONG
) {
385 extra
= MOD_LONGLONG
| MOD_LONG
;
389 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
391 warn(pos
, "Just how %sdo you want this type to be?",
392 modifier_string(dup
));
393 ctype
->modifiers
= old
| mod
| extra
;
396 /* Context mask and value */
397 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
398 warn(pos
, "inconsistend attribute types");
399 thistype
->context
= 0;
400 thistype
->contextmask
= 0;
402 ctype
->context
|= thistype
->context
;
403 ctype
->contextmask
|= thistype
->contextmask
;
406 if (thistype
->alignment
& (thistype
->alignment
-1)) {
407 warn(pos
, "I don't like non-power-of-2 alignments");
408 thistype
->alignment
= 0;
410 if (thistype
->alignment
> ctype
->alignment
)
411 ctype
->alignment
= thistype
->alignment
;
414 ctype
->as
= thistype
->as
;
418 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
422 while ( (token
= next
) != NULL
) {
423 struct ctype thistype
;
425 struct symbol
*s
, *type
;
429 if (token_type(token
) != TOKEN_IDENT
)
431 ident
= token
->ident
;
433 s
= lookup_symbol(ident
, NS_TYPEDEF
);
437 mod
= thistype
.modifiers
;
438 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
440 if (mod
& MOD_SPECIALBITS
) {
441 if (mod
& MOD_STRUCTOF
)
442 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
443 else if (mod
& MOD_UNIONOF
)
444 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
445 else if (mod
& MOD_ENUMOF
)
446 next
= enum_specifier(next
, &thistype
);
447 else if (mod
& MOD_ATTRIBUTE
)
448 next
= attribute_specifier(next
, &thistype
);
449 else if (mod
& MOD_TYPEOF
)
450 next
= typeof_specifier(next
, &thistype
);
451 mod
= thistype
.modifiers
;
453 type
= thistype
.base_type
;
457 if (ctype
->base_type
)
459 /* User types only mix with qualifiers */
460 if (mod
& MOD_USERTYPE
) {
461 if (ctype
->modifiers
& MOD_SPECIFIER
)
464 ctype
->base_type
= type
;
467 apply_ctype(token
->pos
, &thistype
, ctype
);
470 /* Turn the "virtual types" into real types with real sizes etc */
471 if (!ctype
->base_type
&& (ctype
->modifiers
& MOD_SPECIFIER
))
472 ctype
->base_type
= &int_type
;
474 if (ctype
->base_type
== &int_type
) {
475 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
476 ctype
->modifiers
&= ~MOD_SPECIFIER
;
479 if (ctype
->base_type
== &fp_type
) {
480 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
481 ctype
->modifiers
&= ~MOD_SPECIFIER
;
487 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
489 struct expression
*expr
= NULL
;
491 token
= parse_expression(token
, &expr
);
492 sym
->array_size
= expr
;
496 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
497 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
499 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
501 struct ctype
*ctype
= &(*tree
)->ctype
;
503 if (p
&& token_type(token
) == TOKEN_IDENT
) {
509 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
510 struct ctype thistype
= { 0, };
511 token
= attribute_specifier(token
->next
, &thistype
);
512 apply_ctype(token
->pos
, &thistype
, ctype
);
515 if (token_type(token
) != TOKEN_SPECIAL
)
519 * This can be either a parameter list or a grouping.
520 * For the direct (non-abstract) case, we know if must be
521 * a paramter list if we already saw the identifier.
522 * For the abstract case, we know if must be a parameter
523 * list if it is empty or starts with a type.
525 if (token
->special
== '(') {
527 struct token
*next
= token
->next
;
528 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
531 struct symbol
*base_type
= ctype
->base_type
;
532 token
= declarator(next
, tree
, p
);
533 token
= expect(token
, ')', "in nested declarator");
534 while (ctype
->base_type
!= base_type
)
535 ctype
= &ctype
->base_type
->ctype
;
540 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
541 token
= parameter_type_list(next
, sym
);
542 token
= expect(token
, ')', "in function declarator");
545 if (token
->special
== '[') {
546 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
547 token
= abstract_array_declarator(token
->next
, array
);
548 token
= expect(token
, ']', "in abstract_array_declarator");
549 ctype
= &array
->ctype
;
552 if (token
->special
== ':') {
553 struct symbol
*bitfield
;
554 struct expression
*expr
;
555 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
556 token
= conditional_expression(token
->next
, &expr
);
557 bitfield
->fieldwidth
= get_expression_value(expr
);
568 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
570 unsigned long modifiers
;
571 struct symbol
*base_type
;
573 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
574 base_type
= ctype
->base_type
;
575 ctype
->modifiers
= modifiers
;
577 while (match_op(token
,'*')) {
578 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
579 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
580 ptr
->ctype
.as
= ctype
->as
;
581 ptr
->ctype
.context
= ctype
->context
;
582 ptr
->ctype
.contextmask
= ctype
->contextmask
;
583 ptr
->ctype
.base_type
= base_type
;
586 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
587 ctype
->base_type
= base_type
;
590 ctype
->contextmask
= 0;
592 token
= declaration_specifiers(token
->next
, ctype
, 1);
593 modifiers
= ctype
->modifiers
;
598 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
600 token
= pointer(token
, &(*tree
)->ctype
);
601 return direct_declarator(token
, tree
, p
);
604 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
606 while (!match_op(token
, '}')) {
607 struct ctype ctype
= {0, };
609 token
= declaration_specifiers(token
, &ctype
, 0);
611 struct ident
*ident
= NULL
;
612 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
614 token
= declarator(token
, &decl
, &ident
);
615 if (match_op(token
, ':')) {
616 struct expression
*expr
;
617 token
= parse_expression(token
->next
, &expr
);
619 add_symbol(list
, decl
);
620 if (!match_op(token
, ','))
624 if (!match_op(token
, ';'))
631 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
633 struct ident
*ident
= NULL
;
635 struct ctype ctype
= { 0, };
637 token
= declaration_specifiers(token
, &ctype
, 0);
638 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
641 token
= declarator(token
, tree
, &ident
);
645 struct token
*typename(struct token
*token
, struct symbol
**p
)
647 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
649 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
650 return declarator(token
, &sym
, NULL
);
653 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
655 token
= parse_expression(token
, tree
);
656 return expect(token
, ';', "at end of statement");
659 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
661 struct expression
*expr
;
663 /* Allow empty operands */
664 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
667 token
= primary_expression(token
->next
, &expr
);
668 token
= parens_expression(token
, &expr
, "in asm parameter");
669 } while (match_op(token
, ','));
673 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
675 struct expression
*expr
;
678 token
= primary_expression(token
->next
, &expr
);
679 } while (match_op(token
, ','));
683 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
685 struct expression
*expr
;
687 stmt
->type
= STMT_ASM
;
688 if (match_idents(token
, &__volatile___ident
, &volatile_ident
)) {
691 token
= expect(token
, '(', "after asm");
692 token
= parse_expression(token
->next
, &expr
);
693 if (match_op(token
, ':'))
694 token
= parse_asm_operands(token
, stmt
);
695 if (match_op(token
, ':'))
696 token
= parse_asm_operands(token
, stmt
);
697 if (match_op(token
, ':'))
698 token
= parse_asm_clobbers(token
, stmt
);
699 token
= expect(token
, ')', "after asm");
700 return expect(token
, ';', "at end of asm-statement");
703 /* Make a statement out of an expression */
704 static struct statement
*make_statement(struct expression
*expr
)
706 struct statement
*stmt
;
710 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
711 stmt
->expression
= expr
;
716 * All iterators have two symbols associated with them:
717 * the "continue" and "break" symbols, which are targets
718 * for continue and break statements respectively.
720 * They are in a special name-space, but they follow
721 * all the normal visibility rules, so nested iterators
722 * automatically work right.
724 static void start_iterator(struct statement
*stmt
)
726 struct symbol
*cont
, *brk
;
728 start_symbol_scope();
729 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
730 cont
->ident
= &continue_ident
;
731 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
732 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
733 brk
->ident
= &break_ident
;
734 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
736 stmt
->type
= STMT_ITERATOR
;
737 stmt
->iterator_break
= brk
;
738 stmt
->iterator_continue
= cont
;
739 fn_local_symbol(brk
);
740 fn_local_symbol(cont
);
743 static void end_iterator(struct statement
*stmt
)
748 static struct statement
*start_function(struct symbol
*sym
)
751 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
753 start_function_scope();
754 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
755 ret
->ident
= &return_ident
;
756 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
757 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
758 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
759 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
762 fn_local_symbol(ret
);
766 static void end_function(struct symbol
*sym
)
768 end_function_scope();
772 * A "switch()" statement, like an iterator, has a
773 * the "break" symbol associated with it. It works
774 * exactly like the iterator break - it's the target
775 * for any break-statements in scope, and means that
776 * "break" handling doesn't even need to know whether
777 * it's breaking out of an iterator or a switch.
779 * In addition, the "case" symbol is a marker for the
780 * case/default statements to find the switch statement
781 * that they are associated with.
783 static void start_switch(struct statement
*stmt
)
785 struct symbol
*brk
, *switch_case
;
787 start_symbol_scope();
788 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
789 brk
->ident
= &break_ident
;
790 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
792 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
793 switch_case
->ident
= &case_ident
;
794 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
795 switch_case
->stmt
= stmt
;
797 stmt
->type
= STMT_SWITCH
;
798 stmt
->switch_break
= brk
;
799 stmt
->switch_case
= switch_case
;
801 fn_local_symbol(brk
);
802 fn_local_symbol(switch_case
);
805 static void end_switch(struct statement
*stmt
)
807 if (!stmt
->switch_case
->symbol_list
)
808 warn(stmt
->pos
, "switch with no cases");
812 static void add_case_statement(struct statement
*stmt
)
814 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
818 warn(stmt
->pos
, "not in switch scope");
821 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
822 add_symbol(&target
->symbol_list
, sym
);
824 stmt
->case_label
= sym
;
825 fn_local_symbol(sym
);
828 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
830 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
833 error(token
->pos
, "internal error: return without a function target");
834 stmt
->type
= STMT_RETURN
;
835 stmt
->ret_target
= target
;
836 return expression_statement(token
->next
, &stmt
->ret_value
);
839 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
841 struct symbol_list
*syms
;
842 struct expression
*e1
, *e2
, *e3
;
843 struct statement
*iterator
;
845 start_iterator(stmt
);
846 token
= expect(token
->next
, '(', "after 'for'");
850 /* C99 variable declaration? */
851 if (lookup_type(token
)) {
852 token
= external_declaration(token
, &syms
);
854 token
= parse_expression(token
, &e1
);
855 token
= expect(token
, ';', "in 'for'");
857 token
= parse_expression(token
, &e2
);
858 token
= expect(token
, ';', "in 'for'");
859 token
= parse_expression(token
, &e3
);
860 token
= expect(token
, ')', "in 'for'");
861 token
= statement(token
, &iterator
);
863 stmt
->iterator_syms
= syms
;
864 stmt
->iterator_pre_statement
= make_statement(e1
);
865 stmt
->iterator_pre_condition
= e2
;
866 stmt
->iterator_post_statement
= make_statement(e3
);
867 stmt
->iterator_post_condition
= e2
;
868 stmt
->iterator_statement
= iterator
;
874 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
876 struct expression
*expr
;
877 struct statement
*iterator
;
879 start_iterator(stmt
);
880 token
= parens_expression(token
->next
, &expr
, "after 'while'");
881 token
= statement(token
, &iterator
);
883 stmt
->iterator_pre_condition
= expr
;
884 stmt
->iterator_post_condition
= expr
;
885 stmt
->iterator_statement
= iterator
;
891 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
893 struct expression
*expr
;
894 struct statement
*iterator
;
896 start_iterator(stmt
);
897 token
= statement(token
->next
, &iterator
);
898 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
901 warn(token
->pos
, "expected 'while' after 'do'");
902 token
= parens_expression(token
, &expr
, "after 'do-while'");
904 stmt
->iterator_post_condition
= expr
;
905 stmt
->iterator_statement
= iterator
;
908 return expect(token
, ';', "after statement");
911 static struct token
*statement(struct token
*token
, struct statement
**tree
)
913 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
916 if (token_type(token
) == TOKEN_IDENT
) {
917 if (token
->ident
== &if_ident
) {
918 stmt
->type
= STMT_IF
;
919 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
920 token
= statement(token
, &stmt
->if_true
);
921 if (token_type(token
) != TOKEN_IDENT
)
923 if (token
->ident
!= &else_ident
)
925 return statement(token
->next
, &stmt
->if_false
);
928 if (token
->ident
== &return_ident
)
929 return parse_return_statement(token
, stmt
);
931 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
932 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
933 stmt
->type
= STMT_GOTO
;
934 stmt
->goto_label
= target
;
936 warn(stmt
->pos
, "break/continue not in iterator scope");
937 return expect(token
->next
, ';', "at end of statement");
939 if (token
->ident
== &default_ident
) {
941 goto default_statement
;
943 if (token
->ident
== &case_ident
) {
944 token
= parse_expression(token
->next
, &stmt
->case_expression
);
945 if (match_op(token
, SPECIAL_ELLIPSIS
))
946 token
= parse_expression(token
->next
, &stmt
->case_to
);
948 stmt
->type
= STMT_CASE
;
949 token
= expect(token
, ':', "after default/case");
950 add_case_statement(stmt
);
951 return statement(token
, &stmt
->case_statement
);
953 if (token
->ident
== &switch_ident
) {
954 stmt
->type
= STMT_SWITCH
;
956 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
957 token
= statement(token
, &stmt
->switch_statement
);
961 if (token
->ident
== &for_ident
)
962 return parse_for_statement(token
, stmt
);
964 if (token
->ident
== &while_ident
)
965 return parse_while_statement(token
, stmt
);
967 if (token
->ident
== &do_ident
)
968 return parse_do_statement(token
, stmt
);
970 if (token
->ident
== &goto_ident
) {
971 stmt
->type
= STMT_GOTO
;
973 if (match_op(token
, '*')) {
974 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
975 } else if (token_type(token
) == TOKEN_IDENT
) {
976 stmt
->goto_label
= label_symbol(token
);
979 warn(token
->pos
, "Expected identifier or goto expression");
981 return expect(token
, ';', "at end of statement");
983 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
984 return parse_asm(token
->next
, stmt
);
986 if (match_op(token
->next
, ':')) {
987 stmt
->type
= STMT_LABEL
;
988 stmt
->label_identifier
= label_symbol(token
);
989 return statement(token
->next
->next
, &stmt
->label_statement
);
993 if (match_op(token
, '{')) {
994 stmt
->type
= STMT_COMPOUND
;
995 start_symbol_scope();
996 token
= compound_statement(token
->next
, stmt
);
999 return expect(token
, '}', "at end of compound statement");
1002 stmt
->type
= STMT_EXPRESSION
;
1003 return expression_statement(token
, &stmt
->expression
);
1006 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1009 struct statement
* stmt
;
1010 if (eof_token(token
))
1012 if (match_op(token
, '}'))
1014 token
= statement(token
, &stmt
);
1015 add_statement(list
, stmt
);
1020 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1022 struct symbol_list
**list
= &fn
->arguments
;
1024 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1026 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1028 token
= token
->next
;
1032 if (!lookup_type(token
)) {
1033 warn(token
->pos
, "non-ANSI parameter list");
1036 token
= parameter_declaration(token
, &sym
);
1037 /* Special case: (void) */
1038 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1040 add_symbol(list
, sym
);
1041 if (!match_op(token
, ','))
1043 token
= token
->next
;
1049 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1051 while (!eof_token(token
)) {
1052 if (!lookup_type(token
))
1054 token
= external_declaration(token
, &stmt
->syms
);
1056 token
= statement_list(token
, &stmt
->stmts
);
1060 static struct expression
*identifier_expression(struct token
*token
)
1062 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1063 expr
->expr_ident
= token
->ident
;
1067 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1069 int idx_from
, idx_to
;
1070 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1072 idx_from
= get_expression_value(from
);
1075 idx_to
= get_expression_value(to
);
1076 if (idx_to
< idx_from
|| idx_from
< 0)
1077 warn(from
->pos
, "nonsense array initializer index range");
1079 expr
->idx_from
= idx_from
;
1080 expr
->idx_to
= idx_to
;
1084 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1087 struct token
*next
= token
->next
;
1088 struct expression
*expr
;
1090 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1091 add_expression(list
, identifier_expression(next
));
1092 token
= next
->next
->next
;
1093 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1094 add_expression(list
, identifier_expression(token
));
1096 } else if (match_op(token
, '[')) {
1097 struct expression
*from
= NULL
, *to
= NULL
;
1098 token
= constant_expression(token
->next
, &from
);
1099 if (match_op(token
, SPECIAL_ELLIPSIS
))
1100 token
= constant_expression(token
->next
, &to
);
1101 add_expression(list
, index_expression(from
, to
));
1102 token
= expect(token
, ']', "at end of initializer index");
1103 token
= expect(token
, '=', "at end of initializer index");
1107 token
= initializer(&expr
, token
);
1110 add_expression(list
, expr
);
1111 if (!match_op(token
, ','))
1113 token
= token
->next
;
1118 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1120 if (match_op(token
, '{')) {
1121 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1123 token
= initializer_list(&expr
->expr_list
, token
->next
);
1124 return expect(token
, '}', "at end of initializer");
1126 return assignment_expression(token
, tree
);
1129 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1132 warn(sym
->pos
, "no identifier for function argument");
1135 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1138 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1139 struct symbol_list
**list
)
1141 struct symbol
*base_type
= decl
->ctype
.base_type
;
1142 struct statement
*stmt
;
1145 function_symbol_list
= &decl
->symbol_list
;
1146 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1147 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1148 warn(decl
->pos
, "function with external linkage has definition");
1150 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1151 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1153 stmt
= start_function(decl
);
1155 base_type
->stmt
= stmt
;
1156 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1157 declare_argument(arg
, base_type
);
1160 token
= compound_statement(token
->next
, stmt
);
1163 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1164 add_symbol(list
, decl
);
1165 check_declaration(decl
);
1166 function_symbol_list
= NULL
;
1167 return expect(token
, '}', "at end of function");
1170 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1172 struct ident
*ident
= NULL
;
1173 struct symbol
*decl
;
1174 struct ctype ctype
= { 0, };
1175 struct symbol
*base_type
;
1178 /* Top-level inline asm? */
1179 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
)) {
1180 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1181 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1182 struct statement
*stmt
;
1184 anon
->ctype
.base_type
= fn
;
1185 function_symbol_list
= &anon
->symbol_list
;
1186 stmt
= start_function(anon
);
1187 token
= parse_asm(token
->next
, stmt
);
1189 function_symbol_list
= NULL
;
1190 add_symbol(list
, anon
);
1194 /* Parse declaration-specifiers, if any */
1195 token
= declaration_specifiers(token
, &ctype
, 0);
1196 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1197 decl
->ctype
= ctype
;
1198 token
= declarator(token
, &decl
, &ident
);
1200 /* Just a type declaration? */
1202 return expect(token
, ';', "end of type declaration");
1204 decl
->ident
= ident
;
1206 /* type define declaration? */
1207 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1209 /* Typedef's don't have meaningful storage */
1211 ctype
.modifiers
&= ~MOD_STORAGE
;
1212 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1213 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1216 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1218 base_type
= decl
->ctype
.base_type
;
1219 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1220 if (match_op(token
, '{'))
1221 return parse_function_body(token
, decl
, list
);
1223 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1224 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1228 if (token_type(token
) == TOKEN_IDENT
) {
1229 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1230 struct expression
*expr
;
1232 token
= expect(token
->next
, '(', "after asm");
1233 token
= parse_expression(token
->next
, &expr
);
1234 token
= expect(token
, ')', "after asm");
1237 if (!is_typedef
&& match_op(token
, '=')) {
1238 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1239 warn(decl
->pos
, "symbol with external linkage has initializer");
1240 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1242 token
= initializer(&decl
->initializer
, token
->next
);
1245 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1246 add_symbol(list
, decl
);
1247 if (function_symbol_list
)
1248 fn_local_symbol(decl
);
1251 check_declaration(decl
);
1253 if (!match_op(token
, ','))
1256 token
= token
->next
;
1258 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1259 decl
->ctype
= ctype
;
1260 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1261 token
= declarator(token
, &decl
, &ident
);
1263 warn(token
->pos
, "expected identifier name in type definition");
1267 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1269 /* Function declarations are automatically extern unless specifically static */
1270 base_type
= decl
->ctype
.base_type
;
1271 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1272 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1273 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1276 return expect(token
, ';', "at end of declaration");
1279 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1281 while (!eof_token(token
))
1282 token
= external_declaration(token
, list
);
1283 // They aren't needed any more
1284 clear_token_alloc();