check_free_strict: New stricter cross function use after free check
[smatch.git] / expression.c
blobbf81831917f882fadbc74deb06e0d1174ff66c07
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 (*tree)->flags = Int_const_expr; /* sic */
135 token = typename(token, &sym, NULL);
136 if (sym->ident)
137 sparse_error(token->pos,
138 "type expression should not include identifier "
139 "\"%s\"", sym->ident->name);
140 (*tree)->symbol = sym;
141 return token;
144 static struct token *builtin_types_compatible_p_expr(struct token *token,
145 struct expression **tree)
147 struct expression *expr = alloc_expression(
148 token->pos, EXPR_COMPARE);
149 expr->flags = Int_const_expr;
150 expr->op = SPECIAL_EQUAL;
151 token = token->next;
152 if (!match_op(token, '('))
153 return expect(token, '(',
154 "after __builtin_types_compatible_p");
155 token = token->next;
156 token = parse_type(token, &expr->left);
157 if (!match_op(token, ','))
158 return expect(token, ',',
159 "in __builtin_types_compatible_p");
160 token = token->next;
161 token = parse_type(token, &expr->right);
162 if (!match_op(token, ')'))
163 return expect(token, ')',
164 "at end of __builtin_types_compatible_p");
165 token = token->next;
167 *tree = expr;
168 return token;
171 static struct token *builtin_offsetof_expr(struct token *token,
172 struct expression **tree)
174 struct expression *expr = NULL;
175 struct expression **p = &expr;
176 struct symbol *sym;
177 int op = '.';
179 token = token->next;
180 if (!match_op(token, '('))
181 return expect(token, '(', "after __builtin_offset");
183 token = token->next;
184 token = typename(token, &sym, NULL);
185 if (sym->ident)
186 sparse_error(token->pos,
187 "type expression should not include identifier "
188 "\"%s\"", sym->ident->name);
190 if (!match_op(token, ','))
191 return expect(token, ',', "in __builtin_offset");
193 while (1) {
194 struct expression *e;
195 switch (op) {
196 case ')':
197 expr->in = sym;
198 *tree = expr;
199 default:
200 return expect(token, ')', "at end of __builtin_offset");
201 case SPECIAL_DEREFERENCE:
202 e = alloc_expression(token->pos, EXPR_OFFSETOF);
203 e->flags = Int_const_expr;
204 e->op = '[';
205 *p = e;
206 p = &e->down;
207 /* fall through */
208 case '.':
209 token = token->next;
210 e = alloc_expression(token->pos, EXPR_OFFSETOF);
211 e->flags = Int_const_expr;
212 e->op = '.';
213 if (token_type(token) != TOKEN_IDENT) {
214 sparse_error(token->pos, "Expected member name");
215 return token;
217 e->ident = token->ident;
218 token = token->next;
219 break;
220 case '[':
221 token = token->next;
222 e = alloc_expression(token->pos, EXPR_OFFSETOF);
223 e->flags = Int_const_expr;
224 e->op = '[';
225 token = parse_expression(token, &e->index);
226 token = expect(token, ']',
227 "at end of array dereference");
228 if (!e->index)
229 return token;
231 *p = e;
232 p = &e->down;
233 op = token_type(token) == TOKEN_SPECIAL ? token->special : 0;
237 #ifndef ULLONG_MAX
238 #define ULLONG_MAX (~0ULL)
239 #endif
241 static unsigned long long parse_num(const char *nptr, char **end)
243 if (nptr[0] == '0' && tolower(nptr[1]) == 'b')
244 return strtoull(&nptr[2], end, 2);
245 return strtoull(nptr, end, 0);
248 static void get_number_value(struct expression *expr, struct token *token)
250 const char *str = token->number;
251 unsigned long long value;
252 char *end;
253 int size = 0, want_unsigned = 0;
254 int overflow = 0, do_warn = 0;
255 int try_unsigned = 1;
256 int bits;
258 errno = 0;
259 value = parse_num(str, &end);
260 if (end == str)
261 goto Float;
262 if (value == ULLONG_MAX && errno == ERANGE)
263 overflow = 1;
264 while (1) {
265 char c = *end++;
266 if (!c) {
267 break;
268 } else if (c == 'u' || c == 'U') {
269 if (want_unsigned)
270 goto Enoint;
271 want_unsigned = 1;
272 } else if (c == 'l' || c == 'L') {
273 if (size)
274 goto Enoint;
275 size = 1;
276 if (*end == c) {
277 size = 2;
278 end++;
280 } else
281 goto Float;
283 if (overflow)
284 goto Eoverflow;
285 /* OK, it's a valid integer */
286 /* decimals can be unsigned only if directly specified as such */
287 if (str[0] != '0' && !want_unsigned)
288 try_unsigned = 0;
289 if (!size) {
290 bits = bits_in_int - 1;
291 if (!(value & (~1ULL << bits))) {
292 if (!(value & (1ULL << bits))) {
293 goto got_it;
294 } else if (try_unsigned) {
295 want_unsigned = 1;
296 goto got_it;
299 size = 1;
300 do_warn = 1;
302 if (size < 2) {
303 bits = bits_in_long - 1;
304 if (!(value & (~1ULL << bits))) {
305 if (!(value & (1ULL << bits))) {
306 goto got_it;
307 } else if (try_unsigned) {
308 want_unsigned = 1;
309 goto got_it;
311 do_warn |= 2;
313 size = 2;
314 do_warn |= 1;
316 bits = bits_in_longlong - 1;
317 if (value & (~1ULL << bits))
318 goto Eoverflow;
319 if (!(value & (1ULL << bits)))
320 goto got_it;
321 if (!try_unsigned)
322 warning(expr->pos, "decimal constant %s is too big for long long",
323 show_token(token));
324 want_unsigned = 1;
325 got_it:
326 if (do_warn)
327 warning(expr->pos, "constant %s is so big it is%s%s%s",
328 show_token(token),
329 want_unsigned ? " unsigned":"",
330 size > 0 ? " long":"",
331 size > 1 ? " long":"");
332 if (do_warn & 2)
333 warning(expr->pos,
334 "decimal constant %s is between LONG_MAX and ULONG_MAX."
335 " For C99 that means long long, C90 compilers are very "
336 "likely to produce unsigned long (and a warning) here",
337 show_token(token));
338 expr->type = EXPR_VALUE;
339 expr->flags = Int_const_expr;
340 expr->ctype = ctype_integer(size, want_unsigned);
341 expr->value = value;
342 return;
343 Eoverflow:
344 error_die(expr->pos, "constant %s is too big even for unsigned long long",
345 show_token(token));
346 return;
347 Float:
348 expr->fvalue = string_to_ld(str, &end);
349 if (str == end)
350 goto Enoint;
352 if (*end && end[1])
353 goto Enoint;
355 if (*end == 'f' || *end == 'F')
356 expr->ctype = &float_ctype;
357 else if (*end == 'l' || *end == 'L')
358 expr->ctype = &ldouble_ctype;
359 else if (!*end)
360 expr->ctype = &double_ctype;
361 else
362 goto Enoint;
364 expr->flags = Float_literal;
365 expr->type = EXPR_FVALUE;
366 return;
368 Enoint:
369 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
372 struct token *primary_expression(struct token *token, struct expression **tree)
374 struct expression *expr = NULL;
376 switch (token_type(token)) {
377 case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3:
378 expr = alloc_expression(token->pos, EXPR_VALUE);
379 expr->flags = Int_const_expr;
380 expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype;
381 get_char_constant(token, &expr->value);
382 token = token->next;
383 break;
385 case TOKEN_NUMBER:
386 expr = alloc_expression(token->pos, EXPR_VALUE);
387 get_number_value(expr, token); /* will see if it's an integer */
388 token = token->next;
389 break;
391 case TOKEN_ZERO_IDENT: {
392 expr = alloc_expression(token->pos, EXPR_SYMBOL);
393 expr->flags = Int_const_expr;
394 expr->ctype = &int_ctype;
395 expr->symbol = &zero_int;
396 expr->symbol_name = token->ident;
397 token = token->next;
398 break;
401 case TOKEN_IDENT: {
402 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
403 struct token *next = token->next;
405 if (!sym) {
406 sym = handle_func(token);
407 if (token->ident == &__builtin_types_compatible_p_ident) {
408 token = builtin_types_compatible_p_expr(token, &expr);
409 break;
411 if (token->ident == &__builtin_offsetof_ident) {
412 token = builtin_offsetof_expr(token, &expr);
413 break;
415 } else if (sym->enum_member) {
416 expr = alloc_expression(token->pos, EXPR_VALUE);
417 *expr = *sym->initializer;
418 /* we want the right position reported, thus the copy */
419 expr->pos = token->pos;
420 expr->flags = Int_const_expr;
421 token = next;
422 break;
425 expr = alloc_expression(token->pos, EXPR_SYMBOL);
428 * We support types as real first-class citizens, with type
429 * comparisons etc:
431 * if (typeof(a) == int) ..
433 if (sym && sym->namespace == NS_TYPEDEF) {
434 sparse_error(token->pos, "typename in expression");
435 sym = NULL;
437 expr->symbol_name = token->ident;
438 expr->symbol = sym;
439 token = next;
440 break;
443 case TOKEN_STRING:
444 case TOKEN_WIDE_STRING:
445 expr = alloc_expression(token->pos, EXPR_STRING);
446 token = get_string_constant(token, expr);
447 break;
449 case TOKEN_SPECIAL:
450 if (token->special == '(') {
451 expr = alloc_expression(token->pos, EXPR_PREOP);
452 expr->op = '(';
453 token = parens_expression(token, &expr->unop, "in expression");
454 if (expr->unop)
455 expr->flags = expr->unop->flags;
456 break;
458 if (token->special == '[' && lookup_type(token->next)) {
459 expr = alloc_expression(token->pos, EXPR_TYPE);
460 expr->flags = Int_const_expr; /* sic */
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 = Int_const_expr; /* 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 { &__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 unary->flags = unop->flags & Int_const_expr;
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->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 (is_force)
712 warning(sym->pos,
713 "[force] in compound literal");
714 token = initializer(&cast->cast_expression, token);
715 return postfix_expression(token, tree, cast);
717 *tree = cast;
718 if (is_force)
719 cast->type = EXPR_FORCE_CAST;
720 token = cast_expression(token, &v);
721 if (!v)
722 return token;
723 cast->cast_expression = v;
724 if (v->flags & Int_const_expr)
725 cast->flags = Int_const_expr;
726 else if (v->flags & Float_literal) /* and _not_ int */
727 cast->flags = Int_const_expr | Float_literal;
728 return token;
731 return unary_expression(token, tree);
735 * Generic left-to-right binop parsing
737 * This _really_ needs to be inlined, because that makes the inner
738 * function call statically deterministic rather than a totally
739 * unpredictable indirect call. But gcc-3 is so "clever" that it
740 * doesn't do so by default even when you tell it to inline it.
742 * Making it a macro avoids the inlining problem, and also means
743 * that we can pass in the op-comparison as an expression rather
744 * than create a data structure for it.
747 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
748 struct expression *left = NULL; \
749 struct token * next = inner(__token, &left); \
751 if (left) { \
752 while (token_type(next) == TOKEN_SPECIAL) { \
753 struct expression *top, *right = NULL; \
754 int op = next->special; \
756 if (!(compare)) \
757 goto out; \
758 top = alloc_expression(next->pos, type); \
759 next = inner(next->next, &right); \
760 if (!right) { \
761 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
762 break; \
764 top->flags = left->flags & right->flags \
765 & Int_const_expr; \
766 top->op = op; \
767 top->left = left; \
768 top->right = right; \
769 left = top; \
772 out: \
773 *tree = left; \
774 return next; \
776 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
778 LR_BINOP_EXPRESSION(
779 token, tree, EXPR_BINOP, cast_expression,
780 (op == '*') || (op == '/') || (op == '%')
784 static struct token *additive_expression(struct token *token, struct expression **tree)
786 LR_BINOP_EXPRESSION(
787 token, tree, EXPR_BINOP, multiplicative_expression,
788 (op == '+') || (op == '-')
792 static struct token *shift_expression(struct token *token, struct expression **tree)
794 LR_BINOP_EXPRESSION(
795 token, tree, EXPR_BINOP, additive_expression,
796 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
800 static struct token *relational_expression(struct token *token, struct expression **tree)
802 LR_BINOP_EXPRESSION(
803 token, tree, EXPR_COMPARE, shift_expression,
804 (op == '<') || (op == '>') ||
805 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
809 static struct token *equality_expression(struct token *token, struct expression **tree)
811 LR_BINOP_EXPRESSION(
812 token, tree, EXPR_COMPARE, relational_expression,
813 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
817 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
819 LR_BINOP_EXPRESSION(
820 token, tree, EXPR_BINOP, equality_expression,
821 (op == '&')
825 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
827 LR_BINOP_EXPRESSION(
828 token, tree, EXPR_BINOP, bitwise_and_expression,
829 (op == '^')
833 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
835 LR_BINOP_EXPRESSION(
836 token, tree, EXPR_BINOP, bitwise_xor_expression,
837 (op == '|')
841 static struct token *logical_and_expression(struct token *token, struct expression **tree)
843 LR_BINOP_EXPRESSION(
844 token, tree, EXPR_LOGICAL, bitwise_or_expression,
845 (op == SPECIAL_LOGICAL_AND)
849 static struct token *logical_or_expression(struct token *token, struct expression **tree)
851 LR_BINOP_EXPRESSION(
852 token, tree, EXPR_LOGICAL, logical_and_expression,
853 (op == SPECIAL_LOGICAL_OR)
857 struct token *conditional_expression(struct token *token, struct expression **tree)
859 token = logical_or_expression(token, tree);
860 if (*tree && match_op(token, '?')) {
861 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
862 expr->op = token->special;
863 expr->left = *tree;
864 *tree = expr;
865 token = parse_expression(token->next, &expr->cond_true);
866 token = expect(token, ':', "in conditional expression");
867 token = conditional_expression(token, &expr->cond_false);
868 if (expr->left && expr->cond_false) {
869 int is_const = expr->left->flags &
870 expr->cond_false->flags &
871 Int_const_expr;
872 if (expr->cond_true)
873 is_const &= expr->cond_true->flags;
874 expr->flags = is_const;
877 return token;
880 struct token *assignment_expression(struct token *token, struct expression **tree)
882 token = conditional_expression(token, tree);
883 if (*tree && token_type(token) == TOKEN_SPECIAL) {
884 static const int assignments[] = {
885 '=',
886 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
887 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
888 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
889 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
890 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
891 int i, op = token->special;
892 for (i = 0; i < ARRAY_SIZE(assignments); i++)
893 if (assignments[i] == op) {
894 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
895 expr->left = *tree;
896 expr->op = op;
897 *tree = expr;
898 return assignment_expression(token->next, &expr->right);
901 return token;
904 static struct token *comma_expression(struct token *token, struct expression **tree)
906 LR_BINOP_EXPRESSION(
907 token, tree, EXPR_COMMA, assignment_expression,
908 (op == ',')
912 struct token *parse_expression(struct token *token, struct expression **tree)
914 return comma_expression(token,tree);