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_CHAR
;
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 if (match_string_ident(ident
, "__word__") ||
281 match_string_ident(ident
, "word")) {
282 ctype
->modifiers
|= MOD_LONG
;
283 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
286 return "unknown mode attribute";
288 return "expected attribute mode symbol";
291 /* Throw away for now.. */
292 if (match_string_ident(attribute
, "format") ||
293 match_string_ident(attribute
, "__format__"))
295 if (match_string_ident(attribute
, "section") ||
296 match_string_ident(attribute
, "__section__"))
298 if (match_string_ident(attribute
, "unused") ||
299 match_string_ident(attribute
, "__unused__"))
301 if (match_string_ident(attribute
, "const") ||
302 match_string_ident(attribute
, "__const__"))
304 if (match_string_ident(attribute
, "noreturn"))
306 if (match_string_ident(attribute
, "regparm"))
308 if (match_string_ident(attribute
, "weak"))
310 if (match_string_ident(attribute
, "alias"))
312 if (match_string_ident(attribute
, "pure"))
314 if (match_string_ident(attribute
, "always_inline"))
316 if (match_string_ident(attribute
, "syscall_linkage"))
318 if (match_string_ident(attribute
, "visibility"))
321 return "unknown attribute";
324 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
326 ctype
->modifiers
= 0;
327 token
= expect(token
, '(', "after attribute");
328 token
= expect(token
, '(', "after attribute");
332 struct ident
*attribute_name
;
333 struct expression
*attribute_expr
;
335 if (eof_token(token
))
337 if (match_op(token
, ';'))
339 if (token_type(token
) != TOKEN_IDENT
)
341 attribute_name
= token
->ident
;
343 attribute_expr
= NULL
;
344 if (match_op(token
, '('))
345 token
= parens_expression(token
, &attribute_expr
, "in attribute");
346 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
348 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
349 if (!match_op(token
, ','))
354 token
= expect(token
, ')', "after attribute");
355 token
= expect(token
, ')', "after attribute");
359 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
360 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
362 struct symbol
* ctype_integer(unsigned int spec
)
364 static struct symbol
*const integer_ctypes
[][2] = {
365 { &llong_ctype
, &ullong_ctype
},
366 { &long_ctype
, &ulong_ctype
},
367 { &short_ctype
, &ushort_ctype
},
368 { &char_ctype
, &uchar_ctype
},
369 { &int_ctype
, &uint_ctype
},
371 struct symbol
*const (*ctype
)[2];
373 ctype
= integer_ctypes
;
374 if (!(spec
& MOD_LONGLONG
)) {
376 if (!(spec
& MOD_LONG
)) {
378 if (!(spec
& MOD_SHORT
)) {
380 if (!(spec
& MOD_CHAR
))
385 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
388 struct symbol
* ctype_fp(unsigned int spec
)
390 if (spec
& MOD_LONGLONG
)
391 return &ldouble_ctype
;
393 return &double_ctype
;
397 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
399 unsigned long mod
= thistype
->modifiers
;
402 unsigned long old
= ctype
->modifiers
;
403 unsigned long extra
= 0, dup
, conflict
;
405 if (mod
& old
& MOD_LONG
) {
406 extra
= MOD_LONGLONG
| MOD_LONG
;
410 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
412 warn(pos
, "Just how %sdo you want this type to be?",
413 modifier_string(dup
));
415 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
417 warn(pos
, "You cannot have both long and short modifiers.");
419 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
421 warn(pos
, "You cannot have both signed and unsigned modifiers.");
423 // Only one storage modifier allowed, except that "inline" doesn't count.
424 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
425 conflict
&= (conflict
- 1);
427 warn(pos
, "multiple storage classes");
429 ctype
->modifiers
= old
| mod
| extra
;
432 /* Context mask and value */
433 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
434 warn(pos
, "inconsistent attribute types");
435 thistype
->context
= 0;
436 thistype
->contextmask
= 0;
438 ctype
->context
|= thistype
->context
;
439 ctype
->contextmask
|= thistype
->contextmask
;
442 if (thistype
->alignment
& (thistype
->alignment
-1)) {
443 warn(pos
, "I don't like non-power-of-2 alignments");
444 thistype
->alignment
= 0;
446 if (thistype
->alignment
> ctype
->alignment
)
447 ctype
->alignment
= thistype
->alignment
;
450 ctype
->as
= thistype
->as
;
454 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
458 while ( (token
= next
) != NULL
) {
459 struct ctype thistype
;
461 struct symbol
*s
, *type
;
465 if (token_type(token
) != TOKEN_IDENT
)
467 ident
= token
->ident
;
469 s
= lookup_symbol(ident
, NS_TYPEDEF
);
473 mod
= thistype
.modifiers
;
474 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
476 if (mod
& MOD_SPECIALBITS
) {
477 if (mod
& MOD_STRUCTOF
)
478 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
479 else if (mod
& MOD_UNIONOF
)
480 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
481 else if (mod
& MOD_ENUMOF
)
482 next
= enum_specifier(next
, &thistype
);
483 else if (mod
& MOD_ATTRIBUTE
)
484 next
= attribute_specifier(next
, &thistype
);
485 else if (mod
& MOD_TYPEOF
)
486 next
= typeof_specifier(next
, &thistype
);
487 mod
= thistype
.modifiers
;
489 type
= thistype
.base_type
;
493 if (ctype
->base_type
)
495 /* User types only mix with qualifiers */
496 if (mod
& MOD_USERTYPE
) {
497 if (ctype
->modifiers
& MOD_SPECIFIER
)
500 ctype
->base_type
= type
;
503 apply_ctype(token
->pos
, &thistype
, ctype
);
506 /* Turn the "virtual types" into real types with real sizes etc */
507 if (!ctype
->base_type
) {
508 struct symbol
*base
= &incomplete_ctype
;
511 * If we have modifiers, we'll default to an integer
512 * type, and "ctype_integer()" will turn this into
515 if (ctype
->modifiers
& MOD_SPECIFIER
)
517 ctype
->base_type
= base
;
520 if (ctype
->base_type
== &int_type
) {
521 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
522 ctype
->modifiers
&= ~MOD_SPECIFIER
;
525 if (ctype
->base_type
== &fp_type
) {
526 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
527 ctype
->modifiers
&= ~MOD_SPECIFIER
;
533 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
535 struct expression
*expr
= NULL
;
537 token
= parse_expression(token
, &expr
);
538 sym
->array_size
= expr
;
542 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
543 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
545 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
547 struct ctype
*ctype
= &(*tree
)->ctype
;
549 if (p
&& token_type(token
) == TOKEN_IDENT
) {
555 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
556 struct ctype thistype
= { 0, };
557 token
= attribute_specifier(token
->next
, &thistype
);
558 apply_ctype(token
->pos
, &thistype
, ctype
);
561 if (token_type(token
) != TOKEN_SPECIAL
)
565 * This can be either a parameter list or a grouping.
566 * For the direct (non-abstract) case, we know if must be
567 * a paramter list if we already saw the identifier.
568 * For the abstract case, we know if must be a parameter
569 * list if it is empty or starts with a type.
571 if (token
->special
== '(') {
573 struct token
*next
= token
->next
;
574 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
577 struct symbol
*base_type
= ctype
->base_type
;
578 token
= declarator(next
, tree
, p
);
579 token
= expect(token
, ')', "in nested declarator");
580 while (ctype
->base_type
!= base_type
)
581 ctype
= &ctype
->base_type
->ctype
;
586 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
587 token
= parameter_type_list(next
, sym
);
588 token
= expect(token
, ')', "in function declarator");
591 if (token
->special
== '[') {
592 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
593 token
= abstract_array_declarator(token
->next
, array
);
594 token
= expect(token
, ']', "in abstract_array_declarator");
595 ctype
= &array
->ctype
;
598 if (token
->special
== ':') {
599 if (is_int_type (ctype
->base_type
)) {
600 struct symbol
*bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
601 struct expression
*expr
;
602 token
= conditional_expression(token
->next
, &expr
);
603 bitfield
->fieldwidth
= get_expression_value(expr
);
605 error(token
->pos
, "Invalid bitfield specifier for type %s.", show_typename (ctype
->base_type
));
616 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
618 unsigned long modifiers
;
619 struct symbol
*base_type
;
621 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
622 base_type
= ctype
->base_type
;
623 ctype
->modifiers
= modifiers
;
625 while (match_op(token
,'*')) {
626 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
627 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
628 ptr
->ctype
.as
= ctype
->as
;
629 ptr
->ctype
.context
= ctype
->context
;
630 ptr
->ctype
.contextmask
= ctype
->contextmask
;
631 ptr
->ctype
.base_type
= base_type
;
634 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
635 ctype
->base_type
= base_type
;
638 ctype
->contextmask
= 0;
640 token
= declaration_specifiers(token
->next
, ctype
, 1);
641 modifiers
= ctype
->modifiers
;
646 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
648 token
= pointer(token
, &(*tree
)->ctype
);
649 return direct_declarator(token
, tree
, p
);
652 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
654 while (!match_op(token
, '}')) {
655 struct ctype ctype
= {0, };
657 token
= declaration_specifiers(token
, &ctype
, 0);
659 struct ident
*ident
= NULL
;
660 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
662 token
= declarator(token
, &decl
, &ident
);
663 if (match_op(token
, ':')) {
664 struct expression
*expr
;
665 token
= parse_expression(token
->next
, &expr
);
667 add_symbol(list
, decl
);
668 if (!match_op(token
, ','))
672 if (!match_op(token
, ';')) {
673 warn(token
->pos
, "expected ; at end of declaration");
681 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
683 struct ident
*ident
= NULL
;
685 struct ctype ctype
= { 0, };
687 token
= declaration_specifiers(token
, &ctype
, 0);
688 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
691 token
= declarator(token
, tree
, &ident
);
695 struct token
*typename(struct token
*token
, struct symbol
**p
)
697 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
699 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
700 return declarator(token
, &sym
, NULL
);
703 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
705 token
= parse_expression(token
, tree
);
706 return expect(token
, ';', "at end of statement");
709 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
711 struct expression
*expr
;
713 /* Allow empty operands */
714 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
717 if (match_op(token
->next
, '[') &&
718 token_type(token
->next
->next
) == TOKEN_IDENT
&&
719 match_op(token
->next
->next
->next
, ']'))
720 token
= token
->next
->next
->next
;
721 token
= primary_expression(token
->next
, &expr
);
722 token
= parens_expression(token
, &expr
, "in asm parameter");
723 } while (match_op(token
, ','));
727 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
729 struct expression
*expr
;
732 token
= primary_expression(token
->next
, &expr
);
733 } while (match_op(token
, ','));
737 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
739 struct expression
*expr
;
741 stmt
->type
= STMT_ASM
;
742 if (match_idents(token
, &__volatile___ident
, &volatile_ident
)) {
745 token
= expect(token
, '(', "after asm");
746 token
= parse_expression(token
->next
, &expr
);
747 if (match_op(token
, ':'))
748 token
= parse_asm_operands(token
, stmt
);
749 if (match_op(token
, ':'))
750 token
= parse_asm_operands(token
, stmt
);
751 if (match_op(token
, ':'))
752 token
= parse_asm_clobbers(token
, stmt
);
753 token
= expect(token
, ')', "after asm");
754 return expect(token
, ';', "at end of asm-statement");
757 /* Make a statement out of an expression */
758 static struct statement
*make_statement(struct expression
*expr
)
760 struct statement
*stmt
;
764 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
765 stmt
->expression
= expr
;
770 * All iterators have two symbols associated with them:
771 * the "continue" and "break" symbols, which are targets
772 * for continue and break statements respectively.
774 * They are in a special name-space, but they follow
775 * all the normal visibility rules, so nested iterators
776 * automatically work right.
778 static void start_iterator(struct statement
*stmt
)
780 struct symbol
*cont
, *brk
;
782 start_symbol_scope();
783 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
784 cont
->ident
= &continue_ident
;
785 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
786 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
787 brk
->ident
= &break_ident
;
788 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
790 stmt
->type
= STMT_ITERATOR
;
791 stmt
->iterator_break
= brk
;
792 stmt
->iterator_continue
= cont
;
793 fn_local_symbol(brk
);
794 fn_local_symbol(cont
);
797 static void end_iterator(struct statement
*stmt
)
802 static struct statement
*start_function(struct symbol
*sym
)
805 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
807 start_function_scope();
808 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
809 ret
->ident
= &return_ident
;
810 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
811 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
812 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
813 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
816 fn_local_symbol(ret
);
820 static void end_function(struct symbol
*sym
)
822 end_function_scope();
826 * A "switch()" statement, like an iterator, has a
827 * the "break" symbol associated with it. It works
828 * exactly like the iterator break - it's the target
829 * for any break-statements in scope, and means that
830 * "break" handling doesn't even need to know whether
831 * it's breaking out of an iterator or a switch.
833 * In addition, the "case" symbol is a marker for the
834 * case/default statements to find the switch statement
835 * that they are associated with.
837 static void start_switch(struct statement
*stmt
)
839 struct symbol
*brk
, *switch_case
;
841 start_symbol_scope();
842 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
843 brk
->ident
= &break_ident
;
844 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
846 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
847 switch_case
->ident
= &case_ident
;
848 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
849 switch_case
->stmt
= stmt
;
851 stmt
->type
= STMT_SWITCH
;
852 stmt
->switch_break
= brk
;
853 stmt
->switch_case
= switch_case
;
855 fn_local_symbol(brk
);
856 fn_local_symbol(switch_case
);
859 static void end_switch(struct statement
*stmt
)
861 if (!stmt
->switch_case
->symbol_list
)
862 warn(stmt
->pos
, "switch with no cases");
866 static void add_case_statement(struct statement
*stmt
)
868 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
872 warn(stmt
->pos
, "not in switch scope");
875 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
876 add_symbol(&target
->symbol_list
, sym
);
878 stmt
->case_label
= sym
;
879 fn_local_symbol(sym
);
882 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
884 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
887 error(token
->pos
, "internal error: return without a function target");
888 stmt
->type
= STMT_RETURN
;
889 stmt
->ret_target
= target
;
890 return expression_statement(token
->next
, &stmt
->ret_value
);
893 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
895 struct symbol_list
*syms
;
896 struct expression
*e1
, *e2
, *e3
;
897 struct statement
*iterator
;
899 start_iterator(stmt
);
900 token
= expect(token
->next
, '(', "after 'for'");
904 /* C99 variable declaration? */
905 if (lookup_type(token
)) {
906 token
= external_declaration(token
, &syms
);
908 token
= parse_expression(token
, &e1
);
909 token
= expect(token
, ';', "in 'for'");
911 token
= parse_expression(token
, &e2
);
912 token
= expect(token
, ';', "in 'for'");
913 token
= parse_expression(token
, &e3
);
914 token
= expect(token
, ')', "in 'for'");
915 token
= statement(token
, &iterator
);
917 stmt
->iterator_syms
= syms
;
918 stmt
->iterator_pre_statement
= make_statement(e1
);
919 stmt
->iterator_pre_condition
= e2
;
920 stmt
->iterator_post_statement
= make_statement(e3
);
921 stmt
->iterator_post_condition
= e2
;
922 stmt
->iterator_statement
= iterator
;
928 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
930 struct expression
*expr
;
931 struct statement
*iterator
;
933 start_iterator(stmt
);
934 token
= parens_expression(token
->next
, &expr
, "after 'while'");
935 token
= statement(token
, &iterator
);
937 stmt
->iterator_pre_condition
= expr
;
938 stmt
->iterator_post_condition
= expr
;
939 stmt
->iterator_statement
= iterator
;
945 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
947 struct expression
*expr
;
948 struct statement
*iterator
;
950 start_iterator(stmt
);
951 token
= statement(token
->next
, &iterator
);
952 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
955 warn(token
->pos
, "expected 'while' after 'do'");
956 token
= parens_expression(token
, &expr
, "after 'do-while'");
958 stmt
->iterator_post_condition
= expr
;
959 stmt
->iterator_statement
= iterator
;
962 return expect(token
, ';', "after statement");
965 static struct token
*statement(struct token
*token
, struct statement
**tree
)
967 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
970 if (token_type(token
) == TOKEN_IDENT
) {
971 if (token
->ident
== &if_ident
) {
972 stmt
->type
= STMT_IF
;
973 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
974 token
= statement(token
, &stmt
->if_true
);
975 if (token_type(token
) != TOKEN_IDENT
)
977 if (token
->ident
!= &else_ident
)
979 return statement(token
->next
, &stmt
->if_false
);
982 if (token
->ident
== &return_ident
)
983 return parse_return_statement(token
, stmt
);
985 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
986 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
987 stmt
->type
= STMT_GOTO
;
988 stmt
->goto_label
= target
;
990 warn(stmt
->pos
, "break/continue not in iterator scope");
991 return expect(token
->next
, ';', "at end of statement");
993 if (token
->ident
== &default_ident
) {
995 goto default_statement
;
997 if (token
->ident
== &case_ident
) {
998 token
= parse_expression(token
->next
, &stmt
->case_expression
);
999 if (match_op(token
, SPECIAL_ELLIPSIS
))
1000 token
= parse_expression(token
->next
, &stmt
->case_to
);
1002 stmt
->type
= STMT_CASE
;
1003 token
= expect(token
, ':', "after default/case");
1004 add_case_statement(stmt
);
1005 return statement(token
, &stmt
->case_statement
);
1007 if (token
->ident
== &switch_ident
) {
1008 stmt
->type
= STMT_SWITCH
;
1010 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1011 token
= statement(token
, &stmt
->switch_statement
);
1015 if (token
->ident
== &for_ident
)
1016 return parse_for_statement(token
, stmt
);
1018 if (token
->ident
== &while_ident
)
1019 return parse_while_statement(token
, stmt
);
1021 if (token
->ident
== &do_ident
)
1022 return parse_do_statement(token
, stmt
);
1024 if (token
->ident
== &goto_ident
) {
1025 stmt
->type
= STMT_GOTO
;
1026 token
= token
->next
;
1027 if (match_op(token
, '*')) {
1028 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1029 add_statement(&function_computed_goto_list
, stmt
);
1030 } else if (token_type(token
) == TOKEN_IDENT
) {
1031 stmt
->goto_label
= label_symbol(token
);
1032 token
= token
->next
;
1034 warn(token
->pos
, "Expected identifier or goto expression");
1036 return expect(token
, ';', "at end of statement");
1038 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1039 return parse_asm(token
->next
, stmt
);
1041 if (match_op(token
->next
, ':')) {
1042 stmt
->type
= STMT_LABEL
;
1043 stmt
->label_identifier
= label_symbol(token
);
1044 return statement(token
->next
->next
, &stmt
->label_statement
);
1048 if (match_op(token
, '{')) {
1049 stmt
->type
= STMT_COMPOUND
;
1050 start_symbol_scope();
1051 token
= compound_statement(token
->next
, stmt
);
1054 return expect(token
, '}', "at end of compound statement");
1057 stmt
->type
= STMT_EXPRESSION
;
1058 return expression_statement(token
, &stmt
->expression
);
1061 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1064 struct statement
* stmt
;
1065 if (eof_token(token
))
1067 if (match_op(token
, '}'))
1069 token
= statement(token
, &stmt
);
1070 add_statement(list
, stmt
);
1075 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1077 struct symbol_list
**list
= &fn
->arguments
;
1079 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1081 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1083 token
= token
->next
;
1087 if (!lookup_type(token
)) {
1088 warn(token
->pos
, "non-ANSI parameter list");
1091 token
= parameter_declaration(token
, &sym
);
1092 /* Special case: (void) */
1093 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1095 add_symbol(list
, sym
);
1096 if (!match_op(token
, ','))
1098 token
= token
->next
;
1104 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1106 while (!eof_token(token
)) {
1107 if (!lookup_type(token
))
1109 token
= external_declaration(token
, &stmt
->syms
);
1111 token
= statement_list(token
, &stmt
->stmts
);
1115 static struct expression
*identifier_expression(struct token
*token
)
1117 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1118 expr
->expr_ident
= token
->ident
;
1122 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1124 int idx_from
, idx_to
;
1125 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1127 idx_from
= get_expression_value(from
);
1130 idx_to
= get_expression_value(to
);
1131 if (idx_to
< idx_from
|| idx_from
< 0)
1132 warn(from
->pos
, "nonsense array initializer index range");
1134 expr
->idx_from
= idx_from
;
1135 expr
->idx_to
= idx_to
;
1139 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1142 struct token
*next
= token
->next
;
1143 struct expression
*expr
;
1145 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1146 add_expression(list
, identifier_expression(next
));
1147 token
= next
->next
->next
;
1148 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1149 add_expression(list
, identifier_expression(token
));
1151 } else if (match_op(token
, '[')) {
1152 struct expression
*from
= NULL
, *to
= NULL
;
1153 token
= constant_expression(token
->next
, &from
);
1154 if (match_op(token
, SPECIAL_ELLIPSIS
))
1155 token
= constant_expression(token
->next
, &to
);
1156 add_expression(list
, index_expression(from
, to
));
1157 token
= expect(token
, ']', "at end of initializer index");
1158 token
= expect(token
, '=', "at end of initializer index");
1162 token
= initializer(&expr
, token
);
1165 add_expression(list
, expr
);
1166 if (!match_op(token
, ','))
1168 token
= token
->next
;
1173 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1175 if (match_op(token
, '{')) {
1176 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1178 token
= initializer_list(&expr
->expr_list
, token
->next
);
1179 return expect(token
, '}', "at end of initializer");
1181 return assignment_expression(token
, tree
);
1184 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1187 warn(sym
->pos
, "no identifier for function argument");
1190 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1193 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1194 struct symbol_list
**list
)
1196 struct symbol
*base_type
= decl
->ctype
.base_type
;
1197 struct statement
*stmt
;
1200 function_symbol_list
= &decl
->symbol_list
;
1201 function_computed_target_list
= NULL
;
1202 function_computed_goto_list
= NULL
;
1204 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1205 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1206 warn(decl
->pos
, "function with external linkage has definition");
1208 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1209 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1211 stmt
= start_function(decl
);
1213 base_type
->stmt
= stmt
;
1214 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1215 declare_argument(arg
, base_type
);
1218 token
= compound_statement(token
->next
, stmt
);
1221 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1222 add_symbol(list
, decl
);
1223 check_declaration(decl
);
1224 function_symbol_list
= NULL
;
1225 if (function_computed_goto_list
) {
1226 if (!function_computed_target_list
)
1227 warn(decl
->pos
, "function has computed goto but no targets?");
1229 struct statement
*stmt
;
1230 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1231 stmt
->target_list
= function_computed_target_list
;
1235 return expect(token
, '}', "at end of function");
1238 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1240 struct ident
*ident
= NULL
;
1241 struct symbol
*decl
;
1242 struct ctype ctype
= { 0, };
1243 struct symbol
*base_type
;
1246 /* Top-level inline asm? */
1247 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
)) {
1248 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1249 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1250 struct statement
*stmt
;
1252 anon
->ctype
.base_type
= fn
;
1253 function_symbol_list
= &anon
->symbol_list
;
1254 stmt
= start_function(anon
);
1255 token
= parse_asm(token
->next
, stmt
);
1257 function_symbol_list
= NULL
;
1258 add_symbol(list
, anon
);
1262 /* Parse declaration-specifiers, if any */
1263 token
= declaration_specifiers(token
, &ctype
, 0);
1264 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1265 decl
->ctype
= ctype
;
1266 token
= declarator(token
, &decl
, &ident
);
1268 /* Just a type declaration? */
1270 return expect(token
, ';', "end of type declaration");
1272 decl
->ident
= ident
;
1274 /* type define declaration? */
1275 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1277 /* Typedef's don't have meaningful storage */
1279 ctype
.modifiers
&= ~MOD_STORAGE
;
1280 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1281 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1284 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1286 base_type
= decl
->ctype
.base_type
;
1287 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1288 if (match_op(token
, '{'))
1289 return parse_function_body(token
, decl
, list
);
1291 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1292 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1296 if (token_type(token
) == TOKEN_IDENT
) {
1297 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1298 struct expression
*expr
;
1300 token
= expect(token
->next
, '(', "after asm");
1301 token
= parse_expression(token
->next
, &expr
);
1302 token
= expect(token
, ')', "after asm");
1305 if (!is_typedef
&& match_op(token
, '=')) {
1306 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1307 warn(decl
->pos
, "symbol with external linkage has initializer");
1308 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1310 token
= initializer(&decl
->initializer
, token
->next
);
1313 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1314 add_symbol(list
, decl
);
1315 if (function_symbol_list
)
1316 fn_local_symbol(decl
);
1319 check_declaration(decl
);
1321 if (!match_op(token
, ','))
1324 token
= token
->next
;
1326 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1327 decl
->ctype
= ctype
;
1328 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1329 token
= declarator(token
, &decl
, &ident
);
1331 warn(token
->pos
, "expected identifier name in type definition");
1335 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1337 /* Function declarations are automatically extern unless specifically static */
1338 base_type
= decl
->ctype
.base_type
;
1339 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1340 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1341 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1344 return expect(token
, ';', "at end of declaration");
1347 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1349 while (!eof_token(token
))
1350 token
= external_declaration(token
, list
);
1351 // They aren't needed any more
1352 clear_token_alloc();