implied: remove some dead code
[smatch.git] / smatch_type.c
blob869d276de0b5d8b5afaa20db5734b4fd1287efc2
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * The idea here is that you have an expression and you
20 * want to know what the type is for that.
23 #include "smatch.h"
24 #include "smatch_slist.h"
26 struct symbol *get_real_base_type(struct symbol *sym)
28 struct symbol *ret;
30 if (!sym)
31 return NULL;
32 if (sym->type == SYM_BASETYPE)
33 return sym;
34 ret = get_base_type(sym);
35 if (!ret)
36 return NULL;
37 if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
38 return get_real_base_type(ret);
39 return ret;
42 int type_bytes(struct symbol *type)
44 int bits;
46 if (type && type->type == SYM_ARRAY)
47 return array_bytes(type);
49 bits = type_bits(type);
50 if (bits < 0)
51 return 0;
52 return bits_to_bytes(bits);
55 int array_bytes(struct symbol *type)
57 if (!type || type->type != SYM_ARRAY)
58 return 0;
59 return bits_to_bytes(type->bit_size);
62 static struct symbol *get_binop_type(struct expression *expr)
64 struct symbol *left, *right;
66 left = get_type(expr->left);
67 if (!left)
68 return NULL;
70 if (expr->op == SPECIAL_LEFTSHIFT ||
71 expr->op == SPECIAL_RIGHTSHIFT) {
72 if (type_positive_bits(left) < 31)
73 return &int_ctype;
74 return left;
76 right = get_type(expr->right);
77 if (!right)
78 return NULL;
80 if (expr->op == '-' &&
81 (is_ptr_type(left) && is_ptr_type(right)))
82 return ssize_t_ctype;
84 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
85 return left;
86 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
87 return right;
89 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
90 return &int_ctype;
92 if (type_positive_bits(left) > type_positive_bits(right))
93 return left;
94 return right;
97 static struct symbol *get_type_symbol(struct expression *expr)
99 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
100 return NULL;
102 return get_real_base_type(expr->symbol);
105 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
107 struct symbol *tmp, *sub;
109 FOR_EACH_PTR(symbol_list, tmp) {
110 if (!tmp->ident) {
111 sub = get_real_base_type(tmp);
112 sub = get_member_symbol(sub->symbol_list, member);
113 if (sub)
114 return sub;
115 continue;
117 if (tmp->ident == member)
118 return tmp;
119 } END_FOR_EACH_PTR(tmp);
121 return NULL;
124 static struct symbol *get_symbol_from_deref(struct expression *expr)
126 struct ident *member;
127 struct symbol *sym;
129 if (!expr || expr->type != EXPR_DEREF)
130 return NULL;
132 member = expr->member;
133 sym = get_type(expr->deref);
134 if (!sym) {
135 // sm_msg("could not find struct type");
136 return NULL;
138 if (sym->type == SYM_PTR)
139 sym = get_real_base_type(sym);
140 sym = get_member_symbol(sym->symbol_list, member);
141 if (!sym)
142 return NULL;
143 return get_real_base_type(sym);
146 static struct symbol *get_return_type(struct expression *expr)
148 struct symbol *tmp;
150 tmp = get_type(expr->fn);
151 if (!tmp)
152 return NULL;
153 /* this is to handle __builtin_constant_p() */
154 if (tmp->type != SYM_FN)
155 tmp = get_base_type(tmp);
156 return get_real_base_type(tmp);
159 static struct symbol *get_expr_stmt_type(struct statement *stmt)
161 if (stmt->type != STMT_COMPOUND)
162 return NULL;
163 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
164 if (stmt->type == STMT_LABEL)
165 stmt = stmt->label_statement;
166 if (stmt->type != STMT_EXPRESSION)
167 return NULL;
168 return get_type(stmt->expression);
171 static struct symbol *get_select_type(struct expression *expr)
173 struct symbol *one, *two;
175 one = get_type(expr->cond_true);
176 two = get_type(expr->cond_false);
177 if (!one || !two)
178 return NULL;
180 * This is a hack. If the types are not equiv then we
181 * really don't know the type. But I think guessing is
182 * probably Ok here.
184 if (type_positive_bits(one) > type_positive_bits(two))
185 return one;
186 return two;
189 struct symbol *get_pointer_type(struct expression *expr)
191 struct symbol *sym;
193 sym = get_type(expr);
194 if (!sym)
195 return NULL;
196 if (sym->type == SYM_NODE) {
197 sym = get_real_base_type(sym);
198 if (!sym)
199 return NULL;
201 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
202 return NULL;
203 return get_real_base_type(sym);
206 static struct symbol *fake_pointer_sym(struct expression *expr)
208 struct symbol *sym;
209 struct symbol *base;
211 sym = alloc_symbol(expr->pos, SYM_PTR);
212 expr = expr->unop;
213 base = get_type(expr);
214 if (!base)
215 return NULL;
216 sym->ctype.base_type = base;
217 return sym;
220 static struct symbol *get_type_helper(struct expression *expr)
222 struct symbol *ret;
224 expr = strip_parens(expr);
225 if (!expr)
226 return NULL;
228 if (expr->ctype)
229 return expr->ctype;
231 switch (expr->type) {
232 case EXPR_STRING:
233 ret = &string_ctype;
234 break;
235 case EXPR_SYMBOL:
236 ret = get_type_symbol(expr);
237 break;
238 case EXPR_DEREF:
239 ret = get_symbol_from_deref(expr);
240 break;
241 case EXPR_PREOP:
242 case EXPR_POSTOP:
243 if (expr->op == '&')
244 ret = fake_pointer_sym(expr);
245 else if (expr->op == '*')
246 ret = get_pointer_type(expr->unop);
247 else
248 ret = get_type(expr->unop);
249 break;
250 case EXPR_ASSIGNMENT:
251 ret = get_type(expr->left);
252 break;
253 case EXPR_CAST:
254 case EXPR_FORCE_CAST:
255 case EXPR_IMPLIED_CAST:
256 ret = get_real_base_type(expr->cast_type);
257 break;
258 case EXPR_COMPARE:
259 case EXPR_BINOP:
260 ret = get_binop_type(expr);
261 break;
262 case EXPR_CALL:
263 ret = get_return_type(expr);
264 break;
265 case EXPR_STATEMENT:
266 ret = get_expr_stmt_type(expr->statement);
267 break;
268 case EXPR_CONDITIONAL:
269 case EXPR_SELECT:
270 ret = get_select_type(expr);
271 break;
272 case EXPR_SIZEOF:
273 ret = &ulong_ctype;
274 break;
275 case EXPR_LOGICAL:
276 ret = &int_ctype;
277 break;
278 default:
279 return NULL;
282 if (ret && ret->type == SYM_TYPEOF)
283 ret = get_type(ret->initializer);
285 expr->ctype = ret;
286 return ret;
289 static struct symbol *get_final_type_helper(struct expression *expr)
292 * The problem is that I wrote a bunch of Smatch to think that
293 * you could do get_type() on an expression and it would give
294 * you what the comparison was type promoted to. This is wrong
295 * but fixing it is a big of work... Hence this horrible hack.
299 expr = strip_parens(expr);
300 if (!expr)
301 return NULL;
303 if (expr->type == EXPR_COMPARE)
304 return &int_ctype;
306 return NULL;
309 struct symbol *get_type(struct expression *expr)
311 return get_type_helper(expr);
314 struct symbol *get_final_type(struct expression *expr)
316 struct symbol *ret;
318 ret = get_final_type_helper(expr);
319 if (ret)
320 return ret;
321 return get_type_helper(expr);
324 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right)
326 struct symbol *ret = &int_ctype;
328 if (type_positive_bits(left) > type_positive_bits(ret))
329 ret = left;
330 if (type_positive_bits(right) > type_positive_bits(ret))
331 ret = right;
333 if (type_is_ptr(left))
334 ret = left;
335 if (type_is_ptr(right))
336 ret = right;
338 return ret;
341 int type_signed(struct symbol *base_type)
343 if (!base_type)
344 return 0;
345 if (base_type->ctype.modifiers & MOD_SIGNED)
346 return 1;
347 return 0;
350 int expr_unsigned(struct expression *expr)
352 struct symbol *sym;
354 sym = get_type(expr);
355 if (!sym)
356 return 0;
357 if (type_unsigned(sym))
358 return 1;
359 return 0;
362 int expr_signed(struct expression *expr)
364 struct symbol *sym;
366 sym = get_type(expr);
367 if (!sym)
368 return 0;
369 if (type_signed(sym))
370 return 1;
371 return 0;
374 int returns_unsigned(struct symbol *sym)
376 if (!sym)
377 return 0;
378 sym = get_base_type(sym);
379 if (!sym || sym->type != SYM_FN)
380 return 0;
381 sym = get_base_type(sym);
382 return type_unsigned(sym);
385 int is_pointer(struct expression *expr)
387 struct symbol *sym;
389 sym = get_type(expr);
390 if (!sym)
391 return 0;
392 if (sym == &string_ctype)
393 return 0;
394 if (sym->type == SYM_PTR)
395 return 1;
396 return 0;
399 int returns_pointer(struct symbol *sym)
401 if (!sym)
402 return 0;
403 sym = get_base_type(sym);
404 if (!sym || sym->type != SYM_FN)
405 return 0;
406 sym = get_base_type(sym);
407 if (sym->type == SYM_PTR)
408 return 1;
409 return 0;
412 sval_t sval_type_max(struct symbol *base_type)
414 sval_t ret;
416 if (!base_type || !type_bits(base_type))
417 base_type = &llong_ctype;
418 ret.type = base_type;
420 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
421 return ret;
424 sval_t sval_type_min(struct symbol *base_type)
426 sval_t ret;
428 if (!base_type || !type_bits(base_type))
429 base_type = &llong_ctype;
430 ret.type = base_type;
432 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
433 ret.value = 0;
434 return ret;
437 ret.value = (~0ULL) << type_positive_bits(base_type);
439 return ret;
442 int nr_bits(struct expression *expr)
444 struct symbol *type;
446 type = get_type(expr);
447 if (!type)
448 return 0;
449 return type_bits(type);
452 int is_void_pointer(struct expression *expr)
454 struct symbol *type;
456 type = get_type(expr);
457 if (!type || type->type != SYM_PTR)
458 return 0;
459 type = get_real_base_type(type);
460 if (type == &void_ctype)
461 return 1;
462 return 0;
465 int is_char_pointer(struct expression *expr)
467 struct symbol *type;
469 type = get_type(expr);
470 if (!type || type->type != SYM_PTR)
471 return 0;
472 type = get_real_base_type(type);
473 if (type == &char_ctype)
474 return 1;
475 return 0;
478 int is_string(struct expression *expr)
480 expr = strip_expr(expr);
481 if (!expr || expr->type != EXPR_STRING)
482 return 0;
483 if (expr->string)
484 return 1;
485 return 0;
488 int is_static(struct expression *expr)
490 char *name;
491 struct symbol *sym;
492 int ret = 0;
494 name = expr_to_str_sym(expr, &sym);
495 if (!name || !sym)
496 goto free;
498 if (sym->ctype.modifiers & MOD_STATIC)
499 ret = 1;
500 free:
501 free_string(name);
502 return ret;
505 int is_local_variable(struct expression *expr)
507 struct symbol *sym;
508 char *name;
510 name = expr_to_var_sym(expr, &sym);
511 free_string(name);
512 if (!sym || !sym->scope || !sym->scope->token || !cur_func_sym)
513 return 0;
514 if (cmp_pos(sym->scope->token->pos, cur_func_sym->pos) < 0)
515 return 0;
516 if (is_static(expr))
517 return 0;
518 return 1;
521 int types_equiv(struct symbol *one, struct symbol *two)
523 if (!one && !two)
524 return 1;
525 if (!one || !two)
526 return 0;
527 if (one->type != two->type)
528 return 0;
529 if (one->type == SYM_PTR)
530 return types_equiv(get_real_base_type(one), get_real_base_type(two));
531 if (type_positive_bits(one) != type_positive_bits(two))
532 return 0;
533 return 1;
536 int fn_static(void)
538 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
541 const char *global_static(void)
543 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
544 return "static";
545 else
546 return "global";
549 struct symbol *cur_func_return_type(void)
551 struct symbol *sym;
553 sym = get_real_base_type(cur_func_sym);
554 if (!sym || sym->type != SYM_FN)
555 return NULL;
556 sym = get_real_base_type(sym);
557 return sym;
560 struct symbol *get_arg_type(struct expression *fn, int arg)
562 struct symbol *fn_type;
563 struct symbol *tmp;
564 struct symbol *arg_type;
565 int i;
567 fn_type = get_type(fn);
568 if (!fn_type)
569 return NULL;
570 if (fn_type->type == SYM_PTR)
571 fn_type = get_real_base_type(fn_type);
572 if (fn_type->type != SYM_FN)
573 return NULL;
575 i = 0;
576 FOR_EACH_PTR(fn_type->arguments, tmp) {
577 arg_type = get_real_base_type(tmp);
578 if (i == arg) {
579 return arg_type;
581 i++;
582 } END_FOR_EACH_PTR(tmp);
584 return NULL;
587 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
589 struct symbol *tmp, *sub;
590 int chunk_len;
592 if (strncmp(name, ".", 1) == 0)
593 name += 1;
594 else if (strncmp(name, "->", 2) == 0)
595 name += 2;
597 FOR_EACH_PTR(symbol_list, tmp) {
598 if (!tmp->ident) {
599 sub = get_real_base_type(tmp);
600 sub = get_member_from_string(sub->symbol_list, name);
601 if (sub)
602 return sub;
603 continue;
606 if (strcmp(tmp->ident->name, name) == 0)
607 return tmp;
609 chunk_len = tmp->ident->len;
610 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
611 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
612 sub = get_real_base_type(tmp);
613 if (sub->type == SYM_PTR)
614 sub = get_real_base_type(sub);
615 return get_member_from_string(sub->symbol_list, name + chunk_len);
618 } END_FOR_EACH_PTR(tmp);
620 return NULL;
623 struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
625 struct symbol *sym;
627 if (strcmp(key, "$") == 0)
628 return get_type(expr);
630 if (strcmp(key, "*$") == 0) {
631 sym = get_type(expr);
632 if (!sym || sym->type != SYM_PTR)
633 return NULL;
634 return get_real_base_type(sym);
637 sym = get_type(expr);
638 if (!sym)
639 return NULL;
640 if (sym->type == SYM_PTR)
641 sym = get_real_base_type(sym);
643 key = key + 1;
644 sym = get_member_from_string(sym->symbol_list, key);
645 if (!sym)
646 return NULL;
647 return get_real_base_type(sym);
650 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key)
652 struct symbol *type;
654 if (!key)
655 return NULL;
656 if (strcmp(key, "$") == 0)
657 return get_arg_type(fn, param);
658 if (strcmp(key, "*$") == 0) {
659 type = get_arg_type(fn, param);
660 if (!type || type->type != SYM_PTR)
661 return NULL;
662 return get_real_base_type(type);
664 return get_member_type_from_key(arg, key);
667 int is_struct(struct expression *expr)
669 struct symbol *type;
671 type = get_type(expr);
672 if (type && type->type == SYM_STRUCT)
673 return 1;
674 return 0;
677 static struct {
678 struct symbol *sym;
679 const char *name;
680 } base_types[] = {
681 {&bool_ctype, "bool"},
682 {&void_ctype, "void"},
683 {&type_ctype, "type"},
684 {&char_ctype, "char"},
685 {&schar_ctype, "schar"},
686 {&uchar_ctype, "uchar"},
687 {&short_ctype, "short"},
688 {&sshort_ctype, "sshort"},
689 {&ushort_ctype, "ushort"},
690 {&int_ctype, "int"},
691 {&sint_ctype, "sint"},
692 {&uint_ctype, "uint"},
693 {&long_ctype, "long"},
694 {&slong_ctype, "slong"},
695 {&ulong_ctype, "ulong"},
696 {&llong_ctype, "llong"},
697 {&sllong_ctype, "sllong"},
698 {&ullong_ctype, "ullong"},
699 {&lllong_ctype, "lllong"},
700 {&slllong_ctype, "slllong"},
701 {&ulllong_ctype, "ulllong"},
702 {&float_ctype, "float"},
703 {&double_ctype, "double"},
704 {&ldouble_ctype, "ldouble"},
705 {&string_ctype, "string"},
706 {&ptr_ctype, "ptr"},
707 {&lazy_ptr_ctype, "lazy_ptr"},
708 {&incomplete_ctype, "incomplete"},
709 {&label_ctype, "label"},
710 {&bad_ctype, "bad"},
711 {&null_ctype, "null"},
714 static const char *base_type_str(struct symbol *sym)
716 int i;
718 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
719 if (sym == base_types[i].sym)
720 return base_types[i].name;
722 return "<unknown>";
725 static int type_str_helper(char *buf, int size, struct symbol *type)
727 int n;
729 if (!type)
730 return snprintf(buf, size, "<unknown>");
732 if (type->type == SYM_BASETYPE) {
733 return snprintf(buf, size, "%s", base_type_str(type));
734 } else if (type->type == SYM_PTR) {
735 type = get_real_base_type(type);
736 n = type_str_helper(buf, size, type);
737 if (n > size)
738 return n;
739 return n + snprintf(buf + n, size - n, "*");
740 } else if (type->type == SYM_ARRAY) {
741 type = get_real_base_type(type);
742 n = type_str_helper(buf, size, type);
743 if (n > size)
744 return n;
745 return n + snprintf(buf + n, size - n, "[]");
746 } else if (type->type == SYM_STRUCT) {
747 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
748 } else if (type->type == SYM_UNION) {
749 if (type->ident)
750 return snprintf(buf, size, "union %s", type->ident->name);
751 else
752 return snprintf(buf, size, "anonymous union");
753 } else if (type->type == SYM_FN) {
754 struct symbol *arg, *return_type, *arg_type;
755 int i;
757 return_type = get_real_base_type(type);
758 n = type_str_helper(buf, size, return_type);
759 if (n > size)
760 return n;
761 n += snprintf(buf + n, size - n, "(*)(");
762 if (n > size)
763 return n;
765 i = 0;
766 FOR_EACH_PTR(type->arguments, arg) {
767 if (i++)
768 n += snprintf(buf + n, size - n, ", ");
769 if (n > size)
770 return n;
771 arg_type = get_real_base_type(arg);
772 n += type_str_helper(buf + n, size - n, arg_type);
773 if (n > size)
774 return n;
775 } END_FOR_EACH_PTR(arg);
777 return n + snprintf(buf + n, size - n, ")");
778 } else if (type->type == SYM_NODE) {
779 n = snprintf(buf, size, "node {");
780 if (n > size)
781 return n;
782 type = get_real_base_type(type);
783 n += type_str_helper(buf + n, size - n, type);
784 if (n > size)
785 return n;
786 return n + snprintf(buf + n, size - n, "}");
787 } else {
788 return snprintf(buf, size, "<type %d>", type->type);
792 char *type_to_str(struct symbol *type)
794 static char buf[256];
796 buf[0] = '\0';
797 type_str_helper(buf, sizeof(buf), type);
798 return buf;