Add pack_ptr_list() helper function.
[smatch.git] / expression.c
blob66aa6dfd70dfccb3c21952920c1fc0f3c4d279bc
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 "token.h"
23 #include "parse.h"
24 #include "symbol.h"
25 #include "scope.h"
26 #include "expression.h"
27 #include "target.h"
29 static int match_oplist(int op, ...)
31 va_list args;
33 va_start(args, op);
34 for (;;) {
35 int nextop = va_arg(args, int);
36 if (!nextop)
37 return 0;
38 if (op == nextop)
39 return 1;
43 static struct token *comma_expression(struct token *, struct expression **);
45 struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
47 token = expect(token, '(', where);
48 if (match_op(token, '{')) {
49 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
50 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
51 *expr = e;
52 e->statement = stmt;
53 start_symbol_scope();
54 token = compound_statement(token->next, stmt);
55 end_symbol_scope();
56 token = expect(token, '}', "at end of statement expression");
57 } else
58 token = parse_expression(token, expr);
59 return expect(token, ')', where);
63 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
64 * conversion
66 static int convert_one_fn_token(struct token *token)
68 struct symbol *sym = current_fn;
70 if (sym) {
71 struct ident *ident = sym->ident;
72 if (ident) {
73 int len = ident->len;
74 struct string *string;
76 string = __alloc_string(len+1);
77 memcpy(string->data, ident->name, len);
78 string->data[len] = 0;
79 string->length = len+1;
80 token_type(token) = TOKEN_STRING;
81 token->string = string;
82 return 1;
85 return 0;
88 static int convert_function(struct token *next)
90 int retval = 0;
91 for (;;) {
92 struct token *token = next;
93 next = next->next;
94 switch (token_type(token)) {
95 case TOKEN_STRING:
96 continue;
97 case TOKEN_IDENT:
98 if (token->ident == &__func___ident ||
99 token->ident == &__FUNCTION___ident ||
100 token->ident == &__PRETTY_FUNCTION___ident) {
101 if (!convert_one_fn_token(token))
102 break;
103 retval = 1;
104 continue;
106 /* Fall through */
107 default:
108 break;
110 break;
112 return retval;
115 static struct token *string_expression(struct token *token, struct expression *expr)
117 struct string *string = token->string;
118 struct token *next = token->next;
120 convert_function(token);
122 if (token_type(next) == TOKEN_STRING) {
123 int totlen = string->length-1;
124 char *data;
126 do {
127 totlen += next->string->length-1;
128 next = next->next;
129 } while (token_type(next) == TOKEN_STRING);
131 if (totlen > MAX_STRING) {
132 warning(token->pos, "trying to concatenate %d-character string (%d bytes max)", totlen, MAX_STRING);
133 totlen = MAX_STRING;
136 string = __alloc_string(totlen+1);
137 string->length = totlen+1;
138 data = string->data;
139 next = token;
140 do {
141 struct string *s = next->string;
142 int len = s->length-1;
144 if (len > totlen)
145 len = totlen;
146 totlen -= len;
148 next = next->next;
149 memcpy(data, s->data, len);
150 data += len;
151 } while (token_type(next) == TOKEN_STRING);
152 *data = '\0';
154 expr->string = string;
155 return next;
158 #ifndef ULLONG_MAX
159 #define ULLONG_MAX (~0ULL)
160 #endif
162 static void get_number_value(struct expression *expr, struct token *token)
164 const char *str = token->number;
165 unsigned long long value;
166 char *end;
167 unsigned long modifiers = 0;
168 int overflow = 0, do_warn = 0;
169 int try_unsigned = 1;
170 int bits;
172 errno = 0;
173 value = strtoull(str, &end, 0);
174 if (end == str)
175 goto Float;
176 if (value == ULLONG_MAX && errno == ERANGE)
177 overflow = 1;
178 while (1) {
179 unsigned long added;
180 char c = *end++;
181 if (!c) {
182 break;
183 } else if (c == 'u' || c == 'U') {
184 added = MOD_UNSIGNED;
185 } else if (c == 'l' || c == 'L') {
186 added = MOD_LONG;
187 if (*end == c) {
188 added |= MOD_LONGLONG;
189 end++;
191 } else
192 goto Float;
193 if (modifiers & added)
194 goto Enoint;
195 modifiers |= added;
197 if (overflow)
198 goto Eoverflow;
199 /* OK, it's a valid integer */
200 /* decimals can be unsigned only if directly specified as such */
201 if (str[0] != '0' && !(modifiers & MOD_UNSIGNED))
202 try_unsigned = 0;
203 if (!(modifiers & MOD_LONG)) {
204 bits = bits_in_int - 1;
205 if (!(value & (~1ULL << bits))) {
206 if (!(value & (1ULL << bits))) {
207 goto got_it;
208 } else if (try_unsigned) {
209 modifiers |= MOD_UNSIGNED;
210 goto got_it;
213 modifiers |= MOD_LONG;
214 do_warn = 1;
216 if (!(modifiers & MOD_LONGLONG)) {
217 bits = bits_in_long - 1;
218 if (!(value & (~1ULL << bits))) {
219 if (!(value & (1ULL << bits))) {
220 goto got_it;
221 } else if (try_unsigned) {
222 modifiers |= MOD_UNSIGNED;
223 goto got_it;
225 do_warn |= 2;
227 modifiers |= MOD_LONGLONG;
228 do_warn |= 1;
230 bits = bits_in_longlong - 1;
231 if (value & (~1ULL << bits))
232 goto Eoverflow;
233 if (!(value & (1ULL << bits)))
234 goto got_it;
235 if (!try_unsigned)
236 warning(expr->pos, "decimal constant %s is too big for long long",
237 show_token(token));
238 modifiers |= MOD_UNSIGNED;
239 got_it:
240 if (do_warn)
241 warning(expr->pos, "constant %s is so big it is%s%s%s",
242 show_token(token),
243 (modifiers & MOD_UNSIGNED) ? " unsigned":"",
244 (modifiers & MOD_LONG) ? " long":"",
245 (modifiers & MOD_LONGLONG) ? " long":"");
246 if (do_warn & 2)
247 warning(expr->pos,
248 "decimal constant %s is between LONG_MAX and ULONG_MAX."
249 " For C99 that means long long, C90 compilers are very "
250 "likely to produce unsigned long (and a warning) here",
251 show_token(token));
252 expr->type = EXPR_VALUE;
253 expr->ctype = ctype_integer(modifiers);
254 expr->value = value;
255 return;
256 Eoverflow:
257 error_die(expr->pos, "constant %s is too big even for unsigned long long",
258 show_token(token));
259 return;
260 Float:
261 expr->fvalue = string_to_ld(str, &end);
262 if (str == end)
263 goto Enoint;
265 if (*end && end[1])
266 goto Enoint;
268 if (*end == 'f' || *end == 'F')
269 expr->ctype = &float_ctype;
270 else if (*end == 'l' || *end == 'L')
271 expr->ctype = &ldouble_ctype;
272 else if (!*end)
273 expr->ctype = &double_ctype;
274 else
275 goto Enoint;
277 expr->type = EXPR_FVALUE;
278 return;
280 Enoint:
281 error_die(expr->pos, "constant %s is not a valid number", show_token(token));
284 struct token *primary_expression(struct token *token, struct expression **tree)
286 struct expression *expr = NULL;
288 switch (token_type(token)) {
289 case TOKEN_CHAR:
290 expr = alloc_expression(token->pos, EXPR_VALUE);
291 expr->ctype = &int_ctype;
292 expr->value = (unsigned char) token->character;
293 token = token->next;
294 break;
296 case TOKEN_NUMBER:
297 expr = alloc_expression(token->pos, EXPR_VALUE);
298 get_number_value(expr, token);
299 token = token->next;
300 break;
302 case TOKEN_IDENT: {
303 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
304 struct token *next = token->next;
306 if (!sym && convert_function(token))
307 goto handle_string;
309 expr = alloc_expression(token->pos, EXPR_SYMBOL);
312 * We support types as real first-class citizens, with type
313 * comparisons etc:
315 * if (typeof(a) == int) ..
317 if (sym && sym->namespace == NS_TYPEDEF) {
318 warning(token->pos, "typename in expression");
319 sym = NULL;
321 expr->symbol_name = token->ident;
322 expr->symbol = sym;
323 token = next;
324 break;
327 case TOKEN_STRING: {
328 handle_string:
329 expr = alloc_expression(token->pos, EXPR_STRING);
330 token = string_expression(token, expr);
331 break;
334 case TOKEN_SPECIAL:
335 if (token->special == '(') {
336 expr = alloc_expression(token->pos, EXPR_PREOP);
337 expr->op = '(';
338 token = parens_expression(token, &expr->unop, "in expression");
339 break;
341 if (token->special == '[' && lookup_type(token->next)) {
342 expr = alloc_expression(token->pos, EXPR_TYPE);
343 token = typename(token->next, &expr->symbol);
344 token = expect(token, ']', "in type expression");
345 break;
348 default:
351 *tree = expr;
352 return token;
355 static struct token *expression_list(struct token *token, struct expression_list **list)
357 while (!match_op(token, ')')) {
358 struct expression *expr = NULL;
359 token = assignment_expression(token, &expr);
360 if (!expr)
361 break;
362 add_expression(list, expr);
363 if (!match_op(token, ','))
364 break;
365 token = token->next;
367 return token;
371 * extend to deal with the ambiguous C grammar for parsing
372 * a cast expressions followed by an initializer.
374 static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
376 struct expression *expr = cast_init_expr;
378 if (!expr)
379 token = primary_expression(token, &expr);
381 while (expr && token_type(token) == TOKEN_SPECIAL) {
382 switch (token->special) {
383 case '[': { /* Array dereference */
384 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
385 struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
387 deref->op = '*';
388 deref->unop = add;
390 add->op = '+';
391 add->left = expr;
392 token = parse_expression(token->next, &add->right);
393 token = expect(token, ']', "at end of array dereference");
394 expr = deref;
395 continue;
397 case SPECIAL_INCREMENT: /* Post-increment */
398 case SPECIAL_DECREMENT: { /* Post-decrement */
399 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
400 post->op = token->special;
401 post->unop = expr;
402 expr = post;
403 token = token->next;
404 continue;
406 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
407 /* "x->y" is just shorthand for "(*x).y" */
408 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
409 inner->op = '*';
410 inner->unop = expr;
411 expr = inner;
413 /* Fallthrough!! */
414 case '.': { /* Structure member dereference */
415 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
416 deref->op = '.';
417 deref->deref = expr;
418 token = token->next;
419 if (token_type(token) != TOKEN_IDENT) {
420 warning(token->pos, "Expected member name");
421 break;
423 deref->member = token->ident;
424 token = token->next;
425 expr = deref;
426 continue;
429 case '(': { /* Function call */
430 struct expression *call = alloc_expression(token->pos, EXPR_CALL);
431 call->op = '(';
432 call->fn = expr;
433 token = expression_list(token->next, &call->args);
434 token = expect(token, ')', "in function call");
435 expr = call;
436 continue;
439 default:
440 break;
442 break;
444 *tree = expr;
445 return token;
448 static struct token *cast_expression(struct token *token, struct expression **tree);
449 static struct token *unary_expression(struct token *token, struct expression **tree);
451 static struct token *type_info_expression(struct token *token,
452 struct expression **tree, int type)
454 struct expression *expr = alloc_expression(token->pos, type);
456 *tree = expr;
457 token = token->next;
458 if (!match_op(token, '(') || !lookup_type(token->next))
459 return unary_expression(token, &expr->cast_expression);
460 token = typename(token->next, &expr->cast_type);
462 if (!match_op(token, ')')) {
463 static const char * error[] = {
464 [EXPR_SIZEOF] = "at end of sizeof",
465 [EXPR_ALIGNOF] = "at end of __alignof__",
466 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
468 return expect(token, ')', error[type]);
471 token = token->next;
473 * C99 ambiguity: the typename might have been the beginning
474 * of a typed initializer expression..
476 if (match_op(token, '{'))
477 token = initializer(&expr->cast_expression, token);
478 return token;
481 static struct token *unary_expression(struct token *token, struct expression **tree)
483 if (token_type(token) == TOKEN_IDENT) {
484 struct ident *ident = token->ident;
485 if (ident->reserved) {
486 static const struct {
487 struct ident *id;
488 int type;
489 } type_information[] = {
490 { &sizeof_ident, EXPR_SIZEOF },
491 { &__alignof___ident, EXPR_ALIGNOF },
492 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
494 int i;
495 for (i = 0; i < 3; i++) {
496 if (ident == type_information[i].id)
497 return type_info_expression(token, tree, type_information[i].type);
502 if (token_type(token) == TOKEN_SPECIAL) {
503 if (match_oplist(token->special,
504 SPECIAL_INCREMENT, SPECIAL_DECREMENT,
505 '&', '*', '+', '-', '~', '!', 0)) {
506 struct expression *unop;
507 struct expression *unary;
508 struct token *next;
510 next = cast_expression(token->next, &unop);
511 if (!unop) {
512 warning(token->pos, "Syntax error in unary expression");
513 return next;
515 unary = alloc_expression(token->pos, EXPR_PREOP);
516 unary->op = token->special;
517 unary->unop = unop;
518 *tree = unary;
519 return next;
522 /* Gcc extension: &&label gives the address of a label */
523 if (match_op(token, SPECIAL_LOGICAL_AND) &&
524 token_type(token->next) == TOKEN_IDENT) {
525 struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
526 struct symbol *sym = label_symbol(token->next);
527 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
528 sym->ctype.modifiers |= MOD_ADDRESSABLE;
529 add_symbol(&function_computed_target_list, sym);
531 label->label_symbol = sym;
532 *tree = label;
533 return token->next->next;
538 return postfix_expression(token, tree, NULL);
542 * Ambiguity: a '(' can be either a cast-expression or
543 * a primary-expression depending on whether it is followed
544 * by a type or not.
546 * additional ambiguity: a "cast expression" followed by
547 * an initializer is really a postfix-expression.
549 static struct token *cast_expression(struct token *token, struct expression **tree)
551 if (match_op(token, '(')) {
552 struct token *next = token->next;
553 if (lookup_type(next)) {
554 struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
555 struct symbol *sym;
557 token = typename(next, &sym);
558 cast->cast_type = sym;
559 token = expect(token, ')', "at end of cast operator");
560 if (match_op(token, '{')) {
561 token = initializer(&cast->cast_expression, token);
562 return postfix_expression(token, tree, cast);
564 *tree = cast;
565 token = cast_expression(token, &cast->cast_expression);
566 return token;
569 return unary_expression(token, tree);
573 * Generic left-to-right binop parsing
575 * This _really_ needs to be inlined, because that makes the inner
576 * function call statically deterministic rather than a totally
577 * unpredictable indirect call. But gcc-3 is so "clever" that it
578 * doesn't do so by default even when you tell it to inline it.
580 * Making it a macro avoids the inlining problem, and also means
581 * that we can pass in the op-comparison as an expression rather
582 * than create a data structure for it.
585 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
586 struct expression *left = NULL; \
587 struct token * next = inner(token, &left); \
589 if (left) { \
590 while (token_type(next) == TOKEN_SPECIAL) { \
591 struct expression *top, *right = NULL; \
592 int op = next->special; \
594 if (!(compare)) \
595 goto out; \
596 top = alloc_expression(next->pos, type); \
597 next = inner(next->next, &right); \
598 if (!right) { \
599 warning(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
600 break; \
602 top->op = op; \
603 top->left = left; \
604 top->right = right; \
605 left = top; \
608 out: \
609 *tree = left; \
610 return next; \
613 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
615 LR_BINOP_EXPRESSION(
616 token, tree, EXPR_BINOP, cast_expression,
617 (op == '*') || (op == '/') || (op == '%')
621 static struct token *additive_expression(struct token *token, struct expression **tree)
623 LR_BINOP_EXPRESSION(
624 token, tree, EXPR_BINOP, multiplicative_expression,
625 (op == '+') || (op == '-')
629 static struct token *shift_expression(struct token *token, struct expression **tree)
631 LR_BINOP_EXPRESSION(
632 token, tree, EXPR_BINOP, additive_expression,
633 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
637 static struct token *relational_expression(struct token *token, struct expression **tree)
639 LR_BINOP_EXPRESSION(
640 token, tree, EXPR_COMPARE, shift_expression,
641 (op == '<') || (op == '>') ||
642 (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
646 static struct token *equality_expression(struct token *token, struct expression **tree)
648 LR_BINOP_EXPRESSION(
649 token, tree, EXPR_COMPARE, relational_expression,
650 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
654 static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
656 LR_BINOP_EXPRESSION(
657 token, tree, EXPR_BINOP, equality_expression,
658 (op == '&')
662 static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
664 LR_BINOP_EXPRESSION(
665 token, tree, EXPR_BINOP, bitwise_and_expression,
666 (op == '^')
670 static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
672 LR_BINOP_EXPRESSION(
673 token, tree, EXPR_BINOP, bitwise_xor_expression,
674 (op == '|')
678 static struct token *logical_and_expression(struct token *token, struct expression **tree)
680 LR_BINOP_EXPRESSION(
681 token, tree, EXPR_LOGICAL, bitwise_or_expression,
682 (op == SPECIAL_LOGICAL_AND)
686 static struct token *logical_or_expression(struct token *token, struct expression **tree)
688 LR_BINOP_EXPRESSION(
689 token, tree, EXPR_LOGICAL, logical_and_expression,
690 (op == SPECIAL_LOGICAL_OR)
694 struct token *conditional_expression(struct token *token, struct expression **tree)
696 token = logical_or_expression(token, tree);
697 if (match_op(token, '?')) {
698 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
699 expr->op = token->special;
700 expr->left = *tree;
701 *tree = expr;
702 token = parse_expression(token->next, &expr->cond_true);
703 token = expect(token, ':', "in conditional expression");
704 token = conditional_expression(token, &expr->cond_false);
706 return token;
709 struct token *assignment_expression(struct token *token, struct expression **tree)
711 token = conditional_expression(token, tree);
712 if (token_type(token) == TOKEN_SPECIAL) {
713 static const int assignments[] = {
714 '=',
715 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
716 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
717 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
718 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
719 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
720 int i, op = token->special;
721 for (i = 0; i < sizeof(assignments)/sizeof(int); i++)
722 if (assignments[i] == op) {
723 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
724 expr->left = *tree;
725 expr->op = op;
726 *tree = expr;
727 return assignment_expression(token->next, &expr->right);
730 return token;
733 static struct token *comma_expression(struct token *token, struct expression **tree)
735 LR_BINOP_EXPRESSION(
736 token, tree, EXPR_COMMA, assignment_expression,
737 (op == ',')
741 struct token *parse_expression(struct token *token, struct expression **tree)
743 return comma_expression(token,tree);