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
)
120 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
123 return get_real_base_type(expr
->symbol
);
126 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
128 struct symbol
*tmp
, *sub
;
130 FOR_EACH_PTR(symbol_list
, tmp
) {
132 sub
= get_real_base_type(tmp
);
133 sub
= get_member_symbol(sub
->symbol_list
, member
);
138 if (tmp
->ident
== member
)
140 } END_FOR_EACH_PTR(tmp
);
145 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
147 struct ident
*member
;
150 if (!expr
|| expr
->type
!= EXPR_DEREF
)
153 member
= expr
->member
;
154 sym
= get_type(expr
->deref
);
156 // sm_msg("could not find struct type");
159 if (sym
->type
== SYM_PTR
)
160 sym
= get_real_base_type(sym
);
161 sym
= get_member_symbol(sym
->symbol_list
, member
);
164 return get_real_base_type(sym
);
167 static struct symbol
*handle__builtin_choose_expr(struct expression
*expr
)
169 struct expression
*const_expr
, *expr1
, *expr2
;
172 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
173 expr1
= get_argument_from_call_expr(expr
->args
, 1);
174 expr2
= get_argument_from_call_expr(expr
->args
, 2);
176 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
179 return get_type(expr1
);
181 return get_type(expr2
);
184 static struct symbol
*get_return_type(struct expression
*expr
)
188 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
189 return handle__builtin_choose_expr(expr
);
191 tmp
= get_type(expr
->fn
);
194 /* this is to handle __builtin_constant_p() */
195 if (tmp
->type
!= SYM_FN
)
196 tmp
= get_base_type(tmp
);
197 return get_real_base_type(tmp
);
200 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
202 if (stmt
->type
!= STMT_COMPOUND
)
204 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
205 if (stmt
->type
== STMT_LABEL
)
206 stmt
= stmt
->label_statement
;
207 if (stmt
->type
!= STMT_EXPRESSION
)
209 return get_type(stmt
->expression
);
212 static struct symbol
*get_select_type(struct expression
*expr
)
214 struct symbol
*one
, *two
;
216 one
= get_type(expr
->cond_true
);
217 two
= get_type(expr
->cond_false
);
221 * This is a hack. If the types are not equiv then we
222 * really don't know the type. But I think guessing is
225 if (type_positive_bits(one
) > type_positive_bits(two
))
230 struct symbol
*get_pointer_type(struct expression
*expr
)
234 sym
= get_type(expr
);
237 if (sym
->type
== SYM_NODE
) {
238 sym
= get_real_base_type(sym
);
242 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
244 return get_real_base_type(sym
);
247 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
252 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
254 base
= get_type(expr
);
257 sym
->ctype
.base_type
= base
;
261 static struct symbol
*get_type_helper(struct expression
*expr
)
265 expr
= strip_parens(expr
);
272 switch (expr
->type
) {
277 ret
= get_type_symbol(expr
);
280 ret
= get_symbol_from_deref(expr
);
285 ret
= fake_pointer_sym(expr
);
286 else if (expr
->op
== '*')
287 ret
= get_pointer_type(expr
->unop
);
289 ret
= get_type(expr
->unop
);
291 case EXPR_ASSIGNMENT
:
292 ret
= get_type(expr
->left
);
295 case EXPR_FORCE_CAST
:
296 case EXPR_IMPLIED_CAST
:
297 ret
= get_real_base_type(expr
->cast_type
);
301 ret
= get_binop_type(expr
);
304 ret
= get_return_type(expr
);
307 ret
= get_expr_stmt_type(expr
->statement
);
309 case EXPR_CONDITIONAL
:
311 ret
= get_select_type(expr
);
323 return get_type_helper(strip_Generic(expr
));
328 if (ret
&& ret
->type
== SYM_TYPEOF
)
329 ret
= get_type(ret
->initializer
);
335 static struct symbol
*get_final_type_helper(struct expression
*expr
)
338 * The problem is that I wrote a bunch of Smatch to think that
339 * you could do get_type() on an expression and it would give
340 * you what the comparison was type promoted to. This is wrong
341 * but fixing it is a big of work... Hence this horrible hack.
345 expr
= strip_parens(expr
);
349 if (expr
->type
== EXPR_COMPARE
)
351 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
357 struct symbol
*get_type(struct expression
*expr
)
359 return get_type_helper(expr
);
362 struct symbol
*get_comparison_type(struct expression
*expr
)
365 * Eventually we will probably have to figure out how to make get_type()
366 * return &int_ctype so let's create a helper function to transition to.
368 return get_type_helper(expr
);
371 struct symbol
*get_final_type(struct expression
*expr
)
375 ret
= get_final_type_helper(expr
);
378 return get_type_helper(expr
);
381 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
383 struct symbol
*ret
= &int_ctype
;
385 if (type_positive_bits(left
) > type_positive_bits(ret
))
387 if (type_positive_bits(right
) > type_positive_bits(ret
))
390 if (type_is_ptr(left
))
392 if (type_is_ptr(right
))
398 int type_signed(struct symbol
*base_type
)
402 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
407 int expr_unsigned(struct expression
*expr
)
411 sym
= get_type(expr
);
414 if (type_unsigned(sym
))
419 int expr_signed(struct expression
*expr
)
423 sym
= get_type(expr
);
426 if (type_signed(sym
))
431 int returns_unsigned(struct symbol
*sym
)
435 sym
= get_base_type(sym
);
436 if (!sym
|| sym
->type
!= SYM_FN
)
438 sym
= get_base_type(sym
);
439 return type_unsigned(sym
);
442 int is_pointer(struct expression
*expr
)
444 return type_is_ptr(get_final_type(expr
));
447 bool is_void_ptr(struct symbol
*type
)
451 if (type
->type
!= SYM_PTR
)
453 type
= get_real_base_type(type
);
455 return types_equiv(type
, &void_ctype
);
458 int returns_pointer(struct symbol
*sym
)
462 sym
= get_base_type(sym
);
463 if (!sym
|| sym
->type
!= SYM_FN
)
465 sym
= get_base_type(sym
);
466 if (sym
&& sym
->type
== SYM_PTR
)
471 static sval_t
fp_max(struct symbol
*type
)
473 sval_t ret
= { .type
= type
};
475 if (type
== &float_ctype
)
476 ret
.fvalue
= FLT_MAX
;
477 else if (type
== &double_ctype
)
478 ret
.dvalue
= DBL_MAX
;
480 ret
.ldvalue
= LDBL_MAX
;
485 sval_t
sval_type_max(struct symbol
*base_type
)
489 if (type_is_fp(base_type
))
490 return fp_max(base_type
);
492 if (!base_type
|| !type_bits(base_type
))
493 base_type
= &llong_ctype
;
494 ret
.type
= base_type
;
496 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
500 static sval_t
fp_min(struct symbol
*type
)
502 sval_t ret
= { .type
= type
};
504 if (type
== &float_ctype
)
505 ret
.fvalue
= -FLT_MAX
;
506 else if (type
== &double_ctype
)
507 ret
.dvalue
= -DBL_MAX
;
509 ret
.ldvalue
= -LDBL_MAX
;
514 sval_t
sval_type_min(struct symbol
*base_type
)
518 if (type_is_fp(base_type
))
519 return fp_min(base_type
);
521 if (!base_type
|| !type_bits(base_type
))
522 base_type
= &llong_ctype
;
523 ret
.type
= base_type
;
525 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
530 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
535 int nr_bits(struct expression
*expr
)
539 type
= get_type(expr
);
542 return type_bits(type
);
545 int is_void_pointer(struct expression
*expr
)
549 type
= get_type(expr
);
550 if (!type
|| type
->type
!= SYM_PTR
)
552 type
= get_real_base_type(type
);
553 if (type
== &void_ctype
)
558 int is_char_pointer(struct expression
*expr
)
562 type
= get_type(expr
);
563 if (!type
|| type
->type
!= SYM_PTR
)
565 type
= get_real_base_type(type
);
566 if (type
== &char_ctype
)
571 int is_string(struct expression
*expr
)
573 expr
= strip_expr(expr
);
574 if (!expr
|| expr
->type
!= EXPR_STRING
)
581 bool is_struct_ptr(struct symbol
*type
)
583 if (!type
|| type
->type
!= SYM_PTR
)
585 type
= get_real_base_type(type
);
586 if (!type
|| type
->type
!= SYM_STRUCT
)
591 int is_static(struct expression
*expr
)
597 name
= expr_to_str_sym(expr
, &sym
);
601 if (sym
->ctype
.modifiers
& MOD_STATIC
)
608 static struct expression
*get_symbol_expr(struct expression
*expr
)
612 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
613 expr
= strip_expr(expr
->deref
);
617 bool is_local_variable(struct expression
*expr
)
621 expr
= get_symbol_expr(expr
);
622 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
625 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
630 int types_equiv(struct symbol
*one
, struct symbol
*two
)
636 if (one
->type
!= two
->type
)
638 if (one
->type
== SYM_PTR
)
639 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
640 if (type_positive_bits(one
) != type_positive_bits(two
))
645 bool type_fits(struct symbol
*type
, struct symbol
*test
)
653 if (type_bits(test
) > type_bits(type
))
655 if (type_signed(test
) && !type_signed(type
))
657 if (type_positive_bits(test
) > type_positive_bits(type
))
664 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
667 const char *global_static(void)
669 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
675 struct symbol
*cur_func_return_type(void)
679 sym
= get_real_base_type(cur_func_sym
);
680 if (!sym
|| sym
->type
!= SYM_FN
)
682 sym
= get_real_base_type(sym
);
686 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
688 struct symbol
*fn_type
;
690 struct symbol
*arg_type
;
693 fn_type
= get_type(fn
);
696 if (fn_type
->type
== SYM_PTR
)
697 fn_type
= get_real_base_type(fn_type
);
698 if (fn_type
->type
!= SYM_FN
)
702 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
703 arg_type
= get_real_base_type(tmp
);
708 } END_FOR_EACH_PTR(tmp
);
713 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
715 struct symbol
*tmp
, *sub
;
718 if (strncmp(name
, ".", 1) == 0)
720 else if (strncmp(name
, "->", 2) == 0)
723 FOR_EACH_PTR(symbol_list
, tmp
) {
725 sub
= get_real_base_type(tmp
);
726 sub
= get_member_from_string(sub
->symbol_list
, name
);
732 if (strcmp(tmp
->ident
->name
, name
) == 0)
735 chunk_len
= tmp
->ident
->len
;
736 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
737 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
738 sub
= get_real_base_type(tmp
);
739 if (sub
->type
== SYM_PTR
)
740 sub
= get_real_base_type(sub
);
741 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
744 } END_FOR_EACH_PTR(tmp
);
749 static struct symbol
*get_type_from_container_of_key(struct expression
*expr
, const char *key
)
753 expr
= map_container_of_to_simpler_expr_key(expr
, key
, &new_key
);
756 return get_member_type_from_key(expr
, new_key
);
759 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
765 if (strcmp(key
, "$") == 0)
766 return get_type(expr
);
768 if (strcmp(key
, "*$") == 0) {
769 sym
= get_type(expr
);
772 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
774 return get_real_base_type(sym
);
777 if (strstr(key
, "<~$"))
778 return get_type_from_container_of_key(expr
, key
);
780 sym
= get_type(expr
);
783 if (sym
->type
== SYM_PTR
)
784 sym
= get_real_base_type(sym
);
786 while (*key
== '*') {
795 sym
= get_member_from_string(sym
->symbol_list
, key
);
798 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
799 sym
= get_real_base_type(sym
);
800 for (i
= 0; i
< star
; i
++) {
801 if (!sym
|| sym
->type
!= SYM_PTR
)
803 sym
= get_real_base_type(sym
);
808 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
814 if (strcmp(key
, "$") == 0)
815 return get_arg_type(fn
, param
);
816 if (strcmp(key
, "*$") == 0) {
817 type
= get_arg_type(fn
, param
);
818 if (!type
|| type
->type
!= SYM_PTR
)
820 return get_real_base_type(type
);
822 return get_member_type_from_key(arg
, key
);
825 int is_struct(struct expression
*expr
)
829 type
= get_type(expr
);
830 if (type
&& type
->type
== SYM_STRUCT
)
839 {&bool_ctype
, "bool"},
840 {&void_ctype
, "void"},
841 {&type_ctype
, "type"},
842 {&char_ctype
, "char"},
843 {&schar_ctype
, "schar"},
844 {&uchar_ctype
, "uchar"},
845 {&short_ctype
, "short"},
846 {&sshort_ctype
, "sshort"},
847 {&ushort_ctype
, "ushort"},
849 {&sint_ctype
, "sint"},
850 {&uint_ctype
, "uint"},
851 {&long_ctype
, "long"},
852 {&slong_ctype
, "slong"},
853 {&ulong_ctype
, "ulong"},
854 {&llong_ctype
, "llong"},
855 {&sllong_ctype
, "sllong"},
856 {&ullong_ctype
, "ullong"},
857 {&int128_ctype
, "lllong"},
858 {&sint128_ctype
, "slllong"},
859 {&uint128_ctype
, "ulllong"},
860 {&float_ctype
, "float"},
861 {&double_ctype
, "double"},
862 {&ldouble_ctype
, "ldouble"},
863 {&string_ctype
, "string"},
865 {&lazy_ptr_ctype
, "lazy_ptr"},
866 {&incomplete_ctype
, "incomplete"},
867 {&label_ctype
, "label"},
869 {&null_ctype
, "null"},
872 static const char *base_type_str(struct symbol
*sym
)
876 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
877 if (sym
== base_types
[i
].sym
)
878 return base_types
[i
].name
;
883 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
888 return snprintf(buf
, size
, "<null type>");
890 if (type
->type
== SYM_BASETYPE
) {
891 return snprintf(buf
, size
, "%s", base_type_str(type
));
892 } else if (type
->type
== SYM_PTR
) {
893 type
= get_real_base_type(type
);
894 n
= type_str_helper(buf
, size
, type
);
897 return n
+ snprintf(buf
+ n
, size
- n
, "*");
898 } else if (type
->type
== SYM_ARRAY
) {
899 type
= get_real_base_type(type
);
900 n
= type_str_helper(buf
, size
, type
);
903 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
904 } else if (type
->type
== SYM_STRUCT
) {
905 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
906 } else if (type
->type
== SYM_UNION
) {
908 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
910 return snprintf(buf
, size
, "anonymous union");
911 } else if (type
->type
== SYM_FN
) {
912 struct symbol
*arg
, *return_type
, *arg_type
;
915 return_type
= get_real_base_type(type
);
916 n
= type_str_helper(buf
, size
, return_type
);
919 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
924 FOR_EACH_PTR(type
->arguments
, arg
) {
926 n
+= snprintf(buf
+ n
, size
- n
, ", ");
929 arg_type
= get_real_base_type(arg
);
930 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
933 } END_FOR_EACH_PTR(arg
);
935 return n
+ snprintf(buf
+ n
, size
- n
, ")");
936 } else if (type
->type
== SYM_NODE
) {
937 n
= snprintf(buf
, size
, "node {");
940 type
= get_real_base_type(type
);
941 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
944 return n
+ snprintf(buf
+ n
, size
- n
, "}");
945 } else if (type
->type
== SYM_ENUM
) {
946 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
948 return snprintf(buf
, size
, "<type %d>", type
->type
);
952 char *type_to_str(struct symbol
*type
)
954 static char buf
[256];
957 type_str_helper(buf
, sizeof(buf
), type
);
958 return alloc_sname(buf
);