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.
25 #include "smatch_slist.h"
27 struct symbol
*get_real_base_type(struct symbol
*sym
)
33 if (sym
->type
== SYM_BASETYPE
)
35 ret
= get_base_type(sym
);
38 if (ret
->type
== SYM_RESTRICT
|| ret
->type
== SYM_NODE
)
39 return get_real_base_type(ret
);
43 int type_bytes(struct symbol
*type
)
47 if (type
&& type
->type
== SYM_ARRAY
)
48 return array_bytes(type
);
50 bits
= type_bits(type
);
53 return bits_to_bytes(bits
);
56 int array_bytes(struct symbol
*type
)
58 if (!type
|| type
->type
!= SYM_ARRAY
)
60 if (!type
->array_size
)
62 return bits_to_bytes(type
->bit_size
);
65 bool is_long(struct symbol
*type
)
67 if (type
== &ulong_ctype
|| type
== &long_ctype
)
72 bool is_int(struct symbol
*type
)
74 if (type
== &uint_ctype
|| type
== &int_ctype
)
79 static struct symbol
*get_binop_type(struct expression
*expr
)
81 struct symbol
*left
, *right
;
83 left
= get_type(expr
->left
);
87 if (expr
->op
== SPECIAL_LEFTSHIFT
||
88 expr
->op
== SPECIAL_RIGHTSHIFT
) {
89 if (type_positive_bits(left
) < 31)
93 right
= get_type(expr
->right
);
97 if (type_is_fp(left
)) {
98 if (type_is_fp(right
)) {
99 if (type_bits(left
) > type_bits(right
))
106 if (type_is_fp(right
)) {
107 if (type_is_fp(left
)) {
108 if (type_bits(right
) > type_bits(left
))
115 if (expr
->op
== '-' &&
116 (is_ptr_type(left
) && is_ptr_type(right
)))
117 return ssize_t_ctype
;
119 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
121 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
124 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
127 if (type_positive_bits(left
) > type_positive_bits(right
))
129 if (is_long(left
) && is_int(right
))
131 if (is_long(right
) && is_int(left
))
136 static struct symbol
*get_type_symbol(struct expression
*expr
)
140 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
143 type
= get_real_base_type(expr
->symbol
);
144 if (type
== &autotype_ctype
)
145 return get_type(expr
->symbol
->initializer
);
149 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
151 struct symbol
*tmp
, *sub
;
153 FOR_EACH_PTR(symbol_list
, tmp
) {
155 sub
= get_real_base_type(tmp
);
156 sub
= get_member_symbol(sub
->symbol_list
, member
);
161 if (tmp
->ident
== member
)
163 } END_FOR_EACH_PTR(tmp
);
168 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
170 struct ident
*member
;
173 if (!expr
|| expr
->type
!= EXPR_DEREF
)
176 member
= expr
->member
;
177 sym
= get_type(expr
->deref
);
179 // sm_msg("could not find struct type");
182 if (sym
->type
== SYM_PTR
)
183 sym
= get_real_base_type(sym
);
184 sym
= get_member_symbol(sym
->symbol_list
, member
);
187 return get_real_base_type(sym
);
190 static struct symbol
*handle__builtin_choose_expr(struct expression
*expr
)
192 struct expression
*const_expr
, *expr1
, *expr2
;
195 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
196 expr1
= get_argument_from_call_expr(expr
->args
, 1);
197 expr2
= get_argument_from_call_expr(expr
->args
, 2);
199 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
202 return get_type(expr1
);
204 return get_type(expr2
);
207 static struct symbol
*get_return_type(struct expression
*expr
)
211 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
212 return handle__builtin_choose_expr(expr
);
214 tmp
= get_type(expr
->fn
);
217 /* this is to handle __builtin_constant_p() */
218 if (tmp
->type
!= SYM_FN
)
219 tmp
= get_base_type(tmp
);
220 return get_real_base_type(tmp
);
223 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
225 if (stmt
->type
!= STMT_COMPOUND
)
227 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
228 if (stmt
->type
== STMT_LABEL
)
229 stmt
= stmt
->label_statement
;
230 if (stmt
->type
!= STMT_EXPRESSION
)
232 return get_type(stmt
->expression
);
235 static struct symbol
*get_select_type(struct expression
*expr
)
237 struct symbol
*one
, *two
;
239 one
= get_type(expr
->cond_true
);
240 two
= get_type(expr
->cond_false
);
244 * This is a hack. If the types are not equiv then we
245 * really don't know the type. But I think guessing is
248 if (type_positive_bits(one
) > type_positive_bits(two
))
253 struct symbol
*get_pointer_type(struct expression
*expr
)
257 sym
= get_type(expr
);
260 if (sym
->type
== SYM_NODE
) {
261 sym
= get_real_base_type(sym
);
265 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
267 return get_real_base_type(sym
);
270 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
275 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
277 base
= get_type(expr
);
280 sym
->ctype
.base_type
= base
;
284 static struct symbol
*get_type_helper(struct expression
*expr
)
288 expr
= strip_parens(expr
);
295 switch (expr
->type
) {
300 ret
= get_type_symbol(expr
);
303 ret
= get_symbol_from_deref(expr
);
308 ret
= fake_pointer_sym(expr
);
309 else if (expr
->op
== '*')
310 ret
= get_pointer_type(expr
->unop
);
312 ret
= get_type(expr
->unop
);
314 case EXPR_ASSIGNMENT
:
315 ret
= get_type(expr
->left
);
318 case EXPR_FORCE_CAST
:
319 case EXPR_IMPLIED_CAST
:
320 ret
= get_real_base_type(expr
->cast_type
);
324 ret
= get_binop_type(expr
);
327 ret
= get_return_type(expr
);
330 ret
= get_expr_stmt_type(expr
->statement
);
332 case EXPR_CONDITIONAL
:
334 ret
= get_select_type(expr
);
346 struct expression
*tmp
;
348 tmp
= strip_Generic(expr
);
351 return get_type_helper(tmp
);
354 return get_type_helper(expr
->right
);
359 if (ret
&& ret
->type
== SYM_TYPEOF
)
360 ret
= get_type(ret
->initializer
);
366 static struct symbol
*get_final_type_helper(struct expression
*expr
)
369 * The problem is that I wrote a bunch of Smatch to think that
370 * you could do get_type() on an expression and it would give
371 * you what the comparison was type promoted to. This is wrong
372 * but fixing it is a big of work... Hence this horrible hack.
376 expr
= strip_parens(expr
);
380 if (expr
->type
== EXPR_COMPARE
)
382 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
388 struct symbol
*get_type(struct expression
*expr
)
390 return get_type_helper(expr
);
393 struct symbol
*get_comparison_type(struct expression
*expr
)
396 * Eventually we will probably have to figure out how to make get_type()
397 * return &int_ctype so let's create a helper function to transition to.
399 return get_type_helper(expr
);
402 struct symbol
*get_final_type(struct expression
*expr
)
406 ret
= get_final_type_helper(expr
);
409 return get_type_helper(expr
);
412 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
414 struct symbol
*ret
= &int_ctype
;
416 if (type_positive_bits(left
) > type_positive_bits(ret
))
418 if (type_positive_bits(right
) > type_positive_bits(ret
))
421 if (type_is_ptr(left
))
423 if (type_is_ptr(right
))
429 int type_signed(struct symbol
*base_type
)
433 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
438 int expr_unsigned(struct expression
*expr
)
442 sym
= get_type(expr
);
445 if (type_unsigned(sym
))
450 int expr_signed(struct expression
*expr
)
454 sym
= get_type(expr
);
457 if (type_signed(sym
))
462 int returns_unsigned(struct symbol
*sym
)
466 sym
= get_base_type(sym
);
467 if (!sym
|| sym
->type
!= SYM_FN
)
469 sym
= get_base_type(sym
);
470 return type_unsigned(sym
);
473 int is_pointer(struct expression
*expr
)
475 return type_is_ptr(get_final_type(expr
));
478 bool is_void_ptr(struct symbol
*type
)
482 if (type
->type
!= SYM_PTR
)
484 type
= get_real_base_type(type
);
486 return types_equiv(type
, &void_ctype
);
489 int returns_pointer(struct symbol
*sym
)
493 sym
= get_base_type(sym
);
494 if (!sym
|| sym
->type
!= SYM_FN
)
496 sym
= get_base_type(sym
);
497 if (sym
&& sym
->type
== SYM_PTR
)
502 static sval_t
fp_max(struct symbol
*type
)
504 sval_t ret
= { .type
= type
};
506 if (type
== &float_ctype
)
507 ret
.fvalue
= FLT_MAX
;
508 else if (type
== &double_ctype
)
509 ret
.dvalue
= DBL_MAX
;
511 ret
.ldvalue
= LDBL_MAX
;
516 sval_t
sval_type_max(struct symbol
*base_type
)
520 if (type_is_fp(base_type
))
521 return fp_max(base_type
);
523 if (!base_type
|| !type_bits(base_type
))
524 base_type
= &llong_ctype
;
525 ret
.type
= base_type
;
527 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
531 static sval_t
fp_min(struct symbol
*type
)
533 sval_t ret
= { .type
= type
};
535 if (type
== &float_ctype
)
536 ret
.fvalue
= -FLT_MAX
;
537 else if (type
== &double_ctype
)
538 ret
.dvalue
= -DBL_MAX
;
540 ret
.ldvalue
= -LDBL_MAX
;
545 sval_t
sval_type_min(struct symbol
*base_type
)
549 if (type_is_fp(base_type
))
550 return fp_min(base_type
);
552 if (!base_type
|| !type_bits(base_type
))
553 base_type
= &llong_ctype
;
554 ret
.type
= base_type
;
556 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
561 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
566 int nr_bits(struct expression
*expr
)
570 type
= get_type(expr
);
573 return type_bits(type
);
576 int is_void_pointer(struct expression
*expr
)
580 type
= get_type(expr
);
581 if (!type
|| type
->type
!= SYM_PTR
)
583 type
= get_real_base_type(type
);
584 if (type
== &void_ctype
)
589 int is_char_pointer(struct expression
*expr
)
593 type
= get_type(expr
);
594 if (!type
|| type
->type
!= SYM_PTR
)
596 type
= get_real_base_type(type
);
597 if (type
== &char_ctype
)
602 int is_string(struct expression
*expr
)
604 expr
= strip_expr(expr
);
605 if (!expr
|| expr
->type
!= EXPR_STRING
)
612 bool is_struct_ptr(struct symbol
*type
)
614 if (!type
|| type
->type
!= SYM_PTR
)
616 type
= get_real_base_type(type
);
617 if (!type
|| type
->type
!= SYM_STRUCT
)
622 int is_static(struct expression
*expr
)
628 name
= expr_to_str_sym(expr
, &sym
);
632 if (sym
->ctype
.modifiers
& MOD_STATIC
)
639 static struct expression
*get_symbol_expr(struct expression
*expr
)
643 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
644 expr
= strip_expr(expr
->deref
);
648 bool is_local_variable(struct expression
*expr
)
652 expr
= get_symbol_expr(expr
);
653 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
656 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
661 int types_equiv(struct symbol
*one
, struct symbol
*two
)
667 if (one
->type
!= two
->type
)
669 if (one
->type
== SYM_PTR
)
670 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
671 if (type_positive_bits(one
) != type_positive_bits(two
))
676 bool type_fits(struct symbol
*type
, struct symbol
*test
)
684 if (type_bits(test
) > type_bits(type
))
686 if (type_signed(test
) && !type_signed(type
))
688 if (type_positive_bits(test
) > type_positive_bits(type
))
695 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
698 const char *global_static(void)
700 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
706 struct symbol
*cur_func_return_type(void)
710 sym
= get_real_base_type(cur_func_sym
);
711 if (!sym
|| sym
->type
!= SYM_FN
)
713 sym
= get_real_base_type(sym
);
717 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
719 struct symbol
*fn_type
;
721 struct symbol
*arg_type
;
724 fn_type
= get_type(fn
);
727 if (fn_type
->type
== SYM_PTR
)
728 fn_type
= get_real_base_type(fn_type
);
729 if (fn_type
->type
!= SYM_FN
)
733 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
734 arg_type
= get_real_base_type(tmp
);
739 } END_FOR_EACH_PTR(tmp
);
744 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
746 struct symbol
*tmp
, *sub
;
749 if (strncmp(name
, ".", 1) == 0)
751 else if (strncmp(name
, "->", 2) == 0)
754 FOR_EACH_PTR(symbol_list
, tmp
) {
756 sub
= get_real_base_type(tmp
);
757 sub
= get_member_from_string(sub
->symbol_list
, name
);
763 if (strcmp(tmp
->ident
->name
, name
) == 0)
766 chunk_len
= tmp
->ident
->len
;
767 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
768 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
769 sub
= get_real_base_type(tmp
);
770 if (sub
->type
== SYM_PTR
)
771 sub
= get_real_base_type(sub
);
772 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
775 } END_FOR_EACH_PTR(tmp
);
780 static struct symbol
*get_type_from_container_of_key(struct expression
*expr
, const char *key
)
784 expr
= map_container_of_to_simpler_expr_key(expr
, key
, &new_key
);
787 return get_member_type_from_key(expr
, new_key
);
790 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
796 if (strcmp(key
, "$") == 0)
797 return get_type(expr
);
799 if (strcmp(key
, "*$") == 0) {
800 sym
= get_type(expr
);
803 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
805 return get_real_base_type(sym
);
808 if (strstr(key
, "<~$"))
809 return get_type_from_container_of_key(expr
, key
);
811 sym
= get_type(expr
);
814 if (sym
->type
== SYM_PTR
)
815 sym
= get_real_base_type(sym
);
817 while (*key
== '*') {
826 sym
= get_member_from_string(sym
->symbol_list
, key
);
829 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
830 sym
= get_real_base_type(sym
);
831 for (i
= 0; i
< star
; i
++) {
832 if (!sym
|| sym
->type
!= SYM_PTR
)
834 sym
= get_real_base_type(sym
);
839 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
845 if (strcmp(key
, "$") == 0)
846 return get_arg_type(fn
, param
);
847 if (strcmp(key
, "*$") == 0) {
848 type
= get_arg_type(fn
, param
);
849 if (!type
|| type
->type
!= SYM_PTR
)
851 return get_real_base_type(type
);
853 return get_member_type_from_key(arg
, key
);
856 int is_struct(struct expression
*expr
)
860 type
= get_type(expr
);
861 if (type
&& type
->type
== SYM_STRUCT
)
870 {&bool_ctype
, "bool"},
871 {&void_ctype
, "void"},
872 {&type_ctype
, "type"},
873 {&char_ctype
, "char"},
874 {&schar_ctype
, "schar"},
875 {&uchar_ctype
, "uchar"},
876 {&short_ctype
, "short"},
877 {&sshort_ctype
, "sshort"},
878 {&ushort_ctype
, "ushort"},
880 {&sint_ctype
, "sint"},
881 {&uint_ctype
, "uint"},
882 {&long_ctype
, "long"},
883 {&slong_ctype
, "slong"},
884 {&ulong_ctype
, "ulong"},
885 {&llong_ctype
, "llong"},
886 {&sllong_ctype
, "sllong"},
887 {&ullong_ctype
, "ullong"},
888 {&int128_ctype
, "lllong"},
889 {&sint128_ctype
, "slllong"},
890 {&uint128_ctype
, "ulllong"},
891 {&float_ctype
, "float"},
892 {&double_ctype
, "double"},
893 {&ldouble_ctype
, "ldouble"},
894 {&string_ctype
, "string"},
896 {&lazy_ptr_ctype
, "lazy_ptr"},
897 {&incomplete_ctype
, "incomplete"},
898 {&label_ctype
, "label"},
900 {&null_ctype
, "null"},
903 static const char *base_type_str(struct symbol
*sym
)
907 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
908 if (sym
== base_types
[i
].sym
)
909 return base_types
[i
].name
;
914 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
919 return snprintf(buf
, size
, "<null type>");
921 if (type
->type
== SYM_BASETYPE
) {
922 return snprintf(buf
, size
, "%s", base_type_str(type
));
923 } else if (type
->type
== SYM_PTR
) {
924 type
= get_real_base_type(type
);
925 n
= type_str_helper(buf
, size
, type
);
928 return n
+ snprintf(buf
+ n
, size
- n
, "*");
929 } else if (type
->type
== SYM_ARRAY
) {
930 type
= get_real_base_type(type
);
931 n
= type_str_helper(buf
, size
, type
);
934 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
935 } else if (type
->type
== SYM_STRUCT
) {
936 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
937 } else if (type
->type
== SYM_UNION
) {
939 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
941 return snprintf(buf
, size
, "anonymous union");
942 } else if (type
->type
== SYM_FN
) {
943 struct symbol
*arg
, *return_type
, *arg_type
;
946 return_type
= get_real_base_type(type
);
947 n
= type_str_helper(buf
, size
, return_type
);
950 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
955 FOR_EACH_PTR(type
->arguments
, arg
) {
957 n
+= snprintf(buf
+ n
, size
- n
, ", ");
960 arg_type
= get_real_base_type(arg
);
961 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
964 } END_FOR_EACH_PTR(arg
);
966 return n
+ snprintf(buf
+ n
, size
- n
, ")");
967 } else if (type
->type
== SYM_NODE
) {
968 n
= snprintf(buf
, size
, "node {");
971 type
= get_real_base_type(type
);
972 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
975 return n
+ snprintf(buf
+ n
, size
- n
, "}");
976 } else if (type
->type
== SYM_ENUM
) {
977 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
979 return snprintf(buf
, size
, "<type %d>", type
->type
);
983 char *type_to_str(struct symbol
*type
)
985 static char buf
[256];
988 type_str_helper(buf
, sizeof(buf
), type
);
989 return alloc_sname(buf
);