4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * This is the expression parsing part of parsing C.
43 #include "expression.h"
47 ALLOCATOR(type_expression
, "type-expr-maps");
49 static int match_oplist(int op
, ...)
56 nextop
= va_arg(args
, int);
57 } while (nextop
!= 0 && nextop
!= op
);
63 static struct token
*comma_expression(struct token
*, struct expression
**);
65 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
69 token
= expect(token
, '(', where
);
71 if (match_op(token
, '{')) {
72 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
73 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
76 start_label_scope(token
->pos
);
77 token
= compound_statement(token
->next
, stmt
);
79 token
= expect(token
, '}', "at end of statement expression");
81 token
= parse_expression(token
, expr
);
84 sparse_error(token
->pos
, "an expression is expected before ')'");
85 return expect(token
, ')', where
);
88 struct token
*string_expression(struct token
*token
, struct expression
**expr
, const char *where
)
90 struct token
*next
= primary_expression(token
, expr
);
92 if (!*expr
|| (*expr
)->type
!= EXPR_STRING
) {
93 sparse_error(token
->pos
, "string literal expected for %s", where
);
100 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
103 static struct symbol
*handle_func(struct token
*token
)
105 struct ident
*ident
= token
->ident
;
106 struct symbol
*decl
, *array
;
107 struct string
*string
;
110 if (ident
!= &__func___ident
&&
111 ident
!= &__FUNCTION___ident
&&
112 ident
!= &__PRETTY_FUNCTION___ident
)
115 if (!current_fn
|| !current_fn
->ident
)
118 /* OK, it's one of ours */
119 array
= alloc_symbol(token
->pos
, SYM_ARRAY
);
120 array
->ctype
.base_type
= &char_ctype
;
121 array
->ctype
.alignment
= 1;
122 array
->endpos
= token
->pos
;
123 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
124 decl
->ctype
.base_type
= array
;
125 decl
->ctype
.alignment
= 1;
126 decl
->ctype
.modifiers
= MOD_STATIC
;
127 decl
->endpos
= token
->pos
;
129 /* NS_SYMBOL but in function-scope */
130 bind_symbol_with_scope(decl
, ident
, NS_SYMBOL
, function_scope
);
132 len
= current_fn
->ident
->len
;
133 string
= __alloc_string(len
+ 1);
134 memcpy(string
->data
, current_fn
->ident
->name
, len
);
135 string
->data
[len
] = 0;
136 string
->length
= len
+ 1;
138 decl
->initializer
= alloc_expression(token
->pos
, EXPR_STRING
);
139 decl
->initializer
->string
= string
;
140 decl
->initializer
->ctype
= decl
;
141 decl
->array_size
= alloc_const_expression(token
->pos
, len
+ 1);
142 array
->array_size
= decl
->array_size
;
143 decl
->bit_size
= array
->bit_size
= bytes_to_bits(len
+ 1);
148 static struct token
*parse_type(struct token
*token
, struct expression
**tree
)
151 *tree
= alloc_expression(token
->pos
, EXPR_TYPE
);
152 token
= typename(token
, &sym
, NULL
);
154 sparse_error(token
->pos
,
155 "type expression should not include identifier "
156 "\"%s\"", sym
->ident
->name
);
157 (*tree
)->symbol
= sym
;
161 static struct token
*builtin_types_compatible_p_expr(struct token
*token
,
162 struct expression
**tree
)
164 struct expression
*expr
= alloc_expression(
165 token
->pos
, EXPR_COMPARE
);
166 expr
->op
= SPECIAL_EQUAL
;
168 if (!match_op(token
, '('))
169 return expect(token
, '(',
170 "after __builtin_types_compatible_p");
172 token
= parse_type(token
, &expr
->left
);
173 if (!match_op(token
, ','))
174 return expect(token
, ',',
175 "in __builtin_types_compatible_p");
177 token
= parse_type(token
, &expr
->right
);
178 if (!match_op(token
, ')'))
179 return expect(token
, ')',
180 "at end of __builtin_types_compatible_p");
187 static struct token
*builtin_offsetof_expr(struct token
*token
,
188 struct expression
**tree
)
190 struct expression
*expr
= NULL
;
191 struct expression
**p
= &expr
;
196 if (!match_op(token
, '('))
197 return expect(token
, '(', "after __builtin_offset");
200 token
= typename(token
, &sym
, NULL
);
202 sparse_error(token
->pos
,
203 "type expression should not include identifier "
204 "\"%s\"", sym
->ident
->name
);
206 if (!match_op(token
, ','))
207 return expect(token
, ',', "in __builtin_offset");
210 struct expression
*e
;
216 return expect(token
, ')', "at end of __builtin_offset");
217 case SPECIAL_DEREFERENCE
:
218 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
225 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
227 if (token_type(token
) != TOKEN_IDENT
) {
228 sparse_error(token
->pos
, "Expected member name");
231 e
->ident
= token
->ident
;
236 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
238 token
= parse_expression(token
, &e
->index
);
239 token
= expect(token
, ']',
240 "at end of array dereference");
246 op
= token_type(token
) == TOKEN_SPECIAL
? token
->special
: 0;
251 #define ULLONG_MAX (~0ULL)
254 static unsigned long long parse_num(const char *nptr
, char **end
)
256 if (nptr
[0] == '0' && tolower((unsigned char)nptr
[1]) == 'b')
257 return strtoull(&nptr
[2], end
, 2);
258 return strtoull(nptr
, end
, 0);
261 static void get_number_value(struct expression
*expr
, struct token
*token
)
263 const char *str
= token
->number
;
264 unsigned long long value
;
266 int size
= 0, want_unsigned
= 0;
267 int overflow
= 0, do_warn
= 0;
268 int try_unsigned
= 1;
272 value
= parse_num(str
, &end
);
275 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
281 } else if (c
== 'u' || c
== 'U') {
285 } else if (c
== 'l' || c
== 'L') {
298 /* OK, it's a valid integer */
299 /* decimals can be unsigned only if directly specified as such */
300 if (str
[0] != '0' && !want_unsigned
)
303 bits
= bits_in_int
- 1;
304 if (!(value
& (~1ULL << bits
))) {
305 if (!(value
& (1ULL << bits
))) {
307 } else if (try_unsigned
) {
316 bits
= bits_in_long
- 1;
317 if (!(value
& (~1ULL << bits
))) {
318 if (!(value
& (1ULL << bits
))) {
320 } else if (try_unsigned
) {
329 bits
= bits_in_longlong
- 1;
330 if (value
& (~1ULL << bits
))
332 if (!(value
& (1ULL << bits
)))
335 warning(expr
->pos
, "decimal constant %s is too big for long long",
339 if (do_warn
&& Wconstant_suffix
)
340 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
342 want_unsigned
? " unsigned":"",
343 size
> 0 ? " long":"",
344 size
> 1 ? " long":"");
347 "decimal constant %s is between LONG_MAX and ULONG_MAX."
348 " For C99 that means long long, C90 compilers are very "
349 "likely to produce unsigned long (and a warning) here",
351 expr
->type
= EXPR_VALUE
;
352 expr
->flags
= CEF_SET_INT
;
353 expr
->ctype
= ctype_integer(size
, want_unsigned
);
357 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
361 expr
->fvalue
= string_to_ld(str
, &end
);
368 if (*end
== 'f' || *end
== 'F')
369 expr
->ctype
= &float_ctype
;
370 else if (*end
== 'l' || *end
== 'L')
371 expr
->ctype
= &ldouble_ctype
;
373 expr
->ctype
= &double_ctype
;
377 expr
->flags
= CEF_SET_FLOAT
;
378 expr
->type
= EXPR_FVALUE
;
382 sparse_error(expr
->pos
, "constant %s is not a valid number", show_token(token
));
383 expr
->type
= EXPR_VALUE
;
385 expr
->ctype
= &int_ctype
;
388 static struct token
*generic_selection(struct token
*token
, struct expression
**tree
)
390 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_GENERIC
);
391 struct type_expression
**last
= &expr
->map
;
393 token
= expect(token
, '(', "after '_Generic'");
394 token
= assignment_expression(token
, &expr
->control
);
395 if (!match_op(token
, ',')) {
398 while (match_op(token
, ',')) {
400 if (lookup_type(token
)) {
401 struct type_expression
*map
= __alloc_type_expression(0);
402 token
= typename(token
, &map
->type
, NULL
);
403 token
= expect(token
, ':', "after typename");
404 token
= assignment_expression(token
, &map
->expr
);
407 } else if (match_ident(token
, &default_ident
)) {
409 warning(token
->pos
, "multiple default in generic expression");
410 info(expr
->def
->pos
, "note: previous was here");
413 token
= expect(token
, ':', "after typename");
414 token
= assignment_expression(token
, &expr
->def
);
419 return expect(token
, ')', "after expression");
422 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
424 struct expression
*expr
= NULL
;
426 switch (token_type(token
)) {
427 case TOKEN_CHAR
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
428 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
429 expr
->flags
= CEF_SET_CHAR
;
430 expr
->ctype
= token_type(token
) < TOKEN_WIDE_CHAR
? &int_ctype
: &long_ctype
;
431 get_char_constant(token
, &expr
->value
);
436 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
437 get_number_value(expr
, token
); /* will see if it's an integer */
441 case TOKEN_ZERO_IDENT
: {
442 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
443 expr
->flags
= CEF_SET_INT
;
444 expr
->ctype
= &int_ctype
;
445 expr
->symbol
= &zero_int
;
446 expr
->symbol_name
= token
->ident
;
452 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
453 struct token
*next
= token
->next
;
456 sym
= handle_func(token
);
457 if (token
->ident
== &__builtin_types_compatible_p_ident
) {
458 token
= builtin_types_compatible_p_expr(token
, &expr
);
461 if (token
->ident
== &__builtin_offsetof_ident
) {
462 token
= builtin_offsetof_expr(token
, &expr
);
465 if (token
->ident
== &_Generic_ident
) {
466 token
= generic_selection(token
->next
, &expr
);
469 } else if (sym
->enum_member
) {
470 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
471 *expr
= *sym
->initializer
;
472 /* we want the right position reported, thus the copy */
473 expr
->pos
= token
->pos
;
474 expr
->flags
= CEF_SET_ENUM
;
479 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
482 * We support types as real first-class citizens, with type
485 * if (typeof(a) == int) ..
487 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
488 sparse_error(token
->pos
, "typename in expression");
491 expr
->symbol_name
= token
->ident
;
495 * A pointer to an lvalue designating a static storage
496 * duration object is an address constant [6.6(9)].
498 if (sym
&& (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_STATIC
)))
499 expr
->flags
= CEF_ADDR
;
506 case TOKEN_WIDE_STRING
:
507 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
508 token
= get_string_constant(token
, expr
);
512 if (token
->special
== '(') {
513 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
515 token
= parens_expression(token
, &expr
->unop
, "in expression");
518 if (token
->special
== '[' && lookup_type(token
->next
)) {
519 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
520 token
= typename(token
->next
, &expr
->symbol
, NULL
);
521 token
= expect(token
, ']', "in type expression");
532 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
534 while (!match_op(token
, ')')) {
535 struct expression
*expr
= NULL
;
536 token
= assignment_expression(token
, &expr
);
539 add_expression(list
, expr
);
540 if (!match_op(token
, ','))
548 * extend to deal with the ambiguous C grammar for parsing
549 * a cast expressions followed by an initializer.
551 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
553 struct expression
*expr
= cast_init_expr
;
556 token
= primary_expression(token
, &expr
);
558 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
559 switch (token
->special
) {
560 case '[': { /* Array dereference */
561 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
562 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
569 token
= parse_expression(token
->next
, &add
->right
);
570 token
= expect(token
, ']', "at end of array dereference");
574 case SPECIAL_INCREMENT
: /* Post-increment */
575 case SPECIAL_DECREMENT
: { /* Post-decrement */
576 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
577 post
->op
= token
->special
;
583 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
584 /* "x->y" is just shorthand for "(*x).y" */
585 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
591 case '.': { /* Structure member dereference */
592 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
596 if (token_type(token
) != TOKEN_IDENT
) {
597 sparse_error(token
->pos
, "Expected member name");
600 deref
->member
= token
->ident
;
601 deref
->member_offset
= -1;
607 case '(': { /* Function call */
608 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
611 token
= expression_list(token
->next
, &call
->args
);
612 token
= expect(token
, ')', "in function call");
626 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
627 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
629 static struct token
*type_info_expression(struct token
*token
,
630 struct expression
**tree
, int type
)
632 struct expression
*expr
= alloc_expression(token
->pos
, type
);
636 expr
->flags
= CEF_SET_ICE
; /* XXX: VLA support will need that changed */
638 if (!match_op(token
, '(') || !lookup_type(token
->next
))
639 return unary_expression(token
, &expr
->cast_expression
);
641 token
= typename(token
->next
, &expr
->cast_type
, NULL
);
643 if (!match_op(token
, ')')) {
644 static const char * error
[] = {
645 [EXPR_SIZEOF
] = "at end of sizeof",
646 [EXPR_ALIGNOF
] = "at end of __alignof__",
647 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
649 return expect(token
, ')', error
[type
]);
654 * C99 ambiguity: the typename might have been the beginning
655 * of a typed initializer expression..
657 if (match_op(token
, '{')) {
658 struct expression
*cast
= alloc_expression(p
->pos
, EXPR_CAST
);
659 cast
->cast_type
= expr
->cast_type
;
660 expr
->cast_type
= NULL
;
661 expr
->cast_expression
= cast
;
662 token
= initializer(&cast
->cast_expression
, token
);
663 token
= postfix_expression(token
, &expr
->cast_expression
, cast
);
668 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
670 if (token_type(token
) == TOKEN_IDENT
) {
671 struct ident
*ident
= token
->ident
;
672 if (ident
->reserved
) {
673 static const struct {
676 } type_information
[] = {
677 { &sizeof_ident
, EXPR_SIZEOF
},
678 { &__alignof___ident
, EXPR_ALIGNOF
},
679 { &__alignof_ident
, EXPR_ALIGNOF
},
680 { &_Alignof_ident
, EXPR_ALIGNOF
},
681 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
684 for (i
= 0; i
< ARRAY_SIZE(type_information
); i
++) {
685 if (ident
== type_information
[i
].id
)
686 return type_info_expression(token
, tree
, type_information
[i
].type
);
691 if (token_type(token
) == TOKEN_SPECIAL
) {
692 if (match_oplist(token
->special
,
693 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
695 struct expression
*unop
;
696 struct expression
*unary
;
699 next
= cast_expression(token
->next
, &unop
);
701 sparse_error(token
->pos
, "Syntax error in unary expression");
705 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
706 unary
->op
= token
->special
;
711 /* possibly constant ones */
712 if (match_oplist(token
->special
, '+', '-', '~', '!', 0)) {
713 struct expression
*unop
;
714 struct expression
*unary
;
717 next
= cast_expression(token
->next
, &unop
);
719 sparse_error(token
->pos
, "Syntax error in unary expression");
723 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
724 unary
->op
= token
->special
;
729 /* Gcc extension: &&label gives the address of a label */
730 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
731 token_type(token
->next
) == TOKEN_IDENT
) {
732 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
733 struct symbol
*sym
= label_symbol(token
->next
, 1);
734 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
735 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
736 add_symbol(&function_computed_target_list
, sym
);
738 check_label_usage(sym
, token
->pos
);
739 label
->flags
= CEF_ADDR
;
740 label
->label_symbol
= sym
;
742 return token
->next
->next
;
747 return postfix_expression(token
, tree
, NULL
);
751 * Ambiguity: a '(' can be either a cast-expression or
752 * a primary-expression depending on whether it is followed
755 * additional ambiguity: a "cast expression" followed by
756 * an initializer is really a postfix-expression.
758 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
760 if (match_op(token
, '(')) {
761 struct token
*next
= token
->next
;
762 if (lookup_type(next
)) {
763 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
764 struct expression
*v
;
768 token
= typename(next
, &sym
, &is_force
);
769 cast
->cast_type
= sym
;
770 token
= expect(token
, ')', "at end of cast operator");
771 if (match_op(token
, '{')) {
772 if (toplevel(block_scope
))
773 sym
->ctype
.modifiers
|= MOD_TOPLEVEL
;
776 "[force] in compound literal");
777 token
= initializer(&cast
->cast_expression
, token
);
778 return postfix_expression(token
, tree
, cast
);
782 cast
->type
= EXPR_FORCE_CAST
;
783 token
= cast_expression(token
, &v
);
786 cast
->cast_expression
= v
;
790 return unary_expression(token
, tree
);
794 * Generic left-to-right binop parsing
796 * This _really_ needs to be inlined, because that makes the inner
797 * function call statically deterministic rather than a totally
798 * unpredictable indirect call. But gcc-3 is so "clever" that it
799 * doesn't do so by default even when you tell it to inline it.
801 * Making it a macro avoids the inlining problem, and also means
802 * that we can pass in the op-comparison as an expression rather
803 * than create a data structure for it.
806 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
807 struct expression *left = NULL; \
808 struct token * next = inner(__token, &left); \
811 while (token_type(next) == TOKEN_SPECIAL) { \
812 struct expression *top, *right = NULL; \
813 int op = next->special; \
817 top = alloc_expression(next->pos, type); \
818 next = inner(next->next, &right); \
820 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
825 top->right = right; \
833 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
836 token
, tree
, EXPR_BINOP
, cast_expression
,
837 (op
== '*') || (op
== '/') || (op
== '%')
841 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
844 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
845 (op
== '+') || (op
== '-')
849 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
852 token
, tree
, EXPR_BINOP
, additive_expression
,
853 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
857 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
860 token
, tree
, EXPR_COMPARE
, shift_expression
,
861 (op
== '<') || (op
== '>') ||
862 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
866 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
869 token
, tree
, EXPR_COMPARE
, relational_expression
,
870 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
874 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
877 token
, tree
, EXPR_BINOP
, equality_expression
,
882 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
885 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
890 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
893 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
898 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
901 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
902 (op
== SPECIAL_LOGICAL_AND
)
906 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
909 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
910 (op
== SPECIAL_LOGICAL_OR
)
914 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
916 token
= logical_or_expression(token
, tree
);
917 if (*tree
&& match_op(token
, '?')) {
918 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
919 expr
->op
= token
->special
;
920 expr
->conditional
= *tree
;
922 token
= parse_expression(token
->next
, &expr
->cond_true
);
923 token
= expect(token
, ':', "in conditional expression");
924 token
= conditional_expression(token
, &expr
->cond_false
);
929 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
931 token
= conditional_expression(token
, tree
);
932 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
933 static const int assignments
[] = {
935 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
936 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
937 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
938 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
939 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
940 int i
, op
= token
->special
;
941 for (i
= 0; i
< ARRAY_SIZE(assignments
); i
++)
942 if (assignments
[i
] == op
) {
943 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
944 struct token
*next
= token
->next
;
948 token
= assignment_expression(next
, &expr
->right
);
950 expression_error(expr
, "expression expected before '%s'", show_token(token
));
957 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
960 token
, tree
, EXPR_COMMA
, assignment_expression
,
965 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
967 return comma_expression(token
,tree
);