sval: introduce sval_min/max()
[smatch.git] / expression.c
blob1132d51004b88f258c9910d7918439a57f15df52
1 /*
2 * sparse/expression.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * This is the expression parsing part of parsing C.
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "scope.h"
27 #include "expression.h"
28 #include "target.h"
30 static int match_oplist(int op, ...)
32 va_list args;
33 int nextop;
35 va_start(args, op);
36 do {
37 nextop = va_arg(args, int);
38 } while (nextop != 0 && nextop != op);
39 va_end(args);
41 return nextop != 0;
44 static struct token *comma_expression(struct token *, struct expression **);
46 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
48 token = expect(token, '(', where);
49 if (match_op(token, '{')) {
50 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
51 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
52 *expr = e;
53 e->statement = stmt;
54 start_symbol_scope(e->pos);
55 token = compound_statement(token->next, stmt);
56 end_symbol_scope();
57 token = expect(token, '}', "at end of statement expression");
58 } else
59 token = parse_expression(token, expr);
60 return expect(token, ')', where);
64 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
65 * conversion
67 static int convert_one_fn_token(struct token *token)
69 struct symbol *sym = current_fn;
71 if (sym) {
72 struct ident *ident = sym->ident;
73 if (ident) {
74 int len = ident->len;
75 struct string *string;
77 string = __alloc_string(len+1);
78 memcpy(string->data, ident->name, len);
79 string->data[len] = 0;
80 string->length = len+1;
81 token_type(token) = TOKEN_STRING;
82 token->string = string;
83 return 1;
86 return 0;
89 static int convert_function(struct token *next)
91 int retval = 0;
92 for (;;) {
93 struct token *token = next;
94 next = next->next;
95 switch (token_type(token)) {
96 case TOKEN_STRING:
97 continue;
98 case TOKEN_IDENT:
99 if (token->ident == &__func___ident ||
100 token->ident == &__FUNCTION___ident ||
101 token->ident == &__PRETTY_FUNCTION___ident) {
102 if (!convert_one_fn_token(token))
103 break;
104 retval = 1;
105 continue;
107 /* Fall through */
108 default:
109 break;
111 break;
113 return retval;
116 static struct token *parse_type(struct token *token, struct expression **tree)
118 struct symbol *sym;
119 *tree = alloc_expression(token->pos, EXPR_TYPE);
120 (*tree)->flags = Int_const_expr; /* sic */
121 token = typename(token, &sym, NULL);
122 if (sym->ident)
123 sparse_error(token->pos,
124 "type expression should not include identifier "
125 "\"%s\"", sym->ident->name);
126 (*tree)->symbol = sym;
127 return token;
130 static struct token *builtin_types_compatible_p_expr(struct token *token,
131 struct expression **tree)
133 struct expression *expr = alloc_expression(
134 token->pos, EXPR_COMPARE);
135 expr->flags = Int_const_expr;
136 expr->op = SPECIAL_EQUAL;
137 token = token->next;
138 if (!match_op(token, '('))
139 return expect(token, '(',
140 "after __builtin_types_compatible_p");
141 token = token->next;
142 token = parse_type(token, &expr->left);
143 if (!match_op(token, ','))
144 return expect(token, ',',
145 "in __builtin_types_compatible_p");
146 token = token->next;
147 token = parse_type(token, &expr->right);
148 if (!match_op(token, ')'))
149 return expect(token, ')',
150 "at end of __builtin_types_compatible_p");
151 token = token->next;
153 *tree = expr;
154 return token;
157 static struct token *builtin_offsetof_expr(struct token *token,
158 struct expression **tree)
160 struct expression *expr = NULL;
161 struct expression **p = &expr;
162 struct symbol *sym;
163 int op = '.';
165 token = token->next;
166 if (!match_op(token, '('))
167 return expect(token, '(', "after __builtin_offset");
169 token = token->next;
170 token = typename(token, &sym, NULL);
171 if (sym->ident)
172 sparse_error(token->pos,
173 "type expression should not include identifier "
174 "\"%s\"", sym->ident->name);
176 if (!match_op(token, ','))
177 return expect(token, ',', "in __builtin_offset");
179 while (1) {
180 struct expression *e;
181 switch (op) {
182 case ')':
183 expr->in = sym;
184 *tree = expr;
185 default:
186 return expect(token, ')', "at end of __builtin_offset");
187 case SPECIAL_DEREFERENCE:
188 e = alloc_expression(token->pos, EXPR_OFFSETOF);
189 e->flags = Int_const_expr;
190 e->op = '[';
191 *p = e;
192 p = &e->down;
193 /* fall through */
194 case '.':
195 token = token->next;
196 e = alloc_expression(token->pos, EXPR_OFFSETOF);
197 e->flags = Int_const_expr;
198 e->op = '.';
199 if (token_type(token) != TOKEN_IDENT) {
200 sparse_error(token->pos, "Expected member name");
201 return token;
203 e->ident = token->ident;
204 token = token->next;
205 break;
206 case '[':
207 token = token->next;
208 e = alloc_expression(token->pos, EXPR_OFFSETOF);
209 e->flags = Int_const_expr;
210 e->op = '[';
211 token = parse_expression(token, &e->index);
212 token = expect(token, ']',
213 "at end of array dereference");
214 if (!e->index)
215 return token;
217 *p = e;
218 p = &e->down;
219 op = token_type(token) == TOKEN_SPECIAL ? token->special : 0;
223 static struct token *string_expression(struct token *token, struct expression *expr)
225 struct string *string = token->string;
226 struct token *next = token->next;
227 int stringtype = token_type(token);
229 convert_function(token);
231 if (token_type(next) == stringtype) {
232 int totlen = string->length-1;
233 char *data;
235 do {
236 totlen += next->string->length-1;
237 next = next->next;
238 } while (token_type(next) == stringtype);
240 if (totlen > MAX_STRING) {
241 warning(token->pos, "trying to concatenate %d-character string (%d bytes max)", totlen, MAX_STRING);
242 totlen = MAX_STRING;
245 string = __alloc_string(totlen+1);
246 string->length = totlen+1;
247 data = string->data;
248 next = token;
249 do {
250 struct string *s = next->string;
251 int len = s->length-1;
253 if (len > totlen)
254 len = totlen;
255 totlen -= len;
257 next = next->next;
258 memcpy(data, s->data, len);
259 data += len;
260 } while (token_type(next) == stringtype);
261 *data = '\0';
263 expr->string = string;
264 return next;
267 #ifndef ULLONG_MAX
268 #define ULLONG_MAX (~0ULL)
269 #endif
271 static unsigned long long parse_num(const char *nptr, char **end)
273 if (nptr[0] == '0' && tolower(nptr[1]) == 'b')
274 return strtoull(&nptr[2], end, 2);
275 return strtoull(nptr, end, 0);
278 static void get_number_value(struct expression *expr, struct token *token)
280 const char *str = token->number;
281 unsigned long long value;
282 char *end;
283 int size = 0, want_unsigned = 0;
284 int overflow = 0, do_warn = 0;
285 int try_unsigned = 1;
286 int bits;
288 errno = 0;
289 value = parse_num(str, &end);
290 if (end == str)
291 goto Float;
292 if (value == ULLONG_MAX && errno == ERANGE)
293 overflow = 1;
294 while (1) {
295 char c = *end++;
296 if (!c) {
297 break;
298 } else if (c == 'u' || c == 'U') {
299 if (want_unsigned)
300 goto Enoint;
301 want_unsigned = 1;
302 } else if (c == 'l' || c == 'L') {
303 if (size)
304 goto Enoint;
305 size = 1;
306 if (*end == c) {
307 size = 2;
308 end++;
310 } else
311 goto Float;
313 if (overflow)
314 goto Eoverflow;
315 /* OK, it's a valid integer */
316 /* decimals can be unsigned only if directly specified as such */
317 if (str[0] != '0' && !want_unsigned)
318 try_unsigned = 0;
319 if (!size) {
320 bits = bits_in_int - 1;
321 if (!(value & (~1ULL << bits))) {
322 if (!(value & (1ULL << bits))) {
323 goto got_it;
324 } else if (try_unsigned) {
325 want_unsigned = 1;
326 goto got_it;
329 size = 1;
330 do_warn = 1;
332 if (size < 2) {
333 bits = bits_in_long - 1;
334 if (!(value & (~1ULL << bits))) {
335 if (!(value & (1ULL << bits))) {
336 goto got_it;
337 } else if (try_unsigned) {
338 want_unsigned = 1;
339 goto got_it;
341 do_warn |= 2;
343 size = 2;
344 do_warn |= 1;
346 bits = bits_in_longlong - 1;
347 if (value & (~1ULL << bits))
348 goto Eoverflow;
349 if (!(value & (1ULL << bits)))
350 goto got_it;
351 if (!try_unsigned)
352 warning(expr->pos, "decimal constant %s is too big for long long",
353 show_token(token));
354 want_unsigned = 1;
355 got_it:
356 if (do_warn)
357 warning(expr->pos, "constant %s is so big it is%s%s%s",
358 show_token(token),
359 want_unsigned ? " unsigned":"",
360 size > 0 ? " long":"",
361 size > 1 ? " long":"");
362 if (do_warn & 2)
363 warning(expr->pos,
364 "decimal constant %s is between LONG_MAX and ULONG_MAX."
365 " For C99 that means long long, C90 compilers are very "
366 "likely to produce unsigned long (and a warning) here",
367 show_token(token));
368 expr->type = EXPR_VALUE;
369 expr->flags = Int_const_expr;
370 expr->ctype = ctype_integer(size, want_unsigned);
371 expr->value = value;
372 return;
373 Eoverflow:
374 error_die(expr->pos, "constant %s is too big even for unsigned long long",
375 show_token(token));
376 return;
377 Float:
378 expr->fvalue = string_to_ld(str, &end);
379 if (str == end)
380 goto Enoint;
382 if (*end && end[1])
383 goto Enoint;
385 if (*end == 'f' || *end == 'F')
386 expr->ctype = &float_ctype;
387 else if (*end == 'l' || *end == 'L')
388 expr->ctype = &ldouble_ctype;
389 else if (!*end)
390 expr->ctype = &double_ctype;
391 else
392 goto Enoint;
394 expr->flags = Float_literal;
395 expr->type = EXPR_FVALUE;
396 return;
398 Enoint:
399 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
402 struct token *primary_expression(struct token *token, struct expression **tree)
404 struct expression *expr = NULL;
406 switch (token_type(token)) {
407 case TOKEN_CHAR:
408 case TOKEN_WIDE_CHAR:
409 expr = alloc_expression(token->pos, EXPR_VALUE);
410 expr->flags = Int_const_expr;
411 expr->ctype = token_type(token) == TOKEN_CHAR ? &int_ctype : &long_ctype;
412 expr->value = (unsigned char) token->character;
413 token = token->next;
414 break;
416 case TOKEN_NUMBER:
417 expr = alloc_expression(token->pos, EXPR_VALUE);
418 get_number_value(expr, token); /* will see if it's an integer */
419 token = token->next;
420 break;
422 case TOKEN_ZERO_IDENT: {
423 expr = alloc_expression(token->pos, EXPR_SYMBOL);
424 expr->flags = Int_const_expr;
425 expr->ctype = &int_ctype;
426 expr->symbol = &zero_int;
427 expr->symbol_name = token->ident;
428 token = token->next;
429 break;
432 case TOKEN_IDENT: {
433 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
434 struct token *next = token->next;
436 if (!sym) {
437 if (convert_function(token))
438 goto handle_string;
439 if (token->ident == &__builtin_types_compatible_p_ident) {
440 token = builtin_types_compatible_p_expr(token, &expr);
441 break;
443 if (token->ident == &__builtin_offsetof_ident) {
444 token = builtin_offsetof_expr(token, &expr);
445 break;
447 } else if (sym->enum_member) {
448 expr = alloc_expression(token->pos, EXPR_VALUE);
449 *expr = *sym->initializer;
450 /* we want the right position reported, thus the copy */
451 expr->pos = token->pos;
452 expr->flags = Int_const_expr;
453 token = next;
454 break;
457 expr = alloc_expression(token->pos, EXPR_SYMBOL);
460 * We support types as real first-class citizens, with type
461 * comparisons etc:
463 * if (typeof(a) == int) ..
465 if (sym && sym->namespace == NS_TYPEDEF) {
466 sparse_error(token->pos, "typename in expression");
467 sym = NULL;
469 expr->symbol_name = token->ident;
470 expr->symbol = sym;
471 token = next;
472 break;
475 case TOKEN_STRING:
476 case TOKEN_WIDE_STRING: {
477 handle_string:
478 expr = alloc_expression(token->pos, EXPR_STRING);
479 expr->wide = token_type(token) == TOKEN_WIDE_STRING;
480 token = string_expression(token, expr);
481 break;
484 case TOKEN_SPECIAL:
485 if (token->special == '(') {
486 expr = alloc_expression(token->pos, EXPR_PREOP);
487 expr->op = '(';
488 token = parens_expression(token, &expr->unop, "in expression");
489 if (expr->unop)
490 expr->flags = expr->unop->flags;
491 break;
493 if (token->special == '[' && lookup_type(token->next)) {
494 expr = alloc_expression(token->pos, EXPR_TYPE);
495 expr->flags = Int_const_expr; /* sic */
496 token = typename(token->next, &expr->symbol, NULL);
497 token = expect(token, ']', "in type expression");
498 break;
501 default:
504 *tree = expr;
505 return token;
508 static struct token *expression_list(struct token *token, struct expression_list **list)
510 while (!match_op(token, ')')) {
511 struct expression *expr = NULL;
512 token = assignment_expression(token, &expr);
513 if (!expr)
514 break;
515 add_expression(list, expr);
516 if (!match_op(token, ','))
517 break;
518 token = token->next;
520 return token;
524 * extend to deal with the ambiguous C grammar for parsing
525 * a cast expressions followed by an initializer.
527 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
529 struct expression *expr = cast_init_expr;
531 if (!expr)
532 token = primary_expression(token, &expr);
534 while (expr && token_type(token) == TOKEN_SPECIAL) {
535 switch (token->special) {
536 case '[': { /* Array dereference */
537 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
538 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
540 deref->op = '*';
541 deref->unop = add;
543 add->op = '+';
544 add->left = expr;
545 token = parse_expression(token->next, &add->right);
546 token = expect(token, ']', "at end of array dereference");
547 expr = deref;
548 continue;
550 case SPECIAL_INCREMENT: /* Post-increment */
551 case SPECIAL_DECREMENT: { /* Post-decrement */
552 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
553 post->op = token->special;
554 post->unop = expr;
555 expr = post;
556 token = token->next;
557 continue;
559 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
560 /* "x->y" is just shorthand for "(*x).y" */
561 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
562 inner->op = '*';
563 inner->unop = expr;
564 expr = inner;
566 /* Fall through!! */
567 case '.': { /* Structure member dereference */
568 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
569 deref->op = '.';
570 deref->deref = expr;
571 token = token->next;
572 if (token_type(token) != TOKEN_IDENT) {
573 sparse_error(token->pos, "Expected member name");
574 break;
576 deref->member = token->ident;
577 token = token->next;
578 expr = deref;
579 continue;
582 case '(': { /* Function call */
583 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
584 call->op = '(';
585 call->fn = expr;
586 token = expression_list(token->next, &call->args);
587 token = expect(token, ')', "in function call");
588 expr = call;
589 continue;
592 default:
593 break;
595 break;
597 *tree = expr;
598 return token;
601 static struct token *cast_expression(struct token *token, struct expression **tree);
602 static struct token *unary_expression(struct token *token, struct expression **tree);
604 static struct token *type_info_expression(struct token *token,
605 struct expression **tree, int type)
607 struct expression *expr = alloc_expression(token->pos, type);
608 struct token *p;
610 *tree = expr;
611 expr->flags = Int_const_expr; /* XXX: VLA support will need that changed */
612 token = token->next;
613 if (!match_op(token, '(') || !lookup_type(token->next))
614 return unary_expression(token, &expr->cast_expression);
615 p = token;
616 token = typename(token->next, &expr->cast_type, NULL);
618 if (!match_op(token, ')')) {
619 static const char * error[] = {
620 [EXPR_SIZEOF] = "at end of sizeof",
621 [EXPR_ALIGNOF] = "at end of __alignof__",
622 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
624 return expect(token, ')', error[type]);
627 token = token->next;
629 * C99 ambiguity: the typename might have been the beginning
630 * of a typed initializer expression..
632 if (match_op(token, '{')) {
633 struct expression *cast = alloc_expression(p->pos, EXPR_CAST);
634 cast->cast_type = expr->cast_type;
635 expr->cast_type = NULL;
636 expr->cast_expression = cast;
637 token = initializer(&cast->cast_expression, token);
638 token = postfix_expression(token, &expr->cast_expression, cast);
640 return token;
643 static struct token *unary_expression(struct token *token, struct expression **tree)
645 if (token_type(token) == TOKEN_IDENT) {
646 struct ident *ident = token->ident;
647 if (ident->reserved) {
648 static const struct {
649 struct ident *id;
650 int type;
651 } type_information[] = {
652 { &sizeof_ident, EXPR_SIZEOF },
653 { &__alignof___ident, EXPR_ALIGNOF },
654 { &__alignof_ident, EXPR_ALIGNOF },
655 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
657 int i;
658 for (i = 0; i < 3; i++) {
659 if (ident == type_information[i].id)
660 return type_info_expression(token, tree, type_information[i].type);
665 if (token_type(token) == TOKEN_SPECIAL) {
666 if (match_oplist(token->special,
667 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
668 '&', '*', 0)) {
669 struct expression *unop;
670 struct expression *unary;
671 struct token *next;
673 next = cast_expression(token->next, &unop);
674 if (!unop) {
675 sparse_error(token->pos, "Syntax error in unary expression");
676 *tree = NULL;
677 return next;
679 unary = alloc_expression(token->pos, EXPR_PREOP);
680 unary->op = token->special;
681 unary->unop = unop;
682 *tree = unary;
683 return next;
685 /* possibly constant ones */
686 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
687 struct expression *unop;
688 struct expression *unary;
689 struct token *next;
691 next = cast_expression(token->next, &unop);
692 if (!unop) {
693 sparse_error(token->pos, "Syntax error in unary expression");
694 *tree = NULL;
695 return next;
697 unary = alloc_expression(token->pos, EXPR_PREOP);
698 unary->op = token->special;
699 unary->unop = unop;
700 unary->flags = unop->flags & Int_const_expr;
701 *tree = unary;
702 return next;
704 /* Gcc extension: &&label gives the address of a label */
705 if (match_op(token, SPECIAL_LOGICAL_AND) &&
706 token_type(token->next) == TOKEN_IDENT) {
707 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
708 struct symbol *sym = label_symbol(token->next);
709 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
710 sym->ctype.modifiers |= MOD_ADDRESSABLE;
711 add_symbol(&function_computed_target_list, sym);
713 label->label_symbol = sym;
714 *tree = label;
715 return token->next->next;
720 return postfix_expression(token, tree, NULL);
724 * Ambiguity: a '(' can be either a cast-expression or
725 * a primary-expression depending on whether it is followed
726 * by a type or not.
728 * additional ambiguity: a "cast expression" followed by
729 * an initializer is really a postfix-expression.
731 static struct token *cast_expression(struct token *token, struct expression **tree)
733 if (match_op(token, '(')) {
734 struct token *next = token->next;
735 if (lookup_type(next)) {
736 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
737 struct expression *v;
738 struct symbol *sym;
739 int is_force;
741 token = typename(next, &sym, &is_force);
742 cast->cast_type = sym;
743 token = expect(token, ')', "at end of cast operator");
744 if (match_op(token, '{')) {
745 if (is_force)
746 warning(sym->pos,
747 "[force] in compound literal");
748 token = initializer(&cast->cast_expression, token);
749 return postfix_expression(token, tree, cast);
751 *tree = cast;
752 if (is_force)
753 cast->type = EXPR_FORCE_CAST;
754 token = cast_expression(token, &v);
755 if (!v)
756 return token;
757 cast->cast_expression = v;
758 if (v->flags & Int_const_expr)
759 cast->flags = Int_const_expr;
760 else if (v->flags & Float_literal) /* and _not_ int */
761 cast->flags = Int_const_expr | Float_literal;
762 return token;
765 return unary_expression(token, tree);
769 * Generic left-to-right binop parsing
771 * This _really_ needs to be inlined, because that makes the inner
772 * function call statically deterministic rather than a totally
773 * unpredictable indirect call. But gcc-3 is so "clever" that it
774 * doesn't do so by default even when you tell it to inline it.
776 * Making it a macro avoids the inlining problem, and also means
777 * that we can pass in the op-comparison as an expression rather
778 * than create a data structure for it.
781 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
782 struct expression *left = NULL; \
783 struct token * next = inner(__token, &left); \
785 if (left) { \
786 while (token_type(next) == TOKEN_SPECIAL) { \
787 struct expression *top, *right = NULL; \
788 int op = next->special; \
790 if (!(compare)) \
791 goto out; \
792 top = alloc_expression(next->pos, type); \
793 next = inner(next->next, &right); \
794 if (!right) { \
795 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
796 break; \
798 top->flags = left->flags & right->flags \
799 & Int_const_expr; \
800 top->op = op; \
801 top->left = left; \
802 top->right = right; \
803 left = top; \
806 out: \
807 *tree = left; \
808 return next; \
810 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
812 LR_BINOP_EXPRESSION(
813 token, tree, EXPR_BINOP, cast_expression,
814 (op == '*') || (op == '/') || (op == '%')
818 static struct token *additive_expression(struct token *token, struct expression **tree)
820 LR_BINOP_EXPRESSION(
821 token, tree, EXPR_BINOP, multiplicative_expression,
822 (op == '+') || (op == '-')
826 static struct token *shift_expression(struct token *token, struct expression **tree)
828 LR_BINOP_EXPRESSION(
829 token, tree, EXPR_BINOP, additive_expression,
830 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
834 static struct token *relational_expression(struct token *token, struct expression **tree)
836 LR_BINOP_EXPRESSION(
837 token, tree, EXPR_COMPARE, shift_expression,
838 (op == '<') || (op == '>') ||
839 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
843 static struct token *equality_expression(struct token *token, struct expression **tree)
845 LR_BINOP_EXPRESSION(
846 token, tree, EXPR_COMPARE, relational_expression,
847 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
851 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
853 LR_BINOP_EXPRESSION(
854 token, tree, EXPR_BINOP, equality_expression,
855 (op == '&')
859 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
861 LR_BINOP_EXPRESSION(
862 token, tree, EXPR_BINOP, bitwise_and_expression,
863 (op == '^')
867 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
869 LR_BINOP_EXPRESSION(
870 token, tree, EXPR_BINOP, bitwise_xor_expression,
871 (op == '|')
875 static struct token *logical_and_expression(struct token *token, struct expression **tree)
877 LR_BINOP_EXPRESSION(
878 token, tree, EXPR_LOGICAL, bitwise_or_expression,
879 (op == SPECIAL_LOGICAL_AND)
883 static struct token *logical_or_expression(struct token *token, struct expression **tree)
885 LR_BINOP_EXPRESSION(
886 token, tree, EXPR_LOGICAL, logical_and_expression,
887 (op == SPECIAL_LOGICAL_OR)
891 struct token *conditional_expression(struct token *token, struct expression **tree)
893 token = logical_or_expression(token, tree);
894 if (*tree && match_op(token, '?')) {
895 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
896 expr->op = token->special;
897 expr->left = *tree;
898 *tree = expr;
899 token = parse_expression(token->next, &expr->cond_true);
900 token = expect(token, ':', "in conditional expression");
901 token = conditional_expression(token, &expr->cond_false);
902 if (expr->left && expr->cond_false) {
903 int is_const = expr->left->flags &
904 expr->cond_false->flags &
905 Int_const_expr;
906 if (expr->cond_true)
907 is_const &= expr->cond_true->flags;
908 expr->flags = is_const;
911 return token;
914 struct token *assignment_expression(struct token *token, struct expression **tree)
916 token = conditional_expression(token, tree);
917 if (*tree && token_type(token) == TOKEN_SPECIAL) {
918 static const int assignments[] = {
919 '=',
920 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
921 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
922 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
923 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
924 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
925 int i, op = token->special;
926 for (i = 0; i < ARRAY_SIZE(assignments); i++)
927 if (assignments[i] == op) {
928 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
929 expr->left = *tree;
930 expr->op = op;
931 *tree = expr;
932 return assignment_expression(token->next, &expr->right);
935 return token;
938 static struct token *comma_expression(struct token *token, struct expression **tree)
940 LR_BINOP_EXPRESSION(
941 token, tree, EXPR_COMMA, assignment_expression,
942 (op == ',')
946 struct token *parse_expression(struct token *token, struct expression **tree)
948 return comma_expression(token,tree);