unwind: paths where the parent is gone are counted as param_released
[smatch.git] / smatch_type.c
blob7cd2c85fa64e3babb399fc016a766c46c5ed21c2
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 if (!type->array_size)
60 return 0;
61 return bits_to_bytes(type->bit_size);
64 static struct symbol *get_binop_type(struct expression *expr)
66 struct symbol *left, *right;
68 left = get_type(expr->left);
69 if (!left)
70 return NULL;
72 if (expr->op == SPECIAL_LEFTSHIFT ||
73 expr->op == SPECIAL_RIGHTSHIFT) {
74 if (type_positive_bits(left) < 31)
75 return &int_ctype;
76 return left;
78 right = get_type(expr->right);
79 if (!right)
80 return NULL;
82 if (type_is_fp(left)) {
83 if (type_is_fp(right)) {
84 if (type_bits(left) > type_bits(right))
85 return left;
86 return right;
88 return left;
91 if (type_is_fp(right)) {
92 if (type_is_fp(left)) {
93 if (type_bits(right) > type_bits(left))
94 return right;
95 return left;
97 return right;
100 if (expr->op == '-' &&
101 (is_ptr_type(left) && is_ptr_type(right)))
102 return ssize_t_ctype;
104 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
105 return left;
106 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
107 return right;
109 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
110 return &int_ctype;
112 if (type_positive_bits(left) > type_positive_bits(right))
113 return left;
114 return right;
117 static struct symbol *get_type_symbol(struct expression *expr)
119 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
120 return NULL;
122 return get_real_base_type(expr->symbol);
125 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
127 struct symbol *tmp, *sub;
129 FOR_EACH_PTR(symbol_list, tmp) {
130 if (!tmp->ident) {
131 sub = get_real_base_type(tmp);
132 sub = get_member_symbol(sub->symbol_list, member);
133 if (sub)
134 return sub;
135 continue;
137 if (tmp->ident == member)
138 return tmp;
139 } END_FOR_EACH_PTR(tmp);
141 return NULL;
144 static struct symbol *get_symbol_from_deref(struct expression *expr)
146 struct ident *member;
147 struct symbol *sym;
149 if (!expr || expr->type != EXPR_DEREF)
150 return NULL;
152 member = expr->member;
153 sym = get_type(expr->deref);
154 if (!sym) {
155 // sm_msg("could not find struct type");
156 return NULL;
158 if (sym->type == SYM_PTR)
159 sym = get_real_base_type(sym);
160 sym = get_member_symbol(sym->symbol_list, member);
161 if (!sym)
162 return NULL;
163 return get_real_base_type(sym);
166 static struct symbol *handle__builtin_choose_expr(struct expression *expr)
168 struct expression *const_expr, *expr1, *expr2;
169 sval_t sval;
171 const_expr = get_argument_from_call_expr(expr->args, 0);
172 expr1 = get_argument_from_call_expr(expr->args, 1);
173 expr2 = get_argument_from_call_expr(expr->args, 2);
175 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
176 return NULL;
177 if (sval.value)
178 return get_type(expr1);
179 else
180 return get_type(expr2);
183 static struct symbol *get_return_type(struct expression *expr)
185 struct symbol *tmp;
187 if (sym_name_is("__builtin_choose_expr", expr->fn))
188 return handle__builtin_choose_expr(expr);
190 tmp = get_type(expr->fn);
191 if (!tmp)
192 return NULL;
193 /* this is to handle __builtin_constant_p() */
194 if (tmp->type != SYM_FN)
195 tmp = get_base_type(tmp);
196 return get_real_base_type(tmp);
199 static struct symbol *get_expr_stmt_type(struct statement *stmt)
201 if (stmt->type != STMT_COMPOUND)
202 return NULL;
203 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
204 if (stmt->type == STMT_LABEL)
205 stmt = stmt->label_statement;
206 if (stmt->type != STMT_EXPRESSION)
207 return NULL;
208 return get_type(stmt->expression);
211 static struct symbol *get_select_type(struct expression *expr)
213 struct symbol *one, *two;
215 one = get_type(expr->cond_true);
216 two = get_type(expr->cond_false);
217 if (!one || !two)
218 return NULL;
220 * This is a hack. If the types are not equiv then we
221 * really don't know the type. But I think guessing is
222 * probably Ok here.
224 if (type_positive_bits(one) > type_positive_bits(two))
225 return one;
226 return two;
229 struct symbol *get_pointer_type(struct expression *expr)
231 struct symbol *sym;
233 sym = get_type(expr);
234 if (!sym)
235 return NULL;
236 if (sym->type == SYM_NODE) {
237 sym = get_real_base_type(sym);
238 if (!sym)
239 return NULL;
241 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
242 return NULL;
243 return get_real_base_type(sym);
246 static struct symbol *fake_pointer_sym(struct expression *expr)
248 struct symbol *sym;
249 struct symbol *base;
251 sym = alloc_symbol(expr->pos, SYM_PTR);
252 expr = expr->unop;
253 base = get_type(expr);
254 if (!base)
255 return NULL;
256 sym->ctype.base_type = base;
257 return sym;
260 static struct symbol *get_type_helper(struct expression *expr)
262 struct symbol *ret;
264 expr = strip_parens(expr);
265 if (!expr)
266 return NULL;
268 if (expr->ctype)
269 return expr->ctype;
271 switch (expr->type) {
272 case EXPR_STRING:
273 ret = &string_ctype;
274 break;
275 case EXPR_SYMBOL:
276 ret = get_type_symbol(expr);
277 break;
278 case EXPR_DEREF:
279 ret = get_symbol_from_deref(expr);
280 break;
281 case EXPR_PREOP:
282 case EXPR_POSTOP:
283 if (expr->op == '&')
284 ret = fake_pointer_sym(expr);
285 else if (expr->op == '*')
286 ret = get_pointer_type(expr->unop);
287 else
288 ret = get_type(expr->unop);
289 break;
290 case EXPR_ASSIGNMENT:
291 ret = get_type(expr->left);
292 break;
293 case EXPR_CAST:
294 case EXPR_FORCE_CAST:
295 case EXPR_IMPLIED_CAST:
296 ret = get_real_base_type(expr->cast_type);
297 break;
298 case EXPR_COMPARE:
299 case EXPR_BINOP:
300 ret = get_binop_type(expr);
301 break;
302 case EXPR_CALL:
303 ret = get_return_type(expr);
304 break;
305 case EXPR_STATEMENT:
306 ret = get_expr_stmt_type(expr->statement);
307 break;
308 case EXPR_CONDITIONAL:
309 case EXPR_SELECT:
310 ret = get_select_type(expr);
311 break;
312 case EXPR_SIZEOF:
313 ret = &ulong_ctype;
314 break;
315 case EXPR_LOGICAL:
316 ret = &int_ctype;
317 break;
318 case EXPR_OFFSETOF:
319 ret = &ulong_ctype;
320 break;
321 default:
322 return NULL;
325 if (ret && ret->type == SYM_TYPEOF)
326 ret = get_type(ret->initializer);
328 expr->ctype = ret;
329 return ret;
332 static struct symbol *get_final_type_helper(struct expression *expr)
335 * The problem is that I wrote a bunch of Smatch to think that
336 * you could do get_type() on an expression and it would give
337 * you what the comparison was type promoted to. This is wrong
338 * but fixing it is a big of work... Hence this horrible hack.
342 expr = strip_parens(expr);
343 if (!expr)
344 return NULL;
346 if (expr->type == EXPR_COMPARE)
347 return &int_ctype;
349 return NULL;
352 struct symbol *get_type(struct expression *expr)
354 return get_type_helper(expr);
357 struct symbol *get_final_type(struct expression *expr)
359 struct symbol *ret;
361 ret = get_final_type_helper(expr);
362 if (ret)
363 return ret;
364 return get_type_helper(expr);
367 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right)
369 struct symbol *ret = &int_ctype;
371 if (type_positive_bits(left) > type_positive_bits(ret))
372 ret = left;
373 if (type_positive_bits(right) > type_positive_bits(ret))
374 ret = right;
376 if (type_is_ptr(left))
377 ret = left;
378 if (type_is_ptr(right))
379 ret = right;
381 return ret;
384 int type_signed(struct symbol *base_type)
386 if (!base_type)
387 return 0;
388 if (base_type->ctype.modifiers & MOD_SIGNED)
389 return 1;
390 return 0;
393 int expr_unsigned(struct expression *expr)
395 struct symbol *sym;
397 sym = get_type(expr);
398 if (!sym)
399 return 0;
400 if (type_unsigned(sym))
401 return 1;
402 return 0;
405 int expr_signed(struct expression *expr)
407 struct symbol *sym;
409 sym = get_type(expr);
410 if (!sym)
411 return 0;
412 if (type_signed(sym))
413 return 1;
414 return 0;
417 int returns_unsigned(struct symbol *sym)
419 if (!sym)
420 return 0;
421 sym = get_base_type(sym);
422 if (!sym || sym->type != SYM_FN)
423 return 0;
424 sym = get_base_type(sym);
425 return type_unsigned(sym);
428 int is_pointer(struct expression *expr)
430 return type_is_ptr(get_type(expr));
433 int returns_pointer(struct symbol *sym)
435 if (!sym)
436 return 0;
437 sym = get_base_type(sym);
438 if (!sym || sym->type != SYM_FN)
439 return 0;
440 sym = get_base_type(sym);
441 if (sym && sym->type == SYM_PTR)
442 return 1;
443 return 0;
446 static sval_t fp_max(struct symbol *type)
448 sval_t ret = { .type = type };
450 if (type == &float_ctype)
451 ret.fvalue = FLT_MAX;
452 else if (type == &double_ctype)
453 ret.dvalue = DBL_MAX;
454 else
455 ret.ldvalue = LDBL_MAX;
457 return ret;
460 sval_t sval_type_max(struct symbol *base_type)
462 sval_t ret;
464 if (type_is_fp(base_type))
465 return fp_max(base_type);
467 if (!base_type || !type_bits(base_type))
468 base_type = &llong_ctype;
469 ret.type = base_type;
471 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
472 return ret;
475 static sval_t fp_min(struct symbol *type)
477 sval_t ret = { .type = type };
479 if (type == &float_ctype)
480 ret.fvalue = -FLT_MAX;
481 else if (type == &double_ctype)
482 ret.dvalue = -DBL_MAX;
483 else
484 ret.ldvalue = -LDBL_MAX;
486 return ret;
489 sval_t sval_type_min(struct symbol *base_type)
491 sval_t ret;
493 if (type_is_fp(base_type))
494 return fp_min(base_type);
496 if (!base_type || !type_bits(base_type))
497 base_type = &llong_ctype;
498 ret.type = base_type;
500 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
501 ret.value = 0;
502 return ret;
505 ret.value = (~0ULL) << type_positive_bits(base_type);
507 return ret;
510 int nr_bits(struct expression *expr)
512 struct symbol *type;
514 type = get_type(expr);
515 if (!type)
516 return 0;
517 return type_bits(type);
520 int is_void_pointer(struct expression *expr)
522 struct symbol *type;
524 type = get_type(expr);
525 if (!type || type->type != SYM_PTR)
526 return 0;
527 type = get_real_base_type(type);
528 if (type == &void_ctype)
529 return 1;
530 return 0;
533 int is_char_pointer(struct expression *expr)
535 struct symbol *type;
537 type = get_type(expr);
538 if (!type || type->type != SYM_PTR)
539 return 0;
540 type = get_real_base_type(type);
541 if (type == &char_ctype)
542 return 1;
543 return 0;
546 int is_string(struct expression *expr)
548 expr = strip_expr(expr);
549 if (!expr || expr->type != EXPR_STRING)
550 return 0;
551 if (expr->string)
552 return 1;
553 return 0;
556 bool is_struct_ptr(struct symbol *type)
558 if (!type || type->type != SYM_PTR)
559 return false;
560 type = get_real_base_type(type);
561 if (!type || type->type != SYM_STRUCT)
562 return false;
563 return true;
566 int is_static(struct expression *expr)
568 char *name;
569 struct symbol *sym;
570 int ret = 0;
572 name = expr_to_str_sym(expr, &sym);
573 if (!name || !sym)
574 goto free;
576 if (sym->ctype.modifiers & MOD_STATIC)
577 ret = 1;
578 free:
579 free_string(name);
580 return ret;
583 static struct expression *get_symbol_expr(struct expression *expr)
585 if (!expr)
586 return NULL;
587 while (expr && expr->type == EXPR_DEREF && expr->op == '.')
588 expr = strip_expr(expr->deref);
589 return expr;
592 bool is_local_variable(struct expression *expr)
594 struct symbol *sym;
596 expr = get_symbol_expr(expr);
597 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
598 return false;
599 sym = expr->symbol;
600 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
601 return true;
602 return false;
605 int types_equiv(struct symbol *one, struct symbol *two)
607 if (!one && !two)
608 return 1;
609 if (!one || !two)
610 return 0;
611 if (one->type != two->type)
612 return 0;
613 if (one->type == SYM_PTR)
614 return types_equiv(get_real_base_type(one), get_real_base_type(two));
615 if (type_positive_bits(one) != type_positive_bits(two))
616 return 0;
617 return 1;
620 bool type_fits(struct symbol *type, struct symbol *test)
622 if (!type || !test)
623 return false;
625 if (type == test)
626 return true;
628 if (type_bits(test) > type_bits(type))
629 return false;
630 if (type_signed(test) && !type_signed(type))
631 return false;
632 if (type_positive_bits(test) > type_positive_bits(type))
633 return false;
634 return true;
637 int fn_static(void)
639 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
642 const char *global_static(void)
644 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
645 return "static";
646 else
647 return "global";
650 struct symbol *cur_func_return_type(void)
652 struct symbol *sym;
654 sym = get_real_base_type(cur_func_sym);
655 if (!sym || sym->type != SYM_FN)
656 return NULL;
657 sym = get_real_base_type(sym);
658 return sym;
661 struct symbol *get_arg_type(struct expression *fn, int arg)
663 struct symbol *fn_type;
664 struct symbol *tmp;
665 struct symbol *arg_type;
666 int i;
668 fn_type = get_type(fn);
669 if (!fn_type)
670 return NULL;
671 if (fn_type->type == SYM_PTR)
672 fn_type = get_real_base_type(fn_type);
673 if (fn_type->type != SYM_FN)
674 return NULL;
676 i = 0;
677 FOR_EACH_PTR(fn_type->arguments, tmp) {
678 arg_type = get_real_base_type(tmp);
679 if (i == arg) {
680 return arg_type;
682 i++;
683 } END_FOR_EACH_PTR(tmp);
685 return NULL;
688 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
690 struct symbol *tmp, *sub;
691 int chunk_len;
693 if (strncmp(name, ".", 1) == 0)
694 name += 1;
695 else if (strncmp(name, "->", 2) == 0)
696 name += 2;
698 FOR_EACH_PTR(symbol_list, tmp) {
699 if (!tmp->ident) {
700 sub = get_real_base_type(tmp);
701 sub = get_member_from_string(sub->symbol_list, name);
702 if (sub)
703 return sub;
704 continue;
707 if (strcmp(tmp->ident->name, name) == 0)
708 return tmp;
710 chunk_len = tmp->ident->len;
711 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
712 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
713 sub = get_real_base_type(tmp);
714 if (sub->type == SYM_PTR)
715 sub = get_real_base_type(sub);
716 return get_member_from_string(sub->symbol_list, name + chunk_len);
719 } END_FOR_EACH_PTR(tmp);
721 return NULL;
724 struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
726 struct symbol *sym;
727 int star = 0;
728 int i;
730 if (strcmp(key, "$") == 0)
731 return get_type(expr);
733 if (strcmp(key, "*$") == 0) {
734 sym = get_type(expr);
735 if (!sym || sym->type != SYM_PTR)
736 return NULL;
737 return get_real_base_type(sym);
740 sym = get_type(expr);
741 if (!sym)
742 return NULL;
743 if (sym->type == SYM_PTR)
744 sym = get_real_base_type(sym);
746 while (*key == '*') {
747 key++;
748 star++;
751 if (*key != '$')
752 return NULL;
753 key++;
755 sym = get_member_from_string(sym->symbol_list, key);
756 if (!sym)
757 return NULL;
758 if (sym->type == SYM_RESTRICT || sym->type == SYM_NODE)
759 sym = get_real_base_type(sym);
760 for (i = 0; i < star; i++) {
761 if (!sym || sym->type != SYM_PTR)
762 return NULL;
763 sym = get_real_base_type(sym);
765 return sym;
768 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key)
770 struct symbol *type;
772 if (!key)
773 return NULL;
774 if (strcmp(key, "$") == 0)
775 return get_arg_type(fn, param);
776 if (strcmp(key, "*$") == 0) {
777 type = get_arg_type(fn, param);
778 if (!type || type->type != SYM_PTR)
779 return NULL;
780 return get_real_base_type(type);
782 return get_member_type_from_key(arg, key);
785 int is_struct(struct expression *expr)
787 struct symbol *type;
789 type = get_type(expr);
790 if (type && type->type == SYM_STRUCT)
791 return 1;
792 return 0;
795 static struct {
796 struct symbol *sym;
797 const char *name;
798 } base_types[] = {
799 {&bool_ctype, "bool"},
800 {&void_ctype, "void"},
801 {&type_ctype, "type"},
802 {&char_ctype, "char"},
803 {&schar_ctype, "schar"},
804 {&uchar_ctype, "uchar"},
805 {&short_ctype, "short"},
806 {&sshort_ctype, "sshort"},
807 {&ushort_ctype, "ushort"},
808 {&int_ctype, "int"},
809 {&sint_ctype, "sint"},
810 {&uint_ctype, "uint"},
811 {&long_ctype, "long"},
812 {&slong_ctype, "slong"},
813 {&ulong_ctype, "ulong"},
814 {&llong_ctype, "llong"},
815 {&sllong_ctype, "sllong"},
816 {&ullong_ctype, "ullong"},
817 {&int128_ctype, "lllong"},
818 {&sint128_ctype, "slllong"},
819 {&uint128_ctype, "ulllong"},
820 {&float_ctype, "float"},
821 {&double_ctype, "double"},
822 {&ldouble_ctype, "ldouble"},
823 {&string_ctype, "string"},
824 {&ptr_ctype, "ptr"},
825 {&lazy_ptr_ctype, "lazy_ptr"},
826 {&incomplete_ctype, "incomplete"},
827 {&label_ctype, "label"},
828 {&bad_ctype, "bad"},
829 {&null_ctype, "null"},
832 static const char *base_type_str(struct symbol *sym)
834 int i;
836 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
837 if (sym == base_types[i].sym)
838 return base_types[i].name;
840 return "<unknown>";
843 static int type_str_helper(char *buf, int size, struct symbol *type)
845 int n;
847 if (!type)
848 return snprintf(buf, size, "<null type>");
850 if (type->type == SYM_BASETYPE) {
851 return snprintf(buf, size, "%s", base_type_str(type));
852 } else if (type->type == SYM_PTR) {
853 type = get_real_base_type(type);
854 n = type_str_helper(buf, size, type);
855 if (n > size)
856 return n;
857 return n + snprintf(buf + n, size - n, "*");
858 } else if (type->type == SYM_ARRAY) {
859 type = get_real_base_type(type);
860 n = type_str_helper(buf, size, type);
861 if (n > size)
862 return n;
863 return n + snprintf(buf + n, size - n, "[]");
864 } else if (type->type == SYM_STRUCT) {
865 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
866 } else if (type->type == SYM_UNION) {
867 if (type->ident)
868 return snprintf(buf, size, "union %s", type->ident->name);
869 else
870 return snprintf(buf, size, "anonymous union");
871 } else if (type->type == SYM_FN) {
872 struct symbol *arg, *return_type, *arg_type;
873 int i;
875 return_type = get_real_base_type(type);
876 n = type_str_helper(buf, size, return_type);
877 if (n > size)
878 return n;
879 n += snprintf(buf + n, size - n, "(*)(");
880 if (n > size)
881 return n;
883 i = 0;
884 FOR_EACH_PTR(type->arguments, arg) {
885 if (i++)
886 n += snprintf(buf + n, size - n, ", ");
887 if (n > size)
888 return n;
889 arg_type = get_real_base_type(arg);
890 n += type_str_helper(buf + n, size - n, arg_type);
891 if (n > size)
892 return n;
893 } END_FOR_EACH_PTR(arg);
895 return n + snprintf(buf + n, size - n, ")");
896 } else if (type->type == SYM_NODE) {
897 n = snprintf(buf, size, "node {");
898 if (n > size)
899 return n;
900 type = get_real_base_type(type);
901 n += type_str_helper(buf + n, size - n, type);
902 if (n > size)
903 return n;
904 return n + snprintf(buf + n, size - n, "}");
905 } else if (type->type == SYM_ENUM) {
906 return snprintf(buf, size, "enum %s", type->ident ? type->ident->name : "<unknown>");
907 } else {
908 return snprintf(buf, size, "<type %d>", type->type);
912 char *type_to_str(struct symbol *type)
914 static char buf[256];
916 buf[0] = '\0';
917 type_str_helper(buf, sizeof(buf), type);
918 return buf;