array_values: don't crash on invalid code
[smatch.git] / expression.c
blob9907e3287ae29ad7cb79d5baad555eaa64092a65
1 /*
2 * sparse/expression.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * This is the expression parsing part of parsing C.
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <limits.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "token.h"
40 #include "parse.h"
41 #include "symbol.h"
42 #include "scope.h"
43 #include "expression.h"
44 #include "target.h"
45 #include "char.h"
47 static int match_oplist(int op, ...)
49 va_list args;
50 int nextop;
52 va_start(args, op);
53 do {
54 nextop = va_arg(args, int);
55 } while (nextop != 0 && nextop != op);
56 va_end(args);
58 return nextop != 0;
61 static struct token *comma_expression(struct token *, struct expression **);
63 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
65 token = expect(token, '(', where);
66 if (match_op(token, '{')) {
67 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
68 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
69 *expr = e;
70 e->statement = stmt;
71 start_symbol_scope(e->pos);
72 token = compound_statement(token->next, stmt);
73 end_symbol_scope();
74 token = expect(token, '}', "at end of statement expression");
75 } else
76 token = parse_expression(token, expr);
77 return expect(token, ')', where);
81 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
82 * conversion
84 static struct symbol *handle_func(struct token *token)
86 struct ident *ident = token->ident;
87 struct symbol *decl, *array;
88 struct string *string;
89 int len;
91 if (ident != &__func___ident &&
92 ident != &__FUNCTION___ident &&
93 ident != &__PRETTY_FUNCTION___ident)
94 return NULL;
96 if (!current_fn || !current_fn->ident)
97 return NULL;
99 /* OK, it's one of ours */
100 array = alloc_symbol(token->pos, SYM_ARRAY);
101 array->ctype.base_type = &char_ctype;
102 array->ctype.alignment = 1;
103 array->endpos = token->pos;
104 decl = alloc_symbol(token->pos, SYM_NODE);
105 decl->ctype.base_type = array;
106 decl->ctype.alignment = 1;
107 decl->ctype.modifiers = MOD_STATIC;
108 decl->endpos = token->pos;
110 /* function-scope, but in NS_SYMBOL */
111 bind_symbol(decl, ident, NS_LABEL);
112 decl->namespace = NS_SYMBOL;
114 len = current_fn->ident->len;
115 string = __alloc_string(len + 1);
116 memcpy(string->data, current_fn->ident->name, len);
117 string->data[len] = 0;
118 string->length = len + 1;
120 decl->initializer = alloc_expression(token->pos, EXPR_STRING);
121 decl->initializer->string = string;
122 decl->initializer->ctype = decl;
123 decl->array_size = alloc_const_expression(token->pos, len + 1);
124 array->array_size = decl->array_size;
125 decl->bit_size = array->bit_size = bytes_to_bits(len + 1);
127 return decl;
130 static struct token *parse_type(struct token *token, struct expression **tree)
132 struct symbol *sym;
133 *tree = alloc_expression(token->pos, EXPR_TYPE);
134 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 deref->member_offset = -1;
543 token = token->next;
544 expr = deref;
545 continue;
548 case '(': { /* Function call */
549 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
550 call->op = '(';
551 call->fn = expr;
552 token = expression_list(token->next, &call->args);
553 token = expect(token, ')', "in function call");
554 expr = call;
555 continue;
558 default:
559 break;
561 break;
563 *tree = expr;
564 return token;
567 static struct token *cast_expression(struct token *token, struct expression **tree);
568 static struct token *unary_expression(struct token *token, struct expression **tree);
570 static struct token *type_info_expression(struct token *token,
571 struct expression **tree, int type)
573 struct expression *expr = alloc_expression(token->pos, type);
574 struct token *p;
576 *tree = expr;
577 expr->flags = CEF_SET_ICE; /* XXX: VLA support will need that changed */
578 token = token->next;
579 if (!match_op(token, '(') || !lookup_type(token->next))
580 return unary_expression(token, &expr->cast_expression);
581 p = token;
582 token = typename(token->next, &expr->cast_type, NULL);
584 if (!match_op(token, ')')) {
585 static const char * error[] = {
586 [EXPR_SIZEOF] = "at end of sizeof",
587 [EXPR_ALIGNOF] = "at end of __alignof__",
588 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
590 return expect(token, ')', error[type]);
593 token = token->next;
595 * C99 ambiguity: the typename might have been the beginning
596 * of a typed initializer expression..
598 if (match_op(token, '{')) {
599 struct expression *cast = alloc_expression(p->pos, EXPR_CAST);
600 cast->cast_type = expr->cast_type;
601 expr->cast_type = NULL;
602 expr->cast_expression = cast;
603 token = initializer(&cast->cast_expression, token);
604 token = postfix_expression(token, &expr->cast_expression, cast);
606 return token;
609 static struct token *unary_expression(struct token *token, struct expression **tree)
611 if (token_type(token) == TOKEN_IDENT) {
612 struct ident *ident = token->ident;
613 if (ident->reserved) {
614 static const struct {
615 struct ident *id;
616 int type;
617 } type_information[] = {
618 { &sizeof_ident, EXPR_SIZEOF },
619 { &__alignof___ident, EXPR_ALIGNOF },
620 { &__alignof_ident, EXPR_ALIGNOF },
621 { &_Alignof_ident, EXPR_ALIGNOF },
622 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
624 int i;
625 for (i = 0; i < ARRAY_SIZE(type_information); i++) {
626 if (ident == type_information[i].id)
627 return type_info_expression(token, tree, type_information[i].type);
632 if (token_type(token) == TOKEN_SPECIAL) {
633 if (match_oplist(token->special,
634 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
635 '&', '*', 0)) {
636 struct expression *unop;
637 struct expression *unary;
638 struct token *next;
640 next = cast_expression(token->next, &unop);
641 if (!unop) {
642 sparse_error(token->pos, "Syntax error in unary expression");
643 *tree = NULL;
644 return next;
646 unary = alloc_expression(token->pos, EXPR_PREOP);
647 unary->op = token->special;
648 unary->unop = unop;
649 *tree = unary;
650 return next;
652 /* possibly constant ones */
653 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
654 struct expression *unop;
655 struct expression *unary;
656 struct token *next;
658 next = cast_expression(token->next, &unop);
659 if (!unop) {
660 sparse_error(token->pos, "Syntax error in unary expression");
661 *tree = NULL;
662 return next;
664 unary = alloc_expression(token->pos, EXPR_PREOP);
665 unary->op = token->special;
666 unary->unop = unop;
667 *tree = unary;
668 return next;
670 /* Gcc extension: &&label gives the address of a label */
671 if (match_op(token, SPECIAL_LOGICAL_AND) &&
672 token_type(token->next) == TOKEN_IDENT) {
673 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
674 struct symbol *sym = label_symbol(token->next);
675 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
676 sym->ctype.modifiers |= MOD_ADDRESSABLE;
677 add_symbol(&function_computed_target_list, sym);
679 label->flags = CEF_ADDR;
680 label->label_symbol = sym;
681 *tree = label;
682 return token->next->next;
687 return postfix_expression(token, tree, NULL);
691 * Ambiguity: a '(' can be either a cast-expression or
692 * a primary-expression depending on whether it is followed
693 * by a type or not.
695 * additional ambiguity: a "cast expression" followed by
696 * an initializer is really a postfix-expression.
698 static struct token *cast_expression(struct token *token, struct expression **tree)
700 if (match_op(token, '(')) {
701 struct token *next = token->next;
702 if (lookup_type(next)) {
703 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
704 struct expression *v;
705 struct symbol *sym;
706 int is_force;
708 token = typename(next, &sym, &is_force);
709 cast->cast_type = sym;
710 token = expect(token, ')', "at end of cast operator");
711 if (match_op(token, '{')) {
712 if (toplevel(block_scope))
713 sym->ctype.modifiers |= MOD_TOPLEVEL;
714 if (is_force)
715 warning(sym->pos,
716 "[force] in compound literal");
717 token = initializer(&cast->cast_expression, token);
718 return postfix_expression(token, tree, cast);
720 *tree = cast;
721 if (is_force)
722 cast->type = EXPR_FORCE_CAST;
723 token = cast_expression(token, &v);
724 if (!v)
725 return token;
726 cast->cast_expression = v;
727 return token;
730 return unary_expression(token, tree);
734 * Generic left-to-right binop parsing
736 * This _really_ needs to be inlined, because that makes the inner
737 * function call statically deterministic rather than a totally
738 * unpredictable indirect call. But gcc-3 is so "clever" that it
739 * doesn't do so by default even when you tell it to inline it.
741 * Making it a macro avoids the inlining problem, and also means
742 * that we can pass in the op-comparison as an expression rather
743 * than create a data structure for it.
746 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
747 struct expression *left = NULL; \
748 struct token * next = inner(__token, &left); \
750 if (left) { \
751 while (token_type(next) == TOKEN_SPECIAL) { \
752 struct expression *top, *right = NULL; \
753 int op = next->special; \
755 if (!(compare)) \
756 goto out; \
757 top = alloc_expression(next->pos, type); \
758 next = inner(next->next, &right); \
759 if (!right) { \
760 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
761 break; \
763 top->op = op; \
764 top->left = left; \
765 top->right = right; \
766 left = top; \
769 out: \
770 *tree = left; \
771 return next; \
773 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
775 LR_BINOP_EXPRESSION(
776 token, tree, EXPR_BINOP, cast_expression,
777 (op == '*') || (op == '/') || (op == '%')
781 static struct token *additive_expression(struct token *token, struct expression **tree)
783 LR_BINOP_EXPRESSION(
784 token, tree, EXPR_BINOP, multiplicative_expression,
785 (op == '+') || (op == '-')
789 static struct token *shift_expression(struct token *token, struct expression **tree)
791 LR_BINOP_EXPRESSION(
792 token, tree, EXPR_BINOP, additive_expression,
793 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
797 static struct token *relational_expression(struct token *token, struct expression **tree)
799 LR_BINOP_EXPRESSION(
800 token, tree, EXPR_COMPARE, shift_expression,
801 (op == '<') || (op == '>') ||
802 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
806 static struct token *equality_expression(struct token *token, struct expression **tree)
808 LR_BINOP_EXPRESSION(
809 token, tree, EXPR_COMPARE, relational_expression,
810 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
814 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
816 LR_BINOP_EXPRESSION(
817 token, tree, EXPR_BINOP, equality_expression,
818 (op == '&')
822 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
824 LR_BINOP_EXPRESSION(
825 token, tree, EXPR_BINOP, bitwise_and_expression,
826 (op == '^')
830 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
832 LR_BINOP_EXPRESSION(
833 token, tree, EXPR_BINOP, bitwise_xor_expression,
834 (op == '|')
838 static struct token *logical_and_expression(struct token *token, struct expression **tree)
840 LR_BINOP_EXPRESSION(
841 token, tree, EXPR_LOGICAL, bitwise_or_expression,
842 (op == SPECIAL_LOGICAL_AND)
846 static struct token *logical_or_expression(struct token *token, struct expression **tree)
848 LR_BINOP_EXPRESSION(
849 token, tree, EXPR_LOGICAL, logical_and_expression,
850 (op == SPECIAL_LOGICAL_OR)
854 struct token *conditional_expression(struct token *token, struct expression **tree)
856 token = logical_or_expression(token, tree);
857 if (*tree && match_op(token, '?')) {
858 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
859 expr->op = token->special;
860 expr->left = *tree;
861 *tree = expr;
862 token = parse_expression(token->next, &expr->cond_true);
863 token = expect(token, ':', "in conditional expression");
864 token = conditional_expression(token, &expr->cond_false);
866 return token;
869 struct token *assignment_expression(struct token *token, struct expression **tree)
871 token = conditional_expression(token, tree);
872 if (*tree && token_type(token) == TOKEN_SPECIAL) {
873 static const int assignments[] = {
874 '=',
875 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
876 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
877 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
878 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
879 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
880 int i, op = token->special;
881 for (i = 0; i < ARRAY_SIZE(assignments); i++)
882 if (assignments[i] == op) {
883 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
884 expr->left = *tree;
885 expr->op = op;
886 *tree = expr;
887 return assignment_expression(token->next, &expr->right);
890 return token;
893 static struct token *comma_expression(struct token *token, struct expression **tree)
895 LR_BINOP_EXPRESSION(
896 token, tree, EXPR_COMMA, assignment_expression,
897 (op == ',')
901 struct token *parse_expression(struct token *token, struct expression **tree)
903 return comma_expression(token,tree);