2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
9 * Licensed under the Open Software License version 1.1
25 #include "expression.h"
28 static struct symbol_list
**function_symbol_list
;
30 // Add a symbol to the list of function-local symbols
31 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
33 static struct token
*statement(struct token
*token
, struct statement
**tree
);
34 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
);
36 static int match_idents(struct token
*token
, ...)
40 if (token_type(token
) != TOKEN_IDENT
)
43 va_start(args
, token
);
45 struct ident
* next
= va_arg(args
, struct ident
*);
48 if (token
->ident
== next
)
54 struct statement
*alloc_statement(struct position pos
, int type
)
56 struct statement
*stmt
= __alloc_statement(0);
62 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
64 static struct symbol
* indirect(struct position pos
, struct ctype
*ctype
, int type
)
66 struct symbol
*sym
= alloc_symbol(pos
, type
);
68 sym
->ctype
.base_type
= ctype
->base_type
;
69 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
71 ctype
->base_type
= sym
;
72 ctype
->modifiers
&= MOD_STORAGE
;
76 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
78 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
80 sym
= alloc_symbol(token
->pos
, type
);
81 sym
->ident
= token
->ident
;
82 bind_symbol(sym
, token
->ident
, ns
);
83 if (type
== SYM_LABEL
)
90 * NOTE! NS_LABEL is not just a different namespace,
91 * it also ends up using function scope instead of the
92 * regular symbol scope.
94 struct symbol
*label_symbol(struct token
*token
)
96 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
99 struct token
*struct_union_enum_specifier(enum namespace ns
, enum type type
,
100 struct token
*token
, struct ctype
*ctype
,
101 struct token
*(*parse
)(struct token
*, struct symbol
*))
105 ctype
->modifiers
= 0;
106 if (token_type(token
) == TOKEN_IDENT
) {
107 sym
= lookup_or_create_symbol(ns
, type
, token
);
109 ctype
->base_type
= sym
;
110 if (match_op(token
, '{')) {
111 token
= parse(token
->next
, sym
);
112 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
117 // private struct/union/enum type
118 if (!match_op(token
, '{')) {
119 warn(token
->pos
, "expected declaration");
120 ctype
->base_type
= &bad_type
;
124 sym
= alloc_symbol(token
->pos
, type
);
125 token
= parse(token
->next
, sym
);
126 ctype
->base_type
= sym
;
127 return expect(token
, '}', "at end of specifier");
130 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
132 return struct_declaration_list(token
, &sym
->symbol_list
);
135 struct token
*struct_or_union_specifier(enum type type
, struct token
*token
, struct ctype
*ctype
)
137 return struct_union_enum_specifier(NS_STRUCT
, type
, token
, ctype
, parse_struct_declaration
);
140 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
143 while (token_type(token
) == TOKEN_IDENT
) {
144 struct token
*next
= token
->next
;
147 sym
= alloc_symbol(token
->pos
, SYM_ENUM
);
148 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
149 sym
->ctype
.base_type
= parent
;
151 if (match_op(next
, '=')) {
152 struct expression
*expr
;
153 next
= constant_expression(next
->next
, &expr
);
154 nextval
= get_expression_value(expr
);
156 sym
->value
= nextval
;
159 if (!match_op(token
, ','))
162 nextval
= nextval
+ 1;
167 struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
169 return struct_union_enum_specifier(NS_ENUM
, SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
172 struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
176 if (!match_op(token
, '(')) {
177 warn(token
->pos
, "expected '(' after typeof");
180 if (lookup_type(token
->next
)) {
181 token
= typename(token
->next
, &sym
);
184 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
185 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
187 ctype
->modifiers
= 0;
188 ctype
->base_type
= typeof_sym
;
190 return expect(token
, ')', "after typeof");
193 static const char * handle_attribute(struct ctype
*ctype
, struct ident
*attribute
, struct expression
*expr
)
195 if (match_string_ident(attribute
, "packed") ||
196 match_string_ident(attribute
, "__packed__")) {
197 ctype
->alignment
= 1;
200 if (match_string_ident(attribute
, "aligned") ||
201 match_string_ident(attribute
, "__aligned__")) {
202 int alignment
= max_alignment
;
204 alignment
= get_expression_value(expr
);
205 ctype
->alignment
= alignment
;
208 if (match_string_ident(attribute
, "nocast")) {
209 ctype
->modifiers
|= MOD_NOCAST
;
212 if (match_string_ident(attribute
, "noderef")) {
213 ctype
->modifiers
|= MOD_NODEREF
;
216 if (match_string_ident(attribute
, "safe")) {
217 ctype
->modifiers
|= MOD_SAFE
;
220 if (match_string_ident(attribute
, "force")) {
221 ctype
->modifiers
|= MOD_FORCE
;
224 if (match_string_ident(attribute
, "address_space")) {
226 return "expected address space number";
227 ctype
->as
= get_expression_value(expr
);
230 if (match_string_ident(attribute
, "context")) {
231 if (expr
&& expr
->type
== EXPR_COMMA
) {
232 int mask
= get_expression_value(expr
->left
);
233 int value
= get_expression_value(expr
->right
);
235 return "nonsense attribute types";
236 ctype
->contextmask
|= mask
;
237 ctype
->context
|= value
;
240 return "expected context mask and value";
242 if (match_string_ident(attribute
, "mode") ||
243 match_string_ident(attribute
, "__mode__")) {
244 if (expr
&& expr
->type
== EXPR_SYMBOL
) {
245 struct ident
*ident
= expr
->symbol_name
;
248 * Match against __QI__/__HI__/__SI__/__DI__
250 * FIXME! This is broken - we don't actually get
251 * the type information updated properly at this
252 * stage for some reason.
254 if (match_string_ident(ident
, "__QI__")) {
255 ctype
->modifiers
|= MOD_SHORT
;
256 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
259 if (match_string_ident(ident
, "__HI__")) {
260 ctype
->modifiers
|= MOD_SHORT
;
261 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
264 if (match_string_ident(ident
, "__SI__")) {
268 if (match_string_ident(ident
, "__DI__")) {
269 ctype
->modifiers
|= MOD_LONGLONG
;
270 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
273 return "unknown mode attribute";
275 return "expected attribute mode symbol";
278 /* Throw away for now.. */
279 if (match_string_ident(attribute
, "format") ||
280 match_string_ident(attribute
, "__format__"))
282 if (match_string_ident(attribute
, "section") ||
283 match_string_ident(attribute
, "__section__"))
285 if (match_string_ident(attribute
, "unused") ||
286 match_string_ident(attribute
, "__unused__"))
288 if (match_string_ident(attribute
, "const") ||
289 match_string_ident(attribute
, "__const__"))
291 if (match_string_ident(attribute
, "noreturn"))
293 if (match_string_ident(attribute
, "regparm"))
295 if (match_string_ident(attribute
, "weak"))
297 if (match_string_ident(attribute
, "alias"))
299 if (match_string_ident(attribute
, "pure"))
301 if (match_string_ident(attribute
, "always_inline"))
304 return "unknown attribute";
307 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
309 ctype
->modifiers
= 0;
310 token
= expect(token
, '(', "after attribute");
311 token
= expect(token
, '(', "after attribute");
315 struct ident
*attribute_name
;
316 struct expression
*attribute_expr
;
318 if (eof_token(token
))
320 if (match_op(token
, ';'))
322 if (token_type(token
) != TOKEN_IDENT
)
324 attribute_name
= token
->ident
;
326 attribute_expr
= NULL
;
327 if (match_op(token
, '('))
328 token
= parens_expression(token
, &attribute_expr
, "in attribute");
329 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
331 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
332 if (!match_op(token
, ','))
337 token
= expect(token
, ')', "after attribute");
338 token
= expect(token
, ')', "after attribute");
342 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
343 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
345 struct symbol
* ctype_integer(unsigned int spec
)
347 static struct symbol
*const integer_ctypes
[][2] = {
348 { &llong_ctype
, &ullong_ctype
},
349 { &long_ctype
, &ulong_ctype
},
350 { &short_ctype
, &ushort_ctype
},
351 { &char_ctype
, &uchar_ctype
},
352 { &int_ctype
, &uint_ctype
},
354 struct symbol
*const (*ctype
)[2];
356 ctype
= integer_ctypes
;
357 if (!(spec
& MOD_LONGLONG
)) {
359 if (!(spec
& MOD_LONG
)) {
361 if (!(spec
& MOD_SHORT
)) {
363 if (!(spec
& MOD_CHAR
))
368 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
371 struct symbol
* ctype_fp(unsigned int spec
)
373 if (spec
& MOD_LONGLONG
)
374 return &ldouble_ctype
;
376 return &double_ctype
;
380 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
382 unsigned long mod
= thistype
->modifiers
;
385 unsigned long old
= ctype
->modifiers
;
386 unsigned long extra
= 0, dup
;
388 if (mod
& old
& MOD_LONG
) {
389 extra
= MOD_LONGLONG
| MOD_LONG
;
393 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
395 warn(pos
, "Just how %sdo you want this type to be?",
396 modifier_string(dup
));
397 ctype
->modifiers
= old
| mod
| extra
;
400 /* Context mask and value */
401 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
402 warn(pos
, "inconsistend attribute types");
403 thistype
->context
= 0;
404 thistype
->contextmask
= 0;
406 ctype
->context
|= thistype
->context
;
407 ctype
->contextmask
|= thistype
->contextmask
;
410 if (thistype
->alignment
& (thistype
->alignment
-1)) {
411 warn(pos
, "I don't like non-power-of-2 alignments");
412 thistype
->alignment
= 0;
414 if (thistype
->alignment
> ctype
->alignment
)
415 ctype
->alignment
= thistype
->alignment
;
418 ctype
->as
= thistype
->as
;
422 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
426 while ( (token
= next
) != NULL
) {
427 struct ctype thistype
;
429 struct symbol
*s
, *type
;
433 if (token_type(token
) != TOKEN_IDENT
)
435 ident
= token
->ident
;
437 s
= lookup_symbol(ident
, NS_TYPEDEF
);
441 mod
= thistype
.modifiers
;
442 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
444 if (mod
& MOD_SPECIALBITS
) {
445 if (mod
& MOD_STRUCTOF
)
446 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
447 else if (mod
& MOD_UNIONOF
)
448 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
449 else if (mod
& MOD_ENUMOF
)
450 next
= enum_specifier(next
, &thistype
);
451 else if (mod
& MOD_ATTRIBUTE
)
452 next
= attribute_specifier(next
, &thistype
);
453 else if (mod
& MOD_TYPEOF
)
454 next
= typeof_specifier(next
, &thistype
);
455 mod
= thistype
.modifiers
;
457 type
= thistype
.base_type
;
461 if (ctype
->base_type
)
463 /* User types only mix with qualifiers */
464 if (mod
& MOD_USERTYPE
) {
465 if (ctype
->modifiers
& MOD_SPECIFIER
)
468 ctype
->base_type
= type
;
471 apply_ctype(token
->pos
, &thistype
, ctype
);
474 /* Turn the "virtual types" into real types with real sizes etc */
475 if (!ctype
->base_type
&& (ctype
->modifiers
& MOD_SPECIFIER
))
476 ctype
->base_type
= &int_type
;
478 if (ctype
->base_type
== &int_type
) {
479 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
480 ctype
->modifiers
&= ~MOD_SPECIFIER
;
483 if (ctype
->base_type
== &fp_type
) {
484 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
485 ctype
->modifiers
&= ~MOD_SPECIFIER
;
491 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
493 struct expression
*expr
= NULL
;
495 token
= parse_expression(token
, &expr
);
496 sym
->array_size
= expr
;
500 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
501 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
503 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
505 struct ctype
*ctype
= &(*tree
)->ctype
;
507 if (p
&& token_type(token
) == TOKEN_IDENT
) {
513 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
514 struct ctype thistype
= { 0, };
515 token
= attribute_specifier(token
->next
, &thistype
);
516 apply_ctype(token
->pos
, &thistype
, ctype
);
519 if (token_type(token
) != TOKEN_SPECIAL
)
523 * This can be either a parameter list or a grouping.
524 * For the direct (non-abstract) case, we know if must be
525 * a paramter list if we already saw the identifier.
526 * For the abstract case, we know if must be a parameter
527 * list if it is empty or starts with a type.
529 if (token
->special
== '(') {
531 struct token
*next
= token
->next
;
532 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
535 struct symbol
*base_type
= ctype
->base_type
;
536 token
= declarator(next
, tree
, p
);
537 token
= expect(token
, ')', "in nested declarator");
538 while (ctype
->base_type
!= base_type
)
539 ctype
= &ctype
->base_type
->ctype
;
544 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
545 token
= parameter_type_list(next
, sym
);
546 token
= expect(token
, ')', "in function declarator");
549 if (token
->special
== '[') {
550 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
551 token
= abstract_array_declarator(token
->next
, array
);
552 token
= expect(token
, ']', "in abstract_array_declarator");
553 ctype
= &array
->ctype
;
556 if (token
->special
== ':') {
557 struct symbol
*bitfield
;
558 struct expression
*expr
;
559 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
560 token
= conditional_expression(token
->next
, &expr
);
561 bitfield
->fieldwidth
= get_expression_value(expr
);
572 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
574 unsigned long modifiers
;
575 struct symbol
*base_type
;
577 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
578 base_type
= ctype
->base_type
;
579 ctype
->modifiers
= modifiers
;
581 while (match_op(token
,'*')) {
582 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
583 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
584 ptr
->ctype
.as
= ctype
->as
;
585 ptr
->ctype
.context
= ctype
->context
;
586 ptr
->ctype
.contextmask
= ctype
->contextmask
;
587 ptr
->ctype
.base_type
= base_type
;
590 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
591 ctype
->base_type
= base_type
;
594 ctype
->contextmask
= 0;
596 token
= declaration_specifiers(token
->next
, ctype
, 1);
597 modifiers
= ctype
->modifiers
;
602 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
604 token
= pointer(token
, &(*tree
)->ctype
);
605 return direct_declarator(token
, tree
, p
);
608 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
610 while (!match_op(token
, '}')) {
611 struct ctype ctype
= {0, };
613 token
= declaration_specifiers(token
, &ctype
, 0);
615 struct ident
*ident
= NULL
;
616 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
618 token
= declarator(token
, &decl
, &ident
);
619 if (match_op(token
, ':')) {
620 struct expression
*expr
;
621 token
= parse_expression(token
->next
, &expr
);
623 add_symbol(list
, decl
);
624 if (!match_op(token
, ','))
628 if (!match_op(token
, ';'))
635 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
637 struct ident
*ident
= NULL
;
639 struct ctype ctype
= { 0, };
641 token
= declaration_specifiers(token
, &ctype
, 0);
642 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
645 token
= declarator(token
, tree
, &ident
);
649 struct token
*typename(struct token
*token
, struct symbol
**p
)
651 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
653 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
654 return declarator(token
, &sym
, NULL
);
657 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
659 token
= parse_expression(token
, tree
);
660 return expect(token
, ';', "at end of statement");
663 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
665 struct expression
*expr
;
667 /* Allow empty operands */
668 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
671 token
= primary_expression(token
->next
, &expr
);
672 token
= parens_expression(token
, &expr
, "in asm parameter");
673 } while (match_op(token
, ','));
677 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
679 struct expression
*expr
;
682 token
= primary_expression(token
->next
, &expr
);
683 } while (match_op(token
, ','));
687 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
689 struct expression
*expr
;
691 stmt
->type
= STMT_ASM
;
692 if (match_idents(token
, &__volatile___ident
, &volatile_ident
)) {
695 token
= expect(token
, '(', "after asm");
696 token
= parse_expression(token
->next
, &expr
);
697 if (match_op(token
, ':'))
698 token
= parse_asm_operands(token
, stmt
);
699 if (match_op(token
, ':'))
700 token
= parse_asm_operands(token
, stmt
);
701 if (match_op(token
, ':'))
702 token
= parse_asm_clobbers(token
, stmt
);
703 token
= expect(token
, ')', "after asm");
704 return expect(token
, ';', "at end of asm-statement");
707 /* Make a statement out of an expression */
708 static struct statement
*make_statement(struct expression
*expr
)
710 struct statement
*stmt
;
714 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
715 stmt
->expression
= expr
;
720 * All iterators have two symbols associated with them:
721 * the "continue" and "break" symbols, which are targets
722 * for continue and break statements respectively.
724 * They are in a special name-space, but they follow
725 * all the normal visibility rules, so nested iterators
726 * automatically work right.
728 static void start_iterator(struct statement
*stmt
)
730 struct symbol
*cont
, *brk
;
732 start_symbol_scope();
733 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
734 cont
->ident
= &continue_ident
;
735 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
736 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
737 brk
->ident
= &break_ident
;
738 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
740 stmt
->type
= STMT_ITERATOR
;
741 stmt
->iterator_break
= brk
;
742 stmt
->iterator_continue
= cont
;
743 fn_local_symbol(brk
);
744 fn_local_symbol(cont
);
747 static void end_iterator(struct statement
*stmt
)
752 static struct statement
*start_function(struct symbol
*sym
)
755 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
757 start_function_scope();
758 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
759 ret
->ident
= &return_ident
;
760 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
761 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
762 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
763 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
766 fn_local_symbol(ret
);
770 static void end_function(struct symbol
*sym
)
772 end_function_scope();
776 * A "switch()" statement, like an iterator, has a
777 * the "break" symbol associated with it. It works
778 * exactly like the iterator break - it's the target
779 * for any break-statements in scope, and means that
780 * "break" handling doesn't even need to know whether
781 * it's breaking out of an iterator or a switch.
783 * In addition, the "case" symbol is a marker for the
784 * case/default statements to find the switch statement
785 * that they are associated with.
787 static void start_switch(struct statement
*stmt
)
789 struct symbol
*brk
, *switch_case
;
791 start_symbol_scope();
792 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
793 brk
->ident
= &break_ident
;
794 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
796 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
797 switch_case
->ident
= &case_ident
;
798 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
799 switch_case
->stmt
= stmt
;
801 stmt
->type
= STMT_SWITCH
;
802 stmt
->switch_break
= brk
;
803 stmt
->switch_case
= switch_case
;
805 fn_local_symbol(brk
);
806 fn_local_symbol(switch_case
);
809 static void end_switch(struct statement
*stmt
)
811 if (!stmt
->switch_case
->symbol_list
)
812 warn(stmt
->pos
, "switch with no cases");
816 static void add_case_statement(struct statement
*stmt
)
818 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
822 warn(stmt
->pos
, "not in switch scope");
825 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
826 add_symbol(&target
->symbol_list
, sym
);
828 stmt
->case_label
= sym
;
829 fn_local_symbol(sym
);
832 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
834 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
837 error(token
->pos
, "internal error: return without a function target");
838 stmt
->type
= STMT_RETURN
;
839 stmt
->ret_target
= target
;
840 return expression_statement(token
->next
, &stmt
->ret_value
);
843 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
845 struct symbol_list
*syms
;
846 struct expression
*e1
, *e2
, *e3
;
847 struct statement
*iterator
;
849 start_iterator(stmt
);
850 token
= expect(token
->next
, '(', "after 'for'");
854 /* C99 variable declaration? */
855 if (lookup_type(token
)) {
856 token
= external_declaration(token
, &syms
);
858 token
= parse_expression(token
, &e1
);
859 token
= expect(token
, ';', "in 'for'");
861 token
= parse_expression(token
, &e2
);
862 token
= expect(token
, ';', "in 'for'");
863 token
= parse_expression(token
, &e3
);
864 token
= expect(token
, ')', "in 'for'");
865 token
= statement(token
, &iterator
);
867 stmt
->iterator_syms
= syms
;
868 stmt
->iterator_pre_statement
= make_statement(e1
);
869 stmt
->iterator_pre_condition
= e2
;
870 stmt
->iterator_post_statement
= make_statement(e3
);
871 stmt
->iterator_post_condition
= e2
;
872 stmt
->iterator_statement
= iterator
;
878 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
880 struct expression
*expr
;
881 struct statement
*iterator
;
883 start_iterator(stmt
);
884 token
= parens_expression(token
->next
, &expr
, "after 'while'");
885 token
= statement(token
, &iterator
);
887 stmt
->iterator_pre_condition
= expr
;
888 stmt
->iterator_post_condition
= expr
;
889 stmt
->iterator_statement
= iterator
;
895 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
897 struct expression
*expr
;
898 struct statement
*iterator
;
900 start_iterator(stmt
);
901 token
= statement(token
->next
, &iterator
);
902 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
905 warn(token
->pos
, "expected 'while' after 'do'");
906 token
= parens_expression(token
, &expr
, "after 'do-while'");
908 stmt
->iterator_post_condition
= expr
;
909 stmt
->iterator_statement
= iterator
;
912 return expect(token
, ';', "after statement");
915 static struct token
*statement(struct token
*token
, struct statement
**tree
)
917 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
920 if (token_type(token
) == TOKEN_IDENT
) {
921 if (token
->ident
== &if_ident
) {
922 stmt
->type
= STMT_IF
;
923 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
924 token
= statement(token
, &stmt
->if_true
);
925 if (token_type(token
) != TOKEN_IDENT
)
927 if (token
->ident
!= &else_ident
)
929 return statement(token
->next
, &stmt
->if_false
);
932 if (token
->ident
== &return_ident
)
933 return parse_return_statement(token
, stmt
);
935 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
936 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
937 stmt
->type
= STMT_GOTO
;
938 stmt
->goto_label
= target
;
940 warn(stmt
->pos
, "break/continue not in iterator scope");
941 return expect(token
->next
, ';', "at end of statement");
943 if (token
->ident
== &default_ident
) {
945 goto default_statement
;
947 if (token
->ident
== &case_ident
) {
948 token
= parse_expression(token
->next
, &stmt
->case_expression
);
949 if (match_op(token
, SPECIAL_ELLIPSIS
))
950 token
= parse_expression(token
->next
, &stmt
->case_to
);
952 stmt
->type
= STMT_CASE
;
953 token
= expect(token
, ':', "after default/case");
954 add_case_statement(stmt
);
955 return statement(token
, &stmt
->case_statement
);
957 if (token
->ident
== &switch_ident
) {
958 stmt
->type
= STMT_SWITCH
;
960 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
961 token
= statement(token
, &stmt
->switch_statement
);
965 if (token
->ident
== &for_ident
)
966 return parse_for_statement(token
, stmt
);
968 if (token
->ident
== &while_ident
)
969 return parse_while_statement(token
, stmt
);
971 if (token
->ident
== &do_ident
)
972 return parse_do_statement(token
, stmt
);
974 if (token
->ident
== &goto_ident
) {
975 stmt
->type
= STMT_GOTO
;
977 if (match_op(token
, '*')) {
978 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
979 } else if (token_type(token
) == TOKEN_IDENT
) {
980 stmt
->goto_label
= label_symbol(token
);
983 warn(token
->pos
, "Expected identifier or goto expression");
985 return expect(token
, ';', "at end of statement");
987 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
988 return parse_asm(token
->next
, stmt
);
990 if (match_op(token
->next
, ':')) {
991 stmt
->type
= STMT_LABEL
;
992 stmt
->label_identifier
= label_symbol(token
);
993 return statement(token
->next
->next
, &stmt
->label_statement
);
997 if (match_op(token
, '{')) {
998 stmt
->type
= STMT_COMPOUND
;
999 start_symbol_scope();
1000 token
= compound_statement(token
->next
, stmt
);
1003 return expect(token
, '}', "at end of compound statement");
1006 stmt
->type
= STMT_EXPRESSION
;
1007 return expression_statement(token
, &stmt
->expression
);
1010 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1013 struct statement
* stmt
;
1014 if (eof_token(token
))
1016 if (match_op(token
, '}'))
1018 token
= statement(token
, &stmt
);
1019 add_statement(list
, stmt
);
1024 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1026 struct symbol_list
**list
= &fn
->arguments
;
1028 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1030 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1032 token
= token
->next
;
1036 if (!lookup_type(token
)) {
1037 warn(token
->pos
, "non-ANSI parameter list");
1040 token
= parameter_declaration(token
, &sym
);
1041 /* Special case: (void) */
1042 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1044 add_symbol(list
, sym
);
1045 if (!match_op(token
, ','))
1047 token
= token
->next
;
1053 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1055 while (!eof_token(token
)) {
1056 if (!lookup_type(token
))
1058 token
= external_declaration(token
, &stmt
->syms
);
1060 token
= statement_list(token
, &stmt
->stmts
);
1064 static struct expression
*identifier_expression(struct token
*token
)
1066 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1067 expr
->expr_ident
= token
->ident
;
1071 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1073 int idx_from
, idx_to
;
1074 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1076 idx_from
= get_expression_value(from
);
1079 idx_to
= get_expression_value(to
);
1080 if (idx_to
< idx_from
|| idx_from
< 0)
1081 warn(from
->pos
, "nonsense array initializer index range");
1083 expr
->idx_from
= idx_from
;
1084 expr
->idx_to
= idx_to
;
1088 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1091 struct token
*next
= token
->next
;
1092 struct expression
*expr
;
1094 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1095 add_expression(list
, identifier_expression(next
));
1096 token
= next
->next
->next
;
1097 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1098 add_expression(list
, identifier_expression(token
));
1100 } else if (match_op(token
, '[')) {
1101 struct expression
*from
= NULL
, *to
= NULL
;
1102 token
= constant_expression(token
->next
, &from
);
1103 if (match_op(token
, SPECIAL_ELLIPSIS
))
1104 token
= constant_expression(token
->next
, &to
);
1105 add_expression(list
, index_expression(from
, to
));
1106 token
= expect(token
, ']', "at end of initializer index");
1107 token
= expect(token
, '=', "at end of initializer index");
1111 token
= initializer(&expr
, token
);
1114 add_expression(list
, expr
);
1115 if (!match_op(token
, ','))
1117 token
= token
->next
;
1122 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1124 if (match_op(token
, '{')) {
1125 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1127 token
= initializer_list(&expr
->expr_list
, token
->next
);
1128 return expect(token
, '}', "at end of initializer");
1130 return assignment_expression(token
, tree
);
1133 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1136 warn(sym
->pos
, "no identifier for function argument");
1139 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1142 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1143 struct symbol_list
**list
)
1145 struct symbol
*base_type
= decl
->ctype
.base_type
;
1146 struct statement
*stmt
;
1149 function_symbol_list
= &decl
->symbol_list
;
1150 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1151 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1152 warn(decl
->pos
, "function with external linkage has definition");
1154 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1155 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1157 stmt
= start_function(decl
);
1159 base_type
->stmt
= stmt
;
1160 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1161 declare_argument(arg
, base_type
);
1164 token
= compound_statement(token
->next
, stmt
);
1167 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1168 add_symbol(list
, decl
);
1169 check_declaration(decl
);
1170 function_symbol_list
= NULL
;
1171 return expect(token
, '}', "at end of function");
1174 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1176 struct ident
*ident
= NULL
;
1177 struct symbol
*decl
;
1178 struct ctype ctype
= { 0, };
1179 struct symbol
*base_type
;
1182 /* Top-level inline asm? */
1183 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
)) {
1184 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1185 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1186 struct statement
*stmt
;
1188 anon
->ctype
.base_type
= fn
;
1189 function_symbol_list
= &anon
->symbol_list
;
1190 stmt
= start_function(anon
);
1191 token
= parse_asm(token
->next
, stmt
);
1193 function_symbol_list
= NULL
;
1194 add_symbol(list
, anon
);
1198 /* Parse declaration-specifiers, if any */
1199 token
= declaration_specifiers(token
, &ctype
, 0);
1200 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1201 decl
->ctype
= ctype
;
1202 token
= declarator(token
, &decl
, &ident
);
1204 /* Just a type declaration? */
1206 return expect(token
, ';', "end of type declaration");
1208 decl
->ident
= ident
;
1210 /* type define declaration? */
1211 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1213 /* Typedef's don't have meaningful storage */
1215 ctype
.modifiers
&= ~MOD_STORAGE
;
1216 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1217 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1220 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1222 base_type
= decl
->ctype
.base_type
;
1223 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1224 if (match_op(token
, '{'))
1225 return parse_function_body(token
, decl
, list
);
1227 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1228 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1232 if (token_type(token
) == TOKEN_IDENT
) {
1233 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1234 struct expression
*expr
;
1236 token
= expect(token
->next
, '(', "after asm");
1237 token
= parse_expression(token
->next
, &expr
);
1238 token
= expect(token
, ')', "after asm");
1241 if (!is_typedef
&& match_op(token
, '=')) {
1242 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1243 warn(decl
->pos
, "symbol with external linkage has initializer");
1244 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1246 token
= initializer(&decl
->initializer
, token
->next
);
1249 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1250 add_symbol(list
, decl
);
1251 if (function_symbol_list
)
1252 fn_local_symbol(decl
);
1255 check_declaration(decl
);
1257 if (!match_op(token
, ','))
1260 token
= token
->next
;
1262 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1263 decl
->ctype
= ctype
;
1264 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1265 token
= declarator(token
, &decl
, &ident
);
1267 warn(token
->pos
, "expected identifier name in type definition");
1271 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1273 /* Function declarations are automatically extern unless specifically static */
1274 base_type
= decl
->ctype
.base_type
;
1275 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1276 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1277 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1280 return expect(token
, ';', "at end of declaration");
1283 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1285 while (!eof_token(token
))
1286 token
= external_declaration(token
, list
);
1287 // They aren't needed any more
1288 clear_token_alloc();