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
));
333 if (ret
&& ret
->type
== SYM_TYPEOF
)
334 ret
= get_type(ret
->initializer
);
340 static struct symbol
*get_final_type_helper(struct expression
*expr
)
343 * The problem is that I wrote a bunch of Smatch to think that
344 * you could do get_type() on an expression and it would give
345 * you what the comparison was type promoted to. This is wrong
346 * but fixing it is a big of work... Hence this horrible hack.
350 expr
= strip_parens(expr
);
354 if (expr
->type
== EXPR_COMPARE
)
356 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
362 struct symbol
*get_type(struct expression
*expr
)
364 return get_type_helper(expr
);
367 struct symbol
*get_comparison_type(struct expression
*expr
)
370 * Eventually we will probably have to figure out how to make get_type()
371 * return &int_ctype so let's create a helper function to transition to.
373 return get_type_helper(expr
);
376 struct symbol
*get_final_type(struct expression
*expr
)
380 ret
= get_final_type_helper(expr
);
383 return get_type_helper(expr
);
386 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
388 struct symbol
*ret
= &int_ctype
;
390 if (type_positive_bits(left
) > type_positive_bits(ret
))
392 if (type_positive_bits(right
) > type_positive_bits(ret
))
395 if (type_is_ptr(left
))
397 if (type_is_ptr(right
))
403 int type_signed(struct symbol
*base_type
)
407 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
412 int expr_unsigned(struct expression
*expr
)
416 sym
= get_type(expr
);
419 if (type_unsigned(sym
))
424 int expr_signed(struct expression
*expr
)
428 sym
= get_type(expr
);
431 if (type_signed(sym
))
436 int returns_unsigned(struct symbol
*sym
)
440 sym
= get_base_type(sym
);
441 if (!sym
|| sym
->type
!= SYM_FN
)
443 sym
= get_base_type(sym
);
444 return type_unsigned(sym
);
447 int is_pointer(struct expression
*expr
)
449 return type_is_ptr(get_final_type(expr
));
452 bool is_void_ptr(struct symbol
*type
)
456 if (type
->type
!= SYM_PTR
)
458 type
= get_real_base_type(type
);
460 return types_equiv(type
, &void_ctype
);
463 int returns_pointer(struct symbol
*sym
)
467 sym
= get_base_type(sym
);
468 if (!sym
|| sym
->type
!= SYM_FN
)
470 sym
= get_base_type(sym
);
471 if (sym
&& sym
->type
== SYM_PTR
)
476 static sval_t
fp_max(struct symbol
*type
)
478 sval_t ret
= { .type
= type
};
480 if (type
== &float_ctype
)
481 ret
.fvalue
= FLT_MAX
;
482 else if (type
== &double_ctype
)
483 ret
.dvalue
= DBL_MAX
;
485 ret
.ldvalue
= LDBL_MAX
;
490 sval_t
sval_type_max(struct symbol
*base_type
)
494 if (type_is_fp(base_type
))
495 return fp_max(base_type
);
497 if (!base_type
|| !type_bits(base_type
))
498 base_type
= &llong_ctype
;
499 ret
.type
= base_type
;
501 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
505 static sval_t
fp_min(struct symbol
*type
)
507 sval_t ret
= { .type
= type
};
509 if (type
== &float_ctype
)
510 ret
.fvalue
= -FLT_MAX
;
511 else if (type
== &double_ctype
)
512 ret
.dvalue
= -DBL_MAX
;
514 ret
.ldvalue
= -LDBL_MAX
;
519 sval_t
sval_type_min(struct symbol
*base_type
)
523 if (type_is_fp(base_type
))
524 return fp_min(base_type
);
526 if (!base_type
|| !type_bits(base_type
))
527 base_type
= &llong_ctype
;
528 ret
.type
= base_type
;
530 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
535 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
540 int nr_bits(struct expression
*expr
)
544 type
= get_type(expr
);
547 return type_bits(type
);
550 int is_void_pointer(struct expression
*expr
)
554 type
= get_type(expr
);
555 if (!type
|| type
->type
!= SYM_PTR
)
557 type
= get_real_base_type(type
);
558 if (type
== &void_ctype
)
563 int is_char_pointer(struct expression
*expr
)
567 type
= get_type(expr
);
568 if (!type
|| type
->type
!= SYM_PTR
)
570 type
= get_real_base_type(type
);
571 if (type
== &char_ctype
)
576 int is_string(struct expression
*expr
)
578 expr
= strip_expr(expr
);
579 if (!expr
|| expr
->type
!= EXPR_STRING
)
586 bool is_struct_ptr(struct symbol
*type
)
588 if (!type
|| type
->type
!= SYM_PTR
)
590 type
= get_real_base_type(type
);
591 if (!type
|| type
->type
!= SYM_STRUCT
)
596 int is_static(struct expression
*expr
)
602 name
= expr_to_str_sym(expr
, &sym
);
606 if (sym
->ctype
.modifiers
& MOD_STATIC
)
613 static struct expression
*get_symbol_expr(struct expression
*expr
)
617 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
618 expr
= strip_expr(expr
->deref
);
622 bool is_local_variable(struct expression
*expr
)
626 expr
= get_symbol_expr(expr
);
627 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
630 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
635 int types_equiv(struct symbol
*one
, struct symbol
*two
)
641 if (one
->type
!= two
->type
)
643 if (one
->type
== SYM_PTR
)
644 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
645 if (type_positive_bits(one
) != type_positive_bits(two
))
650 bool type_fits(struct symbol
*type
, struct symbol
*test
)
658 if (type_bits(test
) > type_bits(type
))
660 if (type_signed(test
) && !type_signed(type
))
662 if (type_positive_bits(test
) > type_positive_bits(type
))
669 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
672 const char *global_static(void)
674 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
680 struct symbol
*cur_func_return_type(void)
684 sym
= get_real_base_type(cur_func_sym
);
685 if (!sym
|| sym
->type
!= SYM_FN
)
687 sym
= get_real_base_type(sym
);
691 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
693 struct symbol
*fn_type
;
695 struct symbol
*arg_type
;
698 fn_type
= get_type(fn
);
701 if (fn_type
->type
== SYM_PTR
)
702 fn_type
= get_real_base_type(fn_type
);
703 if (fn_type
->type
!= SYM_FN
)
707 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
708 arg_type
= get_real_base_type(tmp
);
713 } END_FOR_EACH_PTR(tmp
);
718 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
720 struct symbol
*tmp
, *sub
;
723 if (strncmp(name
, ".", 1) == 0)
725 else if (strncmp(name
, "->", 2) == 0)
728 FOR_EACH_PTR(symbol_list
, tmp
) {
730 sub
= get_real_base_type(tmp
);
731 sub
= get_member_from_string(sub
->symbol_list
, name
);
737 if (strcmp(tmp
->ident
->name
, name
) == 0)
740 chunk_len
= tmp
->ident
->len
;
741 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
742 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
743 sub
= get_real_base_type(tmp
);
744 if (sub
->type
== SYM_PTR
)
745 sub
= get_real_base_type(sub
);
746 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
749 } END_FOR_EACH_PTR(tmp
);
754 static struct symbol
*get_type_from_container_of_key(struct expression
*expr
, const char *key
)
758 expr
= map_container_of_to_simpler_expr_key(expr
, key
, &new_key
);
761 return get_member_type_from_key(expr
, new_key
);
764 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
770 if (strcmp(key
, "$") == 0)
771 return get_type(expr
);
773 if (strcmp(key
, "*$") == 0) {
774 sym
= get_type(expr
);
777 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
779 return get_real_base_type(sym
);
782 if (strstr(key
, "<~$"))
783 return get_type_from_container_of_key(expr
, key
);
785 sym
= get_type(expr
);
788 if (sym
->type
== SYM_PTR
)
789 sym
= get_real_base_type(sym
);
791 while (*key
== '*') {
800 sym
= get_member_from_string(sym
->symbol_list
, key
);
803 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
804 sym
= get_real_base_type(sym
);
805 for (i
= 0; i
< star
; i
++) {
806 if (!sym
|| sym
->type
!= SYM_PTR
)
808 sym
= get_real_base_type(sym
);
813 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
819 if (strcmp(key
, "$") == 0)
820 return get_arg_type(fn
, param
);
821 if (strcmp(key
, "*$") == 0) {
822 type
= get_arg_type(fn
, param
);
823 if (!type
|| type
->type
!= SYM_PTR
)
825 return get_real_base_type(type
);
827 return get_member_type_from_key(arg
, key
);
830 int is_struct(struct expression
*expr
)
834 type
= get_type(expr
);
835 if (type
&& type
->type
== SYM_STRUCT
)
844 {&bool_ctype
, "bool"},
845 {&void_ctype
, "void"},
846 {&type_ctype
, "type"},
847 {&char_ctype
, "char"},
848 {&schar_ctype
, "schar"},
849 {&uchar_ctype
, "uchar"},
850 {&short_ctype
, "short"},
851 {&sshort_ctype
, "sshort"},
852 {&ushort_ctype
, "ushort"},
854 {&sint_ctype
, "sint"},
855 {&uint_ctype
, "uint"},
856 {&long_ctype
, "long"},
857 {&slong_ctype
, "slong"},
858 {&ulong_ctype
, "ulong"},
859 {&llong_ctype
, "llong"},
860 {&sllong_ctype
, "sllong"},
861 {&ullong_ctype
, "ullong"},
862 {&int128_ctype
, "lllong"},
863 {&sint128_ctype
, "slllong"},
864 {&uint128_ctype
, "ulllong"},
865 {&float_ctype
, "float"},
866 {&double_ctype
, "double"},
867 {&ldouble_ctype
, "ldouble"},
868 {&string_ctype
, "string"},
870 {&lazy_ptr_ctype
, "lazy_ptr"},
871 {&incomplete_ctype
, "incomplete"},
872 {&label_ctype
, "label"},
874 {&null_ctype
, "null"},
877 static const char *base_type_str(struct symbol
*sym
)
881 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
882 if (sym
== base_types
[i
].sym
)
883 return base_types
[i
].name
;
888 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
893 return snprintf(buf
, size
, "<null type>");
895 if (type
->type
== SYM_BASETYPE
) {
896 return snprintf(buf
, size
, "%s", base_type_str(type
));
897 } else if (type
->type
== SYM_PTR
) {
898 type
= get_real_base_type(type
);
899 n
= type_str_helper(buf
, size
, type
);
902 return n
+ snprintf(buf
+ n
, size
- n
, "*");
903 } else if (type
->type
== SYM_ARRAY
) {
904 type
= get_real_base_type(type
);
905 n
= type_str_helper(buf
, size
, type
);
908 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
909 } else if (type
->type
== SYM_STRUCT
) {
910 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
911 } else if (type
->type
== SYM_UNION
) {
913 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
915 return snprintf(buf
, size
, "anonymous union");
916 } else if (type
->type
== SYM_FN
) {
917 struct symbol
*arg
, *return_type
, *arg_type
;
920 return_type
= get_real_base_type(type
);
921 n
= type_str_helper(buf
, size
, return_type
);
924 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
929 FOR_EACH_PTR(type
->arguments
, arg
) {
931 n
+= snprintf(buf
+ n
, size
- n
, ", ");
934 arg_type
= get_real_base_type(arg
);
935 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
938 } END_FOR_EACH_PTR(arg
);
940 return n
+ snprintf(buf
+ n
, size
- n
, ")");
941 } else if (type
->type
== SYM_NODE
) {
942 n
= snprintf(buf
, size
, "node {");
945 type
= get_real_base_type(type
);
946 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
949 return n
+ snprintf(buf
+ n
, size
- n
, "}");
950 } else if (type
->type
== SYM_ENUM
) {
951 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
953 return snprintf(buf
, size
, "<type %d>", type
->type
);
957 char *type_to_str(struct symbol
*type
)
959 static char buf
[256];
962 type_str_helper(buf
, sizeof(buf
), type
);
963 return alloc_sname(buf
);