signed: don't warn about unsigned comparisons to macros
[smatch.git] / expression.c
blob7e06e6014a6b91ae0acd70965d09c96332c5f152
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();
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 void get_number_value(struct expression *expr, struct token *token)
273 const char *str = token->number;
274 unsigned long long value;
275 char *end;
276 int size = 0, want_unsigned = 0;
277 int overflow = 0, do_warn = 0;
278 int try_unsigned = 1;
279 int bits;
281 errno = 0;
282 value = strtoull(str, &end, 0);
283 if (end == str)
284 goto Float;
285 if (value == ULLONG_MAX && errno == ERANGE)
286 overflow = 1;
287 while (1) {
288 char c = *end++;
289 if (!c) {
290 break;
291 } else if (c == 'u' || c == 'U') {
292 if (want_unsigned)
293 goto Enoint;
294 want_unsigned = 1;
295 } else if (c == 'l' || c == 'L') {
296 if (size)
297 goto Enoint;
298 size = 1;
299 if (*end == c) {
300 size = 2;
301 end++;
303 } else
304 goto Float;
306 if (overflow)
307 goto Eoverflow;
308 /* OK, it's a valid integer */
309 /* decimals can be unsigned only if directly specified as such */
310 if (str[0] != '0' && !want_unsigned)
311 try_unsigned = 0;
312 if (!size) {
313 bits = bits_in_int - 1;
314 if (!(value & (~1ULL << bits))) {
315 if (!(value & (1ULL << bits))) {
316 goto got_it;
317 } else if (try_unsigned) {
318 want_unsigned = 1;
319 goto got_it;
322 size = 1;
323 do_warn = 1;
325 if (size < 2) {
326 bits = bits_in_long - 1;
327 if (!(value & (~1ULL << bits))) {
328 if (!(value & (1ULL << bits))) {
329 goto got_it;
330 } else if (try_unsigned) {
331 want_unsigned = 1;
332 goto got_it;
334 do_warn |= 2;
336 size = 2;
337 do_warn |= 1;
339 bits = bits_in_longlong - 1;
340 if (value & (~1ULL << bits))
341 goto Eoverflow;
342 if (!(value & (1ULL << bits)))
343 goto got_it;
344 if (!try_unsigned)
345 warning(expr->pos, "decimal constant %s is too big for long long",
346 show_token(token));
347 want_unsigned = 1;
348 got_it:
349 if (do_warn)
350 warning(expr->pos, "constant %s is so big it is%s%s%s",
351 show_token(token),
352 want_unsigned ? " unsigned":"",
353 size > 0 ? " long":"",
354 size > 1 ? " long":"");
355 if (do_warn & 2)
356 warning(expr->pos,
357 "decimal constant %s is between LONG_MAX and ULONG_MAX."
358 " For C99 that means long long, C90 compilers are very "
359 "likely to produce unsigned long (and a warning) here",
360 show_token(token));
361 expr->type = EXPR_VALUE;
362 expr->flags = Int_const_expr;
363 expr->ctype = ctype_integer(size, want_unsigned);
364 expr->value = value;
365 return;
366 Eoverflow:
367 error_die(expr->pos, "constant %s is too big even for unsigned long long",
368 show_token(token));
369 return;
370 Float:
371 expr->fvalue = string_to_ld(str, &end);
372 if (str == end)
373 goto Enoint;
375 if (*end && end[1])
376 goto Enoint;
378 if (*end == 'f' || *end == 'F')
379 expr->ctype = &float_ctype;
380 else if (*end == 'l' || *end == 'L')
381 expr->ctype = &ldouble_ctype;
382 else if (!*end)
383 expr->ctype = &double_ctype;
384 else
385 goto Enoint;
387 expr->flags = Float_literal;
388 expr->type = EXPR_FVALUE;
389 return;
391 Enoint:
392 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
395 struct token *primary_expression(struct token *token, struct expression **tree)
397 struct expression *expr = NULL;
399 switch (token_type(token)) {
400 case TOKEN_CHAR:
401 case TOKEN_WIDE_CHAR:
402 expr = alloc_expression(token->pos, EXPR_VALUE);
403 expr->flags = Int_const_expr;
404 expr->ctype = token_type(token) == TOKEN_CHAR ? &int_ctype : &long_ctype;
405 expr->value = (unsigned char) token->character;
406 token = token->next;
407 break;
409 case TOKEN_NUMBER:
410 expr = alloc_expression(token->pos, EXPR_VALUE);
411 get_number_value(expr, token); /* will see if it's an integer */
412 token = token->next;
413 break;
415 case TOKEN_ZERO_IDENT: {
416 expr = alloc_expression(token->pos, EXPR_SYMBOL);
417 expr->flags = Int_const_expr;
418 expr->ctype = &int_ctype;
419 expr->symbol = &zero_int;
420 expr->symbol_name = token->ident;
421 token = token->next;
422 break;
425 case TOKEN_IDENT: {
426 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
427 struct token *next = token->next;
429 if (!sym) {
430 if (convert_function(token))
431 goto handle_string;
432 if (token->ident == &__builtin_types_compatible_p_ident) {
433 token = builtin_types_compatible_p_expr(token, &expr);
434 break;
436 if (token->ident == &__builtin_offsetof_ident) {
437 token = builtin_offsetof_expr(token, &expr);
438 break;
440 } else if (sym->enum_member) {
441 expr = alloc_expression(token->pos, EXPR_VALUE);
442 *expr = *sym->initializer;
443 /* we want the right position reported, thus the copy */
444 expr->pos = token->pos;
445 expr->flags = Int_const_expr;
446 token = next;
447 break;
450 expr = alloc_expression(token->pos, EXPR_SYMBOL);
453 * We support types as real first-class citizens, with type
454 * comparisons etc:
456 * if (typeof(a) == int) ..
458 if (sym && sym->namespace == NS_TYPEDEF) {
459 sparse_error(token->pos, "typename in expression");
460 sym = NULL;
462 expr->symbol_name = token->ident;
463 expr->symbol = sym;
464 token = next;
465 break;
468 case TOKEN_STRING:
469 case TOKEN_WIDE_STRING: {
470 handle_string:
471 expr = alloc_expression(token->pos, EXPR_STRING);
472 expr->wide = token_type(token) == TOKEN_WIDE_STRING;
473 token = string_expression(token, expr);
474 break;
477 case TOKEN_SPECIAL:
478 if (token->special == '(') {
479 expr = alloc_expression(token->pos, EXPR_PREOP);
480 expr->op = '(';
481 token = parens_expression(token, &expr->unop, "in expression");
482 if (expr->unop)
483 expr->flags = expr->unop->flags;
484 break;
486 if (token->special == '[' && lookup_type(token->next)) {
487 expr = alloc_expression(token->pos, EXPR_TYPE);
488 expr->flags = Int_const_expr; /* sic */
489 token = typename(token->next, &expr->symbol, NULL);
490 token = expect(token, ']', "in type expression");
491 break;
494 default:
497 *tree = expr;
498 return token;
501 static struct token *expression_list(struct token *token, struct expression_list **list)
503 while (!match_op(token, ')')) {
504 struct expression *expr = NULL;
505 token = assignment_expression(token, &expr);
506 if (!expr)
507 break;
508 add_expression(list, expr);
509 if (!match_op(token, ','))
510 break;
511 token = token->next;
513 return token;
517 * extend to deal with the ambiguous C grammar for parsing
518 * a cast expressions followed by an initializer.
520 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
522 struct expression *expr = cast_init_expr;
524 if (!expr)
525 token = primary_expression(token, &expr);
527 while (expr && token_type(token) == TOKEN_SPECIAL) {
528 switch (token->special) {
529 case '[': { /* Array dereference */
530 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
531 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
533 deref->op = '*';
534 deref->unop = add;
536 add->op = '+';
537 add->left = expr;
538 token = parse_expression(token->next, &add->right);
539 token = expect(token, ']', "at end of array dereference");
540 expr = deref;
541 continue;
543 case SPECIAL_INCREMENT: /* Post-increment */
544 case SPECIAL_DECREMENT: { /* Post-decrement */
545 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
546 post->op = token->special;
547 post->unop = expr;
548 expr = post;
549 token = token->next;
550 continue;
552 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
553 /* "x->y" is just shorthand for "(*x).y" */
554 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
555 inner->op = '*';
556 inner->unop = expr;
557 expr = inner;
559 /* Fall through!! */
560 case '.': { /* Structure member dereference */
561 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
562 deref->op = '.';
563 deref->deref = expr;
564 token = token->next;
565 if (token_type(token) != TOKEN_IDENT) {
566 sparse_error(token->pos, "Expected member name");
567 break;
569 deref->member = token->ident;
570 token = token->next;
571 expr = deref;
572 continue;
575 case '(': { /* Function call */
576 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
577 call->op = '(';
578 call->fn = expr;
579 token = expression_list(token->next, &call->args);
580 token = expect(token, ')', "in function call");
581 expr = call;
582 continue;
585 default:
586 break;
588 break;
590 *tree = expr;
591 return token;
594 static struct token *cast_expression(struct token *token, struct expression **tree);
595 static struct token *unary_expression(struct token *token, struct expression **tree);
597 static struct token *type_info_expression(struct token *token,
598 struct expression **tree, int type)
600 struct expression *expr = alloc_expression(token->pos, type);
601 struct token *p;
603 *tree = expr;
604 expr->flags = Int_const_expr; /* XXX: VLA support will need that changed */
605 token = token->next;
606 if (!match_op(token, '(') || !lookup_type(token->next))
607 return unary_expression(token, &expr->cast_expression);
608 p = token;
609 token = typename(token->next, &expr->cast_type, NULL);
611 if (!match_op(token, ')')) {
612 static const char * error[] = {
613 [EXPR_SIZEOF] = "at end of sizeof",
614 [EXPR_ALIGNOF] = "at end of __alignof__",
615 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
617 return expect(token, ')', error[type]);
620 token = token->next;
622 * C99 ambiguity: the typename might have been the beginning
623 * of a typed initializer expression..
625 if (match_op(token, '{')) {
626 struct expression *cast = alloc_expression(p->pos, EXPR_CAST);
627 cast->cast_type = expr->cast_type;
628 expr->cast_type = NULL;
629 expr->cast_expression = cast;
630 token = initializer(&cast->cast_expression, token);
631 token = postfix_expression(token, &expr->cast_expression, cast);
633 return token;
636 static struct token *unary_expression(struct token *token, struct expression **tree)
638 if (token_type(token) == TOKEN_IDENT) {
639 struct ident *ident = token->ident;
640 if (ident->reserved) {
641 static const struct {
642 struct ident *id;
643 int type;
644 } type_information[] = {
645 { &sizeof_ident, EXPR_SIZEOF },
646 { &__alignof___ident, EXPR_ALIGNOF },
647 { &__alignof_ident, EXPR_ALIGNOF },
648 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
650 int i;
651 for (i = 0; i < 3; i++) {
652 if (ident == type_information[i].id)
653 return type_info_expression(token, tree, type_information[i].type);
658 if (token_type(token) == TOKEN_SPECIAL) {
659 if (match_oplist(token->special,
660 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
661 '&', '*', 0)) {
662 struct expression *unop;
663 struct expression *unary;
664 struct token *next;
666 next = cast_expression(token->next, &unop);
667 if (!unop) {
668 sparse_error(token->pos, "Syntax error in unary expression");
669 *tree = NULL;
670 return next;
672 unary = alloc_expression(token->pos, EXPR_PREOP);
673 unary->op = token->special;
674 unary->unop = unop;
675 *tree = unary;
676 return next;
678 /* possibly constant ones */
679 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
680 struct expression *unop;
681 struct expression *unary;
682 struct token *next;
684 next = cast_expression(token->next, &unop);
685 if (!unop) {
686 sparse_error(token->pos, "Syntax error in unary expression");
687 *tree = NULL;
688 return next;
690 unary = alloc_expression(token->pos, EXPR_PREOP);
691 unary->op = token->special;
692 unary->unop = unop;
693 unary->flags = unop->flags & Int_const_expr;
694 *tree = unary;
695 return next;
697 /* Gcc extension: &&label gives the address of a label */
698 if (match_op(token, SPECIAL_LOGICAL_AND) &&
699 token_type(token->next) == TOKEN_IDENT) {
700 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
701 struct symbol *sym = label_symbol(token->next);
702 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
703 sym->ctype.modifiers |= MOD_ADDRESSABLE;
704 add_symbol(&function_computed_target_list, sym);
706 label->label_symbol = sym;
707 *tree = label;
708 return token->next->next;
713 return postfix_expression(token, tree, NULL);
717 * Ambiguity: a '(' can be either a cast-expression or
718 * a primary-expression depending on whether it is followed
719 * by a type or not.
721 * additional ambiguity: a "cast expression" followed by
722 * an initializer is really a postfix-expression.
724 static struct token *cast_expression(struct token *token, struct expression **tree)
726 if (match_op(token, '(')) {
727 struct token *next = token->next;
728 if (lookup_type(next)) {
729 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
730 struct expression *v;
731 struct symbol *sym;
732 int is_force;
734 token = typename(next, &sym, &is_force);
735 cast->cast_type = sym;
736 token = expect(token, ')', "at end of cast operator");
737 if (match_op(token, '{')) {
738 if (is_force)
739 warning(sym->pos,
740 "[force] in compound literal");
741 token = initializer(&cast->cast_expression, token);
742 return postfix_expression(token, tree, cast);
744 *tree = cast;
745 if (is_force)
746 cast->type = EXPR_FORCE_CAST;
747 token = cast_expression(token, &v);
748 if (!v)
749 return token;
750 cast->cast_expression = v;
751 if (v->flags & Int_const_expr)
752 cast->flags = Int_const_expr;
753 else if (v->flags & Float_literal) /* and _not_ int */
754 cast->flags = Int_const_expr | Float_literal;
755 return token;
758 return unary_expression(token, tree);
762 * Generic left-to-right binop parsing
764 * This _really_ needs to be inlined, because that makes the inner
765 * function call statically deterministic rather than a totally
766 * unpredictable indirect call. But gcc-3 is so "clever" that it
767 * doesn't do so by default even when you tell it to inline it.
769 * Making it a macro avoids the inlining problem, and also means
770 * that we can pass in the op-comparison as an expression rather
771 * than create a data structure for it.
774 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
775 struct expression *left = NULL; \
776 struct token * next = inner(__token, &left); \
778 if (left) { \
779 while (token_type(next) == TOKEN_SPECIAL) { \
780 struct expression *top, *right = NULL; \
781 int op = next->special; \
783 if (!(compare)) \
784 goto out; \
785 top = alloc_expression(next->pos, type); \
786 next = inner(next->next, &right); \
787 if (!right) { \
788 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
789 break; \
791 top->flags = left->flags & right->flags \
792 & Int_const_expr; \
793 top->op = op; \
794 top->left = left; \
795 top->right = right; \
796 left = top; \
799 out: \
800 *tree = left; \
801 return next; \
803 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
805 LR_BINOP_EXPRESSION(
806 token, tree, EXPR_BINOP, cast_expression,
807 (op == '*') || (op == '/') || (op == '%')
811 static struct token *additive_expression(struct token *token, struct expression **tree)
813 LR_BINOP_EXPRESSION(
814 token, tree, EXPR_BINOP, multiplicative_expression,
815 (op == '+') || (op == '-')
819 static struct token *shift_expression(struct token *token, struct expression **tree)
821 LR_BINOP_EXPRESSION(
822 token, tree, EXPR_BINOP, additive_expression,
823 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
827 static struct token *relational_expression(struct token *token, struct expression **tree)
829 LR_BINOP_EXPRESSION(
830 token, tree, EXPR_COMPARE, shift_expression,
831 (op == '<') || (op == '>') ||
832 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
836 static struct token *equality_expression(struct token *token, struct expression **tree)
838 LR_BINOP_EXPRESSION(
839 token, tree, EXPR_COMPARE, relational_expression,
840 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
844 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
846 LR_BINOP_EXPRESSION(
847 token, tree, EXPR_BINOP, equality_expression,
848 (op == '&')
852 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
854 LR_BINOP_EXPRESSION(
855 token, tree, EXPR_BINOP, bitwise_and_expression,
856 (op == '^')
860 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
862 LR_BINOP_EXPRESSION(
863 token, tree, EXPR_BINOP, bitwise_xor_expression,
864 (op == '|')
868 static struct token *logical_and_expression(struct token *token, struct expression **tree)
870 LR_BINOP_EXPRESSION(
871 token, tree, EXPR_LOGICAL, bitwise_or_expression,
872 (op == SPECIAL_LOGICAL_AND)
876 static struct token *logical_or_expression(struct token *token, struct expression **tree)
878 LR_BINOP_EXPRESSION(
879 token, tree, EXPR_LOGICAL, logical_and_expression,
880 (op == SPECIAL_LOGICAL_OR)
884 struct token *conditional_expression(struct token *token, struct expression **tree)
886 token = logical_or_expression(token, tree);
887 if (*tree && match_op(token, '?')) {
888 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
889 expr->op = token->special;
890 expr->left = *tree;
891 *tree = expr;
892 token = parse_expression(token->next, &expr->cond_true);
893 token = expect(token, ':', "in conditional expression");
894 token = conditional_expression(token, &expr->cond_false);
895 if (expr->left && expr->cond_false) {
896 int is_const = expr->left->flags &
897 expr->cond_false->flags &
898 Int_const_expr;
899 if (expr->cond_true)
900 is_const &= expr->cond_true->flags;
901 expr->flags = is_const;
904 return token;
907 struct token *assignment_expression(struct token *token, struct expression **tree)
909 token = conditional_expression(token, tree);
910 if (*tree && token_type(token) == TOKEN_SPECIAL) {
911 static const int assignments[] = {
912 '=',
913 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
914 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
915 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
916 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
917 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
918 int i, op = token->special;
919 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
920 if (assignments[i] == op) {
921 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
922 expr->left = *tree;
923 expr->op = op;
924 *tree = expr;
925 return assignment_expression(token->next, &expr->right);
928 return token;
931 static struct token *comma_expression(struct token *token, struct expression **tree)
933 LR_BINOP_EXPRESSION(
934 token, tree, EXPR_COMMA, assignment_expression,
935 (op == ',')
939 struct token *parse_expression(struct token *token, struct expression **tree)
941 return comma_expression(token,tree);