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.
24 #include "smatch_slist.h"
26 struct symbol
*get_real_base_type(struct symbol
*sym
)
32 if (sym
->type
== SYM_BASETYPE
)
34 ret
= get_base_type(sym
);
37 if (ret
->type
== SYM_RESTRICT
|| ret
->type
== SYM_NODE
)
38 return get_real_base_type(ret
);
42 int type_bytes(struct symbol
*type
)
46 if (type
&& type
->type
== SYM_ARRAY
)
47 return array_bytes(type
);
49 bits
= type_bits(type
);
52 return bits_to_bytes(bits
);
55 int array_bytes(struct symbol
*type
)
57 if (!type
|| type
->type
!= SYM_ARRAY
)
59 return bits_to_bytes(type
->bit_size
);
62 static struct symbol
*get_binop_type(struct expression
*expr
)
64 struct symbol
*left
, *right
;
66 left
= get_type(expr
->left
);
70 if (expr
->op
== SPECIAL_LEFTSHIFT
||
71 expr
->op
== SPECIAL_RIGHTSHIFT
) {
72 if (type_positive_bits(left
) < 31)
76 right
= get_type(expr
->right
);
80 if (type_is_fp(left
)) {
81 if (type_is_fp(right
)) {
82 if (type_bits(left
) > type_bits(right
))
89 if (type_is_fp(right
)) {
90 if (type_is_fp(left
)) {
91 if (type_bits(right
) > type_bits(left
))
98 if (expr
->op
== '-' &&
99 (is_ptr_type(left
) && is_ptr_type(right
)))
100 return ssize_t_ctype
;
102 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
104 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
107 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
110 if (type_positive_bits(left
) > type_positive_bits(right
))
115 static struct symbol
*get_type_symbol(struct expression
*expr
)
117 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
120 return get_real_base_type(expr
->symbol
);
123 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
125 struct symbol
*tmp
, *sub
;
127 FOR_EACH_PTR(symbol_list
, tmp
) {
129 sub
= get_real_base_type(tmp
);
130 sub
= get_member_symbol(sub
->symbol_list
, member
);
135 if (tmp
->ident
== member
)
137 } END_FOR_EACH_PTR(tmp
);
142 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
144 struct ident
*member
;
147 if (!expr
|| expr
->type
!= EXPR_DEREF
)
150 member
= expr
->member
;
151 sym
= get_type(expr
->deref
);
153 // sm_msg("could not find struct type");
156 if (sym
->type
== SYM_PTR
)
157 sym
= get_real_base_type(sym
);
158 sym
= get_member_symbol(sym
->symbol_list
, member
);
161 return get_real_base_type(sym
);
164 static struct symbol
*handle__builtin_choose_expr(struct expression
*expr
)
166 struct expression
*const_expr
, *expr1
, *expr2
;
169 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
170 expr1
= get_argument_from_call_expr(expr
->args
, 1);
171 expr2
= get_argument_from_call_expr(expr
->args
, 2);
173 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
176 return get_type(expr1
);
178 return get_type(expr2
);
181 static struct symbol
*get_return_type(struct expression
*expr
)
185 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
186 return handle__builtin_choose_expr(expr
);
188 tmp
= get_type(expr
->fn
);
191 /* this is to handle __builtin_constant_p() */
192 if (tmp
->type
!= SYM_FN
)
193 tmp
= get_base_type(tmp
);
194 return get_real_base_type(tmp
);
197 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
199 if (stmt
->type
!= STMT_COMPOUND
)
201 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
202 if (stmt
->type
== STMT_LABEL
)
203 stmt
= stmt
->label_statement
;
204 if (stmt
->type
!= STMT_EXPRESSION
)
206 return get_type(stmt
->expression
);
209 static struct symbol
*get_select_type(struct expression
*expr
)
211 struct symbol
*one
, *two
;
213 one
= get_type(expr
->cond_true
);
214 two
= get_type(expr
->cond_false
);
218 * This is a hack. If the types are not equiv then we
219 * really don't know the type. But I think guessing is
222 if (type_positive_bits(one
) > type_positive_bits(two
))
227 struct symbol
*get_pointer_type(struct expression
*expr
)
231 sym
= get_type(expr
);
234 if (sym
->type
== SYM_NODE
) {
235 sym
= get_real_base_type(sym
);
239 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
241 return get_real_base_type(sym
);
244 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
249 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
251 base
= get_type(expr
);
254 sym
->ctype
.base_type
= base
;
258 static struct symbol
*get_type_helper(struct expression
*expr
)
262 expr
= strip_parens(expr
);
269 switch (expr
->type
) {
274 ret
= get_type_symbol(expr
);
277 ret
= get_symbol_from_deref(expr
);
282 ret
= fake_pointer_sym(expr
);
283 else if (expr
->op
== '*')
284 ret
= get_pointer_type(expr
->unop
);
286 ret
= get_type(expr
->unop
);
288 case EXPR_ASSIGNMENT
:
289 ret
= get_type(expr
->left
);
292 case EXPR_FORCE_CAST
:
293 case EXPR_IMPLIED_CAST
:
294 ret
= get_real_base_type(expr
->cast_type
);
298 ret
= get_binop_type(expr
);
301 ret
= get_return_type(expr
);
304 ret
= get_expr_stmt_type(expr
->statement
);
306 case EXPR_CONDITIONAL
:
308 ret
= get_select_type(expr
);
323 if (ret
&& ret
->type
== SYM_TYPEOF
)
324 ret
= get_type(ret
->initializer
);
330 static struct symbol
*get_final_type_helper(struct expression
*expr
)
333 * The problem is that I wrote a bunch of Smatch to think that
334 * you could do get_type() on an expression and it would give
335 * you what the comparison was type promoted to. This is wrong
336 * but fixing it is a big of work... Hence this horrible hack.
340 expr
= strip_parens(expr
);
344 if (expr
->type
== EXPR_COMPARE
)
350 struct symbol
*get_type(struct expression
*expr
)
352 return get_type_helper(expr
);
355 struct symbol
*get_final_type(struct expression
*expr
)
359 ret
= get_final_type_helper(expr
);
362 return get_type_helper(expr
);
365 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
367 struct symbol
*ret
= &int_ctype
;
369 if (type_positive_bits(left
) > type_positive_bits(ret
))
371 if (type_positive_bits(right
) > type_positive_bits(ret
))
374 if (type_is_ptr(left
))
376 if (type_is_ptr(right
))
382 int type_signed(struct symbol
*base_type
)
386 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
391 int expr_unsigned(struct expression
*expr
)
395 sym
= get_type(expr
);
398 if (type_unsigned(sym
))
403 int expr_signed(struct expression
*expr
)
407 sym
= get_type(expr
);
410 if (type_signed(sym
))
415 int returns_unsigned(struct symbol
*sym
)
419 sym
= get_base_type(sym
);
420 if (!sym
|| sym
->type
!= SYM_FN
)
422 sym
= get_base_type(sym
);
423 return type_unsigned(sym
);
426 int is_pointer(struct expression
*expr
)
428 return type_is_ptr(get_type(expr
));
431 int returns_pointer(struct symbol
*sym
)
435 sym
= get_base_type(sym
);
436 if (!sym
|| sym
->type
!= SYM_FN
)
438 sym
= get_base_type(sym
);
439 if (sym
&& sym
->type
== SYM_PTR
)
444 static sval_t
fp_max(struct symbol
*type
)
446 sval_t ret
= { .type
= type
};
448 if (type
== &float_ctype
)
449 ret
.fvalue
= FLT_MAX
;
450 else if (type
== &double_ctype
)
451 ret
.dvalue
= DBL_MAX
;
453 ret
.ldvalue
= LDBL_MAX
;
458 sval_t
sval_type_max(struct symbol
*base_type
)
462 if (type_is_fp(base_type
))
463 return fp_max(base_type
);
465 if (!base_type
|| !type_bits(base_type
))
466 base_type
= &llong_ctype
;
467 ret
.type
= base_type
;
469 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
473 static sval_t
fp_min(struct symbol
*type
)
475 sval_t ret
= { .type
= type
};
477 if (type
== &float_ctype
)
478 ret
.fvalue
= -FLT_MAX
;
479 else if (type
== &double_ctype
)
480 ret
.dvalue
= -DBL_MAX
;
482 ret
.ldvalue
= -LDBL_MAX
;
487 sval_t
sval_type_min(struct symbol
*base_type
)
491 if (type_is_fp(base_type
))
492 return fp_min(base_type
);
494 if (!base_type
|| !type_bits(base_type
))
495 base_type
= &llong_ctype
;
496 ret
.type
= base_type
;
498 if (type_unsigned(base_type
) || is_ptr_type(base_type
)) {
503 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
508 int nr_bits(struct expression
*expr
)
512 type
= get_type(expr
);
515 return type_bits(type
);
518 int is_void_pointer(struct expression
*expr
)
522 type
= get_type(expr
);
523 if (!type
|| type
->type
!= SYM_PTR
)
525 type
= get_real_base_type(type
);
526 if (type
== &void_ctype
)
531 int is_char_pointer(struct expression
*expr
)
535 type
= get_type(expr
);
536 if (!type
|| type
->type
!= SYM_PTR
)
538 type
= get_real_base_type(type
);
539 if (type
== &char_ctype
)
544 int is_string(struct expression
*expr
)
546 expr
= strip_expr(expr
);
547 if (!expr
|| expr
->type
!= EXPR_STRING
)
554 bool is_struct_ptr(struct symbol
*type
)
556 if (!type
|| type
->type
!= SYM_PTR
)
558 type
= get_real_base_type(type
);
559 if (!type
|| type
->type
!= SYM_STRUCT
)
564 int is_static(struct expression
*expr
)
570 name
= expr_to_str_sym(expr
, &sym
);
574 if (sym
->ctype
.modifiers
& MOD_STATIC
)
581 static struct expression
*get_symbol_expr(struct expression
*expr
)
585 while (expr
&& expr
->type
== EXPR_DEREF
&& expr
->op
== '.')
586 expr
= strip_expr(expr
->deref
);
590 bool is_local_variable(struct expression
*expr
)
594 expr
= get_symbol_expr(expr
);
595 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
598 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
603 int types_equiv(struct symbol
*one
, struct symbol
*two
)
609 if (one
->type
!= two
->type
)
611 if (one
->type
== SYM_PTR
)
612 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
613 if (type_positive_bits(one
) != type_positive_bits(two
))
618 bool type_fits(struct symbol
*type
, struct symbol
*test
)
626 if (type_bits(test
) > type_bits(type
))
628 if (type_signed(test
) && !type_signed(type
))
630 if (type_positive_bits(test
) > type_positive_bits(type
))
637 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
640 const char *global_static(void)
642 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
648 struct symbol
*cur_func_return_type(void)
652 sym
= get_real_base_type(cur_func_sym
);
653 if (!sym
|| sym
->type
!= SYM_FN
)
655 sym
= get_real_base_type(sym
);
659 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
661 struct symbol
*fn_type
;
663 struct symbol
*arg_type
;
666 fn_type
= get_type(fn
);
669 if (fn_type
->type
== SYM_PTR
)
670 fn_type
= get_real_base_type(fn_type
);
671 if (fn_type
->type
!= SYM_FN
)
675 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
676 arg_type
= get_real_base_type(tmp
);
681 } END_FOR_EACH_PTR(tmp
);
686 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
688 struct symbol
*tmp
, *sub
;
691 if (strncmp(name
, ".", 1) == 0)
693 else if (strncmp(name
, "->", 2) == 0)
696 FOR_EACH_PTR(symbol_list
, tmp
) {
698 sub
= get_real_base_type(tmp
);
699 sub
= get_member_from_string(sub
->symbol_list
, name
);
705 if (strcmp(tmp
->ident
->name
, name
) == 0)
708 chunk_len
= tmp
->ident
->len
;
709 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
710 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
711 sub
= get_real_base_type(tmp
);
712 if (sub
->type
== SYM_PTR
)
713 sub
= get_real_base_type(sub
);
714 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
717 } END_FOR_EACH_PTR(tmp
);
722 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
728 if (strcmp(key
, "$") == 0)
729 return get_type(expr
);
731 if (strcmp(key
, "*$") == 0) {
732 sym
= get_type(expr
);
733 if (!sym
|| sym
->type
!= SYM_PTR
)
735 return get_real_base_type(sym
);
738 sym
= get_type(expr
);
741 if (sym
->type
== SYM_PTR
)
742 sym
= get_real_base_type(sym
);
744 while (*key
== '*') {
753 sym
= get_member_from_string(sym
->symbol_list
, key
);
756 if (sym
->type
== SYM_RESTRICT
|| sym
->type
== SYM_NODE
)
757 sym
= get_real_base_type(sym
);
758 for (i
= 0; i
< star
; i
++) {
759 if (!sym
|| sym
->type
!= SYM_PTR
)
761 sym
= get_real_base_type(sym
);
766 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
772 if (strcmp(key
, "$") == 0)
773 return get_arg_type(fn
, param
);
774 if (strcmp(key
, "*$") == 0) {
775 type
= get_arg_type(fn
, param
);
776 if (!type
|| type
->type
!= SYM_PTR
)
778 return get_real_base_type(type
);
780 return get_member_type_from_key(arg
, key
);
783 int is_struct(struct expression
*expr
)
787 type
= get_type(expr
);
788 if (type
&& type
->type
== SYM_STRUCT
)
797 {&bool_ctype
, "bool"},
798 {&void_ctype
, "void"},
799 {&type_ctype
, "type"},
800 {&char_ctype
, "char"},
801 {&schar_ctype
, "schar"},
802 {&uchar_ctype
, "uchar"},
803 {&short_ctype
, "short"},
804 {&sshort_ctype
, "sshort"},
805 {&ushort_ctype
, "ushort"},
807 {&sint_ctype
, "sint"},
808 {&uint_ctype
, "uint"},
809 {&long_ctype
, "long"},
810 {&slong_ctype
, "slong"},
811 {&ulong_ctype
, "ulong"},
812 {&llong_ctype
, "llong"},
813 {&sllong_ctype
, "sllong"},
814 {&ullong_ctype
, "ullong"},
815 {&lllong_ctype
, "lllong"},
816 {&slllong_ctype
, "slllong"},
817 {&ulllong_ctype
, "ulllong"},
818 {&float_ctype
, "float"},
819 {&double_ctype
, "double"},
820 {&ldouble_ctype
, "ldouble"},
821 {&string_ctype
, "string"},
823 {&lazy_ptr_ctype
, "lazy_ptr"},
824 {&incomplete_ctype
, "incomplete"},
825 {&label_ctype
, "label"},
827 {&null_ctype
, "null"},
830 static const char *base_type_str(struct symbol
*sym
)
834 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
835 if (sym
== base_types
[i
].sym
)
836 return base_types
[i
].name
;
841 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
846 return snprintf(buf
, size
, "<null type>");
848 if (type
->type
== SYM_BASETYPE
) {
849 return snprintf(buf
, size
, "%s", base_type_str(type
));
850 } else if (type
->type
== SYM_PTR
) {
851 type
= get_real_base_type(type
);
852 n
= type_str_helper(buf
, size
, type
);
855 return n
+ snprintf(buf
+ n
, size
- n
, "*");
856 } else if (type
->type
== SYM_ARRAY
) {
857 type
= get_real_base_type(type
);
858 n
= type_str_helper(buf
, size
, type
);
861 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
862 } else if (type
->type
== SYM_STRUCT
) {
863 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
864 } else if (type
->type
== SYM_UNION
) {
866 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
868 return snprintf(buf
, size
, "anonymous union");
869 } else if (type
->type
== SYM_FN
) {
870 struct symbol
*arg
, *return_type
, *arg_type
;
873 return_type
= get_real_base_type(type
);
874 n
= type_str_helper(buf
, size
, return_type
);
877 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
882 FOR_EACH_PTR(type
->arguments
, arg
) {
884 n
+= snprintf(buf
+ n
, size
- n
, ", ");
887 arg_type
= get_real_base_type(arg
);
888 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
891 } END_FOR_EACH_PTR(arg
);
893 return n
+ snprintf(buf
+ n
, size
- n
, ")");
894 } else if (type
->type
== SYM_NODE
) {
895 n
= snprintf(buf
, size
, "node {");
898 type
= get_real_base_type(type
);
899 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
902 return n
+ snprintf(buf
+ n
, size
- n
, "}");
903 } else if (type
->type
== SYM_ENUM
) {
904 return snprintf(buf
, size
, "enum %s", type
->ident
? type
->ident
->name
: "<unknown>");
906 return snprintf(buf
, size
, "<type %d>", type
->type
);
910 char *type_to_str(struct symbol
*type
)
912 static char buf
[256];
915 type_str_helper(buf
, sizeof(buf
), type
);