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 (expr
->op
== '-' &&
81 (is_ptr_type(left
) && is_ptr_type(right
)))
84 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
86 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
89 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
92 if (type_positive_bits(left
) > type_positive_bits(right
))
97 static struct symbol
*get_type_symbol(struct expression
*expr
)
99 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
102 return get_real_base_type(expr
->symbol
);
105 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
107 struct symbol
*tmp
, *sub
;
109 FOR_EACH_PTR(symbol_list
, tmp
) {
111 sub
= get_real_base_type(tmp
);
112 sub
= get_member_symbol(sub
->symbol_list
, member
);
117 if (tmp
->ident
== member
)
119 } END_FOR_EACH_PTR(tmp
);
124 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
126 struct ident
*member
;
129 if (!expr
|| expr
->type
!= EXPR_DEREF
)
132 member
= expr
->member
;
133 sym
= get_type(expr
->deref
);
135 // sm_msg("could not find struct type");
138 if (sym
->type
== SYM_PTR
)
139 sym
= get_real_base_type(sym
);
140 sym
= get_member_symbol(sym
->symbol_list
, member
);
143 return get_real_base_type(sym
);
146 static struct symbol
*get_return_type(struct expression
*expr
)
150 tmp
= get_type(expr
->fn
);
153 /* this is to handle __builtin_constant_p() */
154 if (tmp
->type
!= SYM_FN
)
155 tmp
= get_base_type(tmp
);
156 return get_real_base_type(tmp
);
159 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
161 if (stmt
->type
!= STMT_COMPOUND
)
163 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
164 if (stmt
->type
== STMT_LABEL
)
165 stmt
= stmt
->label_statement
;
166 if (stmt
->type
!= STMT_EXPRESSION
)
168 return get_type(stmt
->expression
);
171 static struct symbol
*get_select_type(struct expression
*expr
)
173 struct symbol
*one
, *two
;
175 one
= get_type(expr
->cond_true
);
176 two
= get_type(expr
->cond_false
);
180 * This is a hack. If the types are not equiv then we
181 * really don't know the type. But I think guessing is
184 if (type_positive_bits(one
) > type_positive_bits(two
))
189 struct symbol
*get_pointer_type(struct expression
*expr
)
193 sym
= get_type(expr
);
196 if (sym
->type
== SYM_NODE
) {
197 sym
= get_real_base_type(sym
);
201 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
203 return get_real_base_type(sym
);
206 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
211 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
213 base
= get_type(expr
);
216 sym
->ctype
.base_type
= base
;
220 static struct symbol
*get_type_helper(struct expression
*expr
)
224 expr
= strip_parens(expr
);
231 switch (expr
->type
) {
236 ret
= get_type_symbol(expr
);
239 ret
= get_symbol_from_deref(expr
);
244 ret
= fake_pointer_sym(expr
);
245 else if (expr
->op
== '*')
246 ret
= get_pointer_type(expr
->unop
);
248 ret
= get_type(expr
->unop
);
250 case EXPR_ASSIGNMENT
:
251 ret
= get_type(expr
->left
);
254 case EXPR_FORCE_CAST
:
255 case EXPR_IMPLIED_CAST
:
256 ret
= get_real_base_type(expr
->cast_type
);
260 ret
= get_binop_type(expr
);
263 ret
= get_return_type(expr
);
266 ret
= get_expr_stmt_type(expr
->statement
);
268 case EXPR_CONDITIONAL
:
270 ret
= get_select_type(expr
);
282 if (ret
&& ret
->type
== SYM_TYPEOF
)
283 ret
= get_type(ret
->initializer
);
289 static struct symbol
*get_final_type_helper(struct expression
*expr
)
292 * The problem is that I wrote a bunch of Smatch to think that
293 * you could do get_type() on an expression and it would give
294 * you what the comparison was type promoted to. This is wrong
295 * but fixing it is a big of work... Hence this horrible hack.
299 expr
= strip_parens(expr
);
303 if (expr
->type
== EXPR_COMPARE
)
309 struct symbol
*get_type(struct expression
*expr
)
311 return get_type_helper(expr
);
314 struct symbol
*get_final_type(struct expression
*expr
)
318 ret
= get_final_type_helper(expr
);
321 return get_type_helper(expr
);
324 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
326 struct symbol
*ret
= &int_ctype
;
328 if (type_positive_bits(left
) > type_positive_bits(ret
))
330 if (type_positive_bits(right
) > type_positive_bits(ret
))
333 if (type_is_ptr(left
))
335 if (type_is_ptr(right
))
341 int type_signed(struct symbol
*base_type
)
345 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
350 int expr_unsigned(struct expression
*expr
)
354 sym
= get_type(expr
);
357 if (type_unsigned(sym
))
362 int expr_signed(struct expression
*expr
)
366 sym
= get_type(expr
);
369 if (type_signed(sym
))
374 int returns_unsigned(struct symbol
*sym
)
378 sym
= get_base_type(sym
);
379 if (!sym
|| sym
->type
!= SYM_FN
)
381 sym
= get_base_type(sym
);
382 return type_unsigned(sym
);
385 int is_pointer(struct expression
*expr
)
389 sym
= get_type(expr
);
392 if (sym
== &string_ctype
)
394 if (sym
->type
== SYM_PTR
)
399 int returns_pointer(struct symbol
*sym
)
403 sym
= get_base_type(sym
);
404 if (!sym
|| sym
->type
!= SYM_FN
)
406 sym
= get_base_type(sym
);
407 if (sym
->type
== SYM_PTR
)
412 sval_t
sval_type_max(struct symbol
*base_type
)
416 if (!base_type
|| !type_bits(base_type
))
417 base_type
= &llong_ctype
;
418 ret
.type
= base_type
;
420 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
424 sval_t
sval_type_min(struct symbol
*base_type
)
428 if (!base_type
|| !type_bits(base_type
))
429 base_type
= &llong_ctype
;
430 ret
.type
= base_type
;
432 if (type_unsigned(base_type
)) {
437 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
442 int nr_bits(struct expression
*expr
)
446 type
= get_type(expr
);
449 return type_bits(type
);
452 int is_void_pointer(struct expression
*expr
)
456 type
= get_type(expr
);
457 if (!type
|| type
->type
!= SYM_PTR
)
459 type
= get_real_base_type(type
);
460 if (type
== &void_ctype
)
465 int is_char_pointer(struct expression
*expr
)
469 type
= get_type(expr
);
470 if (!type
|| type
->type
!= SYM_PTR
)
472 type
= get_real_base_type(type
);
473 if (type
== &char_ctype
)
478 int is_string(struct expression
*expr
)
480 expr
= strip_expr(expr
);
481 if (!expr
|| expr
->type
!= EXPR_STRING
)
488 int is_static(struct expression
*expr
)
494 name
= expr_to_str_sym(expr
, &sym
);
498 if (sym
->ctype
.modifiers
& MOD_STATIC
)
505 int is_local_variable(struct expression
*expr
)
510 name
= expr_to_var_sym(expr
, &sym
);
512 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
|| !cur_func_sym
)
514 if (cmp_pos(sym
->scope
->token
->pos
, cur_func_sym
->pos
) < 0)
521 int types_equiv(struct symbol
*one
, struct symbol
*two
)
527 if (one
->type
!= two
->type
)
529 if (one
->type
== SYM_PTR
)
530 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
531 if (type_positive_bits(one
) != type_positive_bits(two
))
538 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
541 const char *global_static(void)
543 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
549 struct symbol
*cur_func_return_type(void)
553 sym
= get_real_base_type(cur_func_sym
);
554 if (!sym
|| sym
->type
!= SYM_FN
)
556 sym
= get_real_base_type(sym
);
560 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
562 struct symbol
*fn_type
;
564 struct symbol
*arg_type
;
567 fn_type
= get_type(fn
);
570 if (fn_type
->type
== SYM_PTR
)
571 fn_type
= get_real_base_type(fn_type
);
572 if (fn_type
->type
!= SYM_FN
)
576 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
577 arg_type
= get_real_base_type(tmp
);
582 } END_FOR_EACH_PTR(tmp
);
587 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
589 struct symbol
*tmp
, *sub
;
592 if (strncmp(name
, ".", 1) == 0)
594 else if (strncmp(name
, "->", 2) == 0)
597 FOR_EACH_PTR(symbol_list
, tmp
) {
599 sub
= get_real_base_type(tmp
);
600 sub
= get_member_from_string(sub
->symbol_list
, name
);
606 if (strcmp(tmp
->ident
->name
, name
) == 0)
609 chunk_len
= tmp
->ident
->len
;
610 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
611 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
612 sub
= get_real_base_type(tmp
);
613 if (sub
->type
== SYM_PTR
)
614 sub
= get_real_base_type(sub
);
615 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
618 } END_FOR_EACH_PTR(tmp
);
623 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
627 if (strcmp(key
, "$") == 0)
628 return get_type(expr
);
630 if (strcmp(key
, "*$") == 0) {
631 sym
= get_type(expr
);
632 if (!sym
|| sym
->type
!= SYM_PTR
)
634 return get_real_base_type(sym
);
637 sym
= get_type(expr
);
640 if (sym
->type
== SYM_PTR
)
641 sym
= get_real_base_type(sym
);
644 sym
= get_member_from_string(sym
->symbol_list
, key
);
647 return get_real_base_type(sym
);
650 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
656 if (strcmp(key
, "$") == 0)
657 return get_arg_type(fn
, param
);
658 if (strcmp(key
, "*$") == 0) {
659 type
= get_arg_type(fn
, param
);
660 if (!type
|| type
->type
!= SYM_PTR
)
662 return get_real_base_type(type
);
664 return get_member_type_from_key(arg
, key
);
667 int is_struct(struct expression
*expr
)
671 type
= get_type(expr
);
672 if (type
&& type
->type
== SYM_STRUCT
)
681 {&bool_ctype
, "bool"},
682 {&void_ctype
, "void"},
683 {&type_ctype
, "type"},
684 {&char_ctype
, "char"},
685 {&schar_ctype
, "schar"},
686 {&uchar_ctype
, "uchar"},
687 {&short_ctype
, "short"},
688 {&sshort_ctype
, "sshort"},
689 {&ushort_ctype
, "ushort"},
691 {&sint_ctype
, "sint"},
692 {&uint_ctype
, "uint"},
693 {&long_ctype
, "long"},
694 {&slong_ctype
, "slong"},
695 {&ulong_ctype
, "ulong"},
696 {&llong_ctype
, "llong"},
697 {&sllong_ctype
, "sllong"},
698 {&ullong_ctype
, "ullong"},
699 {&lllong_ctype
, "lllong"},
700 {&slllong_ctype
, "slllong"},
701 {&ulllong_ctype
, "ulllong"},
702 {&float_ctype
, "float"},
703 {&double_ctype
, "double"},
704 {&ldouble_ctype
, "ldouble"},
705 {&string_ctype
, "string"},
707 {&lazy_ptr_ctype
, "lazy_ptr"},
708 {&incomplete_ctype
, "incomplete"},
709 {&label_ctype
, "label"},
711 {&null_ctype
, "null"},
714 static const char *base_type_str(struct symbol
*sym
)
718 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
719 if (sym
== base_types
[i
].sym
)
720 return base_types
[i
].name
;
725 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
730 return snprintf(buf
, size
, "<unknown>");
732 if (type
->type
== SYM_BASETYPE
) {
733 return snprintf(buf
, size
, "%s", base_type_str(type
));
734 } else if (type
->type
== SYM_PTR
) {
735 type
= get_real_base_type(type
);
736 n
= type_str_helper(buf
, size
, type
);
739 return n
+ snprintf(buf
+ n
, size
- n
, "*");
740 } else if (type
->type
== SYM_ARRAY
) {
741 type
= get_real_base_type(type
);
742 n
= type_str_helper(buf
, size
, type
);
745 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
746 } else if (type
->type
== SYM_STRUCT
) {
747 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
748 } else if (type
->type
== SYM_UNION
) {
750 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
752 return snprintf(buf
, size
, "anonymous union");
753 } else if (type
->type
== SYM_FN
) {
754 struct symbol
*arg
, *return_type
, *arg_type
;
757 return_type
= get_real_base_type(type
);
758 n
= type_str_helper(buf
, size
, return_type
);
761 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
766 FOR_EACH_PTR(type
->arguments
, arg
) {
768 n
+= snprintf(buf
+ n
, size
- n
, ", ");
771 arg_type
= get_real_base_type(arg
);
772 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
775 } END_FOR_EACH_PTR(arg
);
777 return n
+ snprintf(buf
+ n
, size
- n
, ")");
778 } else if (type
->type
== SYM_NODE
) {
779 n
= snprintf(buf
, size
, "node {");
782 type
= get_real_base_type(type
);
783 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
786 return n
+ snprintf(buf
+ n
, size
- n
, "}");
788 return snprintf(buf
, size
, "<type %d>", type
->type
);
792 char *type_to_str(struct symbol
*type
)
794 static char buf
[256];
797 type_str_helper(buf
, sizeof(buf
), type
);