Typo: <= instead of >=
[smatch.git] / expression.c
blobf634b0797cd9cd56bcc9001b98b4c385ed655ab3
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, 0);
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, 0);
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;
228 convert_function(token);
230 if (token_type(next) == TOKEN_STRING) {
231 int totlen = string->length-1;
232 char *data;
234 do {
235 totlen += next->string->length-1;
236 next = next->next;
237 } while (token_type(next) == TOKEN_STRING);
239 if (totlen > MAX_STRING) {
240 warning(token->pos, "trying to concatenate %d-character string (%d bytes max)", totlen, MAX_STRING);
241 totlen = MAX_STRING;
244 string = __alloc_string(totlen+1);
245 string->length = totlen+1;
246 data = string->data;
247 next = token;
248 do {
249 struct string *s = next->string;
250 int len = s->length-1;
252 if (len > totlen)
253 len = totlen;
254 totlen -= len;
256 next = next->next;
257 memcpy(data, s->data, len);
258 data += len;
259 } while (token_type(next) == TOKEN_STRING);
260 *data = '\0';
262 expr->string = string;
263 return next;
266 #ifndef ULLONG_MAX
267 #define ULLONG_MAX (~0ULL)
268 #endif
270 static void get_number_value(struct expression *expr, struct token *token)
272 const char *str = token->number;
273 unsigned long long value;
274 char *end;
275 unsigned long modifiers = 0;
276 int overflow = 0, do_warn = 0;
277 int try_unsigned = 1;
278 int bits;
280 errno = 0;
281 value = strtoull(str, &end, 0);
282 if (end == str)
283 goto Float;
284 if (value == ULLONG_MAX && errno == ERANGE)
285 overflow = 1;
286 while (1) {
287 unsigned long added;
288 char c = *end++;
289 if (!c) {
290 break;
291 } else if (c == 'u' || c == 'U') {
292 added = MOD_UNSIGNED;
293 } else if (c == 'l' || c == 'L') {
294 added = MOD_LONG;
295 if (*end == c) {
296 added |= MOD_LONGLONG;
297 end++;
299 } else
300 goto Float;
301 if (modifiers & added)
302 goto Enoint;
303 modifiers |= added;
305 if (overflow)
306 goto Eoverflow;
307 /* OK, it's a valid integer */
308 /* decimals can be unsigned only if directly specified as such */
309 if (str[0] != '0' && !(modifiers & MOD_UNSIGNED))
310 try_unsigned = 0;
311 if (!(modifiers & MOD_LONG)) {
312 bits = bits_in_int - 1;
313 if (!(value & (~1ULL << bits))) {
314 if (!(value & (1ULL << bits))) {
315 goto got_it;
316 } else if (try_unsigned) {
317 modifiers |= MOD_UNSIGNED;
318 goto got_it;
321 modifiers |= MOD_LONG;
322 do_warn = 1;
324 if (!(modifiers & MOD_LONGLONG)) {
325 bits = bits_in_long - 1;
326 if (!(value & (~1ULL << bits))) {
327 if (!(value & (1ULL << bits))) {
328 goto got_it;
329 } else if (try_unsigned) {
330 modifiers |= MOD_UNSIGNED;
331 goto got_it;
333 do_warn |= 2;
335 modifiers |= MOD_LONGLONG;
336 do_warn |= 1;
338 bits = bits_in_longlong - 1;
339 if (value & (~1ULL << bits))
340 goto Eoverflow;
341 if (!(value & (1ULL << bits)))
342 goto got_it;
343 if (!try_unsigned)
344 warning(expr->pos, "decimal constant %s is too big for long long",
345 show_token(token));
346 modifiers |= MOD_UNSIGNED;
347 got_it:
348 if (do_warn)
349 warning(expr->pos, "constant %s is so big it is%s%s%s",
350 show_token(token),
351 (modifiers & MOD_UNSIGNED) ? " unsigned":"",
352 (modifiers & MOD_LONG) ? " long":"",
353 (modifiers & MOD_LONGLONG) ? " long":"");
354 if (do_warn & 2)
355 warning(expr->pos,
356 "decimal constant %s is between LONG_MAX and ULONG_MAX."
357 " For C99 that means long long, C90 compilers are very "
358 "likely to produce unsigned long (and a warning) here",
359 show_token(token));
360 expr->type = EXPR_VALUE;
361 expr->flags = Int_const_expr;
362 expr->ctype = ctype_integer(modifiers);
363 expr->value = value;
364 return;
365 Eoverflow:
366 error_die(expr->pos, "constant %s is too big even for unsigned long long",
367 show_token(token));
368 return;
369 Float:
370 expr->fvalue = string_to_ld(str, &end);
371 if (str == end)
372 goto Enoint;
374 if (*end && end[1])
375 goto Enoint;
377 if (*end == 'f' || *end == 'F')
378 expr->ctype = &float_ctype;
379 else if (*end == 'l' || *end == 'L')
380 expr->ctype = &ldouble_ctype;
381 else if (!*end)
382 expr->ctype = &double_ctype;
383 else
384 goto Enoint;
386 expr->flags = Float_literal;
387 expr->type = EXPR_FVALUE;
388 return;
390 Enoint:
391 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
394 struct token *primary_expression(struct token *token, struct expression **tree)
396 struct expression *expr = NULL;
398 switch (token_type(token)) {
399 case TOKEN_CHAR:
400 expr = alloc_expression(token->pos, EXPR_VALUE);
401 expr->flags = Int_const_expr;
402 expr->ctype = &int_ctype;
403 expr->value = (unsigned char) token->character;
404 token = token->next;
405 break;
407 case TOKEN_NUMBER:
408 expr = alloc_expression(token->pos, EXPR_VALUE);
409 get_number_value(expr, token); /* will see if it's an integer */
410 token = token->next;
411 break;
413 case TOKEN_ZERO_IDENT: {
414 expr = alloc_expression(token->pos, EXPR_SYMBOL);
415 expr->flags = Int_const_expr;
416 expr->ctype = &int_ctype;
417 expr->symbol = &zero_int;
418 expr->symbol_name = token->ident;
419 token = token->next;
420 break;
423 case TOKEN_IDENT: {
424 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
425 struct token *next = token->next;
427 if (!sym) {
428 if (convert_function(token))
429 goto handle_string;
430 if (token->ident == &__builtin_types_compatible_p_ident) {
431 token = builtin_types_compatible_p_expr(token, &expr);
432 break;
434 if (token->ident == &__builtin_offsetof_ident) {
435 token = builtin_offsetof_expr(token, &expr);
436 break;
438 } else if (sym->enum_member) {
439 expr = alloc_expression(token->pos, EXPR_VALUE);
440 *expr = *sym->initializer;
441 /* we want the right position reported, thus the copy */
442 expr->pos = token->pos;
443 expr->flags = Int_const_expr;
444 token = next;
445 break;
448 expr = alloc_expression(token->pos, EXPR_SYMBOL);
451 * We support types as real first-class citizens, with type
452 * comparisons etc:
454 * if (typeof(a) == int) ..
456 if (sym && sym->namespace == NS_TYPEDEF) {
457 sparse_error(token->pos, "typename in expression");
458 sym = NULL;
460 expr->symbol_name = token->ident;
461 expr->symbol = sym;
462 token = next;
463 break;
466 case TOKEN_STRING: {
467 handle_string:
468 expr = alloc_expression(token->pos, EXPR_STRING);
469 token = string_expression(token, expr);
470 break;
473 case TOKEN_SPECIAL:
474 if (token->special == '(') {
475 expr = alloc_expression(token->pos, EXPR_PREOP);
476 expr->op = '(';
477 token = parens_expression(token, &expr->unop, "in expression");
478 if (expr->unop)
479 expr->flags = expr->unop->flags;
480 break;
482 if (token->special == '[' && lookup_type(token->next)) {
483 expr = alloc_expression(token->pos, EXPR_TYPE);
484 expr->flags = Int_const_expr; /* sic */
485 token = typename(token->next, &expr->symbol, 0);
486 token = expect(token, ']', "in type expression");
487 break;
490 default:
493 *tree = expr;
494 return token;
497 static struct token *expression_list(struct token *token, struct expression_list **list)
499 while (!match_op(token, ')')) {
500 struct expression *expr = NULL;
501 token = assignment_expression(token, &expr);
502 if (!expr)
503 break;
504 add_expression(list, expr);
505 if (!match_op(token, ','))
506 break;
507 token = token->next;
509 return token;
513 * extend to deal with the ambiguous C grammar for parsing
514 * a cast expressions followed by an initializer.
516 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
518 struct expression *expr = cast_init_expr;
520 if (!expr)
521 token = primary_expression(token, &expr);
523 while (expr && token_type(token) == TOKEN_SPECIAL) {
524 switch (token->special) {
525 case '[': { /* Array dereference */
526 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
527 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
529 deref->op = '*';
530 deref->unop = add;
532 add->op = '+';
533 add->left = expr;
534 token = parse_expression(token->next, &add->right);
535 token = expect(token, ']', "at end of array dereference");
536 expr = deref;
537 continue;
539 case SPECIAL_INCREMENT: /* Post-increment */
540 case SPECIAL_DECREMENT: { /* Post-decrement */
541 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
542 post->op = token->special;
543 post->unop = expr;
544 expr = post;
545 token = token->next;
546 continue;
548 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
549 /* "x->y" is just shorthand for "(*x).y" */
550 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
551 inner->op = '*';
552 inner->unop = expr;
553 expr = inner;
555 /* Fall through!! */
556 case '.': { /* Structure member dereference */
557 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
558 deref->op = '.';
559 deref->deref = expr;
560 token = token->next;
561 if (token_type(token) != TOKEN_IDENT) {
562 sparse_error(token->pos, "Expected member name");
563 break;
565 deref->member = token->ident;
566 token = token->next;
567 expr = deref;
568 continue;
571 case '(': { /* Function call */
572 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
573 call->op = '(';
574 call->fn = expr;
575 token = expression_list(token->next, &call->args);
576 token = expect(token, ')', "in function call");
577 expr = call;
578 continue;
581 default:
582 break;
584 break;
586 *tree = expr;
587 return token;
590 static struct token *cast_expression(struct token *token, struct expression **tree);
591 static struct token *unary_expression(struct token *token, struct expression **tree);
593 static struct token *type_info_expression(struct token *token,
594 struct expression **tree, int type)
596 struct expression *expr = alloc_expression(token->pos, type);
598 *tree = expr;
599 expr->flags = Int_const_expr; /* XXX: VLA support will need that changed */
600 token = token->next;
601 if (!match_op(token, '(') || !lookup_type(token->next))
602 return unary_expression(token, &expr->cast_expression);
603 token = typename(token->next, &expr->cast_type, 0);
605 if (!match_op(token, ')')) {
606 static const char * error[] = {
607 [EXPR_SIZEOF] = "at end of sizeof",
608 [EXPR_ALIGNOF] = "at end of __alignof__",
609 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
611 return expect(token, ')', error[type]);
614 token = token->next;
616 * C99 ambiguity: the typename might have been the beginning
617 * of a typed initializer expression..
619 if (match_op(token, '{'))
620 token = initializer(&expr->cast_expression, token);
621 return token;
624 static struct token *unary_expression(struct token *token, struct expression **tree)
626 if (token_type(token) == TOKEN_IDENT) {
627 struct ident *ident = token->ident;
628 if (ident->reserved) {
629 static const struct {
630 struct ident *id;
631 int type;
632 } type_information[] = {
633 { &sizeof_ident, EXPR_SIZEOF },
634 { &__alignof___ident, EXPR_ALIGNOF },
635 { &__alignof_ident, EXPR_ALIGNOF },
636 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
638 int i;
639 for (i = 0; i < 3; i++) {
640 if (ident == type_information[i].id)
641 return type_info_expression(token, tree, type_information[i].type);
646 if (token_type(token) == TOKEN_SPECIAL) {
647 if (match_oplist(token->special,
648 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
649 '&', '*', 0)) {
650 struct expression *unop;
651 struct expression *unary;
652 struct token *next;
654 next = cast_expression(token->next, &unop);
655 if (!unop) {
656 sparse_error(token->pos, "Syntax error in unary expression");
657 *tree = NULL;
658 return next;
660 unary = alloc_expression(token->pos, EXPR_PREOP);
661 unary->op = token->special;
662 unary->unop = unop;
663 *tree = unary;
664 return next;
666 /* possibly constant ones */
667 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
668 struct expression *unop;
669 struct expression *unary;
670 struct token *next;
672 next = cast_expression(token->next, &unop);
673 if (!unop) {
674 sparse_error(token->pos, "Syntax error in unary expression");
675 *tree = NULL;
676 return next;
678 unary = alloc_expression(token->pos, EXPR_PREOP);
679 unary->op = token->special;
680 unary->unop = unop;
681 unary->flags = unop->flags & Int_const_expr;
682 *tree = unary;
683 return next;
685 /* Gcc extension: &&label gives the address of a label */
686 if (match_op(token, SPECIAL_LOGICAL_AND) &&
687 token_type(token->next) == TOKEN_IDENT) {
688 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
689 struct symbol *sym = label_symbol(token->next);
690 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
691 sym->ctype.modifiers |= MOD_ADDRESSABLE;
692 add_symbol(&function_computed_target_list, sym);
694 label->label_symbol = sym;
695 *tree = label;
696 return token->next->next;
701 return postfix_expression(token, tree, NULL);
705 * Ambiguity: a '(' can be either a cast-expression or
706 * a primary-expression depending on whether it is followed
707 * by a type or not.
709 * additional ambiguity: a "cast expression" followed by
710 * an initializer is really a postfix-expression.
712 static struct token *cast_expression(struct token *token, struct expression **tree)
714 if (match_op(token, '(')) {
715 struct token *next = token->next;
716 if (lookup_type(next)) {
717 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
718 struct expression *v;
719 struct symbol *sym;
720 int is_force;
722 token = typename(next, &sym, MOD_FORCE);
723 cast->cast_type = sym;
724 is_force = sym->ctype.modifiers & MOD_FORCE;
725 sym->ctype.modifiers &= ~MOD_FORCE;
726 token = expect(token, ')', "at end of cast operator");
727 if (match_op(token, '{')) {
728 if (is_force)
729 warning(sym->pos,
730 "[force] in compound literal");
731 token = initializer(&cast->cast_expression, token);
732 return postfix_expression(token, tree, cast);
734 *tree = cast;
735 if (is_force)
736 cast->type = EXPR_FORCE_CAST;
737 token = cast_expression(token, &v);
738 if (!v)
739 return token;
740 cast->cast_expression = v;
741 if (v->flags & Int_const_expr)
742 cast->flags = Int_const_expr;
743 else if (v->flags & Float_literal) /* and _not_ int */
744 cast->flags = Int_const_expr | Float_literal;
745 return token;
748 return unary_expression(token, tree);
752 * Generic left-to-right binop parsing
754 * This _really_ needs to be inlined, because that makes the inner
755 * function call statically deterministic rather than a totally
756 * unpredictable indirect call. But gcc-3 is so "clever" that it
757 * doesn't do so by default even when you tell it to inline it.
759 * Making it a macro avoids the inlining problem, and also means
760 * that we can pass in the op-comparison as an expression rather
761 * than create a data structure for it.
764 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
765 struct expression *left = NULL; \
766 struct token * next = inner(__token, &left); \
768 if (left) { \
769 while (token_type(next) == TOKEN_SPECIAL) { \
770 struct expression *top, *right = NULL; \
771 int op = next->special; \
773 if (!(compare)) \
774 goto out; \
775 top = alloc_expression(next->pos, type); \
776 next = inner(next->next, &right); \
777 if (!right) { \
778 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
779 break; \
781 top->flags = left->flags & right->flags \
782 & Int_const_expr; \
783 top->op = op; \
784 top->left = left; \
785 top->right = right; \
786 left = top; \
789 out: \
790 *tree = left; \
791 return next; \
793 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
795 LR_BINOP_EXPRESSION(
796 token, tree, EXPR_BINOP, cast_expression,
797 (op == '*') || (op == '/') || (op == '%')
801 static struct token *additive_expression(struct token *token, struct expression **tree)
803 LR_BINOP_EXPRESSION(
804 token, tree, EXPR_BINOP, multiplicative_expression,
805 (op == '+') || (op == '-')
809 static struct token *shift_expression(struct token *token, struct expression **tree)
811 LR_BINOP_EXPRESSION(
812 token, tree, EXPR_BINOP, additive_expression,
813 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
817 static struct token *relational_expression(struct token *token, struct expression **tree)
819 LR_BINOP_EXPRESSION(
820 token, tree, EXPR_COMPARE, shift_expression,
821 (op == '<') || (op == '>') ||
822 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
826 static struct token *equality_expression(struct token *token, struct expression **tree)
828 LR_BINOP_EXPRESSION(
829 token, tree, EXPR_COMPARE, relational_expression,
830 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
834 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
836 LR_BINOP_EXPRESSION(
837 token, tree, EXPR_BINOP, equality_expression,
838 (op == '&')
842 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
844 LR_BINOP_EXPRESSION(
845 token, tree, EXPR_BINOP, bitwise_and_expression,
846 (op == '^')
850 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
852 LR_BINOP_EXPRESSION(
853 token, tree, EXPR_BINOP, bitwise_xor_expression,
854 (op == '|')
858 static struct token *logical_and_expression(struct token *token, struct expression **tree)
860 LR_BINOP_EXPRESSION(
861 token, tree, EXPR_LOGICAL, bitwise_or_expression,
862 (op == SPECIAL_LOGICAL_AND)
866 static struct token *logical_or_expression(struct token *token, struct expression **tree)
868 LR_BINOP_EXPRESSION(
869 token, tree, EXPR_LOGICAL, logical_and_expression,
870 (op == SPECIAL_LOGICAL_OR)
874 struct token *conditional_expression(struct token *token, struct expression **tree)
876 token = logical_or_expression(token, tree);
877 if (*tree && match_op(token, '?')) {
878 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
879 expr->op = token->special;
880 expr->left = *tree;
881 *tree = expr;
882 token = parse_expression(token->next, &expr->cond_true);
883 token = expect(token, ':', "in conditional expression");
884 token = conditional_expression(token, &expr->cond_false);
885 if (expr->left && expr->cond_false) {
886 int is_const = expr->left->flags &
887 expr->cond_false->flags &
888 Int_const_expr;
889 if (expr->cond_true)
890 is_const &= expr->cond_true->flags;
891 expr->flags = is_const;
894 return token;
897 struct token *assignment_expression(struct token *token, struct expression **tree)
899 token = conditional_expression(token, tree);
900 if (*tree && token_type(token) == TOKEN_SPECIAL) {
901 static const int assignments[] = {
902 '=',
903 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
904 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
905 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
906 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
907 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
908 int i, op = token->special;
909 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
910 if (assignments[i] == op) {
911 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
912 expr->left = *tree;
913 expr->op = op;
914 *tree = expr;
915 return assignment_expression(token->next, &expr->right);
918 return token;
921 static struct token *comma_expression(struct token *token, struct expression **tree)
923 LR_BINOP_EXPRESSION(
924 token, tree, EXPR_COMMA, assignment_expression,
925 (op == ',')
929 struct token *parse_expression(struct token *token, struct expression **tree)
931 return comma_expression(token,tree);