build: disable sparse-llvm on non-x86
[smatch.git] / expression.c
blobe5ebad65bda5dbc5b161ffd62125df8e4cd364e3
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();
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 token = typename(token, &sym, NULL);
135 if (sym->ident)
136 sparse_error(token->pos,
137 "type expression should not include identifier "
138 "\"%s\"", sym->ident->name);
139 (*tree)->symbol = sym;
140 return token;
143 static struct token *builtin_types_compatible_p_expr(struct token *token,
144 struct expression **tree)
146 struct expression *expr = alloc_expression(
147 token->pos, EXPR_COMPARE);
148 expr->op = SPECIAL_EQUAL;
149 token = token->next;
150 if (!match_op(token, '('))
151 return expect(token, '(',
152 "after __builtin_types_compatible_p");
153 token = token->next;
154 token = parse_type(token, &expr->left);
155 if (!match_op(token, ','))
156 return expect(token, ',',
157 "in __builtin_types_compatible_p");
158 token = token->next;
159 token = parse_type(token, &expr->right);
160 if (!match_op(token, ')'))
161 return expect(token, ')',
162 "at end of __builtin_types_compatible_p");
163 token = token->next;
165 *tree = expr;
166 return token;
169 static struct token *builtin_offsetof_expr(struct token *token,
170 struct expression **tree)
172 struct expression *expr = NULL;
173 struct expression **p = &expr;
174 struct symbol *sym;
175 int op = '.';
177 token = token->next;
178 if (!match_op(token, '('))
179 return expect(token, '(', "after __builtin_offset");
181 token = token->next;
182 token = typename(token, &sym, NULL);
183 if (sym->ident)
184 sparse_error(token->pos,
185 "type expression should not include identifier "
186 "\"%s\"", sym->ident->name);
188 if (!match_op(token, ','))
189 return expect(token, ',', "in __builtin_offset");
191 while (1) {
192 struct expression *e;
193 switch (op) {
194 case ')':
195 expr->in = sym;
196 *tree = expr;
197 default:
198 return expect(token, ')', "at end of __builtin_offset");
199 case SPECIAL_DEREFERENCE:
200 e = alloc_expression(token->pos, EXPR_OFFSETOF);
201 e->op = '[';
202 *p = e;
203 p = &e->down;
204 /* fall through */
205 case '.':
206 token = token->next;
207 e = alloc_expression(token->pos, EXPR_OFFSETOF);
208 e->op = '.';
209 if (token_type(token) != TOKEN_IDENT) {
210 sparse_error(token->pos, "Expected member name");
211 return token;
213 e->ident = token->ident;
214 token = token->next;
215 break;
216 case '[':
217 token = token->next;
218 e = alloc_expression(token->pos, EXPR_OFFSETOF);
219 e->op = '[';
220 token = parse_expression(token, &e->index);
221 token = expect(token, ']',
222 "at end of array dereference");
223 if (!e->index)
224 return token;
226 *p = e;
227 p = &e->down;
228 op = token_type(token) == TOKEN_SPECIAL ? token->special : 0;
232 #ifndef ULLONG_MAX
233 #define ULLONG_MAX (~0ULL)
234 #endif
236 static unsigned long long parse_num(const char *nptr, char **end)
238 if (nptr[0] == '0' && tolower((unsigned char)nptr[1]) == 'b')
239 return strtoull(&nptr[2], end, 2);
240 return strtoull(nptr, end, 0);
243 static void get_number_value(struct expression *expr, struct token *token)
245 const char *str = token->number;
246 unsigned long long value;
247 char *end;
248 int size = 0, want_unsigned = 0;
249 int overflow = 0, do_warn = 0;
250 int try_unsigned = 1;
251 int bits;
253 errno = 0;
254 value = parse_num(str, &end);
255 if (end == str)
256 goto Float;
257 if (value == ULLONG_MAX && errno == ERANGE)
258 overflow = 1;
259 while (1) {
260 char c = *end++;
261 if (!c) {
262 break;
263 } else if (c == 'u' || c == 'U') {
264 if (want_unsigned)
265 goto Enoint;
266 want_unsigned = 1;
267 } else if (c == 'l' || c == 'L') {
268 if (size)
269 goto Enoint;
270 size = 1;
271 if (*end == c) {
272 size = 2;
273 end++;
275 } else
276 goto Float;
278 if (overflow)
279 goto Eoverflow;
280 /* OK, it's a valid integer */
281 /* decimals can be unsigned only if directly specified as such */
282 if (str[0] != '0' && !want_unsigned)
283 try_unsigned = 0;
284 if (!size) {
285 bits = bits_in_int - 1;
286 if (!(value & (~1ULL << bits))) {
287 if (!(value & (1ULL << bits))) {
288 goto got_it;
289 } else if (try_unsigned) {
290 want_unsigned = 1;
291 goto got_it;
294 size = 1;
295 do_warn = 1;
297 if (size < 2) {
298 bits = bits_in_long - 1;
299 if (!(value & (~1ULL << bits))) {
300 if (!(value & (1ULL << bits))) {
301 goto got_it;
302 } else if (try_unsigned) {
303 want_unsigned = 1;
304 goto got_it;
306 do_warn |= 2;
308 size = 2;
309 do_warn |= 1;
311 bits = bits_in_longlong - 1;
312 if (value & (~1ULL << bits))
313 goto Eoverflow;
314 if (!(value & (1ULL << bits)))
315 goto got_it;
316 if (!try_unsigned)
317 warning(expr->pos, "decimal constant %s is too big for long long",
318 show_token(token));
319 want_unsigned = 1;
320 got_it:
321 if (do_warn)
322 warning(expr->pos, "constant %s is so big it is%s%s%s",
323 show_token(token),
324 want_unsigned ? " unsigned":"",
325 size > 0 ? " long":"",
326 size > 1 ? " long":"");
327 if (do_warn & 2)
328 warning(expr->pos,
329 "decimal constant %s is between LONG_MAX and ULONG_MAX."
330 " For C99 that means long long, C90 compilers are very "
331 "likely to produce unsigned long (and a warning) here",
332 show_token(token));
333 expr->type = EXPR_VALUE;
334 expr->flags = CEF_SET_INT;
335 expr->ctype = ctype_integer(size, want_unsigned);
336 expr->value = value;
337 return;
338 Eoverflow:
339 error_die(expr->pos, "constant %s is too big even for unsigned long long",
340 show_token(token));
341 return;
342 Float:
343 expr->fvalue = string_to_ld(str, &end);
344 if (str == end)
345 goto Enoint;
347 if (*end && end[1])
348 goto Enoint;
350 if (*end == 'f' || *end == 'F')
351 expr->ctype = &float_ctype;
352 else if (*end == 'l' || *end == 'L')
353 expr->ctype = &ldouble_ctype;
354 else if (!*end)
355 expr->ctype = &double_ctype;
356 else
357 goto Enoint;
359 expr->flags = CEF_SET_FLOAT;
360 expr->type = EXPR_FVALUE;
361 return;
363 Enoint:
364 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
367 struct token *primary_expression(struct token *token, struct expression **tree)
369 struct expression *expr = NULL;
371 switch (token_type(token)) {
372 case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3:
373 expr = alloc_expression(token->pos, EXPR_VALUE);
374 expr->flags = CEF_SET_CHAR;
375 expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype;
376 get_char_constant(token, &expr->value);
377 token = token->next;
378 break;
380 case TOKEN_NUMBER:
381 expr = alloc_expression(token->pos, EXPR_VALUE);
382 get_number_value(expr, token); /* will see if it's an integer */
383 token = token->next;
384 break;
386 case TOKEN_ZERO_IDENT: {
387 expr = alloc_expression(token->pos, EXPR_SYMBOL);
388 expr->flags = CEF_SET_INT;
389 expr->ctype = &int_ctype;
390 expr->symbol = &zero_int;
391 expr->symbol_name = token->ident;
392 token = token->next;
393 break;
396 case TOKEN_IDENT: {
397 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
398 struct token *next = token->next;
400 if (!sym) {
401 sym = handle_func(token);
402 if (token->ident == &__builtin_types_compatible_p_ident) {
403 token = builtin_types_compatible_p_expr(token, &expr);
404 break;
406 if (token->ident == &__builtin_offsetof_ident) {
407 token = builtin_offsetof_expr(token, &expr);
408 break;
410 } else if (sym->enum_member) {
411 expr = alloc_expression(token->pos, EXPR_VALUE);
412 *expr = *sym->initializer;
413 /* we want the right position reported, thus the copy */
414 expr->pos = token->pos;
415 expr->flags = CEF_SET_ENUM;
416 token = next;
417 break;
420 expr = alloc_expression(token->pos, EXPR_SYMBOL);
423 * We support types as real first-class citizens, with type
424 * comparisons etc:
426 * if (typeof(a) == int) ..
428 if (sym && sym->namespace == NS_TYPEDEF) {
429 sparse_error(token->pos, "typename in expression");
430 sym = NULL;
432 expr->symbol_name = token->ident;
433 expr->symbol = sym;
436 * A pointer to an lvalue designating a static storage
437 * duration object is an address constant [6.6(9)].
439 if (sym && (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_STATIC)))
440 expr->flags = CEF_ADDR;
442 token = next;
443 break;
446 case TOKEN_STRING:
447 case TOKEN_WIDE_STRING:
448 expr = alloc_expression(token->pos, EXPR_STRING);
449 token = get_string_constant(token, expr);
450 break;
452 case TOKEN_SPECIAL:
453 if (token->special == '(') {
454 expr = alloc_expression(token->pos, EXPR_PREOP);
455 expr->op = '(';
456 token = parens_expression(token, &expr->unop, "in expression");
457 break;
459 if (token->special == '[' && lookup_type(token->next)) {
460 expr = alloc_expression(token->pos, EXPR_TYPE);
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 = CEF_SET_ICE; /* 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 { &_Alignof_ident, EXPR_ALIGNOF },
621 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
623 int i;
624 for (i = 0; i < ARRAY_SIZE(type_information); i++) {
625 if (ident == type_information[i].id)
626 return type_info_expression(token, tree, type_information[i].type);
631 if (token_type(token) == TOKEN_SPECIAL) {
632 if (match_oplist(token->special,
633 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
634 '&', '*', 0)) {
635 struct expression *unop;
636 struct expression *unary;
637 struct token *next;
639 next = cast_expression(token->next, &unop);
640 if (!unop) {
641 sparse_error(token->pos, "Syntax error in unary expression");
642 *tree = NULL;
643 return next;
645 unary = alloc_expression(token->pos, EXPR_PREOP);
646 unary->op = token->special;
647 unary->unop = unop;
648 *tree = unary;
649 return next;
651 /* possibly constant ones */
652 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
653 struct expression *unop;
654 struct expression *unary;
655 struct token *next;
657 next = cast_expression(token->next, &unop);
658 if (!unop) {
659 sparse_error(token->pos, "Syntax error in unary expression");
660 *tree = NULL;
661 return next;
663 unary = alloc_expression(token->pos, EXPR_PREOP);
664 unary->op = token->special;
665 unary->unop = unop;
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->flags = CEF_ADDR;
679 label->label_symbol = sym;
680 *tree = label;
681 return token->next->next;
686 return postfix_expression(token, tree, NULL);
690 * Ambiguity: a '(' can be either a cast-expression or
691 * a primary-expression depending on whether it is followed
692 * by a type or not.
694 * additional ambiguity: a "cast expression" followed by
695 * an initializer is really a postfix-expression.
697 static struct token *cast_expression(struct token *token, struct expression **tree)
699 if (match_op(token, '(')) {
700 struct token *next = token->next;
701 if (lookup_type(next)) {
702 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
703 struct expression *v;
704 struct symbol *sym;
705 int is_force;
707 token = typename(next, &sym, &is_force);
708 cast->cast_type = sym;
709 token = expect(token, ')', "at end of cast operator");
710 if (match_op(token, '{')) {
711 if (toplevel(block_scope))
712 sym->ctype.modifiers |= MOD_TOPLEVEL;
713 if (is_force)
714 warning(sym->pos,
715 "[force] in compound literal");
716 token = initializer(&cast->cast_expression, token);
717 return postfix_expression(token, tree, cast);
719 *tree = cast;
720 if (is_force)
721 cast->type = EXPR_FORCE_CAST;
722 token = cast_expression(token, &v);
723 if (!v)
724 return token;
725 cast->cast_expression = v;
726 return token;
729 return unary_expression(token, tree);
733 * Generic left-to-right binop parsing
735 * This _really_ needs to be inlined, because that makes the inner
736 * function call statically deterministic rather than a totally
737 * unpredictable indirect call. But gcc-3 is so "clever" that it
738 * doesn't do so by default even when you tell it to inline it.
740 * Making it a macro avoids the inlining problem, and also means
741 * that we can pass in the op-comparison as an expression rather
742 * than create a data structure for it.
745 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
746 struct expression *left = NULL; \
747 struct token * next = inner(__token, &left); \
749 if (left) { \
750 while (token_type(next) == TOKEN_SPECIAL) { \
751 struct expression *top, *right = NULL; \
752 int op = next->special; \
754 if (!(compare)) \
755 goto out; \
756 top = alloc_expression(next->pos, type); \
757 next = inner(next->next, &right); \
758 if (!right) { \
759 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
760 break; \
762 top->op = op; \
763 top->left = left; \
764 top->right = right; \
765 left = top; \
768 out: \
769 *tree = left; \
770 return next; \
772 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
774 LR_BINOP_EXPRESSION(
775 token, tree, EXPR_BINOP, cast_expression,
776 (op == '*') || (op == '/') || (op == '%')
780 static struct token *additive_expression(struct token *token, struct expression **tree)
782 LR_BINOP_EXPRESSION(
783 token, tree, EXPR_BINOP, multiplicative_expression,
784 (op == '+') || (op == '-')
788 static struct token *shift_expression(struct token *token, struct expression **tree)
790 LR_BINOP_EXPRESSION(
791 token, tree, EXPR_BINOP, additive_expression,
792 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
796 static struct token *relational_expression(struct token *token, struct expression **tree)
798 LR_BINOP_EXPRESSION(
799 token, tree, EXPR_COMPARE, shift_expression,
800 (op == '<') || (op == '>') ||
801 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
805 static struct token *equality_expression(struct token *token, struct expression **tree)
807 LR_BINOP_EXPRESSION(
808 token, tree, EXPR_COMPARE, relational_expression,
809 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
813 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
815 LR_BINOP_EXPRESSION(
816 token, tree, EXPR_BINOP, equality_expression,
817 (op == '&')
821 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
823 LR_BINOP_EXPRESSION(
824 token, tree, EXPR_BINOP, bitwise_and_expression,
825 (op == '^')
829 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
831 LR_BINOP_EXPRESSION(
832 token, tree, EXPR_BINOP, bitwise_xor_expression,
833 (op == '|')
837 static struct token *logical_and_expression(struct token *token, struct expression **tree)
839 LR_BINOP_EXPRESSION(
840 token, tree, EXPR_LOGICAL, bitwise_or_expression,
841 (op == SPECIAL_LOGICAL_AND)
845 static struct token *logical_or_expression(struct token *token, struct expression **tree)
847 LR_BINOP_EXPRESSION(
848 token, tree, EXPR_LOGICAL, logical_and_expression,
849 (op == SPECIAL_LOGICAL_OR)
853 struct token *conditional_expression(struct token *token, struct expression **tree)
855 token = logical_or_expression(token, tree);
856 if (*tree && match_op(token, '?')) {
857 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
858 expr->op = token->special;
859 expr->left = *tree;
860 *tree = expr;
861 token = parse_expression(token->next, &expr->cond_true);
862 token = expect(token, ':', "in conditional expression");
863 token = conditional_expression(token, &expr->cond_false);
865 return token;
868 struct token *assignment_expression(struct token *token, struct expression **tree)
870 token = conditional_expression(token, tree);
871 if (*tree && token_type(token) == TOKEN_SPECIAL) {
872 static const int assignments[] = {
873 '=',
874 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
875 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
876 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
877 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
878 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
879 int i, op = token->special;
880 for (i = 0; i < ARRAY_SIZE(assignments); i++)
881 if (assignments[i] == op) {
882 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
883 expr->left = *tree;
884 expr->op = op;
885 *tree = expr;
886 return assignment_expression(token->next, &expr->right);
889 return token;
892 static struct token *comma_expression(struct token *token, struct expression **tree)
894 LR_BINOP_EXPRESSION(
895 token, tree, EXPR_COMMA, assignment_expression,
896 (op == ',')
900 struct token *parse_expression(struct token *token, struct expression **tree)
902 return comma_expression(token,tree);