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 return get_type_helper(strip_Generic(expr
));
348 return get_type_helper(expr
->right
);
353 if (ret
&& ret
->type
== SYM_TYPEOF
)
354 ret
= get_type(ret
->initializer
);
360 static struct symbol
*get_final_type_helper(struct expression
*expr
)
363 * The problem is that I wrote a bunch of Smatch to think that
364 * you could do get_type() on an expression and it would give
365 * you what the comparison was type promoted to. This is wrong
366 * but fixing it is a big of work... Hence this horrible hack.
370 expr
= strip_parens(expr
);
374 if (expr
->type
== EXPR_COMPARE
)
376 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
382 struct symbol
*get_type(struct expression
*expr
)
384 return get_type_helper(expr
);
387 struct symbol
*get_comparison_type(struct expression
*expr
)
390 * Eventually we will probably have to figure out how to make get_type()
391 * return &int_ctype so let's create a helper function to transition to.
393 return get_type_helper(expr
);
396 struct symbol
*get_final_type(struct expression
*expr
)
400 ret
= get_final_type_helper(expr
);
403 return get_type_helper(expr
);
406 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
408 struct symbol
*ret
= &int_ctype
;
410 if (type_positive_bits(left
) > type_positive_bits(ret
))
412 if (type_positive_bits(right
) > type_positive_bits(ret
))
415 if (type_is_ptr(left
))
417 if (type_is_ptr(right
))
423 int type_signed(struct symbol
*base_type
)
427 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
432 int expr_unsigned(struct expression
*expr
)
436 sym
= get_type(expr
);
439 if (type_unsigned(sym
))
444 int expr_signed(struct expression
*expr
)
448 sym
= get_type(expr
);
451 if (type_signed(sym
))
456 int returns_unsigned(struct symbol
*sym
)
460 sym
= get_base_type(sym
);
461 if (!sym
|| sym
->type
!= SYM_FN
)
463 sym
= get_base_type(sym
);
464 return type_unsigned(sym
);
467 int is_pointer(struct expression
*expr
)
469 return type_is_ptr(get_final_type(expr
));
472 bool is_void_ptr(struct symbol
*type
)
476 if (type
->type
!= SYM_PTR
)
478 type
= get_real_base_type(type
);
480 return types_equiv(type
, &void_ctype
);
483 int returns_pointer(struct symbol
*sym
)
487 sym
= get_base_type(sym
);
488 if (!sym
|| sym
->type
!= SYM_FN
)
490 sym
= get_base_type(sym
);
491 if (sym
&& sym
->type
== SYM_PTR
)
496 static sval_t
fp_max(struct symbol
*type
)
498 sval_t ret
= { .type
= type
};
500 if (type
== &float_ctype
)
501 ret
.fvalue
= FLT_MAX
;
502 else if (type
== &double_ctype
)
503 ret
.dvalue
= DBL_MAX
;
505 ret
.ldvalue
= LDBL_MAX
;
510 sval_t
sval_type_max(struct symbol
*base_type
)
514 if (type_is_fp(base_type
))
515 return fp_max(base_type
);
517 if (!base_type
|| !type_bits(base_type
))
518 base_type
= &llong_ctype
;
519 ret
.type
= base_type
;
521 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
525 static sval_t
fp_min(struct symbol
*type
)
527 sval_t ret
= { .type
= type
};
529 if (type
== &float_ctype
)
530 ret
.fvalue
= -FLT_MAX
;
531 else if (type
== &double_ctype
)
532 ret
.dvalue
= -DBL_MAX
;
534 ret
.ldvalue
= -LDBL_MAX
;
539 sval_t
sval_type_min(struct symbol
*base_type
)
543 if (type_is_fp(base_type
))
544 return fp_min(base_type
);
546 if (!base_type
|| !type_bits(base_type
))
547 base_type
= &llong_ctype
;
548 ret
.type
= base_type
;
550 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
555 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
560 int nr_bits(struct expression
*expr
)
564 type
= get_type(expr
);
567 return type_bits(type
);
570 int is_void_pointer(struct expression
*expr
)
574 type
= get_type(expr
);
575 if (!type
|| type
->type
!= SYM_PTR
)
577 type
= get_real_base_type(type
);
578 if (type
== &void_ctype
)
583 int is_char_pointer(struct expression
*expr
)
587 type
= get_type(expr
);
588 if (!type
|| type
->type
!= SYM_PTR
)
590 type
= get_real_base_type(type
);
591 if (type
== &char_ctype
)
596 int is_string(struct expression
*expr
)
598 expr
= strip_expr(expr
);
599 if (!expr
|| expr
->type
!= EXPR_STRING
)
606 bool is_struct_ptr(struct symbol
*type
)
608 if (!type
|| type
->type
!= SYM_PTR
)
610 type
= get_real_base_type(type
);
611 if (!type
|| type
->type
!= SYM_STRUCT
)
616 int is_static(struct expression
*expr
)
622 name
= expr_to_str_sym(expr
, &sym
);
626 if (sym
->ctype
.modifiers
& MOD_STATIC
)
633 static struct expression
*get_symbol_expr(struct expression
*expr
)
637 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
638 expr
= strip_expr(expr
->deref
);
642 bool is_local_variable(struct expression
*expr
)
646 expr
= get_symbol_expr(expr
);
647 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
650 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
655 int types_equiv(struct symbol
*one
, struct symbol
*two
)
661 if (one
->type
!= two
->type
)
663 if (one
->type
== SYM_PTR
)
664 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
665 if (type_positive_bits(one
) != type_positive_bits(two
))
670 bool type_fits(struct symbol
*type
, struct symbol
*test
)
678 if (type_bits(test
) > type_bits(type
))
680 if (type_signed(test
) && !type_signed(type
))
682 if (type_positive_bits(test
) > type_positive_bits(type
))
689 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
692 const char *global_static(void)
694 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
700 struct symbol
*cur_func_return_type(void)
704 sym
= get_real_base_type(cur_func_sym
);
705 if (!sym
|| sym
->type
!= SYM_FN
)
707 sym
= get_real_base_type(sym
);
711 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
713 struct symbol
*fn_type
;
715 struct symbol
*arg_type
;
718 fn_type
= get_type(fn
);
721 if (fn_type
->type
== SYM_PTR
)
722 fn_type
= get_real_base_type(fn_type
);
723 if (fn_type
->type
!= SYM_FN
)
727 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
728 arg_type
= get_real_base_type(tmp
);
733 } END_FOR_EACH_PTR(tmp
);
738 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
740 struct symbol
*tmp
, *sub
;
743 if (strncmp(name
, ".", 1) == 0)
745 else if (strncmp(name
, "->", 2) == 0)
748 FOR_EACH_PTR(symbol_list
, tmp
) {
750 sub
= get_real_base_type(tmp
);
751 sub
= get_member_from_string(sub
->symbol_list
, name
);
757 if (strcmp(tmp
->ident
->name
, name
) == 0)
760 chunk_len
= tmp
->ident
->len
;
761 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
762 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
763 sub
= get_real_base_type(tmp
);
764 if (sub
->type
== SYM_PTR
)
765 sub
= get_real_base_type(sub
);
766 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
769 } END_FOR_EACH_PTR(tmp
);
774 static struct symbol
*get_type_from_container_of_key(struct expression
*expr
, const char *key
)
778 expr
= map_container_of_to_simpler_expr_key(expr
, key
, &new_key
);
781 return get_member_type_from_key(expr
, new_key
);
784 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
790 if (strcmp(key
, "$") == 0)
791 return get_type(expr
);
793 if (strcmp(key
, "*$") == 0) {
794 sym
= get_type(expr
);
797 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
799 return get_real_base_type(sym
);
802 if (strstr(key
, "<~$"))
803 return get_type_from_container_of_key(expr
, key
);
805 sym
= get_type(expr
);
808 if (sym
->type
== SYM_PTR
)
809 sym
= get_real_base_type(sym
);
811 while (*key
== '*') {
820 sym
= get_member_from_string(sym
->symbol_list
, key
);
823 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
824 sym
= get_real_base_type(sym
);
825 for (i
= 0; i
< star
; i
++) {
826 if (!sym
|| sym
->type
!= SYM_PTR
)
828 sym
= get_real_base_type(sym
);
833 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
839 if (strcmp(key
, "$") == 0)
840 return get_arg_type(fn
, param
);
841 if (strcmp(key
, "*$") == 0) {
842 type
= get_arg_type(fn
, param
);
843 if (!type
|| type
->type
!= SYM_PTR
)
845 return get_real_base_type(type
);
847 return get_member_type_from_key(arg
, key
);
850 int is_struct(struct expression
*expr
)
854 type
= get_type(expr
);
855 if (type
&& type
->type
== SYM_STRUCT
)
864 {&bool_ctype
, "bool"},
865 {&void_ctype
, "void"},
866 {&type_ctype
, "type"},
867 {&char_ctype
, "char"},
868 {&schar_ctype
, "schar"},
869 {&uchar_ctype
, "uchar"},
870 {&short_ctype
, "short"},
871 {&sshort_ctype
, "sshort"},
872 {&ushort_ctype
, "ushort"},
874 {&sint_ctype
, "sint"},
875 {&uint_ctype
, "uint"},
876 {&long_ctype
, "long"},
877 {&slong_ctype
, "slong"},
878 {&ulong_ctype
, "ulong"},
879 {&llong_ctype
, "llong"},
880 {&sllong_ctype
, "sllong"},
881 {&ullong_ctype
, "ullong"},
882 {&int128_ctype
, "lllong"},
883 {&sint128_ctype
, "slllong"},
884 {&uint128_ctype
, "ulllong"},
885 {&float_ctype
, "float"},
886 {&double_ctype
, "double"},
887 {&ldouble_ctype
, "ldouble"},
888 {&string_ctype
, "string"},
890 {&lazy_ptr_ctype
, "lazy_ptr"},
891 {&incomplete_ctype
, "incomplete"},
892 {&label_ctype
, "label"},
894 {&null_ctype
, "null"},
897 static const char *base_type_str(struct symbol
*sym
)
901 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
902 if (sym
== base_types
[i
].sym
)
903 return base_types
[i
].name
;
908 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
913 return snprintf(buf
, size
, "<null type>");
915 if (type
->type
== SYM_BASETYPE
) {
916 return snprintf(buf
, size
, "%s", base_type_str(type
));
917 } else if (type
->type
== SYM_PTR
) {
918 type
= get_real_base_type(type
);
919 n
= type_str_helper(buf
, size
, type
);
922 return n
+ snprintf(buf
+ n
, size
- n
, "*");
923 } else if (type
->type
== SYM_ARRAY
) {
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_STRUCT
) {
930 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
931 } else if (type
->type
== SYM_UNION
) {
933 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
935 return snprintf(buf
, size
, "anonymous union");
936 } else if (type
->type
== SYM_FN
) {
937 struct symbol
*arg
, *return_type
, *arg_type
;
940 return_type
= get_real_base_type(type
);
941 n
= type_str_helper(buf
, size
, return_type
);
944 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
949 FOR_EACH_PTR(type
->arguments
, arg
) {
951 n
+= snprintf(buf
+ n
, size
- n
, ", ");
954 arg_type
= get_real_base_type(arg
);
955 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
958 } END_FOR_EACH_PTR(arg
);
960 return n
+ snprintf(buf
+ n
, size
- n
, ")");
961 } else if (type
->type
== SYM_NODE
) {
962 n
= snprintf(buf
, size
, "node {");
965 type
= get_real_base_type(type
);
966 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
969 return n
+ snprintf(buf
+ n
, size
- n
, "}");
970 } else if (type
->type
== SYM_ENUM
) {
971 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
973 return snprintf(buf
, size
, "<type %d>", type
->type
);
977 char *type_to_str(struct symbol
*type
)
979 static char buf
[256];
982 type_str_helper(buf
, sizeof(buf
), type
);
983 return alloc_sname(buf
);