math: array parameters can be NULL
[smatch.git] / expression.c
blob49f2124378636520be7b1b4e4f3efade21923cba
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 ALLOCATOR(type_expression, "type-expr-maps");
49 static int match_oplist(int op, ...)
51 va_list args;
52 int nextop;
54 va_start(args, op);
55 do {
56 nextop = va_arg(args, int);
57 } while (nextop != 0 && nextop != op);
58 va_end(args);
60 return nextop != 0;
63 static struct token *comma_expression(struct token *, struct expression **);
65 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
67 struct token *p;
69 token = expect(token, '(', where);
70 p = token;
71 if (match_op(token, '{')) {
72 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
73 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
74 *expr = e;
75 e->statement = stmt;
76 start_label_scope(token->pos);
77 token = compound_statement(token->next, stmt);
78 end_label_scope();
79 token = expect(token, '}', "at end of statement expression");
80 } else
81 token = parse_expression(token, expr);
83 if (token == p)
84 sparse_error(token->pos, "an expression is expected before ')'");
85 return expect(token, ')', where);
88 struct token *string_expression(struct token *token, struct expression **expr, const char *where)
90 struct token *next = primary_expression(token, expr);
92 if (!*expr || (*expr)->type != EXPR_STRING) {
93 sparse_error(token->pos, "string literal expected for %s", where);
94 *expr = NULL;
96 return next;
100 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
101 * conversion
103 static struct symbol *handle_func(struct token *token)
105 struct ident *ident = token->ident;
106 struct symbol *decl, *array;
107 struct string *string;
108 int len;
110 if (ident != &__func___ident &&
111 ident != &__FUNCTION___ident &&
112 ident != &__PRETTY_FUNCTION___ident)
113 return NULL;
115 if (!current_fn || !current_fn->ident)
116 return NULL;
118 /* OK, it's one of ours */
119 array = alloc_symbol(token->pos, SYM_ARRAY);
120 array->ctype.base_type = &char_ctype;
121 array->ctype.alignment = 1;
122 array->endpos = token->pos;
123 decl = alloc_symbol(token->pos, SYM_NODE);
124 decl->ctype.base_type = array;
125 decl->ctype.alignment = 1;
126 decl->ctype.modifiers = MOD_STATIC;
127 decl->endpos = token->pos;
129 /* NS_SYMBOL but in function-scope */
130 bind_symbol_with_scope(decl, ident, NS_SYMBOL, function_scope);
132 len = current_fn->ident->len;
133 string = __alloc_string(len + 1);
134 memcpy(string->data, current_fn->ident->name, len);
135 string->data[len] = 0;
136 string->length = len + 1;
138 decl->initializer = alloc_expression(token->pos, EXPR_STRING);
139 decl->initializer->string = string;
140 decl->initializer->ctype = decl;
141 decl->array_size = alloc_const_expression(token->pos, len + 1);
142 array->array_size = decl->array_size;
143 decl->bit_size = array->bit_size = bytes_to_bits(len + 1);
145 return decl;
148 static struct token *parse_type(struct token *token, struct expression **tree)
150 struct symbol *sym;
151 *tree = alloc_expression(token->pos, EXPR_TYPE);
152 token = typename(token, &sym, NULL);
153 if (sym->ident)
154 sparse_error(token->pos,
155 "type expression should not include identifier "
156 "\"%s\"", sym->ident->name);
157 (*tree)->symbol = sym;
158 return token;
161 static struct token *builtin_types_compatible_p_expr(struct token *token,
162 struct expression **tree)
164 struct expression *expr = alloc_expression(
165 token->pos, EXPR_COMPARE);
166 expr->op = SPECIAL_EQUAL;
167 token = token->next;
168 if (!match_op(token, '('))
169 return expect(token, '(',
170 "after __builtin_types_compatible_p");
171 token = token->next;
172 token = parse_type(token, &expr->left);
173 if (!match_op(token, ','))
174 return expect(token, ',',
175 "in __builtin_types_compatible_p");
176 token = token->next;
177 token = parse_type(token, &expr->right);
178 if (!match_op(token, ')'))
179 return expect(token, ')',
180 "at end of __builtin_types_compatible_p");
181 token = token->next;
183 *tree = expr;
184 return token;
187 static struct token *builtin_offsetof_expr(struct token *token,
188 struct expression **tree)
190 struct expression *expr = NULL;
191 struct expression **p = &expr;
192 struct symbol *sym;
193 int op = '.';
195 token = token->next;
196 if (!match_op(token, '('))
197 return expect(token, '(', "after __builtin_offset");
199 token = token->next;
200 token = typename(token, &sym, NULL);
201 if (sym->ident)
202 sparse_error(token->pos,
203 "type expression should not include identifier "
204 "\"%s\"", sym->ident->name);
206 if (!match_op(token, ','))
207 return expect(token, ',', "in __builtin_offset");
209 while (1) {
210 struct expression *e;
211 switch (op) {
212 case ')':
213 expr->in = sym;
214 *tree = expr;
215 default:
216 return expect(token, ')', "at end of __builtin_offset");
217 case SPECIAL_DEREFERENCE:
218 e = alloc_expression(token->pos, EXPR_OFFSETOF);
219 e->op = '[';
220 *p = e;
221 p = &e->down;
222 /* fall through */
223 case '.':
224 token = token->next;
225 e = alloc_expression(token->pos, EXPR_OFFSETOF);
226 e->op = '.';
227 if (token_type(token) != TOKEN_IDENT) {
228 sparse_error(token->pos, "Expected member name");
229 return token;
231 e->ident = token->ident;
232 token = token->next;
233 break;
234 case '[':
235 token = token->next;
236 e = alloc_expression(token->pos, EXPR_OFFSETOF);
237 e->op = '[';
238 token = parse_expression(token, &e->index);
239 token = expect(token, ']',
240 "at end of array dereference");
241 if (!e->index)
242 return token;
244 *p = e;
245 p = &e->down;
246 op = token_type(token) == TOKEN_SPECIAL ? token->special : 0;
250 #ifndef ULLONG_MAX
251 #define ULLONG_MAX (~0ULL)
252 #endif
254 static unsigned long long parse_num(const char *nptr, char **end)
256 if (nptr[0] == '0' && tolower((unsigned char)nptr[1]) == 'b')
257 return strtoull(&nptr[2], end, 2);
258 return strtoull(nptr, end, 0);
261 static void get_number_value(struct expression *expr, struct token *token)
263 const char *str = token->number;
264 unsigned long long value;
265 char *end;
266 int size = 0, want_unsigned = 0;
267 int overflow = 0, do_warn = 0;
268 int try_unsigned = 1;
269 int bits;
271 errno = 0;
272 value = parse_num(str, &end);
273 if (end == str)
274 goto Float;
275 if (value == ULLONG_MAX && errno == ERANGE)
276 overflow = 1;
277 while (1) {
278 char c = *end++;
279 if (!c) {
280 break;
281 } else if (c == 'u' || c == 'U') {
282 if (want_unsigned)
283 goto Enoint;
284 want_unsigned = 1;
285 } else if (c == 'l' || c == 'L') {
286 if (size)
287 goto Enoint;
288 size = 1;
289 if (*end == c) {
290 size = 2;
291 end++;
293 } else
294 goto Float;
296 if (overflow)
297 goto Eoverflow;
298 /* OK, it's a valid integer */
299 /* decimals can be unsigned only if directly specified as such */
300 if (str[0] != '0' && !want_unsigned)
301 try_unsigned = 0;
302 if (!size) {
303 bits = bits_in_int - 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;
312 size = 1;
313 do_warn = 1;
315 if (size < 2) {
316 bits = bits_in_long - 1;
317 if (!(value & (~1ULL << bits))) {
318 if (!(value & (1ULL << bits))) {
319 goto got_it;
320 } else if (try_unsigned) {
321 want_unsigned = 1;
322 goto got_it;
324 do_warn |= 2;
326 size = 2;
327 do_warn |= 1;
329 bits = bits_in_longlong - 1;
330 if (value & (~1ULL << bits))
331 goto Eoverflow;
332 if (!(value & (1ULL << bits)))
333 goto got_it;
334 if (!try_unsigned)
335 warning(expr->pos, "decimal constant %s is too big for long long",
336 show_token(token));
337 want_unsigned = 1;
338 got_it:
339 if (do_warn && Wconstant_suffix)
340 warning(expr->pos, "constant %s is so big it is%s%s%s",
341 show_token(token),
342 want_unsigned ? " unsigned":"",
343 size > 0 ? " long":"",
344 size > 1 ? " long":"");
345 if (do_warn & 2)
346 warning(expr->pos,
347 "decimal constant %s is between LONG_MAX and ULONG_MAX."
348 " For C99 that means long long, C90 compilers are very "
349 "likely to produce unsigned long (and a warning) here",
350 show_token(token));
351 expr->type = EXPR_VALUE;
352 expr->flags = CEF_SET_INT;
353 expr->ctype = ctype_integer(size, want_unsigned);
354 expr->value = value;
355 return;
356 Eoverflow:
357 error_die(expr->pos, "constant %s is too big even for unsigned long long",
358 show_token(token));
359 return;
360 Float:
361 expr->fvalue = string_to_ld(str, &end);
362 if (str == end)
363 goto Enoint;
365 if (*end && end[1])
366 goto Enoint;
368 if (*end == 'f' || *end == 'F')
369 expr->ctype = &float_ctype;
370 else if (*end == 'l' || *end == 'L')
371 expr->ctype = &ldouble_ctype;
372 else if (!*end)
373 expr->ctype = &double_ctype;
374 else
375 goto Enoint;
377 expr->flags = CEF_SET_FLOAT;
378 expr->type = EXPR_FVALUE;
379 return;
381 Enoint:
382 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
385 static struct token *generic_selection(struct token *token, struct expression **tree)
387 struct expression *expr = alloc_expression(token->pos, EXPR_GENERIC);
388 struct type_expression **last = &expr->map;
390 token = expect(token, '(', "after '_Generic'");
391 token = assignment_expression(token, &expr->control);
392 if (!match_op(token, ',')) {
393 goto end;
395 while (match_op(token, ',')) {
396 token = token->next;
397 if (lookup_type(token)) {
398 struct type_expression *map = __alloc_type_expression(0);
399 token = typename(token, &map->type, NULL);
400 token = expect(token, ':', "after typename");
401 token = assignment_expression(token, &map->expr);
402 *last = map;
403 last = &map->next;
404 } else if (match_ident(token, &default_ident)) {
405 if (expr->def) {
406 warning(token->pos, "multiple default in generic expression");
407 info(expr->def->pos, "note: previous was here");
409 token = token->next;
410 token = expect(token, ':', "after typename");
411 token = assignment_expression(token, &expr->def);
414 end:
415 *tree = expr;
416 return expect(token, ')', "after expression");
419 struct token *primary_expression(struct token *token, struct expression **tree)
421 struct expression *expr = NULL;
423 switch (token_type(token)) {
424 case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3:
425 expr = alloc_expression(token->pos, EXPR_VALUE);
426 expr->flags = CEF_SET_CHAR;
427 expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype;
428 get_char_constant(token, &expr->value);
429 token = token->next;
430 break;
432 case TOKEN_NUMBER:
433 expr = alloc_expression(token->pos, EXPR_VALUE);
434 get_number_value(expr, token); /* will see if it's an integer */
435 token = token->next;
436 break;
438 case TOKEN_ZERO_IDENT: {
439 expr = alloc_expression(token->pos, EXPR_SYMBOL);
440 expr->flags = CEF_SET_INT;
441 expr->ctype = &int_ctype;
442 expr->symbol = &zero_int;
443 expr->symbol_name = token->ident;
444 token = token->next;
445 break;
448 case TOKEN_IDENT: {
449 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
450 struct token *next = token->next;
452 if (!sym) {
453 sym = handle_func(token);
454 if (token->ident == &__builtin_types_compatible_p_ident) {
455 token = builtin_types_compatible_p_expr(token, &expr);
456 break;
458 if (token->ident == &__builtin_offsetof_ident) {
459 token = builtin_offsetof_expr(token, &expr);
460 break;
462 if (token->ident == &_Generic_ident) {
463 token = generic_selection(token->next, &expr);
464 break;
466 } else if (sym->enum_member) {
467 expr = alloc_expression(token->pos, EXPR_VALUE);
468 *expr = *sym->initializer;
469 /* we want the right position reported, thus the copy */
470 expr->pos = token->pos;
471 expr->flags = CEF_SET_ENUM;
472 token = next;
473 break;
476 expr = alloc_expression(token->pos, EXPR_SYMBOL);
479 * We support types as real first-class citizens, with type
480 * comparisons etc:
482 * if (typeof(a) == int) ..
484 if (sym && sym->namespace == NS_TYPEDEF) {
485 sparse_error(token->pos, "typename in expression");
486 sym = NULL;
488 expr->symbol_name = token->ident;
489 expr->symbol = sym;
492 * A pointer to an lvalue designating a static storage
493 * duration object is an address constant [6.6(9)].
495 if (sym && (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_STATIC)))
496 expr->flags = CEF_ADDR;
498 token = next;
499 break;
502 case TOKEN_STRING:
503 case TOKEN_WIDE_STRING:
504 expr = alloc_expression(token->pos, EXPR_STRING);
505 token = get_string_constant(token, expr);
506 break;
508 case TOKEN_SPECIAL:
509 if (token->special == '(') {
510 expr = alloc_expression(token->pos, EXPR_PREOP);
511 expr->op = '(';
512 token = parens_expression(token, &expr->unop, "in expression");
513 break;
515 if (token->special == '[' && lookup_type(token->next)) {
516 expr = alloc_expression(token->pos, EXPR_TYPE);
517 token = typename(token->next, &expr->symbol, NULL);
518 token = expect(token, ']', "in type expression");
519 break;
522 default:
525 *tree = expr;
526 return token;
529 static struct token *expression_list(struct token *token, struct expression_list **list)
531 while (!match_op(token, ')')) {
532 struct expression *expr = NULL;
533 token = assignment_expression(token, &expr);
534 if (!expr)
535 break;
536 add_expression(list, expr);
537 if (!match_op(token, ','))
538 break;
539 token = token->next;
541 return token;
545 * extend to deal with the ambiguous C grammar for parsing
546 * a cast expressions followed by an initializer.
548 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
550 struct expression *expr = cast_init_expr;
552 if (!expr)
553 token = primary_expression(token, &expr);
555 while (expr && token_type(token) == TOKEN_SPECIAL) {
556 switch (token->special) {
557 case '[': { /* Array dereference */
558 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
559 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
561 deref->op = '*';
562 deref->unop = add;
564 add->op = '+';
565 add->left = expr;
566 token = parse_expression(token->next, &add->right);
567 token = expect(token, ']', "at end of array dereference");
568 expr = deref;
569 continue;
571 case SPECIAL_INCREMENT: /* Post-increment */
572 case SPECIAL_DECREMENT: { /* Post-decrement */
573 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
574 post->op = token->special;
575 post->unop = expr;
576 expr = post;
577 token = token->next;
578 continue;
580 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
581 /* "x->y" is just shorthand for "(*x).y" */
582 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
583 inner->op = '*';
584 inner->unop = expr;
585 expr = inner;
587 /* Fall through!! */
588 case '.': { /* Structure member dereference */
589 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
590 deref->op = '.';
591 deref->deref = expr;
592 token = token->next;
593 if (token_type(token) != TOKEN_IDENT) {
594 sparse_error(token->pos, "Expected member name");
595 break;
597 deref->member = token->ident;
598 deref->member_offset = -1;
599 token = token->next;
600 expr = deref;
601 continue;
604 case '(': { /* Function call */
605 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
606 call->op = '(';
607 call->fn = expr;
608 token = expression_list(token->next, &call->args);
609 token = expect(token, ')', "in function call");
610 expr = call;
611 continue;
614 default:
615 break;
617 break;
619 *tree = expr;
620 return token;
623 static struct token *cast_expression(struct token *token, struct expression **tree);
624 static struct token *unary_expression(struct token *token, struct expression **tree);
626 static struct token *type_info_expression(struct token *token,
627 struct expression **tree, int type)
629 struct expression *expr = alloc_expression(token->pos, type);
630 struct token *p;
632 *tree = expr;
633 expr->flags = CEF_SET_ICE; /* XXX: VLA support will need that changed */
634 token = token->next;
635 if (!match_op(token, '(') || !lookup_type(token->next))
636 return unary_expression(token, &expr->cast_expression);
637 p = token;
638 token = typename(token->next, &expr->cast_type, NULL);
640 if (!match_op(token, ')')) {
641 static const char * error[] = {
642 [EXPR_SIZEOF] = "at end of sizeof",
643 [EXPR_ALIGNOF] = "at end of __alignof__",
644 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
646 return expect(token, ')', error[type]);
649 token = token->next;
651 * C99 ambiguity: the typename might have been the beginning
652 * of a typed initializer expression..
654 if (match_op(token, '{')) {
655 struct expression *cast = alloc_expression(p->pos, EXPR_CAST);
656 cast->cast_type = expr->cast_type;
657 expr->cast_type = NULL;
658 expr->cast_expression = cast;
659 token = initializer(&cast->cast_expression, token);
660 token = postfix_expression(token, &expr->cast_expression, cast);
662 return token;
665 static struct token *unary_expression(struct token *token, struct expression **tree)
667 if (token_type(token) == TOKEN_IDENT) {
668 struct ident *ident = token->ident;
669 if (ident->reserved) {
670 static const struct {
671 struct ident *id;
672 int type;
673 } type_information[] = {
674 { &sizeof_ident, EXPR_SIZEOF },
675 { &__alignof___ident, EXPR_ALIGNOF },
676 { &__alignof_ident, EXPR_ALIGNOF },
677 { &_Alignof_ident, EXPR_ALIGNOF },
678 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
680 int i;
681 for (i = 0; i < ARRAY_SIZE(type_information); i++) {
682 if (ident == type_information[i].id)
683 return type_info_expression(token, tree, type_information[i].type);
688 if (token_type(token) == TOKEN_SPECIAL) {
689 if (match_oplist(token->special,
690 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
691 '&', '*', 0)) {
692 struct expression *unop;
693 struct expression *unary;
694 struct token *next;
696 next = cast_expression(token->next, &unop);
697 if (!unop) {
698 sparse_error(token->pos, "Syntax error in unary expression");
699 *tree = NULL;
700 return next;
702 unary = alloc_expression(token->pos, EXPR_PREOP);
703 unary->op = token->special;
704 unary->unop = unop;
705 *tree = unary;
706 return next;
708 /* possibly constant ones */
709 if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
710 struct expression *unop;
711 struct expression *unary;
712 struct token *next;
714 next = cast_expression(token->next, &unop);
715 if (!unop) {
716 sparse_error(token->pos, "Syntax error in unary expression");
717 *tree = NULL;
718 return next;
720 unary = alloc_expression(token->pos, EXPR_PREOP);
721 unary->op = token->special;
722 unary->unop = unop;
723 *tree = unary;
724 return next;
726 /* Gcc extension: &&label gives the address of a label */
727 if (match_op(token, SPECIAL_LOGICAL_AND) &&
728 token_type(token->next) == TOKEN_IDENT) {
729 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
730 struct symbol *sym = label_symbol(token->next, 1);
731 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
732 sym->ctype.modifiers |= MOD_ADDRESSABLE;
733 add_symbol(&function_computed_target_list, sym);
735 check_label_usage(sym, token->pos);
736 label->flags = CEF_ADDR;
737 label->label_symbol = sym;
738 *tree = label;
739 return token->next->next;
744 return postfix_expression(token, tree, NULL);
748 * Ambiguity: a '(' can be either a cast-expression or
749 * a primary-expression depending on whether it is followed
750 * by a type or not.
752 * additional ambiguity: a "cast expression" followed by
753 * an initializer is really a postfix-expression.
755 static struct token *cast_expression(struct token *token, struct expression **tree)
757 if (match_op(token, '(')) {
758 struct token *next = token->next;
759 if (lookup_type(next)) {
760 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
761 struct expression *v;
762 struct symbol *sym;
763 int is_force;
765 token = typename(next, &sym, &is_force);
766 cast->cast_type = sym;
767 token = expect(token, ')', "at end of cast operator");
768 if (match_op(token, '{')) {
769 if (toplevel(block_scope))
770 sym->ctype.modifiers |= MOD_TOPLEVEL;
771 if (is_force)
772 warning(sym->pos,
773 "[force] in compound literal");
774 token = initializer(&cast->cast_expression, token);
775 return postfix_expression(token, tree, cast);
777 *tree = cast;
778 if (is_force)
779 cast->type = EXPR_FORCE_CAST;
780 token = cast_expression(token, &v);
781 if (!v)
782 return token;
783 cast->cast_expression = v;
784 return token;
787 return unary_expression(token, tree);
791 * Generic left-to-right binop parsing
793 * This _really_ needs to be inlined, because that makes the inner
794 * function call statically deterministic rather than a totally
795 * unpredictable indirect call. But gcc-3 is so "clever" that it
796 * doesn't do so by default even when you tell it to inline it.
798 * Making it a macro avoids the inlining problem, and also means
799 * that we can pass in the op-comparison as an expression rather
800 * than create a data structure for it.
803 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
804 struct expression *left = NULL; \
805 struct token * next = inner(__token, &left); \
807 if (left) { \
808 while (token_type(next) == TOKEN_SPECIAL) { \
809 struct expression *top, *right = NULL; \
810 int op = next->special; \
812 if (!(compare)) \
813 goto out; \
814 top = alloc_expression(next->pos, type); \
815 next = inner(next->next, &right); \
816 if (!right) { \
817 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
818 break; \
820 top->op = op; \
821 top->left = left; \
822 top->right = right; \
823 left = top; \
826 out: \
827 *tree = left; \
828 return next; \
830 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
832 LR_BINOP_EXPRESSION(
833 token, tree, EXPR_BINOP, cast_expression,
834 (op == '*') || (op == '/') || (op == '%')
838 static struct token *additive_expression(struct token *token, struct expression **tree)
840 LR_BINOP_EXPRESSION(
841 token, tree, EXPR_BINOP, multiplicative_expression,
842 (op == '+') || (op == '-')
846 static struct token *shift_expression(struct token *token, struct expression **tree)
848 LR_BINOP_EXPRESSION(
849 token, tree, EXPR_BINOP, additive_expression,
850 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
854 static struct token *relational_expression(struct token *token, struct expression **tree)
856 LR_BINOP_EXPRESSION(
857 token, tree, EXPR_COMPARE, shift_expression,
858 (op == '<') || (op == '>') ||
859 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
863 static struct token *equality_expression(struct token *token, struct expression **tree)
865 LR_BINOP_EXPRESSION(
866 token, tree, EXPR_COMPARE, relational_expression,
867 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
871 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
873 LR_BINOP_EXPRESSION(
874 token, tree, EXPR_BINOP, equality_expression,
875 (op == '&')
879 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
881 LR_BINOP_EXPRESSION(
882 token, tree, EXPR_BINOP, bitwise_and_expression,
883 (op == '^')
887 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
889 LR_BINOP_EXPRESSION(
890 token, tree, EXPR_BINOP, bitwise_xor_expression,
891 (op == '|')
895 static struct token *logical_and_expression(struct token *token, struct expression **tree)
897 LR_BINOP_EXPRESSION(
898 token, tree, EXPR_LOGICAL, bitwise_or_expression,
899 (op == SPECIAL_LOGICAL_AND)
903 static struct token *logical_or_expression(struct token *token, struct expression **tree)
905 LR_BINOP_EXPRESSION(
906 token, tree, EXPR_LOGICAL, logical_and_expression,
907 (op == SPECIAL_LOGICAL_OR)
911 struct token *conditional_expression(struct token *token, struct expression **tree)
913 token = logical_or_expression(token, tree);
914 if (*tree && match_op(token, '?')) {
915 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
916 expr->op = token->special;
917 expr->conditional = *tree;
918 *tree = expr;
919 token = parse_expression(token->next, &expr->cond_true);
920 token = expect(token, ':', "in conditional expression");
921 token = conditional_expression(token, &expr->cond_false);
923 return token;
926 struct token *assignment_expression(struct token *token, struct expression **tree)
928 token = conditional_expression(token, tree);
929 if (*tree && token_type(token) == TOKEN_SPECIAL) {
930 static const int assignments[] = {
931 '=',
932 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
933 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
934 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
935 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
936 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
937 int i, op = token->special;
938 for (i = 0; i < ARRAY_SIZE(assignments); i++)
939 if (assignments[i] == op) {
940 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
941 expr->left = *tree;
942 expr->op = op;
943 *tree = expr;
944 return assignment_expression(token->next, &expr->right);
947 return token;
950 static struct token *comma_expression(struct token *token, struct expression **tree)
952 LR_BINOP_EXPRESSION(
953 token, tree, EXPR_COMMA, assignment_expression,
954 (op == ',')
958 struct token *parse_expression(struct token *token, struct expression **tree)
960 return comma_expression(token,tree);