user_data: update to stree
[smatch.git] / expression.c
blobc074679147d9bb75a91972b3d25bd22ebab4cea2
1 /*
2 * sparse/expression.c
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
23 * THE SOFTWARE.
25 * This is the expression parsing part of parsing C.
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <limits.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "token.h"
40 #include "parse.h"
41 #include "symbol.h"
42 #include "scope.h"
43 #include "expression.h"
44 #include "target.h"
45 #include "char.h"
47 static int match_oplist(int op, ...)
49 va_list args;
50 int nextop;
52 va_start(args, op);
53 do {
54 nextop = va_arg(args, int);
55 } while (nextop != 0 && nextop != op);
56 va_end(args);
58 return nextop != 0;
61 static struct token *comma_expression(struct token *, struct expression **);
63 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
65 token = expect(token, '(', where);
66 if (match_op(token, '{')) {
67 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
68 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
69 *expr = e;
70 e->statement = stmt;
71 start_symbol_scope(e->pos);
72 token = compound_statement(token->next, stmt);
73 end_symbol_scope();
74 token = expect(token, '}', "at end of statement expression");
75 } else
76 token = parse_expression(token, expr);
77 return expect(token, ')', where);
81 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
82 * conversion
84 static struct symbol *handle_func(struct token *token)
86 struct ident *ident = token->ident;
87 struct symbol *decl, *array;
88 struct string *string;
89 int len;
91 if (ident != &__func___ident &&
92 ident != &__FUNCTION___ident &&
93 ident != &__PRETTY_FUNCTION___ident)
94 return NULL;
96 if (!current_fn || !current_fn->ident)
97 return NULL;
99 /* OK, it's one of ours */
100 array = alloc_symbol(token->pos, SYM_ARRAY);
101 array->ctype.base_type = &char_ctype;
102 array->ctype.alignment = 1;
103 array->endpos = token->pos;
104 decl = alloc_symbol(token->pos, SYM_NODE);
105 decl->ctype.base_type = array;
106 decl->ctype.alignment = 1;
107 decl->ctype.modifiers = MOD_STATIC;
108 decl->endpos = token->pos;
110 /* function-scope, but in NS_SYMBOL */
111 bind_symbol(decl, ident, NS_LABEL);
112 decl->namespace = NS_SYMBOL;
114 len = current_fn->ident->len;
115 string = __alloc_string(len + 1);
116 memcpy(string->data, current_fn->ident->name, len);
117 string->data[len] = 0;
118 string->length = len + 1;
120 decl->initializer = alloc_expression(token->pos, EXPR_STRING);
121 decl->initializer->string = string;
122 decl->initializer->ctype = decl;
123 decl->array_size = alloc_const_expression(token->pos, len + 1);
124 array->array_size = decl->array_size;
125 decl->bit_size = array->bit_size = bytes_to_bits(len + 1);
127 return decl;
130 static struct token *parse_type(struct token *token, struct expression **tree)
132 struct symbol *sym;
133 *tree = alloc_expression(token->pos, EXPR_TYPE);
134 (*tree)->flags = Int_const_expr; /* sic */
135 token = typename(token, &sym, NULL);
136 if (sym->ident)
137 sparse_error(token->pos,
138 "type expression should not include identifier "
139 "\"%s\"", sym->ident->name);
140 (*tree)->symbol = sym;
141 return token;
144 static struct token *builtin_types_compatible_p_expr(struct token *token,
145 struct expression **tree)
147 struct expression *expr = alloc_expression(
148 token->pos, EXPR_COMPARE);
149 expr->flags = Int_const_expr;
150 expr->op = SPECIAL_EQUAL;
151 token = token->next;
152 if (!match_op(token, '('))
153 return expect(token, '(',
154 "after __builtin_types_compatible_p");
155 token = token->next;
156 token = parse_type(token, &expr->left);
157 if (!match_op(token, ','))
158 return expect(token, ',',
159 "in __builtin_types_compatible_p");
160 token = token->next;
161 token = parse_type(token, &expr->right);
162 if (!match_op(token, ')'))
163 return expect(token, ')',
164 "at end of __builtin_types_compatible_p");
165 token = token->next;
167 *tree = expr;
168 return token;
171 static struct token *builtin_offsetof_expr(struct token *token,
172 struct expression **tree)
174 struct expression *expr = NULL;
175 struct expression **p = &expr;
176 struct symbol *sym;
177 int op = '.';
179 token = token->next;
180 if (!match_op(token, '('))
181 return expect(token, '(', "after __builtin_offset");
183 token = token->next;
184 token = typename(token, &sym, NULL);
185 if (sym->ident)
186 sparse_error(token->pos,
187 "type expression should not include identifier "
188 "\"%s\"", sym->ident->name);
190 if (!match_op(token, ','))
191 return expect(token, ',', "in __builtin_offset");
193 while (1) {
194 struct expression *e;
195 switch (op) {
196 case ')':
197 expr->in = sym;
198 *tree = expr;
199 default:
200 return expect(token, ')', "at end of __builtin_offset");
201 case SPECIAL_DEREFERENCE:
202 e = alloc_expression(token->pos, EXPR_OFFSETOF);
203 e->flags = Int_const_expr;
204 e->op = '[';
205 *p = e;
206 p = &e->down;
207 /* fall through */
208 case '.':
209 token = token->next;
210 e = alloc_expression(token->pos, EXPR_OFFSETOF);
211 e->flags = Int_const_expr;
212 e->op = '.';
213 if (token_type(token) != TOKEN_IDENT) {
214 sparse_error(token->pos, "Expected member name");
215 return token;
217 e->ident = token->ident;
218 token = token->next;
219 break;
220 case '[':
221 token = token->next;
222 e = alloc_expression(token->pos, EXPR_OFFSETOF);
223 e->flags = Int_const_expr;
224 e->op = '[';
225 token = parse_expression(token, &e->index);
226 token = expect(token, ']',
227 "at end of array dereference");
228 if (!e->index)
229 return token;
231 *p = e;
232 p = &e->down;
233 op = token_type(token) == TOKEN_SPECIAL ? token->special : 0;
237 #ifndef ULLONG_MAX
238 #define ULLONG_MAX (~0ULL)
239 #endif
241 static unsigned long long parse_num(const char *nptr, char **end)
243 if (nptr[0] == '0' && tolower(nptr[1]) == 'b')
244 return strtoull(&nptr[2], end, 2);
245 return strtoull(nptr, end, 0);
248 static void get_number_value(struct expression *expr, struct token *token)
250 const char *str = token->number;
251 unsigned long long value;
252 char *end;
253 int size = 0, want_unsigned = 0;
254 int overflow = 0, do_warn = 0;
255 int try_unsigned = 1;
256 int bits;
258 errno = 0;
259 value = parse_num(str, &end);
260 if (end == str)
261 goto Float;
262 if (value == ULLONG_MAX && errno == ERANGE)
263 overflow = 1;
264 while (1) {
265 char c = *end++;
266 if (!c) {
267 break;
268 } else if (c == 'u' || c == 'U') {
269 if (want_unsigned)
270 goto Enoint;
271 want_unsigned = 1;
272 } else if (c == 'l' || c == 'L') {
273 if (size)
274 goto Enoint;
275 size = 1;
276 if (*end == c) {
277 size = 2;
278 end++;
280 } else
281 goto Float;
283 if (overflow)
284 goto Eoverflow;
285 /* OK, it's a valid integer */
286 /* decimals can be unsigned only if directly specified as such */
287 if (str[0] != '0' && !want_unsigned)
288 try_unsigned = 0;
289 if (!size) {
290 bits = bits_in_int - 1;
291 if (!(value & (~1ULL << bits))) {
292 if (!(value & (1ULL << bits))) {
293 goto got_it;
294 } else if (try_unsigned) {
295 want_unsigned = 1;
296 goto got_it;
299 size = 1;
300 do_warn = 1;
302 if (size < 2) {
303 bits = bits_in_long - 1;
304 if (!(value & (~1ULL << bits))) {
305 if (!(value & (1ULL << bits))) {
306 goto got_it;
307 } else if (try_unsigned) {
308 want_unsigned = 1;
309 goto got_it;
311 do_warn |= 2;
313 size = 2;
314 do_warn |= 1;
316 bits = bits_in_longlong - 1;
317 if (value & (~1ULL << bits))
318 goto Eoverflow;
319 if (!(value & (1ULL << bits)))
320 goto got_it;
321 if (!try_unsigned)
322 warning(expr->pos, "decimal constant %s is too big for long long",
323 show_token(token));
324 want_unsigned = 1;
325 got_it:
326 if (do_warn)
327 warning(expr->pos, "constant %s is so big it is%s%s%s",
328 show_token(token),
329 want_unsigned ? " unsigned":"",
330 size > 0 ? " long":"",
331 size > 1 ? " long":"");
332 if (do_warn & 2)
333 warning(expr->pos,
334 "decimal constant %s is between LONG_MAX and ULONG_MAX."
335 " For C99 that means long long, C90 compilers are very "
336 "likely to produce unsigned long (and a warning) here",
337 show_token(token));
338 expr->type = EXPR_VALUE;
339 expr->flags = Int_const_expr;
340 expr->ctype = ctype_integer(size, want_unsigned);
341 expr->value = value;
342 return;
343 Eoverflow:
344 error_die(expr->pos, "constant %s is too big even for unsigned long long",
345 show_token(token));
346 return;
347 Float:
348 expr->fvalue = string_to_ld(str, &end);
349 if (str == end)
350 goto Enoint;
352 if (*end && end[1])
353 goto Enoint;
355 if (*end == 'f' || *end == 'F')
356 expr->ctype = &float_ctype;
357 else if (*end == 'l' || *end == 'L')
358 expr->ctype = &ldouble_ctype;
359 else if (!*end)
360 expr->ctype = &double_ctype;
361 else
362 goto Enoint;
364 expr->flags = Float_literal;
365 expr->type = EXPR_FVALUE;
366 return;
368 Enoint:
369 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
372 struct token *primary_expression(struct token *token, struct expression **tree)
374 struct expression *expr = NULL;
376 switch (token_type(token)) {
377 case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3:
378 expr = alloc_expression(token->pos, EXPR_VALUE);
379 expr->flags = Int_const_expr;
380 expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype;
381 get_char_constant(token, &expr->value);
382 token = token->next;
383 break;
385 case TOKEN_NUMBER:
386 expr = alloc_expression(token->pos, EXPR_VALUE);
387 get_number_value(expr, token); /* will see if it's an integer */
388 token = token->next;
389 break;
391 case TOKEN_ZERO_IDENT: {
392 expr = alloc_expression(token->pos, EXPR_SYMBOL);
393 expr->flags = Int_const_expr;
394 expr->ctype = &int_ctype;
395 expr->symbol = &zero_int;
396 expr->symbol_name = token->ident;
397 token = token->next;
398 break;
401 case TOKEN_IDENT: {
402 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
403 struct token *next = token->next;
405 if (!sym) {
406 sym = handle_func(token);
407 if (token->ident == &__builtin_types_compatible_p_ident) {
408 token = builtin_types_compatible_p_expr(token, &expr);
409 break;
411 if (token->ident == &__builtin_offsetof_ident) {
412 token = builtin_offsetof_expr(token, &expr);
413 break;
415 } else if (sym->enum_member) {
416 expr = alloc_expression(token->pos, EXPR_VALUE);
417 *expr = *sym->initializer;
418 /* we want the right position reported, thus the copy */
419 expr->pos = token->pos;
420 expr->flags = Int_const_expr;
421 token = next;
422 break;
425 expr = alloc_expression(token->pos, EXPR_SYMBOL);
428 * We support types as real first-class citizens, with type
429 * comparisons etc:
431 * if (typeof(a) == int) ..
433 if (sym && sym->namespace == NS_TYPEDEF) {
434 sparse_error(token->pos, "typename in expression");
435 sym = NULL;
437 expr->symbol_name = token->ident;
438 expr->symbol = sym;
439 token = next;
440 break;
443 case TOKEN_STRING:
444 case TOKEN_WIDE_STRING:
445 expr = alloc_expression(token->pos, EXPR_STRING);
446 token = get_string_constant(token, expr);
447 break;
449 case TOKEN_SPECIAL:
450 if (token->special == '(') {
451 expr = alloc_expression(token->pos, EXPR_PREOP);
452 expr->op = '(';
453 token = parens_expression(token, &expr->unop, "in expression");
454 if (expr->unop)
455 expr->flags = expr->unop->flags;
456 break;
458 if (token->special == '[' && lookup_type(token->next)) {
459 expr = alloc_expression(token->pos, EXPR_TYPE);
460 expr->flags = Int_const_expr; /* sic */
461 token = typename(token->next, &expr->symbol, NULL);
462 token = expect(token, ']', "in type expression");
463 break;
466 default:
469 *tree = expr;
470 return token;
473 static struct token *expression_list(struct token *token, struct expression_list **list)
475 while (!match_op(token, ')')) {
476 struct expression *expr = NULL;
477 token = assignment_expression(token, &expr);
478 if (!expr)
479 break;
480 add_expression(list, expr);
481 if (!match_op(token, ','))
482 break;
483 token = token->next;
485 return token;
489 * extend to deal with the ambiguous C grammar for parsing
490 * a cast expressions followed by an initializer.
492 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
494 struct expression *expr = cast_init_expr;
496 if (!expr)
497 token = primary_expression(token, &expr);
499 while (expr && token_type(token) == TOKEN_SPECIAL) {
500 switch (token->special) {
501 case '[': { /* Array dereference */
502 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
503 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
505 deref->op = '*';
506 deref->unop = add;
508 add->op = '+';
509 add->left = expr;
510 token = parse_expression(token->next, &add->right);
511 token = expect(token, ']', "at end of array dereference");
512 expr = deref;
513 continue;
515 case SPECIAL_INCREMENT: /* Post-increment */
516 case SPECIAL_DECREMENT: { /* Post-decrement */
517 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
518 post->op = token->special;
519 post->unop = expr;
520 expr = post;
521 token = token->next;
522 continue;
524 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
525 /* "x->y" is just shorthand for "(*x).y" */
526 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
527 inner->op = '*';
528 inner->unop = expr;
529 expr = inner;
531 /* Fall through!! */
532 case '.': { /* Structure member dereference */
533 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
534 deref->op = '.';
535 deref->deref = expr;
536 token = token->next;
537 if (token_type(token) != TOKEN_IDENT) {
538 sparse_error(token->pos, "Expected member name");
539 break;
541 deref->member = token->ident;
542 token = token->next;
543 expr = deref;
544 continue;
547 case '(': { /* Function call */
548 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
549 call->op = '(';
550 call->fn = expr;
551 token = expression_list(token->next, &call->args);
552 token = expect(token, ')', "in function call");
553 expr = call;
554 continue;
557 default:
558 break;
560 break;
562 *tree = expr;
563 return token;
566 static struct token *cast_expression(struct token *token, struct expression **tree);
567 static struct token *unary_expression(struct token *token, struct expression **tree);
569 static struct token *type_info_expression(struct token *token,
570 struct expression **tree, int type)
572 struct expression *expr = alloc_expression(token->pos, type);
573 struct token *p;
575 *tree = expr;
576 expr->flags = Int_const_expr; /* XXX: VLA support will need that changed */
577 token = token->next;
578 if (!match_op(token, '(') || !lookup_type(token->next))
579 return unary_expression(token, &expr->cast_expression);
580 p = token;
581 token = typename(token->next, &expr->cast_type, NULL);
583 if (!match_op(token, ')')) {
584 static const char * error[] = {
585 [EXPR_SIZEOF] = "at end of sizeof",
586 [EXPR_ALIGNOF] = "at end of __alignof__",
587 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
589 return expect(token, ')', error[type]);
592 token = token->next;
594 * C99 ambiguity: the typename might have been the beginning
595 * of a typed initializer expression..
597 if (match_op(token, '{')) {
598 struct expression *cast = alloc_expression(p->pos, EXPR_CAST);
599 cast->cast_type = expr->cast_type;
600 expr->cast_type = NULL;
601 expr->cast_expression = cast;
602 token = initializer(&cast->cast_expression, token);
603 token = postfix_expression(token, &expr->cast_expression, cast);
605 return token;
608 static struct token *unary_expression(struct token *token, struct expression **tree)
610 if (token_type(token) == TOKEN_IDENT) {
611 struct ident *ident = token->ident;
612 if (ident->reserved) {
613 static const struct {
614 struct ident *id;
615 int type;
616 } type_information[] = {
617 { &sizeof_ident, EXPR_SIZEOF },
618 { &__alignof___ident, EXPR_ALIGNOF },
619 { &__alignof_ident, EXPR_ALIGNOF },
620 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
622 int i;
623 for (i = 0; i < ARRAY_SIZE(type_information); i++) {
624 if (ident == type_information[i].id)
625 return type_info_expression(token, tree, type_information[i].type);
630 if (token_type(token) == TOKEN_SPECIAL) {
631 if (match_oplist(token->special,
632 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
633 '&', '*', 0)) {
634 struct expression *unop;
635 struct expression *unary;
636 struct token *next;
638 next = cast_expression(token->next, &unop);
639 if (!unop) {
640 sparse_error(token->pos, "Syntax error in unary expression");
641 *tree = NULL;
642 return next;
644 unary = alloc_expression(token->pos, EXPR_PREOP);
645 unary->op = token->special;
646 unary->unop = unop;
647 *tree = unary;
648 return next;
650 /* possibly constant ones */
651 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
652 struct expression *unop;
653 struct expression *unary;
654 struct token *next;
656 next = cast_expression(token->next, &unop);
657 if (!unop) {
658 sparse_error(token->pos, "Syntax error in unary expression");
659 *tree = NULL;
660 return next;
662 unary = alloc_expression(token->pos, EXPR_PREOP);
663 unary->op = token->special;
664 unary->unop = unop;
665 unary->flags = unop->flags & Int_const_expr;
666 *tree = unary;
667 return next;
669 /* Gcc extension: &&label gives the address of a label */
670 if (match_op(token, SPECIAL_LOGICAL_AND) &&
671 token_type(token->next) == TOKEN_IDENT) {
672 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
673 struct symbol *sym = label_symbol(token->next);
674 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
675 sym->ctype.modifiers |= MOD_ADDRESSABLE;
676 add_symbol(&function_computed_target_list, sym);
678 label->label_symbol = sym;
679 *tree = label;
680 return token->next->next;
685 return postfix_expression(token, tree, NULL);
689 * Ambiguity: a '(' can be either a cast-expression or
690 * a primary-expression depending on whether it is followed
691 * by a type or not.
693 * additional ambiguity: a "cast expression" followed by
694 * an initializer is really a postfix-expression.
696 static struct token *cast_expression(struct token *token, struct expression **tree)
698 if (match_op(token, '(')) {
699 struct token *next = token->next;
700 if (lookup_type(next)) {
701 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
702 struct expression *v;
703 struct symbol *sym;
704 int is_force;
706 token = typename(next, &sym, &is_force);
707 cast->cast_type = sym;
708 token = expect(token, ')', "at end of cast operator");
709 if (match_op(token, '{')) {
710 if (is_force)
711 warning(sym->pos,
712 "[force] in compound literal");
713 token = initializer(&cast->cast_expression, token);
714 return postfix_expression(token, tree, cast);
716 *tree = cast;
717 if (is_force)
718 cast->type = EXPR_FORCE_CAST;
719 token = cast_expression(token, &v);
720 if (!v)
721 return token;
722 cast->cast_expression = v;
723 if (v->flags & Int_const_expr)
724 cast->flags = Int_const_expr;
725 else if (v->flags & Float_literal) /* and _not_ int */
726 cast->flags = Int_const_expr | Float_literal;
727 return token;
730 return unary_expression(token, tree);
734 * Generic left-to-right binop parsing
736 * This _really_ needs to be inlined, because that makes the inner
737 * function call statically deterministic rather than a totally
738 * unpredictable indirect call. But gcc-3 is so "clever" that it
739 * doesn't do so by default even when you tell it to inline it.
741 * Making it a macro avoids the inlining problem, and also means
742 * that we can pass in the op-comparison as an expression rather
743 * than create a data structure for it.
746 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
747 struct expression *left = NULL; \
748 struct token * next = inner(__token, &left); \
750 if (left) { \
751 while (token_type(next) == TOKEN_SPECIAL) { \
752 struct expression *top, *right = NULL; \
753 int op = next->special; \
755 if (!(compare)) \
756 goto out; \
757 top = alloc_expression(next->pos, type); \
758 next = inner(next->next, &right); \
759 if (!right) { \
760 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
761 break; \
763 top->flags = left->flags & right->flags \
764 & Int_const_expr; \
765 top->op = op; \
766 top->left = left; \
767 top->right = right; \
768 left = top; \
771 out: \
772 *tree = left; \
773 return next; \
775 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
777 LR_BINOP_EXPRESSION(
778 token, tree, EXPR_BINOP, cast_expression,
779 (op == '*') || (op == '/') || (op == '%')
783 static struct token *additive_expression(struct token *token, struct expression **tree)
785 LR_BINOP_EXPRESSION(
786 token, tree, EXPR_BINOP, multiplicative_expression,
787 (op == '+') || (op == '-')
791 static struct token *shift_expression(struct token *token, struct expression **tree)
793 LR_BINOP_EXPRESSION(
794 token, tree, EXPR_BINOP, additive_expression,
795 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
799 static struct token *relational_expression(struct token *token, struct expression **tree)
801 LR_BINOP_EXPRESSION(
802 token, tree, EXPR_COMPARE, shift_expression,
803 (op == '<') || (op == '>') ||
804 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
808 static struct token *equality_expression(struct token *token, struct expression **tree)
810 LR_BINOP_EXPRESSION(
811 token, tree, EXPR_COMPARE, relational_expression,
812 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
816 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
818 LR_BINOP_EXPRESSION(
819 token, tree, EXPR_BINOP, equality_expression,
820 (op == '&')
824 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
826 LR_BINOP_EXPRESSION(
827 token, tree, EXPR_BINOP, bitwise_and_expression,
828 (op == '^')
832 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
834 LR_BINOP_EXPRESSION(
835 token, tree, EXPR_BINOP, bitwise_xor_expression,
836 (op == '|')
840 static struct token *logical_and_expression(struct token *token, struct expression **tree)
842 LR_BINOP_EXPRESSION(
843 token, tree, EXPR_LOGICAL, bitwise_or_expression,
844 (op == SPECIAL_LOGICAL_AND)
848 static struct token *logical_or_expression(struct token *token, struct expression **tree)
850 LR_BINOP_EXPRESSION(
851 token, tree, EXPR_LOGICAL, logical_and_expression,
852 (op == SPECIAL_LOGICAL_OR)
856 struct token *conditional_expression(struct token *token, struct expression **tree)
858 token = logical_or_expression(token, tree);
859 if (*tree && match_op(token, '?')) {
860 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
861 expr->op = token->special;
862 expr->left = *tree;
863 *tree = expr;
864 token = parse_expression(token->next, &expr->cond_true);
865 token = expect(token, ':', "in conditional expression");
866 token = conditional_expression(token, &expr->cond_false);
867 if (expr->left && expr->cond_false) {
868 int is_const = expr->left->flags &
869 expr->cond_false->flags &
870 Int_const_expr;
871 if (expr->cond_true)
872 is_const &= expr->cond_true->flags;
873 expr->flags = is_const;
876 return token;
879 struct token *assignment_expression(struct token *token, struct expression **tree)
881 token = conditional_expression(token, tree);
882 if (*tree && token_type(token) == TOKEN_SPECIAL) {
883 static const int assignments[] = {
884 '=',
885 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
886 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
887 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
888 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
889 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
890 int i, op = token->special;
891 for (i = 0; i < ARRAY_SIZE(assignments); i++)
892 if (assignments[i] == op) {
893 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
894 expr->left = *tree;
895 expr->op = op;
896 *tree = expr;
897 return assignment_expression(token->next, &expr->right);
900 return token;
903 static struct token *comma_expression(struct token *token, struct expression **tree)
905 LR_BINOP_EXPRESSION(
906 token, tree, EXPR_COMMA, assignment_expression,
907 (op == ',')
911 struct token *parse_expression(struct token *token, struct expression **tree)
913 return comma_expression(token,tree);