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 (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
82 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
85 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
88 if (type_positive_bits(left
) > type_positive_bits(right
))
93 static struct symbol
*get_type_symbol(struct expression
*expr
)
95 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
98 return get_real_base_type(expr
->symbol
);
101 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
103 struct symbol
*tmp
, *sub
;
105 FOR_EACH_PTR(symbol_list
, tmp
) {
107 sub
= get_real_base_type(tmp
);
108 sub
= get_member_symbol(sub
->symbol_list
, member
);
113 if (tmp
->ident
== member
)
115 } END_FOR_EACH_PTR(tmp
);
120 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
122 struct ident
*member
;
125 if (!expr
|| expr
->type
!= EXPR_DEREF
)
128 member
= expr
->member
;
129 sym
= get_type(expr
->deref
);
131 // sm_msg("could not find struct type");
134 if (sym
->type
== SYM_PTR
)
135 sym
= get_real_base_type(sym
);
136 sym
= get_member_symbol(sym
->symbol_list
, member
);
139 return get_real_base_type(sym
);
142 static struct symbol
*get_return_type(struct expression
*expr
)
146 tmp
= get_type(expr
->fn
);
149 /* this is to handle __builtin_constant_p() */
150 if (tmp
->type
!= SYM_FN
)
151 tmp
= get_base_type(tmp
);
152 return get_real_base_type(tmp
);
155 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
157 if (stmt
->type
!= STMT_COMPOUND
)
159 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
160 if (stmt
->type
== STMT_LABEL
)
161 stmt
= stmt
->label_statement
;
162 if (stmt
->type
!= STMT_EXPRESSION
)
164 return get_type(stmt
->expression
);
167 static struct symbol
*get_select_type(struct expression
*expr
)
169 struct symbol
*one
, *two
;
171 one
= get_type(expr
->cond_true
);
172 two
= get_type(expr
->cond_false
);
176 * This is a hack. If the types are not equiv then we
177 * really don't know the type. But I think guessing is
180 if (type_positive_bits(one
) > type_positive_bits(two
))
185 struct symbol
*get_pointer_type(struct expression
*expr
)
189 sym
= get_type(expr
);
192 if (sym
->type
== SYM_NODE
) {
193 sym
= get_real_base_type(sym
);
197 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
199 return get_real_base_type(sym
);
202 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
207 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
209 base
= get_type(expr
);
212 sym
->ctype
.base_type
= base
;
216 static struct symbol
*get_type_helper(struct expression
*expr
)
220 expr
= strip_parens(expr
);
227 switch (expr
->type
) {
232 ret
= get_type_symbol(expr
);
235 ret
= get_symbol_from_deref(expr
);
240 ret
= fake_pointer_sym(expr
);
241 else if (expr
->op
== '*')
242 ret
= get_pointer_type(expr
->unop
);
244 ret
= get_type(expr
->unop
);
246 case EXPR_ASSIGNMENT
:
247 ret
= get_type(expr
->left
);
250 case EXPR_FORCE_CAST
:
251 case EXPR_IMPLIED_CAST
:
252 ret
= get_real_base_type(expr
->cast_type
);
256 ret
= get_binop_type(expr
);
259 ret
= get_return_type(expr
);
262 ret
= get_expr_stmt_type(expr
->statement
);
264 case EXPR_CONDITIONAL
:
266 ret
= get_select_type(expr
);
278 if (ret
&& ret
->type
== SYM_TYPEOF
)
279 ret
= get_type(ret
->initializer
);
285 static struct symbol
*get_final_type_helper(struct expression
*expr
)
288 * I'm not totally positive I understand types...
290 * So, when you're doing pointer math, and you do a subtraction, then
291 * the sval_binop() and whatever need to know the type of the pointer
292 * so they can figure out the alignment. But the result is going to be
293 * and ssize_t. So get_operation_type() gives you the pointer type
294 * and get_type() gives you ssize_t.
296 * Most of the time the operation type and the final type are the same
297 * but this just handles the few places where they are different.
301 expr
= strip_parens(expr
);
305 switch (expr
->type
) {
309 struct symbol
*left
, *right
;
314 left
= get_type(expr
->left
);
315 right
= get_type(expr
->right
);
316 if (type_is_ptr(left
) || type_is_ptr(right
))
317 return ssize_t_ctype
;
324 struct symbol
*get_type(struct expression
*expr
)
326 return get_type_helper(expr
);
329 struct symbol
*get_final_type(struct expression
*expr
)
333 ret
= get_final_type_helper(expr
);
336 return get_type_helper(expr
);
339 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
341 struct symbol
*ret
= &int_ctype
;
343 if (type_positive_bits(left
) > type_positive_bits(ret
))
345 if (type_positive_bits(right
) > type_positive_bits(ret
))
348 if (type_is_ptr(left
))
350 if (type_is_ptr(right
))
356 int type_signed(struct symbol
*base_type
)
360 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
365 int expr_unsigned(struct expression
*expr
)
369 sym
= get_type(expr
);
372 if (type_unsigned(sym
))
377 int expr_signed(struct expression
*expr
)
381 sym
= get_type(expr
);
384 if (type_signed(sym
))
389 int returns_unsigned(struct symbol
*sym
)
393 sym
= get_base_type(sym
);
394 if (!sym
|| sym
->type
!= SYM_FN
)
396 sym
= get_base_type(sym
);
397 return type_unsigned(sym
);
400 int is_pointer(struct expression
*expr
)
404 sym
= get_type(expr
);
407 if (sym
== &string_ctype
)
409 if (sym
->type
== SYM_PTR
)
414 int returns_pointer(struct symbol
*sym
)
418 sym
= get_base_type(sym
);
419 if (!sym
|| sym
->type
!= SYM_FN
)
421 sym
= get_base_type(sym
);
422 if (sym
->type
== SYM_PTR
)
427 sval_t
sval_type_max(struct symbol
*base_type
)
431 if (!base_type
|| !type_bits(base_type
))
432 base_type
= &llong_ctype
;
433 ret
.type
= base_type
;
435 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
439 sval_t
sval_type_min(struct symbol
*base_type
)
443 if (!base_type
|| !type_bits(base_type
))
444 base_type
= &llong_ctype
;
445 ret
.type
= base_type
;
447 if (type_unsigned(base_type
)) {
452 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
457 int nr_bits(struct expression
*expr
)
461 type
= get_type(expr
);
464 return type_bits(type
);
467 int is_void_pointer(struct expression
*expr
)
471 type
= get_type(expr
);
472 if (!type
|| type
->type
!= SYM_PTR
)
474 type
= get_real_base_type(type
);
475 if (type
== &void_ctype
)
480 int is_char_pointer(struct expression
*expr
)
484 type
= get_type(expr
);
485 if (!type
|| type
->type
!= SYM_PTR
)
487 type
= get_real_base_type(type
);
488 if (type
== &char_ctype
)
493 int is_string(struct expression
*expr
)
495 expr
= strip_expr(expr
);
496 if (!expr
|| expr
->type
!= EXPR_STRING
)
503 int is_static(struct expression
*expr
)
509 name
= expr_to_str_sym(expr
, &sym
);
513 if (sym
->ctype
.modifiers
& MOD_STATIC
)
520 int is_local_variable(struct expression
*expr
)
525 name
= expr_to_var_sym(expr
, &sym
);
527 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
|| !cur_func_sym
)
529 if (cmp_pos(sym
->scope
->token
->pos
, cur_func_sym
->pos
) < 0)
536 int types_equiv(struct symbol
*one
, struct symbol
*two
)
542 if (one
->type
!= two
->type
)
544 if (one
->type
== SYM_PTR
)
545 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
546 if (type_positive_bits(one
) != type_positive_bits(two
))
553 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
556 const char *global_static(void)
558 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
564 struct symbol
*cur_func_return_type(void)
568 sym
= get_real_base_type(cur_func_sym
);
569 if (!sym
|| sym
->type
!= SYM_FN
)
571 sym
= get_real_base_type(sym
);
575 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
577 struct symbol
*fn_type
;
579 struct symbol
*arg_type
;
582 fn_type
= get_type(fn
);
585 if (fn_type
->type
== SYM_PTR
)
586 fn_type
= get_real_base_type(fn_type
);
587 if (fn_type
->type
!= SYM_FN
)
591 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
592 arg_type
= get_real_base_type(tmp
);
597 } END_FOR_EACH_PTR(tmp
);
602 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
604 struct symbol
*tmp
, *sub
;
607 if (strncmp(name
, ".", 1) == 0)
609 else if (strncmp(name
, "->", 2) == 0)
612 FOR_EACH_PTR(symbol_list
, tmp
) {
614 sub
= get_real_base_type(tmp
);
615 sub
= get_member_from_string(sub
->symbol_list
, name
);
621 if (strcmp(tmp
->ident
->name
, name
) == 0)
624 chunk_len
= tmp
->ident
->len
;
625 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
626 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
627 sub
= get_real_base_type(tmp
);
628 if (sub
->type
== SYM_PTR
)
629 sub
= get_real_base_type(sub
);
630 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
633 } END_FOR_EACH_PTR(tmp
);
638 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
642 if (strcmp(key
, "$") == 0)
643 return get_type(expr
);
645 if (strcmp(key
, "*$") == 0) {
646 sym
= get_type(expr
);
647 if (!sym
|| sym
->type
!= SYM_PTR
)
649 return get_real_base_type(sym
);
652 sym
= get_type(expr
);
655 if (sym
->type
== SYM_PTR
)
656 sym
= get_real_base_type(sym
);
659 sym
= get_member_from_string(sym
->symbol_list
, key
);
662 return get_real_base_type(sym
);
665 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
671 if (strcmp(key
, "$") == 0)
672 return get_arg_type(fn
, param
);
673 if (strcmp(key
, "*$") == 0) {
674 type
= get_arg_type(fn
, param
);
675 if (!type
|| type
->type
!= SYM_PTR
)
677 return get_real_base_type(type
);
679 return get_member_type_from_key(arg
, key
);
682 int is_struct(struct expression
*expr
)
686 type
= get_type(expr
);
687 if (type
&& type
->type
== SYM_STRUCT
)
696 {&bool_ctype
, "bool"},
697 {&void_ctype
, "void"},
698 {&type_ctype
, "type"},
699 {&char_ctype
, "char"},
700 {&schar_ctype
, "schar"},
701 {&uchar_ctype
, "uchar"},
702 {&short_ctype
, "short"},
703 {&sshort_ctype
, "sshort"},
704 {&ushort_ctype
, "ushort"},
706 {&sint_ctype
, "sint"},
707 {&uint_ctype
, "uint"},
708 {&long_ctype
, "long"},
709 {&slong_ctype
, "slong"},
710 {&ulong_ctype
, "ulong"},
711 {&llong_ctype
, "llong"},
712 {&sllong_ctype
, "sllong"},
713 {&ullong_ctype
, "ullong"},
714 {&lllong_ctype
, "lllong"},
715 {&slllong_ctype
, "slllong"},
716 {&ulllong_ctype
, "ulllong"},
717 {&float_ctype
, "float"},
718 {&double_ctype
, "double"},
719 {&ldouble_ctype
, "ldouble"},
720 {&string_ctype
, "string"},
722 {&lazy_ptr_ctype
, "lazy_ptr"},
723 {&incomplete_ctype
, "incomplete"},
724 {&label_ctype
, "label"},
726 {&null_ctype
, "null"},
729 static const char *base_type_str(struct symbol
*sym
)
733 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
734 if (sym
== base_types
[i
].sym
)
735 return base_types
[i
].name
;
740 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
745 return snprintf(buf
, size
, "<unknown>");
747 if (type
->type
== SYM_BASETYPE
) {
748 return snprintf(buf
, size
, "%s", base_type_str(type
));
749 } else if (type
->type
== SYM_PTR
) {
750 type
= get_real_base_type(type
);
751 n
= type_str_helper(buf
, size
, type
);
754 return n
+ snprintf(buf
+ n
, size
- n
, "*");
755 } else if (type
->type
== SYM_ARRAY
) {
756 type
= get_real_base_type(type
);
757 n
= type_str_helper(buf
, size
, type
);
760 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
761 } else if (type
->type
== SYM_STRUCT
) {
762 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
763 } else if (type
->type
== SYM_UNION
) {
765 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
767 return snprintf(buf
, size
, "anonymous union");
768 } else if (type
->type
== SYM_FN
) {
769 struct symbol
*arg
, *return_type
, *arg_type
;
772 return_type
= get_real_base_type(type
);
773 n
= type_str_helper(buf
, size
, return_type
);
776 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
781 FOR_EACH_PTR(type
->arguments
, arg
) {
783 n
+= snprintf(buf
+ n
, size
- n
, ", ");
786 arg_type
= get_real_base_type(arg
);
787 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
790 } END_FOR_EACH_PTR(arg
);
792 return n
+ snprintf(buf
+ n
, size
- n
, ")");
793 } else if (type
->type
== SYM_NODE
) {
794 n
= snprintf(buf
, size
, "node {");
797 type
= get_real_base_type(type
);
798 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
801 return n
+ snprintf(buf
+ n
, size
- n
, "}");
803 return snprintf(buf
, size
, "<type %d>", type
->type
);
807 char *type_to_str(struct symbol
*type
)
809 static char buf
[256];
812 type_str_helper(buf
, sizeof(buf
), type
);