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.
24 #include "smatch_slist.h"
26 struct symbol
*get_real_base_type(struct symbol
*sym
)
32 if (sym
->type
== SYM_BASETYPE
)
34 ret
= get_base_type(sym
);
37 if (ret
->type
== SYM_RESTRICT
|| ret
->type
== SYM_NODE
)
38 return get_real_base_type(ret
);
42 int type_bytes(struct symbol
*type
)
46 if (type
&& type
->type
== SYM_ARRAY
)
47 return array_bytes(type
);
49 bits
= type_bits(type
);
52 return bits_to_bytes(bits
);
55 int array_bytes(struct symbol
*type
)
57 if (!type
|| type
->type
!= SYM_ARRAY
)
59 if (!type
->array_size
)
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
);
72 if (expr
->op
== SPECIAL_LEFTSHIFT
||
73 expr
->op
== SPECIAL_RIGHTSHIFT
) {
74 if (type_positive_bits(left
) < 31)
78 right
= get_type(expr
->right
);
82 if (type_is_fp(left
)) {
83 if (type_is_fp(right
)) {
84 if (type_bits(left
) > type_bits(right
))
91 if (type_is_fp(right
)) {
92 if (type_is_fp(left
)) {
93 if (type_bits(right
) > type_bits(left
))
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
)
106 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
109 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
112 if (type_positive_bits(left
) > type_positive_bits(right
))
117 static struct symbol
*get_type_symbol(struct expression
*expr
)
119 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
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
) {
131 sub
= get_real_base_type(tmp
);
132 sub
= get_member_symbol(sub
->symbol_list
, member
);
137 if (tmp
->ident
== member
)
139 } END_FOR_EACH_PTR(tmp
);
144 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
146 struct ident
*member
;
149 if (!expr
|| expr
->type
!= EXPR_DEREF
)
152 member
= expr
->member
;
153 sym
= get_type(expr
->deref
);
155 // sm_msg("could not find struct type");
158 if (sym
->type
== SYM_PTR
)
159 sym
= get_real_base_type(sym
);
160 sym
= get_member_symbol(sym
->symbol_list
, member
);
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
;
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
)
178 return get_type(expr1
);
180 return get_type(expr2
);
183 static struct symbol
*get_return_type(struct expression
*expr
)
187 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
188 return handle__builtin_choose_expr(expr
);
190 tmp
= get_type(expr
->fn
);
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
)
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
)
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
);
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
224 if (type_positive_bits(one
) > type_positive_bits(two
))
229 struct symbol
*get_pointer_type(struct expression
*expr
)
233 sym
= get_type(expr
);
236 if (sym
->type
== SYM_NODE
) {
237 sym
= get_real_base_type(sym
);
241 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
243 return get_real_base_type(sym
);
246 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
251 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
253 base
= get_type(expr
);
256 sym
->ctype
.base_type
= base
;
260 static struct symbol
*get_type_helper(struct expression
*expr
)
264 expr
= strip_parens(expr
);
271 switch (expr
->type
) {
276 ret
= get_type_symbol(expr
);
279 ret
= get_symbol_from_deref(expr
);
284 ret
= fake_pointer_sym(expr
);
285 else if (expr
->op
== '*')
286 ret
= get_pointer_type(expr
->unop
);
288 ret
= get_type(expr
->unop
);
290 case EXPR_ASSIGNMENT
:
291 ret
= get_type(expr
->left
);
294 case EXPR_FORCE_CAST
:
295 case EXPR_IMPLIED_CAST
:
296 ret
= get_real_base_type(expr
->cast_type
);
300 ret
= get_binop_type(expr
);
303 ret
= get_return_type(expr
);
306 ret
= get_expr_stmt_type(expr
->statement
);
308 case EXPR_CONDITIONAL
:
310 ret
= get_select_type(expr
);
325 if (ret
&& ret
->type
== SYM_TYPEOF
)
326 ret
= get_type(ret
->initializer
);
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
);
346 if (expr
->type
== EXPR_COMPARE
)
352 struct symbol
*get_type(struct expression
*expr
)
354 return get_type_helper(expr
);
357 struct symbol
*get_comparison_type(struct expression
*expr
)
360 * Eventually we will probably have to figure out how to make get_type()
361 * return &int_ctype so let's create a helper function to transition to.
363 return get_type_helper(expr
);
366 struct symbol
*get_final_type(struct expression
*expr
)
370 ret
= get_final_type_helper(expr
);
373 return get_type_helper(expr
);
376 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
378 struct symbol
*ret
= &int_ctype
;
380 if (type_positive_bits(left
) > type_positive_bits(ret
))
382 if (type_positive_bits(right
) > type_positive_bits(ret
))
385 if (type_is_ptr(left
))
387 if (type_is_ptr(right
))
393 int type_signed(struct symbol
*base_type
)
397 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
402 int expr_unsigned(struct expression
*expr
)
406 sym
= get_type(expr
);
409 if (type_unsigned(sym
))
414 int expr_signed(struct expression
*expr
)
418 sym
= get_type(expr
);
421 if (type_signed(sym
))
426 int returns_unsigned(struct symbol
*sym
)
430 sym
= get_base_type(sym
);
431 if (!sym
|| sym
->type
!= SYM_FN
)
433 sym
= get_base_type(sym
);
434 return type_unsigned(sym
);
437 int is_pointer(struct expression
*expr
)
439 return type_is_ptr(get_type(expr
));
442 int returns_pointer(struct symbol
*sym
)
446 sym
= get_base_type(sym
);
447 if (!sym
|| sym
->type
!= SYM_FN
)
449 sym
= get_base_type(sym
);
450 if (sym
&& sym
->type
== SYM_PTR
)
455 static sval_t
fp_max(struct symbol
*type
)
457 sval_t ret
= { .type
= type
};
459 if (type
== &float_ctype
)
460 ret
.fvalue
= FLT_MAX
;
461 else if (type
== &double_ctype
)
462 ret
.dvalue
= DBL_MAX
;
464 ret
.ldvalue
= LDBL_MAX
;
469 sval_t
sval_type_max(struct symbol
*base_type
)
473 if (type_is_fp(base_type
))
474 return fp_max(base_type
);
476 if (!base_type
|| !type_bits(base_type
))
477 base_type
= &llong_ctype
;
478 ret
.type
= base_type
;
480 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
484 static sval_t
fp_min(struct symbol
*type
)
486 sval_t ret
= { .type
= type
};
488 if (type
== &float_ctype
)
489 ret
.fvalue
= -FLT_MAX
;
490 else if (type
== &double_ctype
)
491 ret
.dvalue
= -DBL_MAX
;
493 ret
.ldvalue
= -LDBL_MAX
;
498 sval_t
sval_type_min(struct symbol
*base_type
)
502 if (type_is_fp(base_type
))
503 return fp_min(base_type
);
505 if (!base_type
|| !type_bits(base_type
))
506 base_type
= &llong_ctype
;
507 ret
.type
= base_type
;
509 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
514 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
519 int nr_bits(struct expression
*expr
)
523 type
= get_type(expr
);
526 return type_bits(type
);
529 int is_void_pointer(struct expression
*expr
)
533 type
= get_type(expr
);
534 if (!type
|| type
->type
!= SYM_PTR
)
536 type
= get_real_base_type(type
);
537 if (type
== &void_ctype
)
542 int is_char_pointer(struct expression
*expr
)
546 type
= get_type(expr
);
547 if (!type
|| type
->type
!= SYM_PTR
)
549 type
= get_real_base_type(type
);
550 if (type
== &char_ctype
)
555 int is_string(struct expression
*expr
)
557 expr
= strip_expr(expr
);
558 if (!expr
|| expr
->type
!= EXPR_STRING
)
565 bool is_struct_ptr(struct symbol
*type
)
567 if (!type
|| type
->type
!= SYM_PTR
)
569 type
= get_real_base_type(type
);
570 if (!type
|| type
->type
!= SYM_STRUCT
)
575 int is_static(struct expression
*expr
)
581 name
= expr_to_str_sym(expr
, &sym
);
585 if (sym
->ctype
.modifiers
& MOD_STATIC
)
592 static struct expression
*get_symbol_expr(struct expression
*expr
)
596 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
597 expr
= strip_expr(expr
->deref
);
601 bool is_local_variable(struct expression
*expr
)
605 expr
= get_symbol_expr(expr
);
606 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
609 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
614 int types_equiv(struct symbol
*one
, struct symbol
*two
)
620 if (one
->type
!= two
->type
)
622 if (one
->type
== SYM_PTR
)
623 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
624 if (type_positive_bits(one
) != type_positive_bits(two
))
629 bool type_fits(struct symbol
*type
, struct symbol
*test
)
637 if (type_bits(test
) > type_bits(type
))
639 if (type_signed(test
) && !type_signed(type
))
641 if (type_positive_bits(test
) > type_positive_bits(type
))
648 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
651 const char *global_static(void)
653 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
659 struct symbol
*cur_func_return_type(void)
663 sym
= get_real_base_type(cur_func_sym
);
664 if (!sym
|| sym
->type
!= SYM_FN
)
666 sym
= get_real_base_type(sym
);
670 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
672 struct symbol
*fn_type
;
674 struct symbol
*arg_type
;
677 fn_type
= get_type(fn
);
680 if (fn_type
->type
== SYM_PTR
)
681 fn_type
= get_real_base_type(fn_type
);
682 if (fn_type
->type
!= SYM_FN
)
686 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
687 arg_type
= get_real_base_type(tmp
);
692 } END_FOR_EACH_PTR(tmp
);
697 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
699 struct symbol
*tmp
, *sub
;
702 if (strncmp(name
, ".", 1) == 0)
704 else if (strncmp(name
, "->", 2) == 0)
707 FOR_EACH_PTR(symbol_list
, tmp
) {
709 sub
= get_real_base_type(tmp
);
710 sub
= get_member_from_string(sub
->symbol_list
, name
);
716 if (strcmp(tmp
->ident
->name
, name
) == 0)
719 chunk_len
= tmp
->ident
->len
;
720 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
721 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
722 sub
= get_real_base_type(tmp
);
723 if (sub
->type
== SYM_PTR
)
724 sub
= get_real_base_type(sub
);
725 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
728 } END_FOR_EACH_PTR(tmp
);
733 static struct symbol
*get_type_from_container_of_key(struct expression
*expr
, const char *key
)
737 expr
= map_container_of_to_simpler_expr_key(expr
, key
, &new_key
);
740 return get_member_type_from_key(expr
, new_key
);
743 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
749 if (strcmp(key
, "$") == 0)
750 return get_type(expr
);
752 if (strcmp(key
, "*$") == 0) {
753 sym
= get_type(expr
);
756 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
758 return get_real_base_type(sym
);
761 if (strstr(key
, "<~$"))
762 return get_type_from_container_of_key(expr
, key
);
764 sym
= get_type(expr
);
767 if (sym
->type
== SYM_PTR
)
768 sym
= get_real_base_type(sym
);
770 while (*key
== '*') {
779 sym
= get_member_from_string(sym
->symbol_list
, key
);
782 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
783 sym
= get_real_base_type(sym
);
784 for (i
= 0; i
< star
; i
++) {
785 if (!sym
|| sym
->type
!= SYM_PTR
)
787 sym
= get_real_base_type(sym
);
792 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
798 if (strcmp(key
, "$") == 0)
799 return get_arg_type(fn
, param
);
800 if (strcmp(key
, "*$") == 0) {
801 type
= get_arg_type(fn
, param
);
802 if (!type
|| type
->type
!= SYM_PTR
)
804 return get_real_base_type(type
);
806 return get_member_type_from_key(arg
, key
);
809 int is_struct(struct expression
*expr
)
813 type
= get_type(expr
);
814 if (type
&& type
->type
== SYM_STRUCT
)
823 {&bool_ctype
, "bool"},
824 {&void_ctype
, "void"},
825 {&type_ctype
, "type"},
826 {&char_ctype
, "char"},
827 {&schar_ctype
, "schar"},
828 {&uchar_ctype
, "uchar"},
829 {&short_ctype
, "short"},
830 {&sshort_ctype
, "sshort"},
831 {&ushort_ctype
, "ushort"},
833 {&sint_ctype
, "sint"},
834 {&uint_ctype
, "uint"},
835 {&long_ctype
, "long"},
836 {&slong_ctype
, "slong"},
837 {&ulong_ctype
, "ulong"},
838 {&llong_ctype
, "llong"},
839 {&sllong_ctype
, "sllong"},
840 {&ullong_ctype
, "ullong"},
841 {&int128_ctype
, "lllong"},
842 {&sint128_ctype
, "slllong"},
843 {&uint128_ctype
, "ulllong"},
844 {&float_ctype
, "float"},
845 {&double_ctype
, "double"},
846 {&ldouble_ctype
, "ldouble"},
847 {&string_ctype
, "string"},
849 {&lazy_ptr_ctype
, "lazy_ptr"},
850 {&incomplete_ctype
, "incomplete"},
851 {&label_ctype
, "label"},
853 {&null_ctype
, "null"},
856 static const char *base_type_str(struct symbol
*sym
)
860 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
861 if (sym
== base_types
[i
].sym
)
862 return base_types
[i
].name
;
867 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
872 return snprintf(buf
, size
, "<null type>");
874 if (type
->type
== SYM_BASETYPE
) {
875 return snprintf(buf
, size
, "%s", base_type_str(type
));
876 } else if (type
->type
== SYM_PTR
) {
877 type
= get_real_base_type(type
);
878 n
= type_str_helper(buf
, size
, type
);
881 return n
+ snprintf(buf
+ n
, size
- n
, "*");
882 } else if (type
->type
== SYM_ARRAY
) {
883 type
= get_real_base_type(type
);
884 n
= type_str_helper(buf
, size
, type
);
887 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
888 } else if (type
->type
== SYM_STRUCT
) {
889 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
890 } else if (type
->type
== SYM_UNION
) {
892 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
894 return snprintf(buf
, size
, "anonymous union");
895 } else if (type
->type
== SYM_FN
) {
896 struct symbol
*arg
, *return_type
, *arg_type
;
899 return_type
= get_real_base_type(type
);
900 n
= type_str_helper(buf
, size
, return_type
);
903 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
908 FOR_EACH_PTR(type
->arguments
, arg
) {
910 n
+= snprintf(buf
+ n
, size
- n
, ", ");
913 arg_type
= get_real_base_type(arg
);
914 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
917 } END_FOR_EACH_PTR(arg
);
919 return n
+ snprintf(buf
+ n
, size
- n
, ")");
920 } else if (type
->type
== SYM_NODE
) {
921 n
= snprintf(buf
, size
, "node {");
924 type
= get_real_base_type(type
);
925 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
928 return n
+ snprintf(buf
+ n
, size
- n
, "}");
929 } else if (type
->type
== SYM_ENUM
) {
930 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
932 return snprintf(buf
, size
, "<type %d>", type
->type
);
936 char *type_to_str(struct symbol
*type
)
938 static char buf
[256];
941 type_str_helper(buf
, sizeof(buf
), type
);
942 return alloc_sname(buf
);