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
;
29 struct symbol_list
*function_computed_target_list
;
30 struct statement_list
*function_computed_goto_list
;
32 // Add a symbol to the list of function-local symbols
33 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
35 static struct token
*statement(struct token
*token
, struct statement
**tree
);
36 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
38 static int match_idents(struct token
*token
, ...)
42 if (token_type(token
) != TOKEN_IDENT
)
45 va_start(args
, token
);
47 struct ident
* next
= va_arg(args
, struct ident
*);
50 if (token
->ident
== next
)
56 struct statement
*alloc_statement(struct position pos
, int type
)
58 struct statement
*stmt
= __alloc_statement(0);
64 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
66 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
68 struct symbol
*sym
= alloc_symbol(pos
, type
);
70 sym
->ctype
.base_type
= ctype
->base_type
;
71 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
73 ctype
->base_type
= sym
;
74 ctype
->modifiers
&= MOD_STORAGE
;
78 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
80 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
82 sym
= alloc_symbol(token
->pos
, type
);
83 sym
->ident
= token
->ident
;
84 bind_symbol(sym
, token
->ident
, ns
);
85 if (type
== SYM_LABEL
)
92 * NOTE! NS_LABEL is not just a different namespace,
93 * it also ends up using function scope instead of the
94 * regular symbol scope.
96 struct symbol
*label_symbol(struct token
*token
)
98 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
101 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
102 struct token
*token
, struct ctype
*ctype
,
103 struct token
*(*parse
)(struct token
*, struct symbol
*))
107 ctype
->modifiers
= 0;
108 if (token_type(token
) == TOKEN_IDENT
) {
109 sym
= lookup_or_create_symbol(ns
, type
, token
);
111 ctype
->base_type
= sym
;
112 if (match_op(token
, '{')) {
113 token
= parse(token
->next
, sym
);
114 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
119 // private struct/union/enum type
120 if (!match_op(token
, '{')) {
121 warn(token
->pos
, "expected declaration");
122 ctype
->base_type
= &bad_type
;
126 sym
= alloc_symbol(token
->pos
, type
);
127 token
= parse(token
->next
, sym
);
128 ctype
->base_type
= sym
;
129 return expect(token
, '}', "at end of specifier");
132 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
134 return struct_declaration_list(token
, &sym
->symbol_list
);
137 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
139 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
142 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
145 while (token_type(token
) == TOKEN_IDENT
) {
146 struct token
*next
= token
->next
;
149 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
150 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
151 sym
->ctype
.base_type
= parent
;
152 parent
->ctype
.base_type
= &int_ctype
;
154 if (match_op(next
, '=')) {
155 struct expression
*expr
;
156 next
= constant_expression(next
->next
, &expr
);
157 nextval
= get_expression_value(expr
);
159 sym
->value
= nextval
;
162 if (!match_op(token
, ','))
165 nextval
= nextval
+ 1;
170 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
172 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
175 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
179 if (!match_op(token
, '(')) {
180 warn(token
->pos
, "expected '(' after typeof");
183 if (lookup_type(token
->next
)) {
184 token
= typename(token
->next
, &sym
);
187 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
188 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
190 ctype
->modifiers
= 0;
191 ctype
->base_type
= typeof_sym
;
193 return expect(token
, ')', "after typeof");
196 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
198 if (match_string_ident(attribute
, "packed") ||
199 match_string_ident(attribute
, "__packed__")) {
200 ctype
->alignment
= 1;
203 if (match_string_ident(attribute
, "aligned") ||
204 match_string_ident(attribute
, "__aligned__")) {
205 int alignment
= max_alignment
;
207 alignment
= get_expression_value(expr
);
208 ctype
->alignment
= alignment
;
211 if (match_string_ident(attribute
, "nocast")) {
212 ctype
->modifiers
|= MOD_NOCAST
;
215 if (match_string_ident(attribute
, "noderef")) {
216 ctype
->modifiers
|= MOD_NODEREF
;
219 if (match_string_ident(attribute
, "safe")) {
220 ctype
->modifiers
|= MOD_SAFE
;
223 if (match_string_ident(attribute
, "force")) {
224 ctype
->modifiers
|= MOD_FORCE
;
227 if (match_string_ident(attribute
, "address_space")) {
229 return "expected address space number";
230 ctype
->as
= get_expression_value(expr
);
233 if (match_string_ident(attribute
, "context")) {
234 if (expr
&& expr
->type
== EXPR_COMMA
) {
235 int mask
= get_expression_value(expr
->left
);
236 int value
= get_expression_value(expr
->right
);
238 return "nonsense attribute types";
239 ctype
->contextmask
|= mask
;
240 ctype
->context
|= value
;
243 return "expected context mask and value";
245 if (match_string_ident(attribute
, "mode") ||
246 match_string_ident(attribute
, "__mode__")) {
247 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
248 struct ident
*ident
= expr
->symbol_name
;
251 * Match against __QI__/__HI__/__SI__/__DI__
253 * FIXME! This is broken - we don't actually get
254 * the type information updated properly at this
255 * stage for some reason.
257 if (match_string_ident(ident
, "__QI__") ||
258 match_string_ident(ident
, "QI")) {
259 ctype
->modifiers
|= MOD_SHORT
;
260 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
263 if (match_string_ident(ident
, "__HI__") ||
264 match_string_ident(ident
, "HI")) {
265 ctype
->modifiers
|= MOD_SHORT
;
266 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
269 if (match_string_ident(ident
, "__SI__") ||
270 match_string_ident(ident
, "SI")) {
274 if (match_string_ident(ident
, "__DI__") ||
275 match_string_ident(ident
, "DI")) {
276 ctype
->modifiers
|= MOD_LONGLONG
;
277 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
280 return "unknown mode attribute";
282 return "expected attribute mode symbol";
285 /* Throw away for now.. */
286 if (match_string_ident(attribute
, "format") ||
287 match_string_ident(attribute
, "__format__"))
289 if (match_string_ident(attribute
, "section") ||
290 match_string_ident(attribute
, "__section__"))
292 if (match_string_ident(attribute
, "unused") ||
293 match_string_ident(attribute
, "__unused__"))
295 if (match_string_ident(attribute
, "const") ||
296 match_string_ident(attribute
, "__const__"))
298 if (match_string_ident(attribute
, "noreturn"))
300 if (match_string_ident(attribute
, "regparm"))
302 if (match_string_ident(attribute
, "weak"))
304 if (match_string_ident(attribute
, "alias"))
306 if (match_string_ident(attribute
, "pure"))
308 if (match_string_ident(attribute
, "always_inline"))
310 if (match_string_ident(attribute
, "syscall_linkage"))
313 return "unknown attribute";
316 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
318 ctype
->modifiers
= 0;
319 token
= expect(token
, '(', "after attribute");
320 token
= expect(token
, '(', "after attribute");
324 struct ident
*attribute_name
;
325 struct expression
*attribute_expr
;
327 if (eof_token(token
))
329 if (match_op(token
, ';'))
331 if (token_type(token
) != TOKEN_IDENT
)
333 attribute_name
= token
->ident
;
335 attribute_expr
= NULL
;
336 if (match_op(token
, '('))
337 token
= parens_expression(token
, &attribute_expr
, "in attribute");
338 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
340 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
341 if (!match_op(token
, ','))
346 token
= expect(token
, ')', "after attribute");
347 token
= expect(token
, ')', "after attribute");
351 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
352 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
354 struct symbol
* ctype_integer(unsigned int spec
)
356 static struct symbol
*const integer_ctypes
[][2] = {
357 { &llong_ctype
, &ullong_ctype
},
358 { &long_ctype
, &ulong_ctype
},
359 { &short_ctype
, &ushort_ctype
},
360 { &char_ctype
, &uchar_ctype
},
361 { &int_ctype
, &uint_ctype
},
363 struct symbol
*const (*ctype
)[2];
365 ctype
= integer_ctypes
;
366 if (!(spec
& MOD_LONGLONG
)) {
368 if (!(spec
& MOD_LONG
)) {
370 if (!(spec
& MOD_SHORT
)) {
372 if (!(spec
& MOD_CHAR
))
377 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
380 struct symbol
* ctype_fp(unsigned int spec
)
382 if (spec
& MOD_LONGLONG
)
383 return &ldouble_ctype
;
385 return &double_ctype
;
389 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
391 unsigned long mod
= thistype
->modifiers
;
394 unsigned long old
= ctype
->modifiers
;
395 unsigned long extra
= 0, dup
;
397 if (mod
& old
& MOD_LONG
) {
398 extra
= MOD_LONGLONG
| MOD_LONG
;
402 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
404 warn(pos
, "Just how %sdo you want this type to be?",
405 modifier_string(dup
));
406 ctype
->modifiers
= old
| mod
| extra
;
409 /* Context mask and value */
410 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
411 warn(pos
, "inconsistend attribute types");
412 thistype
->context
= 0;
413 thistype
->contextmask
= 0;
415 ctype
->context
|= thistype
->context
;
416 ctype
->contextmask
|= thistype
->contextmask
;
419 if (thistype
->alignment
& (thistype
->alignment
-1)) {
420 warn(pos
, "I don't like non-power-of-2 alignments");
421 thistype
->alignment
= 0;
423 if (thistype
->alignment
> ctype
->alignment
)
424 ctype
->alignment
= thistype
->alignment
;
427 ctype
->as
= thistype
->as
;
431 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
435 while ( (token
= next
) != NULL
) {
436 struct ctype thistype
;
438 struct symbol
*s
, *type
;
442 if (token_type(token
) != TOKEN_IDENT
)
444 ident
= token
->ident
;
446 s
= lookup_symbol(ident
, NS_TYPEDEF
);
450 mod
= thistype
.modifiers
;
451 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
453 if (mod
& MOD_SPECIALBITS
) {
454 if (mod
& MOD_STRUCTOF
)
455 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
456 else if (mod
& MOD_UNIONOF
)
457 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
458 else if (mod
& MOD_ENUMOF
)
459 next
= enum_specifier(next
, &thistype
);
460 else if (mod
& MOD_ATTRIBUTE
)
461 next
= attribute_specifier(next
, &thistype
);
462 else if (mod
& MOD_TYPEOF
)
463 next
= typeof_specifier(next
, &thistype
);
464 mod
= thistype
.modifiers
;
466 type
= thistype
.base_type
;
470 if (ctype
->base_type
)
472 /* User types only mix with qualifiers */
473 if (mod
& MOD_USERTYPE
) {
474 if (ctype
->modifiers
& MOD_SPECIFIER
)
477 ctype
->base_type
= type
;
480 apply_ctype(token
->pos
, &thistype
, ctype
);
483 /* Turn the "virtual types" into real types with real sizes etc */
484 if (!ctype
->base_type
) {
485 struct symbol
*base
= &incomplete_ctype
;
488 * If we have modifiers, we'll default to an integer
489 * type, and "ctype_integer()" will turn this into
492 if (ctype
->modifiers
& MOD_SPECIFIER
)
494 ctype
->base_type
= base
;
497 if (ctype
->base_type
== &int_type
) {
498 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
499 ctype
->modifiers
&= ~MOD_SPECIFIER
;
502 if (ctype
->base_type
== &fp_type
) {
503 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
504 ctype
->modifiers
&= ~MOD_SPECIFIER
;
510 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
512 struct expression
*expr
= NULL
;
514 token
= parse_expression(token
, &expr
);
515 sym
->array_size
= expr
;
519 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
520 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
522 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
524 struct ctype
*ctype
= &(*tree
)->ctype
;
526 if (p
&& token_type(token
) == TOKEN_IDENT
) {
532 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
533 struct ctype thistype
= { 0, };
534 token
= attribute_specifier(token
->next
, &thistype
);
535 apply_ctype(token
->pos
, &thistype
, ctype
);
538 if (token_type(token
) != TOKEN_SPECIAL
)
542 * This can be either a parameter list or a grouping.
543 * For the direct (non-abstract) case, we know if must be
544 * a paramter list if we already saw the identifier.
545 * For the abstract case, we know if must be a parameter
546 * list if it is empty or starts with a type.
548 if (token
->special
== '(') {
550 struct token
*next
= token
->next
;
551 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
554 struct symbol
*base_type
= ctype
->base_type
;
555 token
= declarator(next
, tree
, p
);
556 token
= expect(token
, ')', "in nested declarator");
557 while (ctype
->base_type
!= base_type
)
558 ctype
= &ctype
->base_type
->ctype
;
563 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
564 token
= parameter_type_list(next
, sym
);
565 token
= expect(token
, ')', "in function declarator");
568 if (token
->special
== '[') {
569 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
570 token
= abstract_array_declarator(token
->next
, array
);
571 token
= expect(token
, ']', "in abstract_array_declarator");
572 ctype
= &array
->ctype
;
575 if (token
->special
== ':') {
576 struct symbol
*bitfield
;
577 struct expression
*expr
;
578 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
579 token
= conditional_expression(token
->next
, &expr
);
580 bitfield
->fieldwidth
= get_expression_value(expr
);
591 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
593 unsigned long modifiers
;
594 struct symbol
*base_type
;
596 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
597 base_type
= ctype
->base_type
;
598 ctype
->modifiers
= modifiers
;
600 while (match_op(token
,'*')) {
601 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
602 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
603 ptr
->ctype
.as
= ctype
->as
;
604 ptr
->ctype
.context
= ctype
->context
;
605 ptr
->ctype
.contextmask
= ctype
->contextmask
;
606 ptr
->ctype
.base_type
= base_type
;
609 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
610 ctype
->base_type
= base_type
;
613 ctype
->contextmask
= 0;
615 token
= declaration_specifiers(token
->next
, ctype
, 1);
616 modifiers
= ctype
->modifiers
;
621 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
623 token
= pointer(token
, &(*tree
)->ctype
);
624 return direct_declarator(token
, tree
, p
);
627 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
629 while (!match_op(token
, '}')) {
630 struct ctype ctype
= {0, };
632 token
= declaration_specifiers(token
, &ctype
, 0);
634 struct ident
*ident
= NULL
;
635 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
637 token
= declarator(token
, &decl
, &ident
);
638 if (match_op(token
, ':')) {
639 struct expression
*expr
;
640 token
= parse_expression(token
->next
, &expr
);
642 add_symbol(list
, decl
);
643 if (!match_op(token
, ','))
647 if (!match_op(token
, ';')) {
648 warn(token
->pos
, "expected ; at end of declaration");
656 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
658 struct ident
*ident
= NULL
;
660 struct ctype ctype
= { 0, };
662 token
= declaration_specifiers(token
, &ctype
, 0);
663 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
666 token
= declarator(token
, tree
, &ident
);
670 struct token
*typename(struct token
*token
, struct symbol
**p
)
672 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
674 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
675 return declarator(token
, &sym
, NULL
);
678 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
680 token
= parse_expression(token
, tree
);
681 return expect(token
, ';', "at end of statement");
684 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
686 struct expression
*expr
;
688 /* Allow empty operands */
689 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
692 if (match_op(token
->next
, '[') &&
693 token_type(token
->next
->next
) == TOKEN_IDENT
&&
694 match_op(token
->next
->next
->next
, ']'))
695 token
= token
->next
->next
->next
;
696 token
= primary_expression(token
->next
, &expr
);
697 token
= parens_expression(token
, &expr
, "in asm parameter");
698 } while (match_op(token
, ','));
702 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
704 struct expression
*expr
;
707 token
= primary_expression(token
->next
, &expr
);
708 } while (match_op(token
, ','));
712 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
714 struct expression
*expr
;
716 stmt
->type
= STMT_ASM
;
717 if (match_idents(token
, &__volatile___ident
, &volatile_ident
)) {
720 token
= expect(token
, '(', "after asm");
721 token
= parse_expression(token
->next
, &expr
);
722 if (match_op(token
, ':'))
723 token
= parse_asm_operands(token
, stmt
);
724 if (match_op(token
, ':'))
725 token
= parse_asm_operands(token
, stmt
);
726 if (match_op(token
, ':'))
727 token
= parse_asm_clobbers(token
, stmt
);
728 token
= expect(token
, ')', "after asm");
729 return expect(token
, ';', "at end of asm-statement");
732 /* Make a statement out of an expression */
733 static struct statement
*make_statement(struct expression
*expr
)
735 struct statement
*stmt
;
739 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
740 stmt
->expression
= expr
;
745 * All iterators have two symbols associated with them:
746 * the "continue" and "break" symbols, which are targets
747 * for continue and break statements respectively.
749 * They are in a special name-space, but they follow
750 * all the normal visibility rules, so nested iterators
751 * automatically work right.
753 static void start_iterator(struct statement
*stmt
)
755 struct symbol
*cont
, *brk
;
757 start_symbol_scope();
758 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
759 cont
->ident
= &continue_ident
;
760 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
761 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
762 brk
->ident
= &break_ident
;
763 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
765 stmt
->type
= STMT_ITERATOR
;
766 stmt
->iterator_break
= brk
;
767 stmt
->iterator_continue
= cont
;
768 fn_local_symbol(brk
);
769 fn_local_symbol(cont
);
772 static void end_iterator(struct statement
*stmt
)
777 static struct statement
*start_function(struct symbol
*sym
)
780 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
782 start_function_scope();
783 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
784 ret
->ident
= &return_ident
;
785 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
786 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
787 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
788 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
791 fn_local_symbol(ret
);
795 static void end_function(struct symbol
*sym
)
797 end_function_scope();
801 * A "switch()" statement, like an iterator, has a
802 * the "break" symbol associated with it. It works
803 * exactly like the iterator break - it's the target
804 * for any break-statements in scope, and means that
805 * "break" handling doesn't even need to know whether
806 * it's breaking out of an iterator or a switch.
808 * In addition, the "case" symbol is a marker for the
809 * case/default statements to find the switch statement
810 * that they are associated with.
812 static void start_switch(struct statement
*stmt
)
814 struct symbol
*brk
, *switch_case
;
816 start_symbol_scope();
817 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
818 brk
->ident
= &break_ident
;
819 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
821 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
822 switch_case
->ident
= &case_ident
;
823 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
824 switch_case
->stmt
= stmt
;
826 stmt
->type
= STMT_SWITCH
;
827 stmt
->switch_break
= brk
;
828 stmt
->switch_case
= switch_case
;
830 fn_local_symbol(brk
);
831 fn_local_symbol(switch_case
);
834 static void end_switch(struct statement
*stmt
)
836 if (!stmt
->switch_case
->symbol_list
)
837 warn(stmt
->pos
, "switch with no cases");
841 static void add_case_statement(struct statement
*stmt
)
843 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
847 warn(stmt
->pos
, "not in switch scope");
850 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
851 add_symbol(&target
->symbol_list
, sym
);
853 stmt
->case_label
= sym
;
854 fn_local_symbol(sym
);
857 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
859 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
862 error(token
->pos
, "internal error: return without a function target");
863 stmt
->type
= STMT_RETURN
;
864 stmt
->ret_target
= target
;
865 return expression_statement(token
->next
, &stmt
->ret_value
);
868 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
870 struct symbol_list
*syms
;
871 struct expression
*e1
, *e2
, *e3
;
872 struct statement
*iterator
;
874 start_iterator(stmt
);
875 token
= expect(token
->next
, '(', "after 'for'");
879 /* C99 variable declaration? */
880 if (lookup_type(token
)) {
881 token
= external_declaration(token
, &syms
);
883 token
= parse_expression(token
, &e1
);
884 token
= expect(token
, ';', "in 'for'");
886 token
= parse_expression(token
, &e2
);
887 token
= expect(token
, ';', "in 'for'");
888 token
= parse_expression(token
, &e3
);
889 token
= expect(token
, ')', "in 'for'");
890 token
= statement(token
, &iterator
);
892 stmt
->iterator_syms
= syms
;
893 stmt
->iterator_pre_statement
= make_statement(e1
);
894 stmt
->iterator_pre_condition
= e2
;
895 stmt
->iterator_post_statement
= make_statement(e3
);
896 stmt
->iterator_post_condition
= e2
;
897 stmt
->iterator_statement
= iterator
;
903 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
905 struct expression
*expr
;
906 struct statement
*iterator
;
908 start_iterator(stmt
);
909 token
= parens_expression(token
->next
, &expr
, "after 'while'");
910 token
= statement(token
, &iterator
);
912 stmt
->iterator_pre_condition
= expr
;
913 stmt
->iterator_post_condition
= expr
;
914 stmt
->iterator_statement
= iterator
;
920 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
922 struct expression
*expr
;
923 struct statement
*iterator
;
925 start_iterator(stmt
);
926 token
= statement(token
->next
, &iterator
);
927 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
930 warn(token
->pos
, "expected 'while' after 'do'");
931 token
= parens_expression(token
, &expr
, "after 'do-while'");
933 stmt
->iterator_post_condition
= expr
;
934 stmt
->iterator_statement
= iterator
;
937 return expect(token
, ';', "after statement");
940 static struct token
*statement(struct token
*token
, struct statement
**tree
)
942 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
945 if (token_type(token
) == TOKEN_IDENT
) {
946 if (token
->ident
== &if_ident
) {
947 stmt
->type
= STMT_IF
;
948 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
949 token
= statement(token
, &stmt
->if_true
);
950 if (token_type(token
) != TOKEN_IDENT
)
952 if (token
->ident
!= &else_ident
)
954 return statement(token
->next
, &stmt
->if_false
);
957 if (token
->ident
== &return_ident
)
958 return parse_return_statement(token
, stmt
);
960 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
961 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
962 stmt
->type
= STMT_GOTO
;
963 stmt
->goto_label
= target
;
965 warn(stmt
->pos
, "break/continue not in iterator scope");
966 return expect(token
->next
, ';', "at end of statement");
968 if (token
->ident
== &default_ident
) {
970 goto default_statement
;
972 if (token
->ident
== &case_ident
) {
973 token
= parse_expression(token
->next
, &stmt
->case_expression
);
974 if (match_op(token
, SPECIAL_ELLIPSIS
))
975 token
= parse_expression(token
->next
, &stmt
->case_to
);
977 stmt
->type
= STMT_CASE
;
978 token
= expect(token
, ':', "after default/case");
979 add_case_statement(stmt
);
980 return statement(token
, &stmt
->case_statement
);
982 if (token
->ident
== &switch_ident
) {
983 stmt
->type
= STMT_SWITCH
;
985 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
986 token
= statement(token
, &stmt
->switch_statement
);
990 if (token
->ident
== &for_ident
)
991 return parse_for_statement(token
, stmt
);
993 if (token
->ident
== &while_ident
)
994 return parse_while_statement(token
, stmt
);
996 if (token
->ident
== &do_ident
)
997 return parse_do_statement(token
, stmt
);
999 if (token
->ident
== &goto_ident
) {
1000 stmt
->type
= STMT_GOTO
;
1001 token
= token
->next
;
1002 if (match_op(token
, '*')) {
1003 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1004 add_statement(&function_computed_goto_list
, stmt
);
1005 } else if (token_type(token
) == TOKEN_IDENT
) {
1006 stmt
->goto_label
= label_symbol(token
);
1007 token
= token
->next
;
1009 warn(token
->pos
, "Expected identifier or goto expression");
1011 return expect(token
, ';', "at end of statement");
1013 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1014 return parse_asm(token
->next
, stmt
);
1016 if (match_op(token
->next
, ':')) {
1017 stmt
->type
= STMT_LABEL
;
1018 stmt
->label_identifier
= label_symbol(token
);
1019 return statement(token
->next
->next
, &stmt
->label_statement
);
1023 if (match_op(token
, '{')) {
1024 stmt
->type
= STMT_COMPOUND
;
1025 start_symbol_scope();
1026 token
= compound_statement(token
->next
, stmt
);
1029 return expect(token
, '}', "at end of compound statement");
1032 stmt
->type
= STMT_EXPRESSION
;
1033 return expression_statement(token
, &stmt
->expression
);
1036 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1039 struct statement
* stmt
;
1040 if (eof_token(token
))
1042 if (match_op(token
, '}'))
1044 token
= statement(token
, &stmt
);
1045 add_statement(list
, stmt
);
1050 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1052 struct symbol_list
**list
= &fn
->arguments
;
1054 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1056 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1058 token
= token
->next
;
1062 if (!lookup_type(token
)) {
1063 warn(token
->pos
, "non-ANSI parameter list");
1066 token
= parameter_declaration(token
, &sym
);
1067 /* Special case: (void) */
1068 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1070 add_symbol(list
, sym
);
1071 if (!match_op(token
, ','))
1073 token
= token
->next
;
1079 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1081 while (!eof_token(token
)) {
1082 if (!lookup_type(token
))
1084 token
= external_declaration(token
, &stmt
->syms
);
1086 token
= statement_list(token
, &stmt
->stmts
);
1090 static struct expression
*identifier_expression(struct token
*token
)
1092 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1093 expr
->expr_ident
= token
->ident
;
1097 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1099 int idx_from
, idx_to
;
1100 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1102 idx_from
= get_expression_value(from
);
1105 idx_to
= get_expression_value(to
);
1106 if (idx_to
< idx_from
|| idx_from
< 0)
1107 warn(from
->pos
, "nonsense array initializer index range");
1109 expr
->idx_from
= idx_from
;
1110 expr
->idx_to
= idx_to
;
1114 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1117 struct token
*next
= token
->next
;
1118 struct expression
*expr
;
1120 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1121 add_expression(list
, identifier_expression(next
));
1122 token
= next
->next
->next
;
1123 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1124 add_expression(list
, identifier_expression(token
));
1126 } else if (match_op(token
, '[')) {
1127 struct expression
*from
= NULL
, *to
= NULL
;
1128 token
= constant_expression(token
->next
, &from
);
1129 if (match_op(token
, SPECIAL_ELLIPSIS
))
1130 token
= constant_expression(token
->next
, &to
);
1131 add_expression(list
, index_expression(from
, to
));
1132 token
= expect(token
, ']', "at end of initializer index");
1133 token
= expect(token
, '=', "at end of initializer index");
1137 token
= initializer(&expr
, token
);
1140 add_expression(list
, expr
);
1141 if (!match_op(token
, ','))
1143 token
= token
->next
;
1148 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1150 if (match_op(token
, '{')) {
1151 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1153 token
= initializer_list(&expr
->expr_list
, token
->next
);
1154 return expect(token
, '}', "at end of initializer");
1156 return assignment_expression(token
, tree
);
1159 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1162 warn(sym
->pos
, "no identifier for function argument");
1165 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1168 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1169 struct symbol_list
**list
)
1171 struct symbol
*base_type
= decl
->ctype
.base_type
;
1172 struct statement
*stmt
;
1175 function_symbol_list
= &decl
->symbol_list
;
1176 function_computed_target_list
= NULL
;
1177 function_computed_goto_list
= NULL
;
1179 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1180 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1181 warn(decl
->pos
, "function with external linkage has definition");
1183 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1184 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1186 stmt
= start_function(decl
);
1188 base_type
->stmt
= stmt
;
1189 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1190 declare_argument(arg
, base_type
);
1193 token
= compound_statement(token
->next
, stmt
);
1196 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1197 add_symbol(list
, decl
);
1198 check_declaration(decl
);
1199 function_symbol_list
= NULL
;
1200 if (function_computed_goto_list
) {
1201 if (!function_computed_target_list
)
1202 warn(decl
->pos
, "function has computed goto but no targets?");
1204 struct statement
*stmt
;
1205 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1206 stmt
->target_list
= function_computed_target_list
;
1210 return expect(token
, '}', "at end of function");
1213 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1215 struct ident
*ident
= NULL
;
1216 struct symbol
*decl
;
1217 struct ctype ctype
= { 0, };
1218 struct symbol
*base_type
;
1221 /* Top-level inline asm? */
1222 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
)) {
1223 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1224 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1225 struct statement
*stmt
;
1227 anon
->ctype
.base_type
= fn
;
1228 function_symbol_list
= &anon
->symbol_list
;
1229 stmt
= start_function(anon
);
1230 token
= parse_asm(token
->next
, stmt
);
1232 function_symbol_list
= NULL
;
1233 add_symbol(list
, anon
);
1237 /* Parse declaration-specifiers, if any */
1238 token
= declaration_specifiers(token
, &ctype
, 0);
1239 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1240 decl
->ctype
= ctype
;
1241 token
= declarator(token
, &decl
, &ident
);
1243 /* Just a type declaration? */
1245 return expect(token
, ';', "end of type declaration");
1247 decl
->ident
= ident
;
1249 /* type define declaration? */
1250 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1252 /* Typedef's don't have meaningful storage */
1254 ctype
.modifiers
&= ~MOD_STORAGE
;
1255 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1256 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1259 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1261 base_type
= decl
->ctype
.base_type
;
1262 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1263 if (match_op(token
, '{'))
1264 return parse_function_body(token
, decl
, list
);
1266 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1267 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1271 if (token_type(token
) == TOKEN_IDENT
) {
1272 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1273 struct expression
*expr
;
1275 token
= expect(token
->next
, '(', "after asm");
1276 token
= parse_expression(token
->next
, &expr
);
1277 token
= expect(token
, ')', "after asm");
1280 if (!is_typedef
&& match_op(token
, '=')) {
1281 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1282 warn(decl
->pos
, "symbol with external linkage has initializer");
1283 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1285 token
= initializer(&decl
->initializer
, token
->next
);
1288 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1289 add_symbol(list
, decl
);
1290 if (function_symbol_list
)
1291 fn_local_symbol(decl
);
1294 check_declaration(decl
);
1296 if (!match_op(token
, ','))
1299 token
= token
->next
;
1301 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1302 decl
->ctype
= ctype
;
1303 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1304 token
= declarator(token
, &decl
, &ident
);
1306 warn(token
->pos
, "expected identifier name in type definition");
1310 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1312 /* Function declarations are automatically extern unless specifically static */
1313 base_type
= decl
->ctype
.base_type
;
1314 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1315 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1316 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1319 return expect(token
, ';', "at end of declaration");
1322 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1324 while (!eof_token(token
))
1325 token
= external_declaration(token
, list
);
1326 // They aren't needed any more
1327 clear_token_alloc();