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
| MOD_EXPLICITLY_SIGNED
);
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") ||
305 match_string_ident(attribute
, "__noreturn__"))
307 if (match_string_ident(attribute
, "regparm"))
309 if (match_string_ident(attribute
, "weak"))
311 if (match_string_ident(attribute
, "alias"))
313 if (match_string_ident(attribute
, "pure"))
315 if (match_string_ident(attribute
, "always_inline"))
317 if (match_string_ident(attribute
, "syscall_linkage"))
319 if (match_string_ident(attribute
, "visibility"))
322 return "unknown attribute";
325 struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
327 ctype
->modifiers
= 0;
328 token
= expect(token
, '(', "after attribute");
329 token
= expect(token
, '(', "after attribute");
333 struct ident
*attribute_name
;
334 struct expression
*attribute_expr
;
336 if (eof_token(token
))
338 if (match_op(token
, ';'))
340 if (token_type(token
) != TOKEN_IDENT
)
342 attribute_name
= token
->ident
;
344 attribute_expr
= NULL
;
345 if (match_op(token
, '('))
346 token
= parens_expression(token
, &attribute_expr
, "in attribute");
347 error
= handle_attribute(ctype
, attribute_name
, attribute_expr
);
349 warn(token
->pos
, "attribute '%s': %s", show_ident(attribute_name
), error
);
350 if (!match_op(token
, ','))
355 token
= expect(token
, ')', "after attribute");
356 token
= expect(token
, ')', "after attribute");
360 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
361 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
363 struct symbol
* ctype_integer(unsigned int spec
)
365 static struct symbol
*const integer_ctypes
[][2] = {
366 { &llong_ctype
, &ullong_ctype
},
367 { &long_ctype
, &ulong_ctype
},
368 { &short_ctype
, &ushort_ctype
},
369 { &char_ctype
, &uchar_ctype
},
370 { &int_ctype
, &uint_ctype
},
372 struct symbol
*const (*ctype
)[2];
374 ctype
= integer_ctypes
;
375 if (!(spec
& MOD_LONGLONG
)) {
377 if (!(spec
& MOD_LONG
)) {
379 if (!(spec
& MOD_SHORT
)) {
381 if (!(spec
& MOD_CHAR
))
386 return ctype
[0][(spec
& MOD_UNSIGNED
) != 0];
389 struct symbol
* ctype_fp(unsigned int spec
)
391 if (spec
& MOD_LONGLONG
)
392 return &ldouble_ctype
;
394 return &double_ctype
;
398 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
400 unsigned long mod
= thistype
->modifiers
;
403 unsigned long old
= ctype
->modifiers
;
404 unsigned long extra
= 0, dup
, conflict
;
406 if (mod
& old
& MOD_LONG
) {
407 extra
= MOD_LONGLONG
| MOD_LONG
;
411 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
413 warn(pos
, "Just how %sdo you want this type to be?",
414 modifier_string(dup
));
416 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
418 warn(pos
, "You cannot have both long and short modifiers.");
420 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
422 warn(pos
, "You cannot have both signed and unsigned modifiers.");
424 // Only one storage modifier allowed, except that "inline" doesn't count.
425 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
426 conflict
&= (conflict
- 1);
428 warn(pos
, "multiple storage classes");
430 ctype
->modifiers
= old
| mod
| extra
;
433 /* Context mask and value */
434 if ((ctype
->context
^ thistype
->context
) & (ctype
->contextmask
& thistype
->contextmask
)) {
435 warn(pos
, "inconsistent attribute types");
436 thistype
->context
= 0;
437 thistype
->contextmask
= 0;
439 ctype
->context
|= thistype
->context
;
440 ctype
->contextmask
|= thistype
->contextmask
;
443 if (thistype
->alignment
& (thistype
->alignment
-1)) {
444 warn(pos
, "I don't like non-power-of-2 alignments");
445 thistype
->alignment
= 0;
447 if (thistype
->alignment
> ctype
->alignment
)
448 ctype
->alignment
= thistype
->alignment
;
451 ctype
->as
= thistype
->as
;
454 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
456 unsigned long banned
, wrong
;
457 unsigned long this_mod
= s
->ctype
.modifiers
;
458 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
459 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
461 if (this_mod
& (MOD_STRUCTOF
| MOD_UNIONOF
| MOD_ENUMOF
))
462 banned
= BANNED_SIZE
| BANNED_SIGN
;
463 else if (this_mod
& MOD_SPECIALBITS
)
465 else if (s
->ctype
.base_type
== &fp_type
)
466 banned
= BANNED_SIGN
;
467 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
473 // vector_type <-- whatever that is
474 banned
= BANNED_SIZE
| BANNED_SIGN
;
477 wrong
= mod
& banned
;
479 warn(*pos
, "modifier %sis invalid in this context",
480 modifier_string (wrong
));
484 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
488 while ( (token
= next
) != NULL
) {
489 struct ctype thistype
;
491 struct symbol
*s
, *type
;
495 if (token_type(token
) != TOKEN_IDENT
)
497 ident
= token
->ident
;
499 s
= lookup_symbol(ident
, NS_TYPEDEF
);
503 mod
= thistype
.modifiers
;
504 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
506 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
507 if (mod
& MOD_SPECIALBITS
) {
508 if (mod
& MOD_STRUCTOF
)
509 next
= struct_or_union_specifier(SYM_STRUCT
, next
, &thistype
);
510 else if (mod
& MOD_UNIONOF
)
511 next
= struct_or_union_specifier(SYM_UNION
, next
, &thistype
);
512 else if (mod
& MOD_ENUMOF
)
513 next
= enum_specifier(next
, &thistype
);
514 else if (mod
& MOD_ATTRIBUTE
)
515 next
= attribute_specifier(next
, &thistype
);
516 else if (mod
& MOD_TYPEOF
)
517 next
= typeof_specifier(next
, &thistype
);
518 mod
= thistype
.modifiers
;
520 type
= thistype
.base_type
;
524 if (ctype
->base_type
)
526 /* User types only mix with qualifiers */
527 if (mod
& MOD_USERTYPE
) {
528 if (ctype
->modifiers
& MOD_SPECIFIER
)
531 ctype
->base_type
= type
;
534 apply_ctype(token
->pos
, &thistype
, ctype
);
537 /* Turn the "virtual types" into real types with real sizes etc */
538 if (!ctype
->base_type
) {
539 struct symbol
*base
= &incomplete_ctype
;
542 * If we have modifiers, we'll default to an integer
543 * type, and "ctype_integer()" will turn this into
546 if (ctype
->modifiers
& MOD_SPECIFIER
)
548 ctype
->base_type
= base
;
551 if (ctype
->base_type
== &int_type
) {
552 ctype
->base_type
= ctype_integer(ctype
->modifiers
& MOD_SPECIFIER
);
553 ctype
->modifiers
&= ~MOD_SPECIFIER
;
556 if (ctype
->base_type
== &fp_type
) {
557 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
558 ctype
->modifiers
&= ~MOD_SPECIFIER
;
564 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
566 struct expression
*expr
= NULL
;
568 token
= parse_expression(token
, &expr
);
569 sym
->array_size
= expr
;
573 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
574 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
);
576 static struct token
*direct_declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
578 struct ctype
*ctype
= &(*tree
)->ctype
;
580 if (p
&& token_type(token
) == TOKEN_IDENT
) {
586 if (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
587 struct ctype thistype
= { 0, };
588 token
= attribute_specifier(token
->next
, &thistype
);
589 apply_ctype(token
->pos
, &thistype
, ctype
);
592 if (token_type(token
) != TOKEN_SPECIAL
)
596 * This can be either a parameter list or a grouping.
597 * For the direct (non-abstract) case, we know if must be
598 * a paramter list if we already saw the identifier.
599 * For the abstract case, we know if must be a parameter
600 * list if it is empty or starts with a type.
602 if (token
->special
== '(') {
604 struct token
*next
= token
->next
;
605 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
608 struct symbol
*base_type
= ctype
->base_type
;
609 token
= declarator(next
, tree
, p
);
610 token
= expect(token
, ')', "in nested declarator");
611 while (ctype
->base_type
!= base_type
)
612 ctype
= &ctype
->base_type
->ctype
;
617 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
618 token
= parameter_type_list(next
, sym
);
619 token
= expect(token
, ')', "in function declarator");
622 if (token
->special
== '[') {
623 struct symbol
*array
= indirect(token
->pos
, ctype
, SYM_ARRAY
);
624 token
= abstract_array_declarator(token
->next
, array
);
625 token
= expect(token
, ']', "in abstract_array_declarator");
626 ctype
= &array
->ctype
;
637 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
639 unsigned long modifiers
;
640 struct symbol
*base_type
;
642 modifiers
= ctype
->modifiers
& ~(MOD_TYPEDEF
| MOD_ATTRIBUTE
);
643 base_type
= ctype
->base_type
;
644 ctype
->modifiers
= modifiers
;
646 while (match_op(token
,'*')) {
647 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
648 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
649 ptr
->ctype
.as
= ctype
->as
;
650 ptr
->ctype
.context
= ctype
->context
;
651 ptr
->ctype
.contextmask
= ctype
->contextmask
;
652 ptr
->ctype
.base_type
= base_type
;
655 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
656 ctype
->base_type
= base_type
;
659 ctype
->contextmask
= 0;
661 token
= declaration_specifiers(token
->next
, ctype
, 1);
662 modifiers
= ctype
->modifiers
;
667 static struct token
*declarator(struct token
*token
, struct symbol
**tree
, struct ident
**p
)
669 token
= pointer(token
, &(*tree
)->ctype
);
670 return direct_declarator(token
, tree
, p
);
673 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
675 while (!match_op(token
, '}')) {
676 struct ctype ctype
= {0, };
678 token
= declaration_specifiers(token
, &ctype
, 0);
680 struct ident
*ident
= NULL
;
681 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
683 token
= declarator(token
, &decl
, &ident
);
684 if (match_op(token
, ':')) {
685 struct ctype
*ctype
= &decl
->ctype
;
686 struct expression
*expr
;
687 struct symbol
*bitfield
;
690 if (is_int_type (ctype
->base_type
)) {
691 bitfield
= indirect(token
->pos
, ctype
, SYM_BITFIELD
);
692 token
= conditional_expression(token
->next
, &expr
);
693 width
= get_expression_value(expr
);
694 bitfield
->fieldwidth
= width
;
696 warn(token
->pos
, "invalid negative bitfield width, %lld.", width
);
697 bitfield
->fieldwidth
= 8;
698 } else if (decl
->ident
&& width
== 0) {
699 warn(token
->pos
, "invalid named zero-width bitfield `%s'",
700 show_ident (decl
->ident
));
701 bitfield
->fieldwidth
= 8;
702 } else if (width
!= bitfield
->fieldwidth
) {
704 unsigned int stupid_gcc
= -1;
705 bitfield
->fieldwidth
= stupid_gcc
;
706 warn(token
->pos
, "truncating large bitfield from %lld to %d bits", width
, bitfield
->fieldwidth
);
709 warn(token
->pos
, "invalid bitfield specifier for type %s.", show_typename (ctype
->base_type
));
710 // Parse this to recover gracefully.
711 token
= conditional_expression(token
->next
, &expr
);
713 while (match_idents(token
, &__attribute___ident
, &__attribute_ident
, NULL
)) {
714 struct ctype thistype
= { 0, };
715 token
= attribute_specifier(token
->next
, &thistype
);
716 apply_ctype(token
->pos
, &thistype
, ctype
);
719 add_symbol(list
, decl
);
720 if (!match_op(token
, ','))
724 if (!match_op(token
, ';')) {
725 warn(token
->pos
, "expected ; at end of declaration");
733 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
735 struct ident
*ident
= NULL
;
737 struct ctype ctype
= { 0, };
739 token
= declaration_specifiers(token
, &ctype
, 0);
740 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
743 token
= declarator(token
, tree
, &ident
);
747 struct token
*typename(struct token
*token
, struct symbol
**p
)
749 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
751 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
752 return declarator(token
, &sym
, NULL
);
755 struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
757 token
= parse_expression(token
, tree
);
758 return expect(token
, ';', "at end of statement");
761 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
)
763 struct expression
*expr
;
765 /* Allow empty operands */
766 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
769 if (match_op(token
->next
, '[') &&
770 token_type(token
->next
->next
) == TOKEN_IDENT
&&
771 match_op(token
->next
->next
->next
, ']'))
772 token
= token
->next
->next
->next
;
773 token
= primary_expression(token
->next
, &expr
);
774 token
= parens_expression(token
, &expr
, "in asm parameter");
775 } while (match_op(token
, ','));
779 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
781 struct expression
*expr
;
784 token
= primary_expression(token
->next
, &expr
);
785 } while (match_op(token
, ','));
789 static struct token
*parse_asm(struct token
*token
, struct statement
*stmt
)
791 struct expression
*expr
;
793 stmt
->type
= STMT_ASM
;
794 if (match_idents(token
, &__volatile___ident
, &volatile_ident
, NULL
)) {
797 token
= expect(token
, '(', "after asm");
798 token
= parse_expression(token
->next
, &expr
);
799 if (match_op(token
, ':'))
800 token
= parse_asm_operands(token
, stmt
);
801 if (match_op(token
, ':'))
802 token
= parse_asm_operands(token
, stmt
);
803 if (match_op(token
, ':'))
804 token
= parse_asm_clobbers(token
, stmt
);
805 token
= expect(token
, ')', "after asm");
806 return expect(token
, ';', "at end of asm-statement");
809 /* Make a statement out of an expression */
810 static struct statement
*make_statement(struct expression
*expr
)
812 struct statement
*stmt
;
816 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
817 stmt
->expression
= expr
;
822 * All iterators have two symbols associated with them:
823 * the "continue" and "break" symbols, which are targets
824 * for continue and break statements respectively.
826 * They are in a special name-space, but they follow
827 * all the normal visibility rules, so nested iterators
828 * automatically work right.
830 static void start_iterator(struct statement
*stmt
)
832 struct symbol
*cont
, *brk
;
834 start_symbol_scope();
835 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
836 cont
->ident
= &continue_ident
;
837 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
838 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
839 brk
->ident
= &break_ident
;
840 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
842 stmt
->type
= STMT_ITERATOR
;
843 stmt
->iterator_break
= brk
;
844 stmt
->iterator_continue
= cont
;
845 fn_local_symbol(brk
);
846 fn_local_symbol(cont
);
849 static void end_iterator(struct statement
*stmt
)
854 static struct statement
*start_function(struct symbol
*sym
)
857 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
859 start_function_scope();
860 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
861 ret
->ident
= &return_ident
;
862 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
863 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
864 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
865 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
868 fn_local_symbol(ret
);
872 static void end_function(struct symbol
*sym
)
874 end_function_scope();
878 * A "switch()" statement, like an iterator, has a
879 * the "break" symbol associated with it. It works
880 * exactly like the iterator break - it's the target
881 * for any break-statements in scope, and means that
882 * "break" handling doesn't even need to know whether
883 * it's breaking out of an iterator or a switch.
885 * In addition, the "case" symbol is a marker for the
886 * case/default statements to find the switch statement
887 * that they are associated with.
889 static void start_switch(struct statement
*stmt
)
891 struct symbol
*brk
, *switch_case
;
893 start_symbol_scope();
894 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
895 brk
->ident
= &break_ident
;
896 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
898 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
899 switch_case
->ident
= &case_ident
;
900 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
901 switch_case
->stmt
= stmt
;
903 stmt
->type
= STMT_SWITCH
;
904 stmt
->switch_break
= brk
;
905 stmt
->switch_case
= switch_case
;
907 fn_local_symbol(brk
);
908 fn_local_symbol(switch_case
);
911 static void end_switch(struct statement
*stmt
)
913 if (!stmt
->switch_case
->symbol_list
)
914 warn(stmt
->pos
, "switch with no cases");
918 static void add_case_statement(struct statement
*stmt
)
920 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
924 warn(stmt
->pos
, "not in switch scope");
927 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
928 add_symbol(&target
->symbol_list
, sym
);
930 stmt
->case_label
= sym
;
931 fn_local_symbol(sym
);
934 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
936 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
939 error(token
->pos
, "internal error: return without a function target");
940 stmt
->type
= STMT_RETURN
;
941 stmt
->ret_target
= target
;
942 return expression_statement(token
->next
, &stmt
->ret_value
);
945 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
947 struct symbol_list
*syms
;
948 struct expression
*e1
, *e2
, *e3
;
949 struct statement
*iterator
;
951 start_iterator(stmt
);
952 token
= expect(token
->next
, '(', "after 'for'");
956 /* C99 variable declaration? */
957 if (lookup_type(token
)) {
958 token
= external_declaration(token
, &syms
);
960 token
= parse_expression(token
, &e1
);
961 token
= expect(token
, ';', "in 'for'");
963 token
= parse_expression(token
, &e2
);
964 token
= expect(token
, ';', "in 'for'");
965 token
= parse_expression(token
, &e3
);
966 token
= expect(token
, ')', "in 'for'");
967 token
= statement(token
, &iterator
);
969 stmt
->iterator_syms
= syms
;
970 stmt
->iterator_pre_statement
= make_statement(e1
);
971 stmt
->iterator_pre_condition
= e2
;
972 stmt
->iterator_post_statement
= make_statement(e3
);
973 stmt
->iterator_post_condition
= e2
;
974 stmt
->iterator_statement
= iterator
;
980 struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
982 struct expression
*expr
;
983 struct statement
*iterator
;
985 start_iterator(stmt
);
986 token
= parens_expression(token
->next
, &expr
, "after 'while'");
987 token
= statement(token
, &iterator
);
989 stmt
->iterator_pre_condition
= expr
;
990 stmt
->iterator_post_condition
= expr
;
991 stmt
->iterator_statement
= iterator
;
997 struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
999 struct expression
*expr
;
1000 struct statement
*iterator
;
1002 start_iterator(stmt
);
1003 token
= statement(token
->next
, &iterator
);
1004 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1005 token
= token
->next
;
1007 warn(token
->pos
, "expected 'while' after 'do'");
1008 token
= parens_expression(token
, &expr
, "after 'do-while'");
1010 stmt
->iterator_post_condition
= expr
;
1011 stmt
->iterator_statement
= iterator
;
1014 return expect(token
, ';', "after statement");
1017 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1019 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1022 if (token_type(token
) == TOKEN_IDENT
) {
1023 if (token
->ident
== &if_ident
) {
1024 stmt
->type
= STMT_IF
;
1025 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1026 token
= statement(token
, &stmt
->if_true
);
1027 if (token_type(token
) != TOKEN_IDENT
)
1029 if (token
->ident
!= &else_ident
)
1031 return statement(token
->next
, &stmt
->if_false
);
1034 if (token
->ident
== &return_ident
)
1035 return parse_return_statement(token
, stmt
);
1037 if (token
->ident
== &break_ident
|| token
->ident
== &continue_ident
) {
1038 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1039 stmt
->type
= STMT_GOTO
;
1040 stmt
->goto_label
= target
;
1042 warn(stmt
->pos
, "break/continue not in iterator scope");
1043 return expect(token
->next
, ';', "at end of statement");
1045 if (token
->ident
== &default_ident
) {
1046 token
= token
->next
;
1047 goto default_statement
;
1049 if (token
->ident
== &case_ident
) {
1050 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1051 if (match_op(token
, SPECIAL_ELLIPSIS
))
1052 token
= parse_expression(token
->next
, &stmt
->case_to
);
1054 stmt
->type
= STMT_CASE
;
1055 token
= expect(token
, ':', "after default/case");
1056 add_case_statement(stmt
);
1057 return statement(token
, &stmt
->case_statement
);
1059 if (token
->ident
== &switch_ident
) {
1060 stmt
->type
= STMT_SWITCH
;
1062 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1063 token
= statement(token
, &stmt
->switch_statement
);
1067 if (token
->ident
== &for_ident
)
1068 return parse_for_statement(token
, stmt
);
1070 if (token
->ident
== &while_ident
)
1071 return parse_while_statement(token
, stmt
);
1073 if (token
->ident
== &do_ident
)
1074 return parse_do_statement(token
, stmt
);
1076 if (token
->ident
== &goto_ident
) {
1077 stmt
->type
= STMT_GOTO
;
1078 token
= token
->next
;
1079 if (match_op(token
, '*')) {
1080 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1081 add_statement(&function_computed_goto_list
, stmt
);
1082 } else if (token_type(token
) == TOKEN_IDENT
) {
1083 stmt
->goto_label
= label_symbol(token
);
1084 token
= token
->next
;
1086 warn(token
->pos
, "Expected identifier or goto expression");
1088 return expect(token
, ';', "at end of statement");
1090 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1091 return parse_asm(token
->next
, stmt
);
1093 if (match_op(token
->next
, ':')) {
1094 stmt
->type
= STMT_LABEL
;
1095 stmt
->label_identifier
= label_symbol(token
);
1096 return statement(token
->next
->next
, &stmt
->label_statement
);
1100 if (match_op(token
, '{')) {
1101 stmt
->type
= STMT_COMPOUND
;
1102 start_symbol_scope();
1103 token
= compound_statement(token
->next
, stmt
);
1106 return expect(token
, '}', "at end of compound statement");
1109 stmt
->type
= STMT_EXPRESSION
;
1110 return expression_statement(token
, &stmt
->expression
);
1113 struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1116 struct statement
* stmt
;
1117 if (eof_token(token
))
1119 if (match_op(token
, '}'))
1121 token
= statement(token
, &stmt
);
1122 add_statement(list
, stmt
);
1127 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1129 struct symbol_list
**list
= &fn
->arguments
;
1131 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1133 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1135 token
= token
->next
;
1139 if (!lookup_type(token
)) {
1140 warn(token
->pos
, "non-ANSI parameter list");
1143 token
= parameter_declaration(token
, &sym
);
1144 if (sym
->ctype
.base_type
== &void_ctype
) {
1145 /* Special case: (void) */
1146 if (!*list
&& !sym
->ident
)
1148 warn(token
->pos
, "void parameter");
1150 add_symbol(list
, sym
);
1151 if (!match_op(token
, ','))
1153 token
= token
->next
;
1159 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1161 while (!eof_token(token
)) {
1162 if (!lookup_type(token
))
1164 token
= external_declaration(token
, &stmt
->syms
);
1166 token
= statement_list(token
, &stmt
->stmts
);
1170 static struct expression
*identifier_expression(struct token
*token
)
1172 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1173 expr
->expr_ident
= token
->ident
;
1177 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1179 int idx_from
, idx_to
;
1180 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1182 idx_from
= get_expression_value(from
);
1185 idx_to
= get_expression_value(to
);
1186 if (idx_to
< idx_from
|| idx_from
< 0)
1187 warn(from
->pos
, "nonsense array initializer index range");
1189 expr
->idx_from
= idx_from
;
1190 expr
->idx_to
= idx_to
;
1194 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1197 struct token
*next
= token
->next
;
1198 struct expression
*expr
;
1200 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
) && match_op(next
->next
, '=')) {
1201 add_expression(list
, identifier_expression(next
));
1202 token
= next
->next
->next
;
1203 } else if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1204 add_expression(list
, identifier_expression(token
));
1206 } else if (match_op(token
, '[')) {
1207 struct expression
*from
= NULL
, *to
= NULL
;
1208 token
= constant_expression(token
->next
, &from
);
1209 if (match_op(token
, SPECIAL_ELLIPSIS
))
1210 token
= constant_expression(token
->next
, &to
);
1211 add_expression(list
, index_expression(from
, to
));
1212 token
= expect(token
, ']', "at end of initializer index");
1213 token
= expect(token
, '=', "at end of initializer index");
1217 token
= initializer(&expr
, token
);
1220 add_expression(list
, expr
);
1221 if (!match_op(token
, ','))
1223 token
= token
->next
;
1228 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1230 if (match_op(token
, '{')) {
1231 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1233 token
= initializer_list(&expr
->expr_list
, token
->next
);
1234 return expect(token
, '}', "at end of initializer");
1236 return assignment_expression(token
, tree
);
1239 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1242 warn(sym
->pos
, "no identifier for function argument");
1245 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1248 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1249 struct symbol_list
**list
)
1251 struct symbol
*base_type
= decl
->ctype
.base_type
;
1252 struct statement
*stmt
;
1255 function_symbol_list
= &decl
->symbol_list
;
1256 function_computed_target_list
= NULL
;
1257 function_computed_goto_list
= NULL
;
1259 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1260 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1261 warn(decl
->pos
, "function with external linkage has definition");
1263 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1264 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1266 stmt
= start_function(decl
);
1268 base_type
->stmt
= stmt
;
1269 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1270 declare_argument(arg
, base_type
);
1273 token
= compound_statement(token
->next
, stmt
);
1276 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1277 add_symbol(list
, decl
);
1278 check_declaration(decl
);
1279 function_symbol_list
= NULL
;
1280 if (function_computed_goto_list
) {
1281 if (!function_computed_target_list
)
1282 warn(decl
->pos
, "function has computed goto but no targets?");
1284 struct statement
*stmt
;
1285 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1286 stmt
->target_list
= function_computed_target_list
;
1290 return expect(token
, '}', "at end of function");
1293 static struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
1295 struct ident
*ident
= NULL
;
1296 struct symbol
*decl
;
1297 struct ctype ctype
= { 0, };
1298 struct symbol
*base_type
;
1301 /* Top-level inline asm? */
1302 if (match_idents(token
, &asm_ident
, &__asm___ident
, &__asm_ident
, NULL
)) {
1303 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
1304 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
1305 struct statement
*stmt
;
1307 anon
->ctype
.base_type
= fn
;
1308 function_symbol_list
= &anon
->symbol_list
;
1309 stmt
= start_function(anon
);
1310 token
= parse_asm(token
->next
, stmt
);
1312 function_symbol_list
= NULL
;
1313 add_symbol(list
, anon
);
1317 /* Parse declaration-specifiers, if any */
1318 token
= declaration_specifiers(token
, &ctype
, 0);
1319 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1320 decl
->ctype
= ctype
;
1321 token
= declarator(token
, &decl
, &ident
);
1323 /* Just a type declaration? */
1325 return expect(token
, ';', "end of type declaration");
1327 decl
->ident
= ident
;
1329 /* type define declaration? */
1330 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
1332 /* Typedef's don't have meaningful storage */
1334 ctype
.modifiers
&= ~MOD_STORAGE
;
1335 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
1336 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
1339 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1341 base_type
= decl
->ctype
.base_type
;
1342 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1343 if (match_op(token
, '{'))
1344 return parse_function_body(token
, decl
, list
);
1346 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1347 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1348 } else if (!is_typedef
&& base_type
== &void_ctype
) {
1349 warn(token
->pos
, "void declaration");
1353 if (token_type(token
) == TOKEN_IDENT
) {
1354 if (token
->ident
== &asm_ident
|| token
->ident
== &__asm_ident
|| token
->ident
== &__asm___ident
) {
1355 struct expression
*expr
;
1357 token
= expect(token
->next
, '(', "after asm");
1358 token
= parse_expression(token
->next
, &expr
);
1359 token
= expect(token
, ')', "after asm");
1362 if (!is_typedef
&& match_op(token
, '=')) {
1363 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1364 warn(decl
->pos
, "symbol with external linkage has initializer");
1365 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
1367 token
= initializer(&decl
->initializer
, token
->next
);
1370 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
1371 add_symbol(list
, decl
);
1372 if (function_symbol_list
)
1373 fn_local_symbol(decl
);
1376 check_declaration(decl
);
1378 if (!match_op(token
, ','))
1381 token
= token
->next
;
1383 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1384 decl
->ctype
= ctype
;
1385 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
1386 token
= declarator(token
, &decl
, &ident
);
1388 warn(token
->pos
, "expected identifier name in type definition");
1392 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
1394 /* Function declarations are automatically extern unless specifically static */
1395 base_type
= decl
->ctype
.base_type
;
1396 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
1397 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1398 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1401 return expect(token
, ';', "at end of declaration");
1404 void translation_unit(struct token
*token
, struct symbol_list
**list
)
1406 while (!eof_token(token
))
1407 token
= external_declaration(token
, list
);
1408 // They aren't needed any more
1409 clear_token_alloc();