Handle preprocessor "# <nr> <file>" the same as #line <nr> <file>".
[smatch.git] / expression.c
blob98c64ec6bad62e785bf18ff6bd03870772bf8cc2
1 /*
2 * sparse/expression.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * This is the expression parsing part of parsing C.
11 #define _ISOC99_SOURCE
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <limits.h>
22 #include "lib.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);
63 static struct token *string_expression(struct token *token, struct expression *expr)
65 struct string *string = token->string;
66 struct token *next = token->next;
68 if (token_type(next) == TOKEN_STRING) {
69 int totlen = string->length-1;
70 char *data;
72 do {
73 totlen += next->string->length-1;
74 next = next->next;
75 } while (token_type(next) == TOKEN_STRING);
77 if (totlen > MAX_STRING) {
78 warn(token->pos, "trying to concatenate %d-character string (%d bytes max)", totlen, MAX_STRING);
79 totlen = MAX_STRING;
82 string = __alloc_string(totlen+1);
83 string->length = totlen+1;
84 data = string->data;
85 next = token;
86 do {
87 struct string *s = next->string;
88 int len = s->length-1;
90 if (len > totlen)
91 len = totlen;
92 totlen -= len;
94 next = next->next;
95 memcpy(data, s->data, len);
96 data += len;
97 } while (token_type(next) == TOKEN_STRING);
98 *data = '\0';
100 expr->string = string;
101 return next;
104 #ifndef ULLONG_MAX
105 #define ULLONG_MAX (~0ULL)
106 #endif
108 static void get_number_value(struct expression *expr, struct token *token)
110 const char *str = token->number;
111 unsigned long long value;
112 char *end;
113 unsigned long modifiers = 0;
114 int overflow = 0, do_warn = 0;
115 int try_unsigned = 1;
116 int bits;
118 errno = 0;
119 value = strtoull(str, &end, 0);
120 if (end == str)
121 goto Float;
122 if (value == ULLONG_MAX && errno == ERANGE)
123 overflow = 1;
124 while (1) {
125 unsigned long added;
126 char c = *end++;
127 if (!c) {
128 break;
129 } else if (c == 'u' || c == 'U') {
130 added = MOD_UNSIGNED;
131 } else if (c == 'l' || c == 'L') {
132 added = MOD_LONG;
133 if (*end == c) {
134 added |= MOD_LONGLONG;
135 end++;
137 } else
138 goto Float;
139 if (modifiers & added)
140 goto Enoint;
141 modifiers |= added;
143 if (overflow)
144 goto Eoverflow;
145 /* OK, it's a valid integer */
146 /* decimals can be unsigned only if directly specified as such */
147 if (str[0] != '0' && !(modifiers & MOD_UNSIGNED))
148 try_unsigned = 0;
149 if (!(modifiers & MOD_LONG)) {
150 bits = bits_in_int - 1;
151 if (!(value & (~1ULL << bits))) {
152 if (!(value & (1ULL << bits))) {
153 goto got_it;
154 } else if (try_unsigned) {
155 modifiers |= MOD_UNSIGNED;
156 goto got_it;
159 modifiers |= MOD_LONG;
160 do_warn = 1;
162 if (!(modifiers & MOD_LONGLONG)) {
163 bits = bits_in_long - 1;
164 if (!(value & (~1ULL << bits))) {
165 if (!(value & (1ULL << bits))) {
166 goto got_it;
167 } else if (try_unsigned) {
168 modifiers |= MOD_UNSIGNED;
169 goto got_it;
171 do_warn |= 2;
173 modifiers |= MOD_LONGLONG;
174 do_warn |= 1;
176 bits = bits_in_longlong - 1;
177 if (value & (~1ULL << bits))
178 goto Eoverflow;
179 if (!(value & (1ULL << bits)))
180 goto got_it;
181 if (!try_unsigned)
182 warn(expr->pos, "decimal constant %s is too big for long long",
183 show_token(token));
184 modifiers |= MOD_UNSIGNED;
185 got_it:
186 if (do_warn)
187 warn(expr->pos, "constant %s is so big it is%s%s%s",
188 show_token(token),
189 (modifiers & MOD_UNSIGNED) ? " unsigned":"",
190 (modifiers & MOD_LONG) ? " long":"",
191 (modifiers & MOD_LONGLONG) ? " long":"");
192 if (do_warn & 2)
193 warn(expr->pos,
194 "decimal constant %s is between LONG_MAX and ULONG_MAX."
195 " For C99 that means long long, C90 compilers are very "
196 "likely to produce unsigned long (and a warning) here",
197 show_token(token));
198 expr->type = EXPR_VALUE;
199 expr->ctype = ctype_integer(modifiers);
200 expr->value = value;
201 return;
202 Eoverflow:
203 error(expr->pos, "constant %s is too big even for unsigned long long",
204 show_token(token));
205 return;
206 Float:
207 expr->fvalue = strtold(str, &end);
208 if (str == end)
209 goto Enoint;
211 if (*end && end[1])
212 goto Enoint;
214 if (*end == 'f' || *end == 'F')
215 expr->ctype = &float_ctype;
216 else if (*end == 'l' || *end == 'L')
217 expr->ctype = &ldouble_ctype;
218 else if (!*end)
219 expr->ctype = &double_ctype;
220 else
221 goto Enoint;
223 expr->type = EXPR_FVALUE;
224 return;
226 Enoint:
227 error(expr->pos, "constant %s is not a valid number", show_token(token));
230 struct token *primary_expression(struct token *token, struct expression **tree)
232 struct expression *expr = NULL;
234 switch (token_type(token)) {
235 case TOKEN_CHAR:
236 expr = alloc_expression(token->pos, EXPR_VALUE);
237 expr->ctype = &int_ctype;
238 expr->value = (unsigned char) token->character;
239 token = token->next;
240 break;
242 case TOKEN_NUMBER:
243 expr = alloc_expression(token->pos, EXPR_VALUE);
244 get_number_value(expr, token);
245 token = token->next;
246 break;
248 case TOKEN_IDENT: {
249 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
250 struct token *next = token->next;
252 expr = alloc_expression(token->pos, EXPR_SYMBOL);
255 * We support types as real first-class citizens, with type
256 * comparisons etc:
258 * if (typeof(a) == int) ..
260 if (sym && sym->namespace == NS_TYPEDEF) {
261 warn(token->pos, "typename in expression");
262 sym = NULL;
264 expr->symbol_name = token->ident;
265 expr->symbol = sym;
266 token = next;
267 break;
270 case TOKEN_STRING: {
271 expr = alloc_expression(token->pos, EXPR_STRING);
272 token = string_expression(token, expr);
273 break;
276 case TOKEN_SPECIAL:
277 if (token->special == '(') {
278 expr = alloc_expression(token->pos, EXPR_PREOP);
279 expr->op = '(';
280 token = parens_expression(token, &expr->unop, "in expression");
281 break;
283 if (token->special == '[' && lookup_type(token->next)) {
284 expr = alloc_expression(token->pos, EXPR_TYPE);
285 token = typename(token->next, &expr->symbol);
286 token = expect(token, ']', "in type expression");
287 break;
290 default:
293 *tree = expr;
294 return token;
297 static struct token *expression_list(struct token *token, struct expression_list **list)
299 while (!match_op(token, ')')) {
300 struct expression *expr = NULL;
301 token = assignment_expression(token, &expr);
302 if (!expr)
303 break;
304 add_expression(list, expr);
305 if (!match_op(token, ','))
306 break;
307 token = token->next;
309 return token;
313 * extend to deal with the ambiguous C grammar for parsing
314 * a cast expressions followed by an initializer.
316 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
318 struct expression *expr = cast_init_expr;
320 if (!expr)
321 token = primary_expression(token, &expr);
323 while (expr && token_type(token) == TOKEN_SPECIAL) {
324 switch (token->special) {
325 case '[': { /* Array dereference */
326 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
327 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
329 deref->op = '*';
330 deref->unop = add;
332 add->op = '+';
333 add->left = expr;
334 token = parse_expression(token->next, &add->right);
335 token = expect(token, ']', "at end of array dereference");
336 expr = deref;
337 continue;
339 case SPECIAL_INCREMENT: /* Post-increment */
340 case SPECIAL_DECREMENT: { /* Post-decrement */
341 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
342 post->op = token->special;
343 post->unop = expr;
344 expr = post;
345 token = token->next;
346 continue;
348 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
349 /* "x->y" is just shorthand for "(*x).y" */
350 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
351 inner->op = '*';
352 inner->unop = expr;
353 expr = inner;
355 /* Fallthrough!! */
356 case '.': { /* Structure member dereference */
357 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
358 deref->op = '.';
359 deref->deref = expr;
360 token = token->next;
361 if (token_type(token) != TOKEN_IDENT) {
362 warn(token->pos, "Expected member name");
363 break;
365 deref->member = token->ident;
366 token = token->next;
367 expr = deref;
368 continue;
371 case '(': { /* Function call */
372 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
373 call->op = '(';
374 call->fn = expr;
375 token = expression_list(token->next, &call->args);
376 token = expect(token, ')', "in function call");
377 expr = call;
378 continue;
381 default:
382 break;
384 break;
386 *tree = expr;
387 return token;
390 static struct token *cast_expression(struct token *token, struct expression **tree);
391 static struct token *unary_expression(struct token *token, struct expression **tree)
393 if (token_type(token) == TOKEN_IDENT) {
394 if (token->ident == &sizeof_ident) {
395 struct expression *sizeof_ex
396 = alloc_expression(token->pos, EXPR_SIZEOF);
397 *tree = sizeof_ex;
398 tree = &sizeof_ex->unop;
399 token = token->next;
400 if (!match_op(token, '(') || !lookup_type(token->next))
401 return unary_expression(token, &sizeof_ex->cast_expression);
402 token = typename(token->next, &sizeof_ex->cast_type);
404 if (!match_op(token, ')'))
405 return expect(token, ')', "at end of sizeof type-name");
407 token = token->next;
409 * C99 ambiguity: the typename might have been the beginning
410 * of a typed initializer expression..
412 if (match_op(token, '{'))
413 token = initializer(&sizeof_ex->cast_expression, token);
414 return token;
415 } else if (token->ident == &__alignof___ident) {
416 struct expression *alignof_ex
417 = alloc_expression(token->pos, EXPR_ALIGNOF);
418 *tree = alignof_ex;
419 tree = &alignof_ex->unop;
420 token = token->next;
421 if (!match_op(token, '(') || !lookup_type(token->next))
422 return unary_expression(token, &alignof_ex->cast_expression);
423 token = typename(token->next, &alignof_ex->cast_type);
424 return expect(token, ')', "at end of alignof type-name");
428 if (token_type(token) == TOKEN_SPECIAL) {
429 if (match_oplist(token->special,
430 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
431 '&', '*', '+', '-', '~', '!', 0)) {
432 struct expression *unop;
433 struct expression *unary;
434 struct token *next;
436 next = cast_expression(token->next, &unop);
437 if (!unop) {
438 warn(token->pos, "Syntax error in unary expression");
439 return next;
441 unary = alloc_expression(token->pos, EXPR_PREOP);
442 unary->op = token->special;
443 unary->unop = unop;
444 *tree = unary;
445 return next;
448 /* Gcc extension: &&label gives the address of a label */
449 if (match_op(token, SPECIAL_LOGICAL_AND) &&
450 token_type(token->next) == TOKEN_IDENT) {
451 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
452 label->label_symbol = label_symbol(token->next);
453 *tree = label;
454 return token->next->next;
459 return postfix_expression(token, tree, NULL);
463 * Ambiguity: a '(' can be either a cast-expression or
464 * a primary-expression depending on whether it is followed
465 * by a type or not.
467 * additional ambiguity: a "cast expression" followed by
468 * an initializer is really a postfix-expression.
470 static struct token *cast_expression(struct token *token, struct expression **tree)
472 if (match_op(token, '(')) {
473 struct token *next = token->next;
474 if (lookup_type(next)) {
475 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
476 struct symbol *sym;
478 token = typename(next, &sym);
479 cast->cast_type = sym;
480 token = expect(token, ')', "at end of cast operator");
481 if (match_op(token, '{')) {
482 token = initializer(&cast->cast_expression, token);
483 return postfix_expression(token, tree, cast);
485 *tree = cast;
486 token = cast_expression(token, &cast->cast_expression);
487 return token;
490 return unary_expression(token, tree);
494 * Generic left-to-right binop parsing
496 * This _really_ needs to be inlined, because that makes the inner
497 * function call statically deterministic rather than a totally
498 * unpredictable indirect call. But gcc-3 is so "clever" that it
499 * doesn't do so by default even when you tell it to inline it.
501 * Making it a macro avoids the inlining problem, and also means
502 * that we can pass in the op-comparison as an expression rather
503 * than create a data structure for it.
506 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
507 struct expression *left = NULL; \
508 struct token * next = inner(token, &left); \
510 if (left) { \
511 while (token_type(next) == TOKEN_SPECIAL) { \
512 struct expression *top, *right = NULL; \
513 int op = next->special; \
515 if (!(compare)) \
516 goto out; \
517 top = alloc_expression(next->pos, type); \
518 next = inner(next->next, &right); \
519 if (!right) { \
520 warn(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
521 break; \
523 top->op = op; \
524 top->left = left; \
525 top->right = right; \
526 left = top; \
529 out: \
530 *tree = left; \
531 return next; \
534 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
536 LR_BINOP_EXPRESSION(
537 token, tree, EXPR_BINOP, cast_expression,
538 (op == '*') || (op == '/') || (op == '%')
542 static struct token *additive_expression(struct token *token, struct expression **tree)
544 LR_BINOP_EXPRESSION(
545 token, tree, EXPR_BINOP, multiplicative_expression,
546 (op == '+') || (op == '-')
550 static struct token *shift_expression(struct token *token, struct expression **tree)
552 LR_BINOP_EXPRESSION(
553 token, tree, EXPR_BINOP, additive_expression,
554 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
558 static struct token *relational_expression(struct token *token, struct expression **tree)
560 LR_BINOP_EXPRESSION(
561 token, tree, EXPR_COMPARE, shift_expression,
562 (op == '<') || (op == '>') ||
563 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
567 static struct token *equality_expression(struct token *token, struct expression **tree)
569 LR_BINOP_EXPRESSION(
570 token, tree, EXPR_COMPARE, relational_expression,
571 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
575 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
577 LR_BINOP_EXPRESSION(
578 token, tree, EXPR_BINOP, equality_expression,
579 (op == '&')
583 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
585 LR_BINOP_EXPRESSION(
586 token, tree, EXPR_BINOP, bitwise_and_expression,
587 (op == '^')
591 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
593 LR_BINOP_EXPRESSION(
594 token, tree, EXPR_BINOP, bitwise_xor_expression,
595 (op == '|')
599 static struct token *logical_and_expression(struct token *token, struct expression **tree)
601 LR_BINOP_EXPRESSION(
602 token, tree, EXPR_LOGICAL, bitwise_or_expression,
603 (op == SPECIAL_LOGICAL_AND)
607 static struct token *logical_or_expression(struct token *token, struct expression **tree)
609 LR_BINOP_EXPRESSION(
610 token, tree, EXPR_LOGICAL, logical_and_expression,
611 (op == SPECIAL_LOGICAL_OR)
615 struct token *conditional_expression(struct token *token, struct expression **tree)
617 token = logical_or_expression(token, tree);
618 if (match_op(token, '?')) {
619 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
620 expr->op = token->special;
621 expr->left = *tree;
622 *tree = expr;
623 token = parse_expression(token->next, &expr->cond_true);
624 token = expect(token, ':', "in conditional expression");
625 token = conditional_expression(token, &expr->cond_false);
627 return token;
630 struct token *assignment_expression(struct token *token, struct expression **tree)
632 token = conditional_expression(token, tree);
633 if (token_type(token) == TOKEN_SPECIAL) {
634 static const int assignments[] = {
635 '=',
636 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
637 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
638 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
639 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
640 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
641 int i, op = token->special;
642 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
643 if (assignments[i] == op) {
644 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
645 expr->left = *tree;
646 expr->op = op;
647 *tree = expr;
648 return assignment_expression(token->next, &expr->right);
651 return token;
654 static struct token *comma_expression(struct token *token, struct expression **tree)
656 LR_BINOP_EXPRESSION(
657 token, tree, EXPR_COMMA, assignment_expression,
658 (op == ',')
662 struct token *parse_expression(struct token *token, struct expression **tree)
664 return comma_expression(token,tree);