validation/old-initializer.c: Make the_s static to avoid extraneous warning.
[smatch.git] / expression.c
bloba40ab2bca00ce7b2e5d9445b7f1147a779d5ccae
1 /*
2 * sparse/expression.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * This is the expression parsing part of parsing C.
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "scope.h"
27 #include "expression.h"
28 #include "target.h"
30 static int match_oplist(int op, ...)
32 va_list args;
34 va_start(args, op);
35 for (;;) {
36 int nextop = va_arg(args, int);
37 if (!nextop)
38 return 0;
39 if (op == nextop)
40 return 1;
44 static struct token *comma_expression(struct token *, struct expression **);
46 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
48 token = expect(token, '(', where);
49 if (match_op(token, '{')) {
50 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
51 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
52 *expr = e;
53 e->statement = stmt;
54 start_symbol_scope();
55 token = compound_statement(token->next, stmt);
56 end_symbol_scope();
57 token = expect(token, '}', "at end of statement expression");
58 } else
59 token = parse_expression(token, expr);
60 return expect(token, ')', where);
64 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
65 * conversion
67 static int convert_one_fn_token(struct token *token)
69 struct symbol *sym = current_fn;
71 if (sym) {
72 struct ident *ident = sym->ident;
73 if (ident) {
74 int len = ident->len;
75 struct string *string;
77 string = __alloc_string(len+1);
78 memcpy(string->data, ident->name, len);
79 string->data[len] = 0;
80 string->length = len+1;
81 token_type(token) = TOKEN_STRING;
82 token->string = string;
83 return 1;
86 return 0;
89 static int convert_function(struct token *next)
91 int retval = 0;
92 for (;;) {
93 struct token *token = next;
94 next = next->next;
95 switch (token_type(token)) {
96 case TOKEN_STRING:
97 continue;
98 case TOKEN_IDENT:
99 if (token->ident == &__func___ident ||
100 token->ident == &__FUNCTION___ident ||
101 token->ident == &__PRETTY_FUNCTION___ident) {
102 if (!convert_one_fn_token(token))
103 break;
104 retval = 1;
105 continue;
107 /* Fall through */
108 default:
109 break;
111 break;
113 return retval;
116 static struct token *parse_type(struct token *token, struct expression **tree)
118 struct symbol *sym;
119 *tree = alloc_expression(token->pos, EXPR_TYPE);
120 token = typename(token, &sym);
121 if (sym->ident)
122 sparse_error(token->pos,
123 "type expression should not include identifier "
124 "\"%s\"", sym->ident->name);
125 (*tree)->symbol = sym;
126 return token;
129 static struct token *builtin_types_compatible_p_expr(struct token *token,
130 struct expression **tree)
132 struct expression *expr = alloc_expression(
133 token->pos, EXPR_COMPARE);
134 expr->op = SPECIAL_EQUAL;
135 token = token->next;
136 if (!match_op(token, '('))
137 return expect(token, '(',
138 "after __builtin_types_compatible_p");
139 token = token->next;
140 token = parse_type(token, &expr->left);
141 if (!match_op(token, ','))
142 return expect(token, ',',
143 "in __builtin_types_compatible_p");
144 token = token->next;
145 token = parse_type(token, &expr->right);
146 if (!match_op(token, ')'))
147 return expect(token, ')',
148 "at end of __builtin_types_compatible_p");
149 token = token->next;
151 *tree = expr;
152 return token;
155 static struct token *string_expression(struct token *token, struct expression *expr)
157 struct string *string = token->string;
158 struct token *next = token->next;
160 convert_function(token);
162 if (token_type(next) == TOKEN_STRING) {
163 int totlen = string->length-1;
164 char *data;
166 do {
167 totlen += next->string->length-1;
168 next = next->next;
169 } while (token_type(next) == TOKEN_STRING);
171 if (totlen > MAX_STRING) {
172 warning(token->pos, "trying to concatenate %d-character string (%d bytes max)", totlen, MAX_STRING);
173 totlen = MAX_STRING;
176 string = __alloc_string(totlen+1);
177 string->length = totlen+1;
178 data = string->data;
179 next = token;
180 do {
181 struct string *s = next->string;
182 int len = s->length-1;
184 if (len > totlen)
185 len = totlen;
186 totlen -= len;
188 next = next->next;
189 memcpy(data, s->data, len);
190 data += len;
191 } while (token_type(next) == TOKEN_STRING);
192 *data = '\0';
194 expr->string = string;
195 return next;
198 #ifndef ULLONG_MAX
199 #define ULLONG_MAX (~0ULL)
200 #endif
202 static void get_number_value(struct expression *expr, struct token *token)
204 const char *str = token->number;
205 unsigned long long value;
206 char *end;
207 unsigned long modifiers = 0;
208 int overflow = 0, do_warn = 0;
209 int try_unsigned = 1;
210 int bits;
212 errno = 0;
213 value = strtoull(str, &end, 0);
214 if (end == str)
215 goto Float;
216 if (value == ULLONG_MAX && errno == ERANGE)
217 overflow = 1;
218 while (1) {
219 unsigned long added;
220 char c = *end++;
221 if (!c) {
222 break;
223 } else if (c == 'u' || c == 'U') {
224 added = MOD_UNSIGNED;
225 } else if (c == 'l' || c == 'L') {
226 added = MOD_LONG;
227 if (*end == c) {
228 added |= MOD_LONGLONG;
229 end++;
231 } else
232 goto Float;
233 if (modifiers & added)
234 goto Enoint;
235 modifiers |= added;
237 if (overflow)
238 goto Eoverflow;
239 /* OK, it's a valid integer */
240 /* decimals can be unsigned only if directly specified as such */
241 if (str[0] != '0' && !(modifiers & MOD_UNSIGNED))
242 try_unsigned = 0;
243 if (!(modifiers & MOD_LONG)) {
244 bits = bits_in_int - 1;
245 if (!(value & (~1ULL << bits))) {
246 if (!(value & (1ULL << bits))) {
247 goto got_it;
248 } else if (try_unsigned) {
249 modifiers |= MOD_UNSIGNED;
250 goto got_it;
253 modifiers |= MOD_LONG;
254 do_warn = 1;
256 if (!(modifiers & MOD_LONGLONG)) {
257 bits = bits_in_long - 1;
258 if (!(value & (~1ULL << bits))) {
259 if (!(value & (1ULL << bits))) {
260 goto got_it;
261 } else if (try_unsigned) {
262 modifiers |= MOD_UNSIGNED;
263 goto got_it;
265 do_warn |= 2;
267 modifiers |= MOD_LONGLONG;
268 do_warn |= 1;
270 bits = bits_in_longlong - 1;
271 if (value & (~1ULL << bits))
272 goto Eoverflow;
273 if (!(value & (1ULL << bits)))
274 goto got_it;
275 if (!try_unsigned)
276 warning(expr->pos, "decimal constant %s is too big for long long",
277 show_token(token));
278 modifiers |= MOD_UNSIGNED;
279 got_it:
280 if (do_warn)
281 warning(expr->pos, "constant %s is so big it is%s%s%s",
282 show_token(token),
283 (modifiers & MOD_UNSIGNED) ? " unsigned":"",
284 (modifiers & MOD_LONG) ? " long":"",
285 (modifiers & MOD_LONGLONG) ? " long":"");
286 if (do_warn & 2)
287 warning(expr->pos,
288 "decimal constant %s is between LONG_MAX and ULONG_MAX."
289 " For C99 that means long long, C90 compilers are very "
290 "likely to produce unsigned long (and a warning) here",
291 show_token(token));
292 expr->type = EXPR_VALUE;
293 expr->ctype = ctype_integer(modifiers);
294 expr->value = value;
295 return;
296 Eoverflow:
297 error_die(expr->pos, "constant %s is too big even for unsigned long long",
298 show_token(token));
299 return;
300 Float:
301 expr->fvalue = string_to_ld(str, &end);
302 if (str == end)
303 goto Enoint;
305 if (*end && end[1])
306 goto Enoint;
308 if (*end == 'f' || *end == 'F')
309 expr->ctype = &float_ctype;
310 else if (*end == 'l' || *end == 'L')
311 expr->ctype = &ldouble_ctype;
312 else if (!*end)
313 expr->ctype = &double_ctype;
314 else
315 goto Enoint;
317 expr->type = EXPR_FVALUE;
318 return;
320 Enoint:
321 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
324 struct token *primary_expression(struct token *token, struct expression **tree)
326 struct expression *expr = NULL;
328 switch (token_type(token)) {
329 case TOKEN_CHAR:
330 expr = alloc_expression(token->pos, EXPR_VALUE);
331 expr->ctype = &int_ctype;
332 expr->value = (unsigned char) token->character;
333 token = token->next;
334 break;
336 case TOKEN_NUMBER:
337 expr = alloc_expression(token->pos, EXPR_VALUE);
338 get_number_value(expr, token);
339 token = token->next;
340 break;
342 case TOKEN_ZERO_IDENT: {
343 expr = alloc_expression(token->pos, EXPR_SYMBOL);
344 expr->ctype = &int_ctype;
345 expr->symbol = &zero_int;
346 expr->symbol_name = token->ident;
347 token = token->next;
348 break;
351 case TOKEN_IDENT: {
352 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
353 struct token *next = token->next;
355 if (!sym) {
356 if (convert_function(token))
357 goto handle_string;
358 if (token->ident == &__builtin_types_compatible_p_ident) {
359 token = builtin_types_compatible_p_expr(token, &expr);
360 break;
362 } else if (sym->enum_member) {
363 expr = alloc_expression(token->pos, EXPR_VALUE);
364 *expr = *sym->initializer;
365 /* we want the right position reported, thus the copy */
366 expr->pos = token->pos;
367 token = next;
368 break;
371 expr = alloc_expression(token->pos, EXPR_SYMBOL);
374 * We support types as real first-class citizens, with type
375 * comparisons etc:
377 * if (typeof(a) == int) ..
379 if (sym && sym->namespace == NS_TYPEDEF) {
380 sparse_error(token->pos, "typename in expression");
381 sym = NULL;
383 expr->symbol_name = token->ident;
384 expr->symbol = sym;
385 token = next;
386 break;
389 case TOKEN_STRING: {
390 handle_string:
391 expr = alloc_expression(token->pos, EXPR_STRING);
392 token = string_expression(token, expr);
393 break;
396 case TOKEN_SPECIAL:
397 if (token->special == '(') {
398 expr = alloc_expression(token->pos, EXPR_PREOP);
399 expr->op = '(';
400 token = parens_expression(token, &expr->unop, "in expression");
401 break;
403 if (token->special == '[' && lookup_type(token->next)) {
404 expr = alloc_expression(token->pos, EXPR_TYPE);
405 token = typename(token->next, &expr->symbol);
406 token = expect(token, ']', "in type expression");
407 break;
410 default:
413 *tree = expr;
414 return token;
417 static struct token *expression_list(struct token *token, struct expression_list **list)
419 while (!match_op(token, ')')) {
420 struct expression *expr = NULL;
421 token = assignment_expression(token, &expr);
422 if (!expr)
423 break;
424 add_expression(list, expr);
425 if (!match_op(token, ','))
426 break;
427 token = token->next;
429 return token;
433 * extend to deal with the ambiguous C grammar for parsing
434 * a cast expressions followed by an initializer.
436 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
438 struct expression *expr = cast_init_expr;
440 if (!expr)
441 token = primary_expression(token, &expr);
443 while (expr && token_type(token) == TOKEN_SPECIAL) {
444 switch (token->special) {
445 case '[': { /* Array dereference */
446 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
447 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
449 deref->op = '*';
450 deref->unop = add;
452 add->op = '+';
453 add->left = expr;
454 token = parse_expression(token->next, &add->right);
455 token = expect(token, ']', "at end of array dereference");
456 expr = deref;
457 continue;
459 case SPECIAL_INCREMENT: /* Post-increment */
460 case SPECIAL_DECREMENT: { /* Post-decrement */
461 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
462 post->op = token->special;
463 post->unop = expr;
464 expr = post;
465 token = token->next;
466 continue;
468 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
469 /* "x->y" is just shorthand for "(*x).y" */
470 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
471 inner->op = '*';
472 inner->unop = expr;
473 expr = inner;
475 /* Fall through!! */
476 case '.': { /* Structure member dereference */
477 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
478 deref->op = '.';
479 deref->deref = expr;
480 token = token->next;
481 if (token_type(token) != TOKEN_IDENT) {
482 sparse_error(token->pos, "Expected member name");
483 break;
485 deref->member = token->ident;
486 token = token->next;
487 expr = deref;
488 continue;
491 case '(': { /* Function call */
492 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
493 call->op = '(';
494 call->fn = expr;
495 token = expression_list(token->next, &call->args);
496 token = expect(token, ')', "in function call");
497 expr = call;
498 continue;
501 default:
502 break;
504 break;
506 *tree = expr;
507 return token;
510 static struct token *cast_expression(struct token *token, struct expression **tree);
511 static struct token *unary_expression(struct token *token, struct expression **tree);
513 static struct token *type_info_expression(struct token *token,
514 struct expression **tree, int type)
516 struct expression *expr = alloc_expression(token->pos, type);
518 *tree = expr;
519 token = token->next;
520 if (!match_op(token, '(') || !lookup_type(token->next))
521 return unary_expression(token, &expr->cast_expression);
522 token = typename(token->next, &expr->cast_type);
524 if (!match_op(token, ')')) {
525 static const char * error[] = {
526 [EXPR_SIZEOF] = "at end of sizeof",
527 [EXPR_ALIGNOF] = "at end of __alignof__",
528 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
530 return expect(token, ')', error[type]);
533 token = token->next;
535 * C99 ambiguity: the typename might have been the beginning
536 * of a typed initializer expression..
538 if (match_op(token, '{'))
539 token = initializer(&expr->cast_expression, token);
540 return token;
543 static struct token *unary_expression(struct token *token, struct expression **tree)
545 if (token_type(token) == TOKEN_IDENT) {
546 struct ident *ident = token->ident;
547 if (ident->reserved) {
548 static const struct {
549 struct ident *id;
550 int type;
551 } type_information[] = {
552 { &sizeof_ident, EXPR_SIZEOF },
553 { &__alignof___ident, EXPR_ALIGNOF },
554 { &__alignof_ident, EXPR_ALIGNOF },
555 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
557 int i;
558 for (i = 0; i < 3; i++) {
559 if (ident == type_information[i].id)
560 return type_info_expression(token, tree, type_information[i].type);
565 if (token_type(token) == TOKEN_SPECIAL) {
566 if (match_oplist(token->special,
567 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
568 '&', '*', '+', '-', '~', '!', 0)) {
569 struct expression *unop;
570 struct expression *unary;
571 struct token *next;
573 next = cast_expression(token->next, &unop);
574 if (!unop) {
575 sparse_error(token->pos, "Syntax error in unary expression");
576 return next;
578 unary = alloc_expression(token->pos, EXPR_PREOP);
579 unary->op = token->special;
580 unary->unop = unop;
581 *tree = unary;
582 return next;
585 /* Gcc extension: &&label gives the address of a label */
586 if (match_op(token, SPECIAL_LOGICAL_AND) &&
587 token_type(token->next) == TOKEN_IDENT) {
588 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
589 struct symbol *sym = label_symbol(token->next);
590 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
591 sym->ctype.modifiers |= MOD_ADDRESSABLE;
592 add_symbol(&function_computed_target_list, sym);
594 label->label_symbol = sym;
595 *tree = label;
596 return token->next->next;
601 return postfix_expression(token, tree, NULL);
605 * Ambiguity: a '(' can be either a cast-expression or
606 * a primary-expression depending on whether it is followed
607 * by a type or not.
609 * additional ambiguity: a "cast expression" followed by
610 * an initializer is really a postfix-expression.
612 static struct token *cast_expression(struct token *token, struct expression **tree)
614 if (match_op(token, '(')) {
615 struct token *next = token->next;
616 if (lookup_type(next)) {
617 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
618 struct symbol *sym;
620 token = typename(next, &sym);
621 cast->cast_type = sym;
622 token = expect(token, ')', "at end of cast operator");
623 if (match_op(token, '{')) {
624 token = initializer(&cast->cast_expression, token);
625 return postfix_expression(token, tree, cast);
627 *tree = cast;
628 token = cast_expression(token, &cast->cast_expression);
629 return token;
632 return unary_expression(token, tree);
636 * Generic left-to-right binop parsing
638 * This _really_ needs to be inlined, because that makes the inner
639 * function call statically deterministic rather than a totally
640 * unpredictable indirect call. But gcc-3 is so "clever" that it
641 * doesn't do so by default even when you tell it to inline it.
643 * Making it a macro avoids the inlining problem, and also means
644 * that we can pass in the op-comparison as an expression rather
645 * than create a data structure for it.
648 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
649 struct expression *left = NULL; \
650 struct token * next = inner(token, &left); \
652 if (left) { \
653 while (token_type(next) == TOKEN_SPECIAL) { \
654 struct expression *top, *right = NULL; \
655 int op = next->special; \
657 if (!(compare)) \
658 goto out; \
659 top = alloc_expression(next->pos, type); \
660 next = inner(next->next, &right); \
661 if (!right) { \
662 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
663 break; \
665 top->op = op; \
666 top->left = left; \
667 top->right = right; \
668 left = top; \
671 out: \
672 *tree = left; \
673 return next; \
676 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
678 LR_BINOP_EXPRESSION(
679 token, tree, EXPR_BINOP, cast_expression,
680 (op == '*') || (op == '/') || (op == '%')
684 static struct token *additive_expression(struct token *token, struct expression **tree)
686 LR_BINOP_EXPRESSION(
687 token, tree, EXPR_BINOP, multiplicative_expression,
688 (op == '+') || (op == '-')
692 static struct token *shift_expression(struct token *token, struct expression **tree)
694 LR_BINOP_EXPRESSION(
695 token, tree, EXPR_BINOP, additive_expression,
696 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
700 static struct token *relational_expression(struct token *token, struct expression **tree)
702 LR_BINOP_EXPRESSION(
703 token, tree, EXPR_COMPARE, shift_expression,
704 (op == '<') || (op == '>') ||
705 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
709 static struct token *equality_expression(struct token *token, struct expression **tree)
711 LR_BINOP_EXPRESSION(
712 token, tree, EXPR_COMPARE, relational_expression,
713 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
717 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
719 LR_BINOP_EXPRESSION(
720 token, tree, EXPR_BINOP, equality_expression,
721 (op == '&')
725 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
727 LR_BINOP_EXPRESSION(
728 token, tree, EXPR_BINOP, bitwise_and_expression,
729 (op == '^')
733 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
735 LR_BINOP_EXPRESSION(
736 token, tree, EXPR_BINOP, bitwise_xor_expression,
737 (op == '|')
741 static struct token *logical_and_expression(struct token *token, struct expression **tree)
743 LR_BINOP_EXPRESSION(
744 token, tree, EXPR_LOGICAL, bitwise_or_expression,
745 (op == SPECIAL_LOGICAL_AND)
749 static struct token *logical_or_expression(struct token *token, struct expression **tree)
751 LR_BINOP_EXPRESSION(
752 token, tree, EXPR_LOGICAL, logical_and_expression,
753 (op == SPECIAL_LOGICAL_OR)
757 struct token *conditional_expression(struct token *token, struct expression **tree)
759 token = logical_or_expression(token, tree);
760 if (*tree && match_op(token, '?')) {
761 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
762 expr->op = token->special;
763 expr->left = *tree;
764 *tree = expr;
765 token = parse_expression(token->next, &expr->cond_true);
766 token = expect(token, ':', "in conditional expression");
767 token = conditional_expression(token, &expr->cond_false);
769 return token;
772 struct token *assignment_expression(struct token *token, struct expression **tree)
774 token = conditional_expression(token, tree);
775 if (*tree && token_type(token) == TOKEN_SPECIAL) {
776 static const int assignments[] = {
777 '=',
778 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
779 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
780 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
781 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
782 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
783 int i, op = token->special;
784 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
785 if (assignments[i] == op) {
786 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
787 expr->left = *tree;
788 expr->op = op;
789 *tree = expr;
790 return assignment_expression(token->next, &expr->right);
793 return token;
796 static struct token *comma_expression(struct token *token, struct expression **tree)
798 LR_BINOP_EXPRESSION(
799 token, tree, EXPR_COMMA, assignment_expression,
800 (op == ',')
804 struct token *parse_expression(struct token *token, struct expression **tree)
806 return comma_expression(token,tree);