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
;
150 parent
->ctype
.base_type
= &int_ctype
;
152 if (match_op(next
, '=')) {
153 struct expression
*expr
;
154 next
= constant_expression(next
->next
, &expr
);
155 nextval
= get_expression_value(expr
);
157 sym
->value
= nextval
;
160 if (!match_op(token
, ','))
163 nextval
= nextval
+ 1;
168 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
170 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
173 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
177 if (!match_op(token
, '(')) {
178 warn(token
->pos
, "expected '(' after typeof");
181 if (lookup_type(token
->next
)) {
182 token
= typename(token
->next
, &sym
);
185 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
186 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
188 ctype
->modifiers
= 0;
189 ctype
->base_type
= typeof_sym
;
191 return expect(token
, ')', "after typeof");
194 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
196 if (match_string_ident(attribute
, "packed") ||
197 match_string_ident(attribute
, "__packed__")) {
198 ctype
->alignment
= 1;
201 if (match_string_ident(attribute
, "aligned") ||
202 match_string_ident(attribute
, "__aligned__")) {
203 int alignment
= max_alignment
;
205 alignment
= get_expression_value(expr
);
206 ctype
->alignment
= alignment
;
209 if (match_string_ident(attribute
, "nocast")) {
210 ctype
->modifiers
|= MOD_NOCAST
;
213 if (match_string_ident(attribute
, "noderef")) {
214 ctype
->modifiers
|= MOD_NODEREF
;
217 if (match_string_ident(attribute
, "safe")) {
218 ctype
->modifiers
|= MOD_SAFE
;
221 if (match_string_ident(attribute
, "force")) {
222 ctype
->modifiers
|= MOD_FORCE
;
225 if (match_string_ident(attribute
, "address_space")) {
227 return "expected address space number";
228 ctype
->as
= get_expression_value(expr
);
231 if (match_string_ident(attribute
, "context")) {
232 if (expr
&& expr
->type
== EXPR_COMMA
) {
233 int mask
= get_expression_value(expr
->left
);
234 int value
= get_expression_value(expr
->right
);
236 return "nonsense attribute types";
237 ctype
->contextmask
|= mask
;
238 ctype
->context
|= value
;
241 return "expected context mask and value";
243 if (match_string_ident(attribute
, "mode") ||
244 match_string_ident(attribute
, "__mode__")) {
245 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
246 struct ident
*ident
= expr
->symbol_name
;
249 * Match against __QI__/__HI__/__SI__/__DI__
251 * FIXME! This is broken - we don't actually get
252 * the type information updated properly at this
253 * stage for some reason.
255 if (match_string_ident(ident
, "__QI__") ||
256 match_string_ident(ident
, "QI")) {
257 ctype
->modifiers
|= MOD_SHORT
;
258 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
261 if (match_string_ident(ident
, "__HI__") ||
262 match_string_ident(ident
, "HI")) {
263 ctype
->modifiers
|= MOD_SHORT
;
264 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
267 if (match_string_ident(ident
, "__SI__") ||
268 match_string_ident(ident
, "SI")) {
272 if (match_string_ident(ident
, "__DI__") ||
273 match_string_ident(ident
, "DI")) {
274 ctype
->modifiers
|= MOD_LONGLONG
;
275 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
278 return "unknown mode attribute";
280 return "expected attribute mode symbol";
283 /* Throw away for now.. */
284 if (match_string_ident(attribute
, "format") ||
285 match_string_ident(attribute
, "__format__"))
287 if (match_string_ident(attribute
, "section") ||
288 match_string_ident(attribute
, "__section__"))
290 if (match_string_ident(attribute
, "unused") ||
291 match_string_ident(attribute
, "__unused__"))
293 if (match_string_ident(attribute
, "const") ||
294 match_string_ident(attribute
, "__const__"))
296 if (match_string_ident(attribute
, "noreturn"))
298 if (match_string_ident(attribute
, "regparm"))
300 if (match_string_ident(attribute
, "weak"))
302 if (match_string_ident(attribute
, "alias"))
304 if (match_string_ident(attribute
, "pure"))
306 if (match_string_ident(attribute
, "always_inline"))
308 if (match_string_ident(attribute
, "syscall_linkage"))
311 return "unknown attribute";
314 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
316 ctype
->modifiers
= 0;
317 token
= expect(token
, '(', "after attribute");
318 token
= expect(token
, '(', "after attribute");
322 struct ident
*attribute_name
;
323 struct expression
*attribute_expr
;
325 if (eof_token(token
))
327 if (match_op(token
, ';'))
329 if (token_type(token
) != TOKEN_IDENT
)
331 attribute_name
= token
->ident
;
333 attribute_expr
= NULL
;
334 if (match_op(token
, '('))
335 token
= parens_expression(token
, &attribute_expr
, "in attribute");
336 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
338 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
339 if (!match_op(token
, ','))
344 token
= expect(token
, ')', "after attribute");
345 token
= expect(token
, ')', "after attribute");
349 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
350 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
352 struct symbol
* ctype_integer(unsigned int spec
)
354 static struct symbol
*const integer_ctypes
[][2] = {
355 { &llong_ctype
, &ullong_ctype
},
356 { &long_ctype
, &ulong_ctype
},
357 { &short_ctype
, &ushort_ctype
},
358 { &char_ctype
, &uchar_ctype
},
359 { &int_ctype
, &uint_ctype
},
361 struct symbol
*const (*ctype
)[2];
363 ctype
= integer_ctypes
;
364 if (!(spec
& MOD_LONGLONG
)) {
366 if (!(spec
& MOD_LONG
)) {
368 if (!(spec
& MOD_SHORT
)) {
370 if (!(spec
& MOD_CHAR
))
375 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
378 struct symbol
* ctype_fp(unsigned int spec
)
380 if (spec
& MOD_LONGLONG
)
381 return &ldouble_ctype
;
383 return &double_ctype
;
387 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
389 unsigned long mod
= thistype
->modifiers
;
392 unsigned long old
= ctype
->modifiers
;
393 unsigned long extra
= 0, dup
;
395 if (mod
& old
& MOD_LONG
) {
396 extra
= MOD_LONGLONG
| MOD_LONG
;
400 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
402 warn(pos
, "Just how %sdo you want this type to be?",
403 modifier_string(dup
));
404 ctype
->modifiers
= old
| mod
| extra
;
407 /* Context mask and value */
408 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
409 warn(pos
, "inconsistend attribute types");
410 thistype
->context
= 0;
411 thistype
->contextmask
= 0;
413 ctype
->context
|= thistype
->context
;
414 ctype
->contextmask
|= thistype
->contextmask
;
417 if (thistype
->alignment
& (thistype
->alignment
-1)) {
418 warn(pos
, "I don't like non-power-of-2 alignments");
419 thistype
->alignment
= 0;
421 if (thistype
->alignment
> ctype
->alignment
)
422 ctype
->alignment
= thistype
->alignment
;
425 ctype
->as
= thistype
->as
;
429 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
433 while ( (token
= next
) != NULL
) {
434 struct ctype thistype
;
436 struct symbol
*s
, *type
;
440 if (token_type(token
) != TOKEN_IDENT
)
442 ident
= token
->ident
;
444 s
= lookup_symbol(ident
, NS_TYPEDEF
);
448 mod
= thistype
.modifiers
;
449 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
451 if (mod
& MOD_SPECIALBITS
) {
452 if (mod
& MOD_STRUCTOF
)
453 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
454 else if (mod
& MOD_UNIONOF
)
455 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
456 else if (mod
& MOD_ENUMOF
)
457 next
= enum_specifier(next
, &thistype
);
458 else if (mod
& MOD_ATTRIBUTE
)
459 next
= attribute_specifier(next
, &thistype
);
460 else if (mod
& MOD_TYPEOF
)
461 next
= typeof_specifier(next
, &thistype
);
462 mod
= thistype
.modifiers
;
464 type
= thistype
.base_type
;
468 if (ctype
->base_type
)
470 /* User types only mix with qualifiers */
471 if (mod
& MOD_USERTYPE
) {
472 if (ctype
->modifiers
& MOD_SPECIFIER
)
475 ctype
->base_type
= type
;
478 apply_ctype(token
->pos
, &thistype
, ctype
);
481 /* Turn the "virtual types" into real types with real sizes etc */
482 if (!ctype
->base_type
) {
483 struct symbol
*base
= &incomplete_ctype
;
486 * If we have modifiers, we'll default to an integer
487 * type, and "ctype_integer()" will turn this into
490 if (ctype
->modifiers
& MOD_SPECIFIER
)
492 ctype
->base_type
= base
;
495 if (ctype
->base_type
== &int_type
) {
496 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
497 ctype
->modifiers
&= ~MOD_SPECIFIER
;
500 if (ctype
->base_type
== &fp_type
) {
501 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
502 ctype
->modifiers
&= ~MOD_SPECIFIER
;
508 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
510 struct expression
*expr
= NULL
;
512 token
= parse_expression(token
, &expr
);
513 sym
->array_size
= expr
;
517 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
518 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
520 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
522 struct ctype
*ctype
= &(*tree
)->ctype
;
524 if (p
&& token_type(token
) == TOKEN_IDENT
) {
530 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
531 struct ctype thistype
= { 0, };
532 token
= attribute_specifier(token
->next
, &thistype
);
533 apply_ctype(token
->pos
, &thistype
, ctype
);
536 if (token_type(token
) != TOKEN_SPECIAL
)
540 * This can be either a parameter list or a grouping.
541 * For the direct (non-abstract) case, we know if must be
542 * a paramter list if we already saw the identifier.
543 * For the abstract case, we know if must be a parameter
544 * list if it is empty or starts with a type.
546 if (token
->special
== '(') {
548 struct token
*next
= token
->next
;
549 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
552 struct symbol
*base_type
= ctype
->base_type
;
553 token
= declarator(next
, tree
, p
);
554 token
= expect(token
, ')', "in nested declarator");
555 while (ctype
->base_type
!= base_type
)
556 ctype
= &ctype
->base_type
->ctype
;
561 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
562 token
= parameter_type_list(next
, sym
);
563 token
= expect(token
, ')', "in function declarator");
566 if (token
->special
== '[') {
567 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
568 token
= abstract_array_declarator(token
->next
, array
);
569 token
= expect(token
, ']', "in abstract_array_declarator");
570 ctype
= &array
->ctype
;
573 if (token
->special
== ':') {
574 struct symbol
*bitfield
;
575 struct expression
*expr
;
576 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
577 token
= conditional_expression(token
->next
, &expr
);
578 bitfield
->fieldwidth
= get_expression_value(expr
);
589 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
591 unsigned long modifiers
;
592 struct symbol
*base_type
;
594 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
595 base_type
= ctype
->base_type
;
596 ctype
->modifiers
= modifiers
;
598 while (match_op(token
,'*')) {
599 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
600 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
601 ptr
->ctype
.as
= ctype
->as
;
602 ptr
->ctype
.context
= ctype
->context
;
603 ptr
->ctype
.contextmask
= ctype
->contextmask
;
604 ptr
->ctype
.base_type
= base_type
;
607 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
608 ctype
->base_type
= base_type
;
611 ctype
->contextmask
= 0;
613 token
= declaration_specifiers(token
->next
, ctype
, 1);
614 modifiers
= ctype
->modifiers
;
619 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
621 token
= pointer(token
, &(*tree
)->ctype
);
622 return direct_declarator(token
, tree
, p
);
625 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
627 while (!match_op(token
, '}')) {
628 struct ctype ctype
= {0, };
630 token
= declaration_specifiers(token
, &ctype
, 0);
632 struct ident
*ident
= NULL
;
633 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
635 token
= declarator(token
, &decl
, &ident
);
636 if (match_op(token
, ':')) {
637 struct expression
*expr
;
638 token
= parse_expression(token
->next
, &expr
);
640 add_symbol(list
, decl
);
641 if (!match_op(token
, ','))
645 if (!match_op(token
, ';')) {
646 warn(token
->pos
, "expected ; at end of declaration");
654 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
656 struct ident
*ident
= NULL
;
658 struct ctype ctype
= { 0, };
660 token
= declaration_specifiers(token
, &ctype
, 0);
661 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
664 token
= declarator(token
, tree
, &ident
);
668 struct token
*typename(struct token
*token
, struct symbol
**p
)
670 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
672 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
673 return declarator(token
, &sym
, NULL
);
676 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
678 token
= parse_expression(token
, tree
);
679 return expect(token
, ';', "at end of statement");
682 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
684 struct expression
*expr
;
686 /* Allow empty operands */
687 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
690 if (match_op(token
->next
, '[') &&
691 token_type(token
->next
->next
) == TOKEN_IDENT
&&
692 match_op(token
->next
->next
->next
, ']'))
693 token
= token
->next
->next
->next
;
694 token
= primary_expression(token
->next
, &expr
);
695 token
= parens_expression(token
, &expr
, "in asm parameter");
696 } while (match_op(token
, ','));
700 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
702 struct expression
*expr
;
705 token
= primary_expression(token
->next
, &expr
);
706 } while (match_op(token
, ','));
710 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
712 struct expression
*expr
;
714 stmt
->type
= STMT_ASM
;
715 if (match_idents(token
, &__volatile___ident
, &volatile_ident
)) {
718 token
= expect(token
, '(', "after asm");
719 token
= parse_expression(token
->next
, &expr
);
720 if (match_op(token
, ':'))
721 token
= parse_asm_operands(token
, stmt
);
722 if (match_op(token
, ':'))
723 token
= parse_asm_operands(token
, stmt
);
724 if (match_op(token
, ':'))
725 token
= parse_asm_clobbers(token
, stmt
);
726 token
= expect(token
, ')', "after asm");
727 return expect(token
, ';', "at end of asm-statement");
730 /* Make a statement out of an expression */
731 static struct statement
*make_statement(struct expression
*expr
)
733 struct statement
*stmt
;
737 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
738 stmt
->expression
= expr
;
743 * All iterators have two symbols associated with them:
744 * the "continue" and "break" symbols, which are targets
745 * for continue and break statements respectively.
747 * They are in a special name-space, but they follow
748 * all the normal visibility rules, so nested iterators
749 * automatically work right.
751 static void start_iterator(struct statement
*stmt
)
753 struct symbol
*cont
, *brk
;
755 start_symbol_scope();
756 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
757 cont
->ident
= &continue_ident
;
758 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
759 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
760 brk
->ident
= &break_ident
;
761 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
763 stmt
->type
= STMT_ITERATOR
;
764 stmt
->iterator_break
= brk
;
765 stmt
->iterator_continue
= cont
;
766 fn_local_symbol(brk
);
767 fn_local_symbol(cont
);
770 static void end_iterator(struct statement
*stmt
)
775 static struct statement
*start_function(struct symbol
*sym
)
778 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
780 start_function_scope();
781 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
782 ret
->ident
= &return_ident
;
783 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
784 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
785 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
786 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
789 fn_local_symbol(ret
);
793 static void end_function(struct symbol
*sym
)
795 end_function_scope();
799 * A "switch()" statement, like an iterator, has a
800 * the "break" symbol associated with it. It works
801 * exactly like the iterator break - it's the target
802 * for any break-statements in scope, and means that
803 * "break" handling doesn't even need to know whether
804 * it's breaking out of an iterator or a switch.
806 * In addition, the "case" symbol is a marker for the
807 * case/default statements to find the switch statement
808 * that they are associated with.
810 static void start_switch(struct statement
*stmt
)
812 struct symbol
*brk
, *switch_case
;
814 start_symbol_scope();
815 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
816 brk
->ident
= &break_ident
;
817 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
819 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
820 switch_case
->ident
= &case_ident
;
821 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
822 switch_case
->stmt
= stmt
;
824 stmt
->type
= STMT_SWITCH
;
825 stmt
->switch_break
= brk
;
826 stmt
->switch_case
= switch_case
;
828 fn_local_symbol(brk
);
829 fn_local_symbol(switch_case
);
832 static void end_switch(struct statement
*stmt
)
834 if (!stmt
->switch_case
->symbol_list
)
835 warn(stmt
->pos
, "switch with no cases");
839 static void add_case_statement(struct statement
*stmt
)
841 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
845 warn(stmt
->pos
, "not in switch scope");
848 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
849 add_symbol(&target
->symbol_list
, sym
);
851 stmt
->case_label
= sym
;
852 fn_local_symbol(sym
);
855 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
857 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
860 error(token
->pos
, "internal error: return without a function target");
861 stmt
->type
= STMT_RETURN
;
862 stmt
->ret_target
= target
;
863 return expression_statement(token
->next
, &stmt
->ret_value
);
866 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
868 struct symbol_list
*syms
;
869 struct expression
*e1
, *e2
, *e3
;
870 struct statement
*iterator
;
872 start_iterator(stmt
);
873 token
= expect(token
->next
, '(', "after 'for'");
877 /* C99 variable declaration? */
878 if (lookup_type(token
)) {
879 token
= external_declaration(token
, &syms
);
881 token
= parse_expression(token
, &e1
);
882 token
= expect(token
, ';', "in 'for'");
884 token
= parse_expression(token
, &e2
);
885 token
= expect(token
, ';', "in 'for'");
886 token
= parse_expression(token
, &e3
);
887 token
= expect(token
, ')', "in 'for'");
888 token
= statement(token
, &iterator
);
890 stmt
->iterator_syms
= syms
;
891 stmt
->iterator_pre_statement
= make_statement(e1
);
892 stmt
->iterator_pre_condition
= e2
;
893 stmt
->iterator_post_statement
= make_statement(e3
);
894 stmt
->iterator_post_condition
= e2
;
895 stmt
->iterator_statement
= iterator
;
901 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
903 struct expression
*expr
;
904 struct statement
*iterator
;
906 start_iterator(stmt
);
907 token
= parens_expression(token
->next
, &expr
, "after 'while'");
908 token
= statement(token
, &iterator
);
910 stmt
->iterator_pre_condition
= expr
;
911 stmt
->iterator_post_condition
= expr
;
912 stmt
->iterator_statement
= iterator
;
918 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
920 struct expression
*expr
;
921 struct statement
*iterator
;
923 start_iterator(stmt
);
924 token
= statement(token
->next
, &iterator
);
925 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
928 warn(token
->pos
, "expected 'while' after 'do'");
929 token
= parens_expression(token
, &expr
, "after 'do-while'");
931 stmt
->iterator_post_condition
= expr
;
932 stmt
->iterator_statement
= iterator
;
935 return expect(token
, ';', "after statement");
938 static struct token
*statement(struct token
*token
, struct statement
**tree
)
940 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
943 if (token_type(token
) == TOKEN_IDENT
) {
944 if (token
->ident
== &if_ident
) {
945 stmt
->type
= STMT_IF
;
946 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
947 token
= statement(token
, &stmt
->if_true
);
948 if (token_type(token
) != TOKEN_IDENT
)
950 if (token
->ident
!= &else_ident
)
952 return statement(token
->next
, &stmt
->if_false
);
955 if (token
->ident
== &return_ident
)
956 return parse_return_statement(token
, stmt
);
958 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
959 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
960 stmt
->type
= STMT_GOTO
;
961 stmt
->goto_label
= target
;
963 warn(stmt
->pos
, "break/continue not in iterator scope");
964 return expect(token
->next
, ';', "at end of statement");
966 if (token
->ident
== &default_ident
) {
968 goto default_statement
;
970 if (token
->ident
== &case_ident
) {
971 token
= parse_expression(token
->next
, &stmt
->case_expression
);
972 if (match_op(token
, SPECIAL_ELLIPSIS
))
973 token
= parse_expression(token
->next
, &stmt
->case_to
);
975 stmt
->type
= STMT_CASE
;
976 token
= expect(token
, ':', "after default/case");
977 add_case_statement(stmt
);
978 return statement(token
, &stmt
->case_statement
);
980 if (token
->ident
== &switch_ident
) {
981 stmt
->type
= STMT_SWITCH
;
983 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
984 token
= statement(token
, &stmt
->switch_statement
);
988 if (token
->ident
== &for_ident
)
989 return parse_for_statement(token
, stmt
);
991 if (token
->ident
== &while_ident
)
992 return parse_while_statement(token
, stmt
);
994 if (token
->ident
== &do_ident
)
995 return parse_do_statement(token
, stmt
);
997 if (token
->ident
== &goto_ident
) {
998 stmt
->type
= STMT_GOTO
;
1000 if (match_op(token
, '*')) {
1001 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1002 } else if (token_type(token
) == TOKEN_IDENT
) {
1003 stmt
->goto_label
= label_symbol(token
);
1004 token
= token
->next
;
1006 warn(token
->pos
, "Expected identifier or goto expression");
1008 return expect(token
, ';', "at end of statement");
1010 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1011 return parse_asm(token
->next
, stmt
);
1013 if (match_op(token
->next
, ':')) {
1014 stmt
->type
= STMT_LABEL
;
1015 stmt
->label_identifier
= label_symbol(token
);
1016 return statement(token
->next
->next
, &stmt
->label_statement
);
1020 if (match_op(token
, '{')) {
1021 stmt
->type
= STMT_COMPOUND
;
1022 start_symbol_scope();
1023 token
= compound_statement(token
->next
, stmt
);
1026 return expect(token
, '}', "at end of compound statement");
1029 stmt
->type
= STMT_EXPRESSION
;
1030 return expression_statement(token
, &stmt
->expression
);
1033 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1036 struct statement
* stmt
;
1037 if (eof_token(token
))
1039 if (match_op(token
, '}'))
1041 token
= statement(token
, &stmt
);
1042 add_statement(list
, stmt
);
1047 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1049 struct symbol_list
**list
= &fn
->arguments
;
1051 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1053 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1055 token
= token
->next
;
1059 if (!lookup_type(token
)) {
1060 warn(token
->pos
, "non-ANSI parameter list");
1063 token
= parameter_declaration(token
, &sym
);
1064 /* Special case: (void) */
1065 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1067 add_symbol(list
, sym
);
1068 if (!match_op(token
, ','))
1070 token
= token
->next
;
1076 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1078 while (!eof_token(token
)) {
1079 if (!lookup_type(token
))
1081 token
= external_declaration(token
, &stmt
->syms
);
1083 token
= statement_list(token
, &stmt
->stmts
);
1087 static struct expression
*identifier_expression(struct token
*token
)
1089 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1090 expr
->expr_ident
= token
->ident
;
1094 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1096 int idx_from
, idx_to
;
1097 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1099 idx_from
= get_expression_value(from
);
1102 idx_to
= get_expression_value(to
);
1103 if (idx_to
< idx_from
|| idx_from
< 0)
1104 warn(from
->pos
, "nonsense array initializer index range");
1106 expr
->idx_from
= idx_from
;
1107 expr
->idx_to
= idx_to
;
1111 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1114 struct token
*next
= token
->next
;
1115 struct expression
*expr
;
1117 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1118 add_expression(list
, identifier_expression(next
));
1119 token
= next
->next
->next
;
1120 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1121 add_expression(list
, identifier_expression(token
));
1123 } else if (match_op(token
, '[')) {
1124 struct expression
*from
= NULL
, *to
= NULL
;
1125 token
= constant_expression(token
->next
, &from
);
1126 if (match_op(token
, SPECIAL_ELLIPSIS
))
1127 token
= constant_expression(token
->next
, &to
);
1128 add_expression(list
, index_expression(from
, to
));
1129 token
= expect(token
, ']', "at end of initializer index");
1130 token
= expect(token
, '=', "at end of initializer index");
1134 token
= initializer(&expr
, token
);
1137 add_expression(list
, expr
);
1138 if (!match_op(token
, ','))
1140 token
= token
->next
;
1145 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1147 if (match_op(token
, '{')) {
1148 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1150 token
= initializer_list(&expr
->expr_list
, token
->next
);
1151 return expect(token
, '}', "at end of initializer");
1153 return assignment_expression(token
, tree
);
1156 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1159 warn(sym
->pos
, "no identifier for function argument");
1162 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1165 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1166 struct symbol_list
**list
)
1168 struct symbol
*base_type
= decl
->ctype
.base_type
;
1169 struct statement
*stmt
;
1172 function_symbol_list
= &decl
->symbol_list
;
1173 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1174 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1175 warn(decl
->pos
, "function with external linkage has definition");
1177 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1178 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1180 stmt
= start_function(decl
);
1182 base_type
->stmt
= stmt
;
1183 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1184 declare_argument(arg
, base_type
);
1187 token
= compound_statement(token
->next
, stmt
);
1190 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1191 add_symbol(list
, decl
);
1192 check_declaration(decl
);
1193 function_symbol_list
= NULL
;
1194 return expect(token
, '}', "at end of function");
1197 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1199 struct ident
*ident
= NULL
;
1200 struct symbol
*decl
;
1201 struct ctype ctype
= { 0, };
1202 struct symbol
*base_type
;
1205 /* Top-level inline asm? */
1206 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
)) {
1207 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1208 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1209 struct statement
*stmt
;
1211 anon
->ctype
.base_type
= fn
;
1212 function_symbol_list
= &anon
->symbol_list
;
1213 stmt
= start_function(anon
);
1214 token
= parse_asm(token
->next
, stmt
);
1216 function_symbol_list
= NULL
;
1217 add_symbol(list
, anon
);
1221 /* Parse declaration-specifiers, if any */
1222 token
= declaration_specifiers(token
, &ctype
, 0);
1223 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1224 decl
->ctype
= ctype
;
1225 token
= declarator(token
, &decl
, &ident
);
1227 /* Just a type declaration? */
1229 return expect(token
, ';', "end of type declaration");
1231 decl
->ident
= ident
;
1233 /* type define declaration? */
1234 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1236 /* Typedef's don't have meaningful storage */
1238 ctype
.modifiers
&= ~MOD_STORAGE
;
1239 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1240 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1243 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1245 base_type
= decl
->ctype
.base_type
;
1246 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1247 if (match_op(token
, '{'))
1248 return parse_function_body(token
, decl
, list
);
1250 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1251 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1255 if (token_type(token
) == TOKEN_IDENT
) {
1256 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1257 struct expression
*expr
;
1259 token
= expect(token
->next
, '(', "after asm");
1260 token
= parse_expression(token
->next
, &expr
);
1261 token
= expect(token
, ')', "after asm");
1264 if (!is_typedef
&& match_op(token
, '=')) {
1265 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1266 warn(decl
->pos
, "symbol with external linkage has initializer");
1267 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1269 token
= initializer(&decl
->initializer
, token
->next
);
1272 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1273 add_symbol(list
, decl
);
1274 if (function_symbol_list
)
1275 fn_local_symbol(decl
);
1278 check_declaration(decl
);
1280 if (!match_op(token
, ','))
1283 token
= token
->next
;
1285 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1286 decl
->ctype
= ctype
;
1287 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1288 token
= declarator(token
, &decl
, &ident
);
1290 warn(token
->pos
, "expected identifier name in type definition");
1294 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1296 /* Function declarations are automatically extern unless specifically static */
1297 base_type
= decl
->ctype
.base_type
;
1298 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1299 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1300 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1303 return expect(token
, ';', "at end of declaration");
1306 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1308 while (!eof_token(token
))
1309 token
= external_declaration(token
, list
);
1310 // They aren't needed any more
1311 clear_token_alloc();