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 static struct symbol
*get_binop_type(struct expression
*expr
)
67 struct symbol
*left
, *right
;
69 left
= get_type(expr
->left
);
73 if (expr
->op
== SPECIAL_LEFTSHIFT
||
74 expr
->op
== SPECIAL_RIGHTSHIFT
) {
75 if (type_positive_bits(left
) < 31)
79 right
= get_type(expr
->right
);
83 if (type_is_fp(left
)) {
84 if (type_is_fp(right
)) {
85 if (type_bits(left
) > type_bits(right
))
92 if (type_is_fp(right
)) {
93 if (type_is_fp(left
)) {
94 if (type_bits(right
) > type_bits(left
))
101 if (expr
->op
== '-' &&
102 (is_ptr_type(left
) && is_ptr_type(right
)))
103 return ssize_t_ctype
;
105 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
107 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
110 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
113 if (type_positive_bits(left
) > type_positive_bits(right
))
118 static struct symbol
*get_type_symbol(struct expression
*expr
)
122 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
125 type
= get_real_base_type(expr
->symbol
);
126 if (type
== &autotype_ctype
)
127 return get_type(expr
->symbol
->initializer
);
131 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
133 struct symbol
*tmp
, *sub
;
135 FOR_EACH_PTR(symbol_list
, tmp
) {
137 sub
= get_real_base_type(tmp
);
138 sub
= get_member_symbol(sub
->symbol_list
, member
);
143 if (tmp
->ident
== member
)
145 } END_FOR_EACH_PTR(tmp
);
150 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
152 struct ident
*member
;
155 if (!expr
|| expr
->type
!= EXPR_DEREF
)
158 member
= expr
->member
;
159 sym
= get_type(expr
->deref
);
161 // sm_msg("could not find struct type");
164 if (sym
->type
== SYM_PTR
)
165 sym
= get_real_base_type(sym
);
166 sym
= get_member_symbol(sym
->symbol_list
, member
);
169 return get_real_base_type(sym
);
172 static struct symbol
*handle__builtin_choose_expr(struct expression
*expr
)
174 struct expression
*const_expr
, *expr1
, *expr2
;
177 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
178 expr1
= get_argument_from_call_expr(expr
->args
, 1);
179 expr2
= get_argument_from_call_expr(expr
->args
, 2);
181 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
184 return get_type(expr1
);
186 return get_type(expr2
);
189 static struct symbol
*get_return_type(struct expression
*expr
)
193 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
194 return handle__builtin_choose_expr(expr
);
196 tmp
= get_type(expr
->fn
);
199 /* this is to handle __builtin_constant_p() */
200 if (tmp
->type
!= SYM_FN
)
201 tmp
= get_base_type(tmp
);
202 return get_real_base_type(tmp
);
205 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
207 if (stmt
->type
!= STMT_COMPOUND
)
209 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
210 if (stmt
->type
== STMT_LABEL
)
211 stmt
= stmt
->label_statement
;
212 if (stmt
->type
!= STMT_EXPRESSION
)
214 return get_type(stmt
->expression
);
217 static struct symbol
*get_select_type(struct expression
*expr
)
219 struct symbol
*one
, *two
;
221 one
= get_type(expr
->cond_true
);
222 two
= get_type(expr
->cond_false
);
226 * This is a hack. If the types are not equiv then we
227 * really don't know the type. But I think guessing is
230 if (type_positive_bits(one
) > type_positive_bits(two
))
235 struct symbol
*get_pointer_type(struct expression
*expr
)
239 sym
= get_type(expr
);
242 if (sym
->type
== SYM_NODE
) {
243 sym
= get_real_base_type(sym
);
247 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
249 return get_real_base_type(sym
);
252 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
257 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
259 base
= get_type(expr
);
262 sym
->ctype
.base_type
= base
;
266 static struct symbol
*get_type_helper(struct expression
*expr
)
270 expr
= strip_parens(expr
);
277 switch (expr
->type
) {
282 ret
= get_type_symbol(expr
);
285 ret
= get_symbol_from_deref(expr
);
290 ret
= fake_pointer_sym(expr
);
291 else if (expr
->op
== '*')
292 ret
= get_pointer_type(expr
->unop
);
294 ret
= get_type(expr
->unop
);
296 case EXPR_ASSIGNMENT
:
297 ret
= get_type(expr
->left
);
300 case EXPR_FORCE_CAST
:
301 case EXPR_IMPLIED_CAST
:
302 ret
= get_real_base_type(expr
->cast_type
);
306 ret
= get_binop_type(expr
);
309 ret
= get_return_type(expr
);
312 ret
= get_expr_stmt_type(expr
->statement
);
314 case EXPR_CONDITIONAL
:
316 ret
= get_select_type(expr
);
328 return get_type_helper(strip_Generic(expr
));
330 return get_type_helper(expr
->right
);
335 if (ret
&& ret
->type
== SYM_TYPEOF
)
336 ret
= get_type(ret
->initializer
);
342 static struct symbol
*get_final_type_helper(struct expression
*expr
)
345 * The problem is that I wrote a bunch of Smatch to think that
346 * you could do get_type() on an expression and it would give
347 * you what the comparison was type promoted to. This is wrong
348 * but fixing it is a big of work... Hence this horrible hack.
352 expr
= strip_parens(expr
);
356 if (expr
->type
== EXPR_COMPARE
)
358 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
364 struct symbol
*get_type(struct expression
*expr
)
366 return get_type_helper(expr
);
369 struct symbol
*get_comparison_type(struct expression
*expr
)
372 * Eventually we will probably have to figure out how to make get_type()
373 * return &int_ctype so let's create a helper function to transition to.
375 return get_type_helper(expr
);
378 struct symbol
*get_final_type(struct expression
*expr
)
382 ret
= get_final_type_helper(expr
);
385 return get_type_helper(expr
);
388 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
390 struct symbol
*ret
= &int_ctype
;
392 if (type_positive_bits(left
) > type_positive_bits(ret
))
394 if (type_positive_bits(right
) > type_positive_bits(ret
))
397 if (type_is_ptr(left
))
399 if (type_is_ptr(right
))
405 int type_signed(struct symbol
*base_type
)
409 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
414 int expr_unsigned(struct expression
*expr
)
418 sym
= get_type(expr
);
421 if (type_unsigned(sym
))
426 int expr_signed(struct expression
*expr
)
430 sym
= get_type(expr
);
433 if (type_signed(sym
))
438 int returns_unsigned(struct symbol
*sym
)
442 sym
= get_base_type(sym
);
443 if (!sym
|| sym
->type
!= SYM_FN
)
445 sym
= get_base_type(sym
);
446 return type_unsigned(sym
);
449 int is_pointer(struct expression
*expr
)
451 return type_is_ptr(get_final_type(expr
));
454 bool is_void_ptr(struct symbol
*type
)
458 if (type
->type
!= SYM_PTR
)
460 type
= get_real_base_type(type
);
462 return types_equiv(type
, &void_ctype
);
465 int returns_pointer(struct symbol
*sym
)
469 sym
= get_base_type(sym
);
470 if (!sym
|| sym
->type
!= SYM_FN
)
472 sym
= get_base_type(sym
);
473 if (sym
&& sym
->type
== SYM_PTR
)
478 static sval_t
fp_max(struct symbol
*type
)
480 sval_t ret
= { .type
= type
};
482 if (type
== &float_ctype
)
483 ret
.fvalue
= FLT_MAX
;
484 else if (type
== &double_ctype
)
485 ret
.dvalue
= DBL_MAX
;
487 ret
.ldvalue
= LDBL_MAX
;
492 sval_t
sval_type_max(struct symbol
*base_type
)
496 if (type_is_fp(base_type
))
497 return fp_max(base_type
);
499 if (!base_type
|| !type_bits(base_type
))
500 base_type
= &llong_ctype
;
501 ret
.type
= base_type
;
503 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
507 static sval_t
fp_min(struct symbol
*type
)
509 sval_t ret
= { .type
= type
};
511 if (type
== &float_ctype
)
512 ret
.fvalue
= -FLT_MAX
;
513 else if (type
== &double_ctype
)
514 ret
.dvalue
= -DBL_MAX
;
516 ret
.ldvalue
= -LDBL_MAX
;
521 sval_t
sval_type_min(struct symbol
*base_type
)
525 if (type_is_fp(base_type
))
526 return fp_min(base_type
);
528 if (!base_type
|| !type_bits(base_type
))
529 base_type
= &llong_ctype
;
530 ret
.type
= base_type
;
532 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
537 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
542 int nr_bits(struct expression
*expr
)
546 type
= get_type(expr
);
549 return type_bits(type
);
552 int is_void_pointer(struct expression
*expr
)
556 type
= get_type(expr
);
557 if (!type
|| type
->type
!= SYM_PTR
)
559 type
= get_real_base_type(type
);
560 if (type
== &void_ctype
)
565 int is_char_pointer(struct expression
*expr
)
569 type
= get_type(expr
);
570 if (!type
|| type
->type
!= SYM_PTR
)
572 type
= get_real_base_type(type
);
573 if (type
== &char_ctype
)
578 int is_string(struct expression
*expr
)
580 expr
= strip_expr(expr
);
581 if (!expr
|| expr
->type
!= EXPR_STRING
)
588 bool is_struct_ptr(struct symbol
*type
)
590 if (!type
|| type
->type
!= SYM_PTR
)
592 type
= get_real_base_type(type
);
593 if (!type
|| type
->type
!= SYM_STRUCT
)
598 int is_static(struct expression
*expr
)
604 name
= expr_to_str_sym(expr
, &sym
);
608 if (sym
->ctype
.modifiers
& MOD_STATIC
)
615 static struct expression
*get_symbol_expr(struct expression
*expr
)
619 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
620 expr
= strip_expr(expr
->deref
);
624 bool is_local_variable(struct expression
*expr
)
628 expr
= get_symbol_expr(expr
);
629 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
632 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
637 int types_equiv(struct symbol
*one
, struct symbol
*two
)
643 if (one
->type
!= two
->type
)
645 if (one
->type
== SYM_PTR
)
646 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
647 if (type_positive_bits(one
) != type_positive_bits(two
))
652 bool type_fits(struct symbol
*type
, struct symbol
*test
)
660 if (type_bits(test
) > type_bits(type
))
662 if (type_signed(test
) && !type_signed(type
))
664 if (type_positive_bits(test
) > type_positive_bits(type
))
671 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
674 const char *global_static(void)
676 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
682 struct symbol
*cur_func_return_type(void)
686 sym
= get_real_base_type(cur_func_sym
);
687 if (!sym
|| sym
->type
!= SYM_FN
)
689 sym
= get_real_base_type(sym
);
693 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
695 struct symbol
*fn_type
;
697 struct symbol
*arg_type
;
700 fn_type
= get_type(fn
);
703 if (fn_type
->type
== SYM_PTR
)
704 fn_type
= get_real_base_type(fn_type
);
705 if (fn_type
->type
!= SYM_FN
)
709 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
710 arg_type
= get_real_base_type(tmp
);
715 } END_FOR_EACH_PTR(tmp
);
720 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
722 struct symbol
*tmp
, *sub
;
725 if (strncmp(name
, ".", 1) == 0)
727 else if (strncmp(name
, "->", 2) == 0)
730 FOR_EACH_PTR(symbol_list
, tmp
) {
732 sub
= get_real_base_type(tmp
);
733 sub
= get_member_from_string(sub
->symbol_list
, name
);
739 if (strcmp(tmp
->ident
->name
, name
) == 0)
742 chunk_len
= tmp
->ident
->len
;
743 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
744 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
745 sub
= get_real_base_type(tmp
);
746 if (sub
->type
== SYM_PTR
)
747 sub
= get_real_base_type(sub
);
748 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
751 } END_FOR_EACH_PTR(tmp
);
756 static struct symbol
*get_type_from_container_of_key(struct expression
*expr
, const char *key
)
760 expr
= map_container_of_to_simpler_expr_key(expr
, key
, &new_key
);
763 return get_member_type_from_key(expr
, new_key
);
766 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
772 if (strcmp(key
, "$") == 0)
773 return get_type(expr
);
775 if (strcmp(key
, "*$") == 0) {
776 sym
= get_type(expr
);
779 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
781 return get_real_base_type(sym
);
784 if (strstr(key
, "<~$"))
785 return get_type_from_container_of_key(expr
, key
);
787 sym
= get_type(expr
);
790 if (sym
->type
== SYM_PTR
)
791 sym
= get_real_base_type(sym
);
793 while (*key
== '*') {
802 sym
= get_member_from_string(sym
->symbol_list
, key
);
805 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
806 sym
= get_real_base_type(sym
);
807 for (i
= 0; i
< star
; i
++) {
808 if (!sym
|| sym
->type
!= SYM_PTR
)
810 sym
= get_real_base_type(sym
);
815 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
821 if (strcmp(key
, "$") == 0)
822 return get_arg_type(fn
, param
);
823 if (strcmp(key
, "*$") == 0) {
824 type
= get_arg_type(fn
, param
);
825 if (!type
|| type
->type
!= SYM_PTR
)
827 return get_real_base_type(type
);
829 return get_member_type_from_key(arg
, key
);
832 int is_struct(struct expression
*expr
)
836 type
= get_type(expr
);
837 if (type
&& type
->type
== SYM_STRUCT
)
846 {&bool_ctype
, "bool"},
847 {&void_ctype
, "void"},
848 {&type_ctype
, "type"},
849 {&char_ctype
, "char"},
850 {&schar_ctype
, "schar"},
851 {&uchar_ctype
, "uchar"},
852 {&short_ctype
, "short"},
853 {&sshort_ctype
, "sshort"},
854 {&ushort_ctype
, "ushort"},
856 {&sint_ctype
, "sint"},
857 {&uint_ctype
, "uint"},
858 {&long_ctype
, "long"},
859 {&slong_ctype
, "slong"},
860 {&ulong_ctype
, "ulong"},
861 {&llong_ctype
, "llong"},
862 {&sllong_ctype
, "sllong"},
863 {&ullong_ctype
, "ullong"},
864 {&int128_ctype
, "lllong"},
865 {&sint128_ctype
, "slllong"},
866 {&uint128_ctype
, "ulllong"},
867 {&float_ctype
, "float"},
868 {&double_ctype
, "double"},
869 {&ldouble_ctype
, "ldouble"},
870 {&string_ctype
, "string"},
872 {&lazy_ptr_ctype
, "lazy_ptr"},
873 {&incomplete_ctype
, "incomplete"},
874 {&label_ctype
, "label"},
876 {&null_ctype
, "null"},
879 static const char *base_type_str(struct symbol
*sym
)
883 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
884 if (sym
== base_types
[i
].sym
)
885 return base_types
[i
].name
;
890 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
895 return snprintf(buf
, size
, "<null type>");
897 if (type
->type
== SYM_BASETYPE
) {
898 return snprintf(buf
, size
, "%s", base_type_str(type
));
899 } else if (type
->type
== SYM_PTR
) {
900 type
= get_real_base_type(type
);
901 n
= type_str_helper(buf
, size
, type
);
904 return n
+ snprintf(buf
+ n
, size
- n
, "*");
905 } else if (type
->type
== SYM_ARRAY
) {
906 type
= get_real_base_type(type
);
907 n
= type_str_helper(buf
, size
, type
);
910 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
911 } else if (type
->type
== SYM_STRUCT
) {
912 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
913 } else if (type
->type
== SYM_UNION
) {
915 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
917 return snprintf(buf
, size
, "anonymous union");
918 } else if (type
->type
== SYM_FN
) {
919 struct symbol
*arg
, *return_type
, *arg_type
;
922 return_type
= get_real_base_type(type
);
923 n
= type_str_helper(buf
, size
, return_type
);
926 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
931 FOR_EACH_PTR(type
->arguments
, arg
) {
933 n
+= snprintf(buf
+ n
, size
- n
, ", ");
936 arg_type
= get_real_base_type(arg
);
937 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
940 } END_FOR_EACH_PTR(arg
);
942 return n
+ snprintf(buf
+ n
, size
- n
, ")");
943 } else if (type
->type
== SYM_NODE
) {
944 n
= snprintf(buf
, size
, "node {");
947 type
= get_real_base_type(type
);
948 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
951 return n
+ snprintf(buf
+ n
, size
- n
, "}");
952 } else if (type
->type
== SYM_ENUM
) {
953 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
955 return snprintf(buf
, size
, "<type %d>", type
->type
);
959 char *type_to_str(struct symbol
*type
)
961 static char buf
[256];
964 type_str_helper(buf
, sizeof(buf
), type
);
965 return alloc_sname(buf
);