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 match_string_ident(ident
, "QI")) {
256 ctype
->modifiers
|= MOD_SHORT
;
257 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
260 if (match_string_ident(ident
, "__HI__") ||
261 match_string_ident(ident
, "HI")) {
262 ctype
->modifiers
|= MOD_SHORT
;
263 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
266 if (match_string_ident(ident
, "__SI__") ||
267 match_string_ident(ident
, "SI")) {
271 if (match_string_ident(ident
, "__DI__") ||
272 match_string_ident(ident
, "DI")) {
273 ctype
->modifiers
|= MOD_LONGLONG
;
274 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
277 return "unknown mode attribute";
279 return "expected attribute mode symbol";
282 /* Throw away for now.. */
283 if (match_string_ident(attribute
, "format") ||
284 match_string_ident(attribute
, "__format__"))
286 if (match_string_ident(attribute
, "section") ||
287 match_string_ident(attribute
, "__section__"))
289 if (match_string_ident(attribute
, "unused") ||
290 match_string_ident(attribute
, "__unused__"))
292 if (match_string_ident(attribute
, "const") ||
293 match_string_ident(attribute
, "__const__"))
295 if (match_string_ident(attribute
, "noreturn"))
297 if (match_string_ident(attribute
, "regparm"))
299 if (match_string_ident(attribute
, "weak"))
301 if (match_string_ident(attribute
, "alias"))
303 if (match_string_ident(attribute
, "pure"))
305 if (match_string_ident(attribute
, "always_inline"))
308 return "unknown attribute";
311 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
313 ctype
->modifiers
= 0;
314 token
= expect(token
, '(', "after attribute");
315 token
= expect(token
, '(', "after attribute");
319 struct ident
*attribute_name
;
320 struct expression
*attribute_expr
;
322 if (eof_token(token
))
324 if (match_op(token
, ';'))
326 if (token_type(token
) != TOKEN_IDENT
)
328 attribute_name
= token
->ident
;
330 attribute_expr
= NULL
;
331 if (match_op(token
, '('))
332 token
= parens_expression(token
, &attribute_expr
, "in attribute");
333 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
335 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
336 if (!match_op(token
, ','))
341 token
= expect(token
, ')', "after attribute");
342 token
= expect(token
, ')', "after attribute");
346 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
347 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
349 struct symbol
* ctype_integer(unsigned int spec
)
351 static struct symbol
*const integer_ctypes
[][2] = {
352 { &llong_ctype
, &ullong_ctype
},
353 { &long_ctype
, &ulong_ctype
},
354 { &short_ctype
, &ushort_ctype
},
355 { &char_ctype
, &uchar_ctype
},
356 { &int_ctype
, &uint_ctype
},
358 struct symbol
*const (*ctype
)[2];
360 ctype
= integer_ctypes
;
361 if (!(spec
& MOD_LONGLONG
)) {
363 if (!(spec
& MOD_LONG
)) {
365 if (!(spec
& MOD_SHORT
)) {
367 if (!(spec
& MOD_CHAR
))
372 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
375 struct symbol
* ctype_fp(unsigned int spec
)
377 if (spec
& MOD_LONGLONG
)
378 return &ldouble_ctype
;
380 return &double_ctype
;
384 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
386 unsigned long mod
= thistype
->modifiers
;
389 unsigned long old
= ctype
->modifiers
;
390 unsigned long extra
= 0, dup
;
392 if (mod
& old
& MOD_LONG
) {
393 extra
= MOD_LONGLONG
| MOD_LONG
;
397 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
399 warn(pos
, "Just how %sdo you want this type to be?",
400 modifier_string(dup
));
401 ctype
->modifiers
= old
| mod
| extra
;
404 /* Context mask and value */
405 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
406 warn(pos
, "inconsistend attribute types");
407 thistype
->context
= 0;
408 thistype
->contextmask
= 0;
410 ctype
->context
|= thistype
->context
;
411 ctype
->contextmask
|= thistype
->contextmask
;
414 if (thistype
->alignment
& (thistype
->alignment
-1)) {
415 warn(pos
, "I don't like non-power-of-2 alignments");
416 thistype
->alignment
= 0;
418 if (thistype
->alignment
> ctype
->alignment
)
419 ctype
->alignment
= thistype
->alignment
;
422 ctype
->as
= thistype
->as
;
426 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
430 while ( (token
= next
) != NULL
) {
431 struct ctype thistype
;
433 struct symbol
*s
, *type
;
437 if (token_type(token
) != TOKEN_IDENT
)
439 ident
= token
->ident
;
441 s
= lookup_symbol(ident
, NS_TYPEDEF
);
445 mod
= thistype
.modifiers
;
446 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
448 if (mod
& MOD_SPECIALBITS
) {
449 if (mod
& MOD_STRUCTOF
)
450 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
451 else if (mod
& MOD_UNIONOF
)
452 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
453 else if (mod
& MOD_ENUMOF
)
454 next
= enum_specifier(next
, &thistype
);
455 else if (mod
& MOD_ATTRIBUTE
)
456 next
= attribute_specifier(next
, &thistype
);
457 else if (mod
& MOD_TYPEOF
)
458 next
= typeof_specifier(next
, &thistype
);
459 mod
= thistype
.modifiers
;
461 type
= thistype
.base_type
;
465 if (ctype
->base_type
)
467 /* User types only mix with qualifiers */
468 if (mod
& MOD_USERTYPE
) {
469 if (ctype
->modifiers
& MOD_SPECIFIER
)
472 ctype
->base_type
= type
;
475 apply_ctype(token
->pos
, &thistype
, ctype
);
478 /* Turn the "virtual types" into real types with real sizes etc */
479 if (!ctype
->base_type
) {
480 struct symbol
*base
= &incomplete_ctype
;
483 * If we have modifiers, we'll default to an integer
484 * type, and "ctype_integer()" will turn this into
487 if (ctype
->modifiers
& MOD_SPECIFIER
)
489 ctype
->base_type
= base
;
492 if (ctype
->base_type
== &int_type
) {
493 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
494 ctype
->modifiers
&= ~MOD_SPECIFIER
;
497 if (ctype
->base_type
== &fp_type
) {
498 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
499 ctype
->modifiers
&= ~MOD_SPECIFIER
;
505 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
507 struct expression
*expr
= NULL
;
509 token
= parse_expression(token
, &expr
);
510 sym
->array_size
= expr
;
514 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
515 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
517 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
519 struct ctype
*ctype
= &(*tree
)->ctype
;
521 if (p
&& token_type(token
) == TOKEN_IDENT
) {
527 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
528 struct ctype thistype
= { 0, };
529 token
= attribute_specifier(token
->next
, &thistype
);
530 apply_ctype(token
->pos
, &thistype
, ctype
);
533 if (token_type(token
) != TOKEN_SPECIAL
)
537 * This can be either a parameter list or a grouping.
538 * For the direct (non-abstract) case, we know if must be
539 * a paramter list if we already saw the identifier.
540 * For the abstract case, we know if must be a parameter
541 * list if it is empty or starts with a type.
543 if (token
->special
== '(') {
545 struct token
*next
= token
->next
;
546 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
549 struct symbol
*base_type
= ctype
->base_type
;
550 token
= declarator(next
, tree
, p
);
551 token
= expect(token
, ')', "in nested declarator");
552 while (ctype
->base_type
!= base_type
)
553 ctype
= &ctype
->base_type
->ctype
;
558 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
559 token
= parameter_type_list(next
, sym
);
560 token
= expect(token
, ')', "in function declarator");
563 if (token
->special
== '[') {
564 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
565 token
= abstract_array_declarator(token
->next
, array
);
566 token
= expect(token
, ']', "in abstract_array_declarator");
567 ctype
= &array
->ctype
;
570 if (token
->special
== ':') {
571 struct symbol
*bitfield
;
572 struct expression
*expr
;
573 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
574 token
= conditional_expression(token
->next
, &expr
);
575 bitfield
->fieldwidth
= get_expression_value(expr
);
586 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
588 unsigned long modifiers
;
589 struct symbol
*base_type
;
591 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
592 base_type
= ctype
->base_type
;
593 ctype
->modifiers
= modifiers
;
595 while (match_op(token
,'*')) {
596 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
597 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
598 ptr
->ctype
.as
= ctype
->as
;
599 ptr
->ctype
.context
= ctype
->context
;
600 ptr
->ctype
.contextmask
= ctype
->contextmask
;
601 ptr
->ctype
.base_type
= base_type
;
604 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
605 ctype
->base_type
= base_type
;
608 ctype
->contextmask
= 0;
610 token
= declaration_specifiers(token
->next
, ctype
, 1);
611 modifiers
= ctype
->modifiers
;
616 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
618 token
= pointer(token
, &(*tree
)->ctype
);
619 return direct_declarator(token
, tree
, p
);
622 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
624 while (!match_op(token
, '}')) {
625 struct ctype ctype
= {0, };
627 token
= declaration_specifiers(token
, &ctype
, 0);
629 struct ident
*ident
= NULL
;
630 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
632 token
= declarator(token
, &decl
, &ident
);
633 if (match_op(token
, ':')) {
634 struct expression
*expr
;
635 token
= parse_expression(token
->next
, &expr
);
637 add_symbol(list
, decl
);
638 if (!match_op(token
, ','))
642 if (!match_op(token
, ';'))
649 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
651 struct ident
*ident
= NULL
;
653 struct ctype ctype
= { 0, };
655 token
= declaration_specifiers(token
, &ctype
, 0);
656 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
659 token
= declarator(token
, tree
, &ident
);
663 struct token
*typename(struct token
*token
, struct symbol
**p
)
665 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
667 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
668 return declarator(token
, &sym
, NULL
);
671 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
673 token
= parse_expression(token
, tree
);
674 return expect(token
, ';', "at end of statement");
677 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
679 struct expression
*expr
;
681 /* Allow empty operands */
682 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
685 token
= primary_expression(token
->next
, &expr
);
686 token
= parens_expression(token
, &expr
, "in asm parameter");
687 } while (match_op(token
, ','));
691 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
693 struct expression
*expr
;
696 token
= primary_expression(token
->next
, &expr
);
697 } while (match_op(token
, ','));
701 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
703 struct expression
*expr
;
705 stmt
->type
= STMT_ASM
;
706 if (match_idents(token
, &__volatile___ident
, &volatile_ident
)) {
709 token
= expect(token
, '(', "after asm");
710 token
= parse_expression(token
->next
, &expr
);
711 if (match_op(token
, ':'))
712 token
= parse_asm_operands(token
, stmt
);
713 if (match_op(token
, ':'))
714 token
= parse_asm_operands(token
, stmt
);
715 if (match_op(token
, ':'))
716 token
= parse_asm_clobbers(token
, stmt
);
717 token
= expect(token
, ')', "after asm");
718 return expect(token
, ';', "at end of asm-statement");
721 /* Make a statement out of an expression */
722 static struct statement
*make_statement(struct expression
*expr
)
724 struct statement
*stmt
;
728 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
729 stmt
->expression
= expr
;
734 * All iterators have two symbols associated with them:
735 * the "continue" and "break" symbols, which are targets
736 * for continue and break statements respectively.
738 * They are in a special name-space, but they follow
739 * all the normal visibility rules, so nested iterators
740 * automatically work right.
742 static void start_iterator(struct statement
*stmt
)
744 struct symbol
*cont
, *brk
;
746 start_symbol_scope();
747 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
748 cont
->ident
= &continue_ident
;
749 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
750 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
751 brk
->ident
= &break_ident
;
752 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
754 stmt
->type
= STMT_ITERATOR
;
755 stmt
->iterator_break
= brk
;
756 stmt
->iterator_continue
= cont
;
757 fn_local_symbol(brk
);
758 fn_local_symbol(cont
);
761 static void end_iterator(struct statement
*stmt
)
766 static struct statement
*start_function(struct symbol
*sym
)
769 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
771 start_function_scope();
772 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
773 ret
->ident
= &return_ident
;
774 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
775 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
776 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
777 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
780 fn_local_symbol(ret
);
784 static void end_function(struct symbol
*sym
)
786 end_function_scope();
790 * A "switch()" statement, like an iterator, has a
791 * the "break" symbol associated with it. It works
792 * exactly like the iterator break - it's the target
793 * for any break-statements in scope, and means that
794 * "break" handling doesn't even need to know whether
795 * it's breaking out of an iterator or a switch.
797 * In addition, the "case" symbol is a marker for the
798 * case/default statements to find the switch statement
799 * that they are associated with.
801 static void start_switch(struct statement
*stmt
)
803 struct symbol
*brk
, *switch_case
;
805 start_symbol_scope();
806 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
807 brk
->ident
= &break_ident
;
808 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
810 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
811 switch_case
->ident
= &case_ident
;
812 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
813 switch_case
->stmt
= stmt
;
815 stmt
->type
= STMT_SWITCH
;
816 stmt
->switch_break
= brk
;
817 stmt
->switch_case
= switch_case
;
819 fn_local_symbol(brk
);
820 fn_local_symbol(switch_case
);
823 static void end_switch(struct statement
*stmt
)
825 if (!stmt
->switch_case
->symbol_list
)
826 warn(stmt
->pos
, "switch with no cases");
830 static void add_case_statement(struct statement
*stmt
)
832 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
836 warn(stmt
->pos
, "not in switch scope");
839 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
840 add_symbol(&target
->symbol_list
, sym
);
842 stmt
->case_label
= sym
;
843 fn_local_symbol(sym
);
846 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
848 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
851 error(token
->pos
, "internal error: return without a function target");
852 stmt
->type
= STMT_RETURN
;
853 stmt
->ret_target
= target
;
854 return expression_statement(token
->next
, &stmt
->ret_value
);
857 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
859 struct symbol_list
*syms
;
860 struct expression
*e1
, *e2
, *e3
;
861 struct statement
*iterator
;
863 start_iterator(stmt
);
864 token
= expect(token
->next
, '(', "after 'for'");
868 /* C99 variable declaration? */
869 if (lookup_type(token
)) {
870 token
= external_declaration(token
, &syms
);
872 token
= parse_expression(token
, &e1
);
873 token
= expect(token
, ';', "in 'for'");
875 token
= parse_expression(token
, &e2
);
876 token
= expect(token
, ';', "in 'for'");
877 token
= parse_expression(token
, &e3
);
878 token
= expect(token
, ')', "in 'for'");
879 token
= statement(token
, &iterator
);
881 stmt
->iterator_syms
= syms
;
882 stmt
->iterator_pre_statement
= make_statement(e1
);
883 stmt
->iterator_pre_condition
= e2
;
884 stmt
->iterator_post_statement
= make_statement(e3
);
885 stmt
->iterator_post_condition
= e2
;
886 stmt
->iterator_statement
= iterator
;
892 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
894 struct expression
*expr
;
895 struct statement
*iterator
;
897 start_iterator(stmt
);
898 token
= parens_expression(token
->next
, &expr
, "after 'while'");
899 token
= statement(token
, &iterator
);
901 stmt
->iterator_pre_condition
= expr
;
902 stmt
->iterator_post_condition
= expr
;
903 stmt
->iterator_statement
= iterator
;
909 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
911 struct expression
*expr
;
912 struct statement
*iterator
;
914 start_iterator(stmt
);
915 token
= statement(token
->next
, &iterator
);
916 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
919 warn(token
->pos
, "expected 'while' after 'do'");
920 token
= parens_expression(token
, &expr
, "after 'do-while'");
922 stmt
->iterator_post_condition
= expr
;
923 stmt
->iterator_statement
= iterator
;
926 return expect(token
, ';', "after statement");
929 static struct token
*statement(struct token
*token
, struct statement
**tree
)
931 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
934 if (token_type(token
) == TOKEN_IDENT
) {
935 if (token
->ident
== &if_ident
) {
936 stmt
->type
= STMT_IF
;
937 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
938 token
= statement(token
, &stmt
->if_true
);
939 if (token_type(token
) != TOKEN_IDENT
)
941 if (token
->ident
!= &else_ident
)
943 return statement(token
->next
, &stmt
->if_false
);
946 if (token
->ident
== &return_ident
)
947 return parse_return_statement(token
, stmt
);
949 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
950 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
951 stmt
->type
= STMT_GOTO
;
952 stmt
->goto_label
= target
;
954 warn(stmt
->pos
, "break/continue not in iterator scope");
955 return expect(token
->next
, ';', "at end of statement");
957 if (token
->ident
== &default_ident
) {
959 goto default_statement
;
961 if (token
->ident
== &case_ident
) {
962 token
= parse_expression(token
->next
, &stmt
->case_expression
);
963 if (match_op(token
, SPECIAL_ELLIPSIS
))
964 token
= parse_expression(token
->next
, &stmt
->case_to
);
966 stmt
->type
= STMT_CASE
;
967 token
= expect(token
, ':', "after default/case");
968 add_case_statement(stmt
);
969 return statement(token
, &stmt
->case_statement
);
971 if (token
->ident
== &switch_ident
) {
972 stmt
->type
= STMT_SWITCH
;
974 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
975 token
= statement(token
, &stmt
->switch_statement
);
979 if (token
->ident
== &for_ident
)
980 return parse_for_statement(token
, stmt
);
982 if (token
->ident
== &while_ident
)
983 return parse_while_statement(token
, stmt
);
985 if (token
->ident
== &do_ident
)
986 return parse_do_statement(token
, stmt
);
988 if (token
->ident
== &goto_ident
) {
989 stmt
->type
= STMT_GOTO
;
991 if (match_op(token
, '*')) {
992 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
993 } else if (token_type(token
) == TOKEN_IDENT
) {
994 stmt
->goto_label
= label_symbol(token
);
997 warn(token
->pos
, "Expected identifier or goto expression");
999 return expect(token
, ';', "at end of statement");
1001 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1002 return parse_asm(token
->next
, stmt
);
1004 if (match_op(token
->next
, ':')) {
1005 stmt
->type
= STMT_LABEL
;
1006 stmt
->label_identifier
= label_symbol(token
);
1007 return statement(token
->next
->next
, &stmt
->label_statement
);
1011 if (match_op(token
, '{')) {
1012 stmt
->type
= STMT_COMPOUND
;
1013 start_symbol_scope();
1014 token
= compound_statement(token
->next
, stmt
);
1017 return expect(token
, '}', "at end of compound statement");
1020 stmt
->type
= STMT_EXPRESSION
;
1021 return expression_statement(token
, &stmt
->expression
);
1024 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1027 struct statement
* stmt
;
1028 if (eof_token(token
))
1030 if (match_op(token
, '}'))
1032 token
= statement(token
, &stmt
);
1033 add_statement(list
, stmt
);
1038 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1040 struct symbol_list
**list
= &fn
->arguments
;
1042 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1044 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1046 token
= token
->next
;
1050 if (!lookup_type(token
)) {
1051 warn(token
->pos
, "non-ANSI parameter list");
1054 token
= parameter_declaration(token
, &sym
);
1055 /* Special case: (void) */
1056 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
1058 add_symbol(list
, sym
);
1059 if (!match_op(token
, ','))
1061 token
= token
->next
;
1067 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1069 while (!eof_token(token
)) {
1070 if (!lookup_type(token
))
1072 token
= external_declaration(token
, &stmt
->syms
);
1074 token
= statement_list(token
, &stmt
->stmts
);
1078 static struct expression
*identifier_expression(struct token
*token
)
1080 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1081 expr
->expr_ident
= token
->ident
;
1085 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1087 int idx_from
, idx_to
;
1088 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1090 idx_from
= get_expression_value(from
);
1093 idx_to
= get_expression_value(to
);
1094 if (idx_to
< idx_from
|| idx_from
< 0)
1095 warn(from
->pos
, "nonsense array initializer index range");
1097 expr
->idx_from
= idx_from
;
1098 expr
->idx_to
= idx_to
;
1102 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1105 struct token
*next
= token
->next
;
1106 struct expression
*expr
;
1108 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1109 add_expression(list
, identifier_expression(next
));
1110 token
= next
->next
->next
;
1111 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1112 add_expression(list
, identifier_expression(token
));
1114 } else if (match_op(token
, '[')) {
1115 struct expression
*from
= NULL
, *to
= NULL
;
1116 token
= constant_expression(token
->next
, &from
);
1117 if (match_op(token
, SPECIAL_ELLIPSIS
))
1118 token
= constant_expression(token
->next
, &to
);
1119 add_expression(list
, index_expression(from
, to
));
1120 token
= expect(token
, ']', "at end of initializer index");
1121 token
= expect(token
, '=', "at end of initializer index");
1125 token
= initializer(&expr
, token
);
1128 add_expression(list
, expr
);
1129 if (!match_op(token
, ','))
1131 token
= token
->next
;
1136 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1138 if (match_op(token
, '{')) {
1139 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1141 token
= initializer_list(&expr
->expr_list
, token
->next
);
1142 return expect(token
, '}', "at end of initializer");
1144 return assignment_expression(token
, tree
);
1147 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1150 warn(sym
->pos
, "no identifier for function argument");
1153 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1156 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1157 struct symbol_list
**list
)
1159 struct symbol
*base_type
= decl
->ctype
.base_type
;
1160 struct statement
*stmt
;
1163 function_symbol_list
= &decl
->symbol_list
;
1164 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1165 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1166 warn(decl
->pos
, "function with external linkage has definition");
1168 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1169 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1171 stmt
= start_function(decl
);
1173 base_type
->stmt
= stmt
;
1174 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1175 declare_argument(arg
, base_type
);
1178 token
= compound_statement(token
->next
, stmt
);
1181 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1182 add_symbol(list
, decl
);
1183 check_declaration(decl
);
1184 function_symbol_list
= NULL
;
1185 return expect(token
, '}', "at end of function");
1188 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1190 struct ident
*ident
= NULL
;
1191 struct symbol
*decl
;
1192 struct ctype ctype
= { 0, };
1193 struct symbol
*base_type
;
1196 /* Top-level inline asm? */
1197 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
)) {
1198 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1199 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1200 struct statement
*stmt
;
1202 anon
->ctype
.base_type
= fn
;
1203 function_symbol_list
= &anon
->symbol_list
;
1204 stmt
= start_function(anon
);
1205 token
= parse_asm(token
->next
, stmt
);
1207 function_symbol_list
= NULL
;
1208 add_symbol(list
, anon
);
1212 /* Parse declaration-specifiers, if any */
1213 token
= declaration_specifiers(token
, &ctype
, 0);
1214 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1215 decl
->ctype
= ctype
;
1216 token
= declarator(token
, &decl
, &ident
);
1218 /* Just a type declaration? */
1220 return expect(token
, ';', "end of type declaration");
1222 decl
->ident
= ident
;
1224 /* type define declaration? */
1225 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1227 /* Typedef's don't have meaningful storage */
1229 ctype
.modifiers
&= ~MOD_STORAGE
;
1230 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1231 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1234 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1236 base_type
= decl
->ctype
.base_type
;
1237 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1238 if (match_op(token
, '{'))
1239 return parse_function_body(token
, decl
, list
);
1241 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1242 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1246 if (token_type(token
) == TOKEN_IDENT
) {
1247 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1248 struct expression
*expr
;
1250 token
= expect(token
->next
, '(', "after asm");
1251 token
= parse_expression(token
->next
, &expr
);
1252 token
= expect(token
, ')', "after asm");
1255 if (!is_typedef
&& match_op(token
, '=')) {
1256 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1257 warn(decl
->pos
, "symbol with external linkage has initializer");
1258 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1260 token
= initializer(&decl
->initializer
, token
->next
);
1263 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1264 add_symbol(list
, decl
);
1265 if (function_symbol_list
)
1266 fn_local_symbol(decl
);
1269 check_declaration(decl
);
1271 if (!match_op(token
, ','))
1274 token
= token
->next
;
1276 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1277 decl
->ctype
= ctype
;
1278 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1279 token
= declarator(token
, &decl
, &ident
);
1281 warn(token
->pos
, "expected identifier name in type definition");
1285 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1287 /* Function declarations are automatically extern unless specifically static */
1288 base_type
= decl
->ctype
.base_type
;
1289 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1290 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1291 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1294 return expect(token
, ';', "at end of declaration");
1297 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1299 while (!eof_token(token
))
1300 token
= external_declaration(token
, list
);
1301 // They aren't needed any more
1302 clear_token_alloc();