[PATCH] Warning for mixing enums of different types
[smatch.git] / expression.c
blobaabea6cbb6246f4616b12b2e8b1bfcdaed2eab08
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_ZERO_IDENT: {
305 expr = alloc_expression(token->pos, EXPR_SYMBOL);
306 expr->ctype = &int_ctype;
307 expr->symbol = &zero_int;
308 expr->symbol_name = token->ident;
309 token = token->next;
310 break;
313 case TOKEN_IDENT: {
314 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
315 struct token *next = token->next;
317 if (!sym && convert_function(token))
318 goto handle_string;
320 expr = alloc_expression(token->pos, EXPR_SYMBOL);
323 * We support types as real first-class citizens, with type
324 * comparisons etc:
326 * if (typeof(a) == int) ..
328 if (sym && sym->namespace == NS_TYPEDEF) {
329 sparse_error(token->pos, "typename in expression");
330 sym = NULL;
332 expr->symbol_name = token->ident;
333 expr->symbol = sym;
334 token = next;
335 break;
338 case TOKEN_STRING: {
339 handle_string:
340 expr = alloc_expression(token->pos, EXPR_STRING);
341 token = string_expression(token, expr);
342 break;
345 case TOKEN_SPECIAL:
346 if (token->special == '(') {
347 expr = alloc_expression(token->pos, EXPR_PREOP);
348 expr->op = '(';
349 token = parens_expression(token, &expr->unop, "in expression");
350 break;
352 if (token->special == '[' && lookup_type(token->next)) {
353 expr = alloc_expression(token->pos, EXPR_TYPE);
354 token = typename(token->next, &expr->symbol);
355 token = expect(token, ']', "in type expression");
356 break;
359 default:
362 *tree = expr;
363 return token;
366 static struct token *expression_list(struct token *token, struct expression_list **list)
368 while (!match_op(token, ')')) {
369 struct expression *expr = NULL;
370 token = assignment_expression(token, &expr);
371 if (!expr)
372 break;
373 add_expression(list, expr);
374 if (!match_op(token, ','))
375 break;
376 token = token->next;
378 return token;
382 * extend to deal with the ambiguous C grammar for parsing
383 * a cast expressions followed by an initializer.
385 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
387 struct expression *expr = cast_init_expr;
389 if (!expr)
390 token = primary_expression(token, &expr);
392 while (expr && token_type(token) == TOKEN_SPECIAL) {
393 switch (token->special) {
394 case '[': { /* Array dereference */
395 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
396 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
398 deref->op = '*';
399 deref->unop = add;
401 add->op = '+';
402 add->left = expr;
403 token = parse_expression(token->next, &add->right);
404 token = expect(token, ']', "at end of array dereference");
405 expr = deref;
406 continue;
408 case SPECIAL_INCREMENT: /* Post-increment */
409 case SPECIAL_DECREMENT: { /* Post-decrement */
410 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
411 post->op = token->special;
412 post->unop = expr;
413 expr = post;
414 token = token->next;
415 continue;
417 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
418 /* "x->y" is just shorthand for "(*x).y" */
419 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
420 inner->op = '*';
421 inner->unop = expr;
422 expr = inner;
424 /* Fallthrough!! */
425 case '.': { /* Structure member dereference */
426 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
427 deref->op = '.';
428 deref->deref = expr;
429 token = token->next;
430 if (token_type(token) != TOKEN_IDENT) {
431 sparse_error(token->pos, "Expected member name");
432 break;
434 deref->member = token->ident;
435 token = token->next;
436 expr = deref;
437 continue;
440 case '(': { /* Function call */
441 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
442 call->op = '(';
443 call->fn = expr;
444 token = expression_list(token->next, &call->args);
445 token = expect(token, ')', "in function call");
446 expr = call;
447 continue;
450 default:
451 break;
453 break;
455 *tree = expr;
456 return token;
459 static struct token *cast_expression(struct token *token, struct expression **tree);
460 static struct token *unary_expression(struct token *token, struct expression **tree);
462 static struct token *type_info_expression(struct token *token,
463 struct expression **tree, int type)
465 struct expression *expr = alloc_expression(token->pos, type);
467 *tree = expr;
468 token = token->next;
469 if (!match_op(token, '(') || !lookup_type(token->next))
470 return unary_expression(token, &expr->cast_expression);
471 token = typename(token->next, &expr->cast_type);
473 if (!match_op(token, ')')) {
474 static const char * error[] = {
475 [EXPR_SIZEOF] = "at end of sizeof",
476 [EXPR_ALIGNOF] = "at end of __alignof__",
477 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
479 return expect(token, ')', error[type]);
482 token = token->next;
484 * C99 ambiguity: the typename might have been the beginning
485 * of a typed initializer expression..
487 if (match_op(token, '{'))
488 token = initializer(&expr->cast_expression, token);
489 return token;
492 static struct token *unary_expression(struct token *token, struct expression **tree)
494 if (token_type(token) == TOKEN_IDENT) {
495 struct ident *ident = token->ident;
496 if (ident->reserved) {
497 static const struct {
498 struct ident *id;
499 int type;
500 } type_information[] = {
501 { &sizeof_ident, EXPR_SIZEOF },
502 { &__alignof___ident, EXPR_ALIGNOF },
503 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
505 int i;
506 for (i = 0; i < 3; i++) {
507 if (ident == type_information[i].id)
508 return type_info_expression(token, tree, type_information[i].type);
513 if (token_type(token) == TOKEN_SPECIAL) {
514 if (match_oplist(token->special,
515 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
516 '&', '*', '+', '-', '~', '!', 0)) {
517 struct expression *unop;
518 struct expression *unary;
519 struct token *next;
521 next = cast_expression(token->next, &unop);
522 if (!unop) {
523 sparse_error(token->pos, "Syntax error in unary expression");
524 return next;
526 unary = alloc_expression(token->pos, EXPR_PREOP);
527 unary->op = token->special;
528 unary->unop = unop;
529 *tree = unary;
530 return next;
533 /* Gcc extension: &&label gives the address of a label */
534 if (match_op(token, SPECIAL_LOGICAL_AND) &&
535 token_type(token->next) == TOKEN_IDENT) {
536 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
537 struct symbol *sym = label_symbol(token->next);
538 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
539 sym->ctype.modifiers |= MOD_ADDRESSABLE;
540 add_symbol(&function_computed_target_list, sym);
542 label->label_symbol = sym;
543 *tree = label;
544 return token->next->next;
549 return postfix_expression(token, tree, NULL);
553 * Ambiguity: a '(' can be either a cast-expression or
554 * a primary-expression depending on whether it is followed
555 * by a type or not.
557 * additional ambiguity: a "cast expression" followed by
558 * an initializer is really a postfix-expression.
560 static struct token *cast_expression(struct token *token, struct expression **tree)
562 if (match_op(token, '(')) {
563 struct token *next = token->next;
564 if (lookup_type(next)) {
565 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
566 struct symbol *sym;
568 token = typename(next, &sym);
569 cast->cast_type = sym;
570 token = expect(token, ')', "at end of cast operator");
571 if (match_op(token, '{')) {
572 token = initializer(&cast->cast_expression, token);
573 return postfix_expression(token, tree, cast);
575 *tree = cast;
576 token = cast_expression(token, &cast->cast_expression);
577 return token;
580 return unary_expression(token, tree);
584 * Generic left-to-right binop parsing
586 * This _really_ needs to be inlined, because that makes the inner
587 * function call statically deterministic rather than a totally
588 * unpredictable indirect call. But gcc-3 is so "clever" that it
589 * doesn't do so by default even when you tell it to inline it.
591 * Making it a macro avoids the inlining problem, and also means
592 * that we can pass in the op-comparison as an expression rather
593 * than create a data structure for it.
596 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
597 struct expression *left = NULL; \
598 struct token * next = inner(token, &left); \
600 if (left) { \
601 while (token_type(next) == TOKEN_SPECIAL) { \
602 struct expression *top, *right = NULL; \
603 int op = next->special; \
605 if (!(compare)) \
606 goto out; \
607 top = alloc_expression(next->pos, type); \
608 next = inner(next->next, &right); \
609 if (!right) { \
610 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
611 break; \
613 top->op = op; \
614 top->left = left; \
615 top->right = right; \
616 left = top; \
619 out: \
620 *tree = left; \
621 return next; \
624 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
626 LR_BINOP_EXPRESSION(
627 token, tree, EXPR_BINOP, cast_expression,
628 (op == '*') || (op == '/') || (op == '%')
632 static struct token *additive_expression(struct token *token, struct expression **tree)
634 LR_BINOP_EXPRESSION(
635 token, tree, EXPR_BINOP, multiplicative_expression,
636 (op == '+') || (op == '-')
640 static struct token *shift_expression(struct token *token, struct expression **tree)
642 LR_BINOP_EXPRESSION(
643 token, tree, EXPR_BINOP, additive_expression,
644 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
648 static struct token *relational_expression(struct token *token, struct expression **tree)
650 LR_BINOP_EXPRESSION(
651 token, tree, EXPR_COMPARE, shift_expression,
652 (op == '<') || (op == '>') ||
653 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
657 static struct token *equality_expression(struct token *token, struct expression **tree)
659 LR_BINOP_EXPRESSION(
660 token, tree, EXPR_COMPARE, relational_expression,
661 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
665 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
667 LR_BINOP_EXPRESSION(
668 token, tree, EXPR_BINOP, equality_expression,
669 (op == '&')
673 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
675 LR_BINOP_EXPRESSION(
676 token, tree, EXPR_BINOP, bitwise_and_expression,
677 (op == '^')
681 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
683 LR_BINOP_EXPRESSION(
684 token, tree, EXPR_BINOP, bitwise_xor_expression,
685 (op == '|')
689 static struct token *logical_and_expression(struct token *token, struct expression **tree)
691 LR_BINOP_EXPRESSION(
692 token, tree, EXPR_LOGICAL, bitwise_or_expression,
693 (op == SPECIAL_LOGICAL_AND)
697 static struct token *logical_or_expression(struct token *token, struct expression **tree)
699 LR_BINOP_EXPRESSION(
700 token, tree, EXPR_LOGICAL, logical_and_expression,
701 (op == SPECIAL_LOGICAL_OR)
705 struct token *conditional_expression(struct token *token, struct expression **tree)
707 token = logical_or_expression(token, tree);
708 if (*tree && match_op(token, '?')) {
709 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
710 expr->op = token->special;
711 expr->left = *tree;
712 *tree = expr;
713 token = parse_expression(token->next, &expr->cond_true);
714 token = expect(token, ':', "in conditional expression");
715 token = conditional_expression(token, &expr->cond_false);
717 return token;
720 struct token *assignment_expression(struct token *token, struct expression **tree)
722 token = conditional_expression(token, tree);
723 if (*tree && token_type(token) == TOKEN_SPECIAL) {
724 static const int assignments[] = {
725 '=',
726 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
727 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
728 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
729 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
730 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
731 int i, op = token->special;
732 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
733 if (assignments[i] == op) {
734 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
735 expr->left = *tree;
736 expr->op = op;
737 *tree = expr;
738 return assignment_expression(token->next, &expr->right);
741 return token;
744 static struct token *comma_expression(struct token *token, struct expression **tree)
746 LR_BINOP_EXPRESSION(
747 token, tree, EXPR_COMMA, assignment_expression,
748 (op == ',')
752 struct token *parse_expression(struct token *token, struct expression **tree)
754 return comma_expression(token,tree);