Get comparison sizes right.
[smatch.git] / expression.c
blob8a1b40e65726d3d66ecc195e9f4eb2954e334176
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 "allocate.h"
24 #include "token.h"
25 #include "parse.h"
26 #include "symbol.h"
27 #include "scope.h"
28 #include "expression.h"
29 #include "target.h"
31 static int match_oplist(int op, ...)
33 va_list args;
35 va_start(args, op);
36 for (;;) {
37 int nextop = va_arg(args, int);
38 if (!nextop)
39 return 0;
40 if (op == nextop)
41 return 1;
45 static struct token *comma_expression(struct token *, struct expression **);
47 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
49 token = expect(token, '(', where);
50 if (match_op(token, '{')) {
51 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
52 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
53 *expr = e;
54 e->statement = stmt;
55 start_symbol_scope();
56 token = compound_statement(token->next, stmt);
57 end_symbol_scope();
58 token = expect(token, '}', "at end of statement expression");
59 } else
60 token = parse_expression(token, expr);
61 return expect(token, ')', where);
65 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
66 * conversion
68 static int convert_one_fn_token(struct token *token)
70 struct symbol *sym = current_fn;
72 if (sym) {
73 struct ident *ident = sym->ident;
74 if (ident) {
75 int len = ident->len;
76 struct string *string;
78 string = __alloc_string(len+1);
79 memcpy(string->data, ident->name, len);
80 string->data[len] = 0;
81 string->length = len+1;
82 token_type(token) = TOKEN_STRING;
83 token->string = string;
84 return 1;
87 return 0;
90 static int convert_function(struct token *next)
92 int retval = 0;
93 for (;;) {
94 struct token *token = next;
95 next = next->next;
96 switch (token_type(token)) {
97 case TOKEN_STRING:
98 continue;
99 case TOKEN_IDENT:
100 if (token->ident == &__func___ident ||
101 token->ident == &__FUNCTION___ident ||
102 token->ident == &__PRETTY_FUNCTION___ident) {
103 if (!convert_one_fn_token(token))
104 break;
105 retval = 1;
106 continue;
108 /* Fall through */
109 default:
110 break;
112 break;
114 return retval;
117 static struct token *string_expression(struct token *token, struct expression *expr)
119 struct string *string = token->string;
120 struct token *next = token->next;
122 convert_function(token);
124 if (token_type(next) == TOKEN_STRING) {
125 int totlen = string->length-1;
126 char *data;
128 do {
129 totlen += next->string->length-1;
130 next = next->next;
131 } while (token_type(next) == TOKEN_STRING);
133 if (totlen > MAX_STRING) {
134 warning(token->pos, "trying to concatenate %d-character string (%d bytes max)", totlen, MAX_STRING);
135 totlen = MAX_STRING;
138 string = __alloc_string(totlen+1);
139 string->length = totlen+1;
140 data = string->data;
141 next = token;
142 do {
143 struct string *s = next->string;
144 int len = s->length-1;
146 if (len > totlen)
147 len = totlen;
148 totlen -= len;
150 next = next->next;
151 memcpy(data, s->data, len);
152 data += len;
153 } while (token_type(next) == TOKEN_STRING);
154 *data = '\0';
156 expr->string = string;
157 return next;
160 #ifndef ULLONG_MAX
161 #define ULLONG_MAX (~0ULL)
162 #endif
164 static void get_number_value(struct expression *expr, struct token *token)
166 const char *str = token->number;
167 unsigned long long value;
168 char *end;
169 unsigned long modifiers = 0;
170 int overflow = 0, do_warn = 0;
171 int try_unsigned = 1;
172 int bits;
174 errno = 0;
175 value = strtoull(str, &end, 0);
176 if (end == str)
177 goto Float;
178 if (value == ULLONG_MAX && errno == ERANGE)
179 overflow = 1;
180 while (1) {
181 unsigned long added;
182 char c = *end++;
183 if (!c) {
184 break;
185 } else if (c == 'u' || c == 'U') {
186 added = MOD_UNSIGNED;
187 } else if (c == 'l' || c == 'L') {
188 added = MOD_LONG;
189 if (*end == c) {
190 added |= MOD_LONGLONG;
191 end++;
193 } else
194 goto Float;
195 if (modifiers & added)
196 goto Enoint;
197 modifiers |= added;
199 if (overflow)
200 goto Eoverflow;
201 /* OK, it's a valid integer */
202 /* decimals can be unsigned only if directly specified as such */
203 if (str[0] != '0' && !(modifiers & MOD_UNSIGNED))
204 try_unsigned = 0;
205 if (!(modifiers & MOD_LONG)) {
206 bits = bits_in_int - 1;
207 if (!(value & (~1ULL << bits))) {
208 if (!(value & (1ULL << bits))) {
209 goto got_it;
210 } else if (try_unsigned) {
211 modifiers |= MOD_UNSIGNED;
212 goto got_it;
215 modifiers |= MOD_LONG;
216 do_warn = 1;
218 if (!(modifiers & MOD_LONGLONG)) {
219 bits = bits_in_long - 1;
220 if (!(value & (~1ULL << bits))) {
221 if (!(value & (1ULL << bits))) {
222 goto got_it;
223 } else if (try_unsigned) {
224 modifiers |= MOD_UNSIGNED;
225 goto got_it;
227 do_warn |= 2;
229 modifiers |= MOD_LONGLONG;
230 do_warn |= 1;
232 bits = bits_in_longlong - 1;
233 if (value & (~1ULL << bits))
234 goto Eoverflow;
235 if (!(value & (1ULL << bits)))
236 goto got_it;
237 if (!try_unsigned)
238 warning(expr->pos, "decimal constant %s is too big for long long",
239 show_token(token));
240 modifiers |= MOD_UNSIGNED;
241 got_it:
242 if (do_warn)
243 warning(expr->pos, "constant %s is so big it is%s%s%s",
244 show_token(token),
245 (modifiers & MOD_UNSIGNED) ? " unsigned":"",
246 (modifiers & MOD_LONG) ? " long":"",
247 (modifiers & MOD_LONGLONG) ? " long":"");
248 if (do_warn & 2)
249 warning(expr->pos,
250 "decimal constant %s is between LONG_MAX and ULONG_MAX."
251 " For C99 that means long long, C90 compilers are very "
252 "likely to produce unsigned long (and a warning) here",
253 show_token(token));
254 expr->type = EXPR_VALUE;
255 expr->ctype = ctype_integer(modifiers);
256 expr->value = value;
257 return;
258 Eoverflow:
259 error_die(expr->pos, "constant %s is too big even for unsigned long long",
260 show_token(token));
261 return;
262 Float:
263 expr->fvalue = string_to_ld(str, &end);
264 if (str == end)
265 goto Enoint;
267 if (*end && end[1])
268 goto Enoint;
270 if (*end == 'f' || *end == 'F')
271 expr->ctype = &float_ctype;
272 else if (*end == 'l' || *end == 'L')
273 expr->ctype = &ldouble_ctype;
274 else if (!*end)
275 expr->ctype = &double_ctype;
276 else
277 goto Enoint;
279 expr->type = EXPR_FVALUE;
280 return;
282 Enoint:
283 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
286 struct token *primary_expression(struct token *token, struct expression **tree)
288 struct expression *expr = NULL;
290 switch (token_type(token)) {
291 case TOKEN_CHAR:
292 expr = alloc_expression(token->pos, EXPR_VALUE);
293 expr->ctype = &int_ctype;
294 expr->value = (unsigned char) token->character;
295 token = token->next;
296 break;
298 case TOKEN_NUMBER:
299 expr = alloc_expression(token->pos, EXPR_VALUE);
300 get_number_value(expr, token);
301 token = token->next;
302 break;
304 case TOKEN_IDENT: {
305 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
306 struct token *next = token->next;
308 if (!sym && convert_function(token))
309 goto handle_string;
311 expr = alloc_expression(token->pos, EXPR_SYMBOL);
314 * We support types as real first-class citizens, with type
315 * comparisons etc:
317 * if (typeof(a) == int) ..
319 if (sym && sym->namespace == NS_TYPEDEF) {
320 warning(token->pos, "typename in expression");
321 sym = NULL;
323 expr->symbol_name = token->ident;
324 expr->symbol = sym;
325 token = next;
326 break;
329 case TOKEN_STRING: {
330 handle_string:
331 expr = alloc_expression(token->pos, EXPR_STRING);
332 token = string_expression(token, expr);
333 break;
336 case TOKEN_SPECIAL:
337 if (token->special == '(') {
338 expr = alloc_expression(token->pos, EXPR_PREOP);
339 expr->op = '(';
340 token = parens_expression(token, &expr->unop, "in expression");
341 break;
343 if (token->special == '[' && lookup_type(token->next)) {
344 expr = alloc_expression(token->pos, EXPR_TYPE);
345 token = typename(token->next, &expr->symbol);
346 token = expect(token, ']', "in type expression");
347 break;
350 default:
353 *tree = expr;
354 return token;
357 static struct token *expression_list(struct token *token, struct expression_list **list)
359 while (!match_op(token, ')')) {
360 struct expression *expr = NULL;
361 token = assignment_expression(token, &expr);
362 if (!expr)
363 break;
364 add_expression(list, expr);
365 if (!match_op(token, ','))
366 break;
367 token = token->next;
369 return token;
373 * extend to deal with the ambiguous C grammar for parsing
374 * a cast expressions followed by an initializer.
376 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
378 struct expression *expr = cast_init_expr;
380 if (!expr)
381 token = primary_expression(token, &expr);
383 while (expr && token_type(token) == TOKEN_SPECIAL) {
384 switch (token->special) {
385 case '[': { /* Array dereference */
386 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
387 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
389 deref->op = '*';
390 deref->unop = add;
392 add->op = '+';
393 add->left = expr;
394 token = parse_expression(token->next, &add->right);
395 token = expect(token, ']', "at end of array dereference");
396 expr = deref;
397 continue;
399 case SPECIAL_INCREMENT: /* Post-increment */
400 case SPECIAL_DECREMENT: { /* Post-decrement */
401 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
402 post->op = token->special;
403 post->unop = expr;
404 expr = post;
405 token = token->next;
406 continue;
408 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
409 /* "x->y" is just shorthand for "(*x).y" */
410 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
411 inner->op = '*';
412 inner->unop = expr;
413 expr = inner;
415 /* Fallthrough!! */
416 case '.': { /* Structure member dereference */
417 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
418 deref->op = '.';
419 deref->deref = expr;
420 token = token->next;
421 if (token_type(token) != TOKEN_IDENT) {
422 warning(token->pos, "Expected member name");
423 break;
425 deref->member = token->ident;
426 token = token->next;
427 expr = deref;
428 continue;
431 case '(': { /* Function call */
432 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
433 call->op = '(';
434 call->fn = expr;
435 token = expression_list(token->next, &call->args);
436 token = expect(token, ')', "in function call");
437 expr = call;
438 continue;
441 default:
442 break;
444 break;
446 *tree = expr;
447 return token;
450 static struct token *cast_expression(struct token *token, struct expression **tree);
451 static struct token *unary_expression(struct token *token, struct expression **tree);
453 static struct token *type_info_expression(struct token *token,
454 struct expression **tree, int type)
456 struct expression *expr = alloc_expression(token->pos, type);
458 *tree = expr;
459 token = token->next;
460 if (!match_op(token, '(') || !lookup_type(token->next))
461 return unary_expression(token, &expr->cast_expression);
462 token = typename(token->next, &expr->cast_type);
464 if (!match_op(token, ')')) {
465 static const char * error[] = {
466 [EXPR_SIZEOF] = "at end of sizeof",
467 [EXPR_ALIGNOF] = "at end of __alignof__",
468 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
470 return expect(token, ')', error[type]);
473 token = token->next;
475 * C99 ambiguity: the typename might have been the beginning
476 * of a typed initializer expression..
478 if (match_op(token, '{'))
479 token = initializer(&expr->cast_expression, token);
480 return token;
483 static struct token *unary_expression(struct token *token, struct expression **tree)
485 if (token_type(token) == TOKEN_IDENT) {
486 struct ident *ident = token->ident;
487 if (ident->reserved) {
488 static const struct {
489 struct ident *id;
490 int type;
491 } type_information[] = {
492 { &sizeof_ident, EXPR_SIZEOF },
493 { &__alignof___ident, EXPR_ALIGNOF },
494 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
496 int i;
497 for (i = 0; i < 3; i++) {
498 if (ident == type_information[i].id)
499 return type_info_expression(token, tree, type_information[i].type);
504 if (token_type(token) == TOKEN_SPECIAL) {
505 if (match_oplist(token->special,
506 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
507 '&', '*', '+', '-', '~', '!', 0)) {
508 struct expression *unop;
509 struct expression *unary;
510 struct token *next;
512 next = cast_expression(token->next, &unop);
513 if (!unop) {
514 warning(token->pos, "Syntax error in unary expression");
515 return next;
517 unary = alloc_expression(token->pos, EXPR_PREOP);
518 unary->op = token->special;
519 unary->unop = unop;
520 *tree = unary;
521 return next;
524 /* Gcc extension: &&label gives the address of a label */
525 if (match_op(token, SPECIAL_LOGICAL_AND) &&
526 token_type(token->next) == TOKEN_IDENT) {
527 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
528 struct symbol *sym = label_symbol(token->next);
529 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
530 sym->ctype.modifiers |= MOD_ADDRESSABLE;
531 add_symbol(&function_computed_target_list, sym);
533 label->label_symbol = sym;
534 *tree = label;
535 return token->next->next;
540 return postfix_expression(token, tree, NULL);
544 * Ambiguity: a '(' can be either a cast-expression or
545 * a primary-expression depending on whether it is followed
546 * by a type or not.
548 * additional ambiguity: a "cast expression" followed by
549 * an initializer is really a postfix-expression.
551 static struct token *cast_expression(struct token *token, struct expression **tree)
553 if (match_op(token, '(')) {
554 struct token *next = token->next;
555 if (lookup_type(next)) {
556 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
557 struct symbol *sym;
559 token = typename(next, &sym);
560 cast->cast_type = sym;
561 token = expect(token, ')', "at end of cast operator");
562 if (match_op(token, '{')) {
563 token = initializer(&cast->cast_expression, token);
564 return postfix_expression(token, tree, cast);
566 *tree = cast;
567 token = cast_expression(token, &cast->cast_expression);
568 return token;
571 return unary_expression(token, tree);
575 * Generic left-to-right binop parsing
577 * This _really_ needs to be inlined, because that makes the inner
578 * function call statically deterministic rather than a totally
579 * unpredictable indirect call. But gcc-3 is so "clever" that it
580 * doesn't do so by default even when you tell it to inline it.
582 * Making it a macro avoids the inlining problem, and also means
583 * that we can pass in the op-comparison as an expression rather
584 * than create a data structure for it.
587 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
588 struct expression *left = NULL; \
589 struct token * next = inner(token, &left); \
591 if (left) { \
592 while (token_type(next) == TOKEN_SPECIAL) { \
593 struct expression *top, *right = NULL; \
594 int op = next->special; \
596 if (!(compare)) \
597 goto out; \
598 top = alloc_expression(next->pos, type); \
599 next = inner(next->next, &right); \
600 if (!right) { \
601 warning(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
602 break; \
604 top->op = op; \
605 top->left = left; \
606 top->right = right; \
607 left = top; \
610 out: \
611 *tree = left; \
612 return next; \
615 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
617 LR_BINOP_EXPRESSION(
618 token, tree, EXPR_BINOP, cast_expression,
619 (op == '*') || (op == '/') || (op == '%')
623 static struct token *additive_expression(struct token *token, struct expression **tree)
625 LR_BINOP_EXPRESSION(
626 token, tree, EXPR_BINOP, multiplicative_expression,
627 (op == '+') || (op == '-')
631 static struct token *shift_expression(struct token *token, struct expression **tree)
633 LR_BINOP_EXPRESSION(
634 token, tree, EXPR_BINOP, additive_expression,
635 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
639 static struct token *relational_expression(struct token *token, struct expression **tree)
641 LR_BINOP_EXPRESSION(
642 token, tree, EXPR_COMPARE, shift_expression,
643 (op == '<') || (op == '>') ||
644 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
648 static struct token *equality_expression(struct token *token, struct expression **tree)
650 LR_BINOP_EXPRESSION(
651 token, tree, EXPR_COMPARE, relational_expression,
652 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
656 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
658 LR_BINOP_EXPRESSION(
659 token, tree, EXPR_BINOP, equality_expression,
660 (op == '&')
664 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
666 LR_BINOP_EXPRESSION(
667 token, tree, EXPR_BINOP, bitwise_and_expression,
668 (op == '^')
672 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
674 LR_BINOP_EXPRESSION(
675 token, tree, EXPR_BINOP, bitwise_xor_expression,
676 (op == '|')
680 static struct token *logical_and_expression(struct token *token, struct expression **tree)
682 LR_BINOP_EXPRESSION(
683 token, tree, EXPR_LOGICAL, bitwise_or_expression,
684 (op == SPECIAL_LOGICAL_AND)
688 static struct token *logical_or_expression(struct token *token, struct expression **tree)
690 LR_BINOP_EXPRESSION(
691 token, tree, EXPR_LOGICAL, logical_and_expression,
692 (op == SPECIAL_LOGICAL_OR)
696 struct token *conditional_expression(struct token *token, struct expression **tree)
698 token = logical_or_expression(token, tree);
699 if (match_op(token, '?')) {
700 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
701 expr->op = token->special;
702 expr->left = *tree;
703 *tree = expr;
704 token = parse_expression(token->next, &expr->cond_true);
705 token = expect(token, ':', "in conditional expression");
706 token = conditional_expression(token, &expr->cond_false);
708 return token;
711 struct token *assignment_expression(struct token *token, struct expression **tree)
713 token = conditional_expression(token, tree);
714 if (token_type(token) == TOKEN_SPECIAL) {
715 static const int assignments[] = {
716 '=',
717 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
718 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
719 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
720 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
721 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
722 int i, op = token->special;
723 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
724 if (assignments[i] == op) {
725 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
726 expr->left = *tree;
727 expr->op = op;
728 *tree = expr;
729 return assignment_expression(token->next, &expr->right);
732 return token;
735 static struct token *comma_expression(struct token *token, struct expression **tree)
737 LR_BINOP_EXPRESSION(
738 token, tree, EXPR_COMMA, assignment_expression,
739 (op == ',')
743 struct token *parse_expression(struct token *token, struct expression **tree)
745 return comma_expression(token,tree);