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 ret
= get_base_type(sym
);
35 if (ret
->type
== SYM_RESTRICT
|| ret
->type
== SYM_NODE
)
36 return get_real_base_type(ret
);
40 int type_bytes(struct symbol
*type
)
44 if (type
&& type
->type
== SYM_ARRAY
)
45 return array_bytes(type
);
47 bits
= type_bits(type
);
50 return bits_to_bytes(bits
);
53 int array_bytes(struct symbol
*type
)
55 if (!type
|| type
->type
!= SYM_ARRAY
)
57 return bits_to_bytes(type
->bit_size
);
60 static struct symbol
*get_binop_type(struct expression
*expr
)
62 struct symbol
*left
, *right
;
64 left
= get_type(expr
->left
);
68 if (expr
->op
== SPECIAL_LEFTSHIFT
||
69 expr
->op
== SPECIAL_RIGHTSHIFT
) {
70 if (type_positive_bits(left
) < 31)
74 right
= get_type(expr
->right
);
78 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
80 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
83 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
86 if (type_positive_bits(left
) > type_positive_bits(right
))
91 static struct symbol
*get_type_symbol(struct expression
*expr
)
93 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
96 return get_real_base_type(expr
->symbol
);
99 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
101 struct symbol
*tmp
, *sub
;
103 FOR_EACH_PTR(symbol_list
, tmp
) {
105 sub
= get_real_base_type(tmp
);
106 sub
= get_member_symbol(sub
->symbol_list
, member
);
111 if (tmp
->ident
== member
)
113 } END_FOR_EACH_PTR(tmp
);
118 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
120 struct ident
*member
;
123 if (!expr
|| expr
->type
!= EXPR_DEREF
)
126 member
= expr
->member
;
127 sym
= get_type(expr
->deref
);
129 // sm_msg("could not find struct type");
132 if (sym
->type
== SYM_PTR
)
133 sym
= get_real_base_type(sym
);
134 sym
= get_member_symbol(sym
->symbol_list
, member
);
137 return get_real_base_type(sym
);
140 static struct symbol
*get_return_type(struct expression
*expr
)
144 tmp
= get_type(expr
->fn
);
147 /* this is to handle __builtin_constant_p() */
148 if (tmp
->type
!= SYM_FN
)
149 tmp
= get_base_type(tmp
);
150 return get_real_base_type(tmp
);
153 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
155 if (stmt
->type
!= STMT_COMPOUND
)
157 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
158 if (stmt
->type
== STMT_LABEL
)
159 stmt
= stmt
->label_statement
;
160 if (stmt
->type
!= STMT_EXPRESSION
)
162 return get_type(stmt
->expression
);
165 static struct symbol
*get_select_type(struct expression
*expr
)
167 struct symbol
*one
, *two
;
169 one
= get_type(expr
->cond_true
);
170 two
= get_type(expr
->cond_false
);
174 * This is a hack. If the types are not equiv then we
175 * really don't know the type. But I think guessing is
178 if (type_positive_bits(one
) > type_positive_bits(two
))
183 struct symbol
*get_pointer_type(struct expression
*expr
)
187 sym
= get_type(expr
);
190 if (sym
->type
== SYM_NODE
) {
191 sym
= get_real_base_type(sym
);
195 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
197 return get_real_base_type(sym
);
200 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
205 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
207 base
= get_type(expr
);
210 sym
->ctype
.base_type
= base
;
214 static struct symbol
*get_type_helper(struct expression
*expr
)
218 expr
= strip_parens(expr
);
225 switch (expr
->type
) {
230 ret
= get_type_symbol(expr
);
233 ret
= get_symbol_from_deref(expr
);
238 ret
= fake_pointer_sym(expr
);
239 else if (expr
->op
== '*')
240 ret
= get_pointer_type(expr
->unop
);
242 ret
= get_type(expr
->unop
);
244 case EXPR_ASSIGNMENT
:
245 ret
= get_type(expr
->left
);
248 case EXPR_FORCE_CAST
:
249 case EXPR_IMPLIED_CAST
:
250 ret
= get_real_base_type(expr
->cast_type
);
254 ret
= get_binop_type(expr
);
257 ret
= get_return_type(expr
);
260 ret
= get_expr_stmt_type(expr
->statement
);
262 case EXPR_CONDITIONAL
:
264 ret
= get_select_type(expr
);
276 if (ret
&& ret
->type
== SYM_TYPEOF
)
277 ret
= get_type(ret
->initializer
);
283 static struct symbol
*get_final_type_helper(struct expression
*expr
)
286 * I'm not totally positive I understand types...
288 * So, when you're doing pointer math, and you do a subtraction, then
289 * the sval_binop() and whatever need to know the type of the pointer
290 * so they can figure out the alignment. But the result is going to be
291 * and ssize_t. So get_operation_type() gives you the pointer type
292 * and get_type() gives you ssize_t.
294 * Most of the time the operation type and the final type are the same
295 * but this just handles the few places where they are different.
299 expr
= strip_parens(expr
);
303 switch (expr
->type
) {
307 struct symbol
*left
, *right
;
312 left
= get_type(expr
->left
);
313 right
= get_type(expr
->right
);
314 if (type_is_ptr(left
) || type_is_ptr(right
))
315 return ssize_t_ctype
;
322 struct symbol
*get_type(struct expression
*expr
)
324 return get_type_helper(expr
);
327 struct symbol
*get_final_type(struct expression
*expr
)
331 ret
= get_final_type_helper(expr
);
334 return get_type_helper(expr
);
337 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
339 struct symbol
*ret
= &int_ctype
;
341 if (type_positive_bits(left
) > type_positive_bits(ret
))
343 if (type_positive_bits(right
) > type_positive_bits(ret
))
346 if (type_is_ptr(left
))
348 if (type_is_ptr(right
))
354 int type_signed(struct symbol
*base_type
)
358 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
363 int expr_unsigned(struct expression
*expr
)
367 sym
= get_type(expr
);
370 if (type_unsigned(sym
))
375 int expr_signed(struct expression
*expr
)
379 sym
= get_type(expr
);
382 if (type_signed(sym
))
387 int returns_unsigned(struct symbol
*sym
)
391 sym
= get_base_type(sym
);
392 if (!sym
|| sym
->type
!= SYM_FN
)
394 sym
= get_base_type(sym
);
395 return type_unsigned(sym
);
398 int is_pointer(struct expression
*expr
)
402 sym
= get_type(expr
);
405 if (sym
== &string_ctype
)
407 if (sym
->type
== SYM_PTR
)
412 int returns_pointer(struct symbol
*sym
)
416 sym
= get_base_type(sym
);
417 if (!sym
|| sym
->type
!= SYM_FN
)
419 sym
= get_base_type(sym
);
420 if (sym
->type
== SYM_PTR
)
425 sval_t
sval_type_max(struct symbol
*base_type
)
429 if (!base_type
|| !type_bits(base_type
))
430 base_type
= &llong_ctype
;
431 ret
.type
= base_type
;
433 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
437 sval_t
sval_type_min(struct symbol
*base_type
)
441 if (!base_type
|| !type_bits(base_type
))
442 base_type
= &llong_ctype
;
443 ret
.type
= base_type
;
445 if (type_unsigned(base_type
)) {
450 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
455 int nr_bits(struct expression
*expr
)
459 type
= get_type(expr
);
462 return type_bits(type
);
465 int is_void_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
== &void_ctype
)
478 int is_char_pointer(struct expression
*expr
)
482 type
= get_type(expr
);
483 if (!type
|| type
->type
!= SYM_PTR
)
485 type
= get_real_base_type(type
);
486 if (type
== &char_ctype
)
491 int is_string(struct expression
*expr
)
493 expr
= strip_expr(expr
);
494 if (!expr
|| expr
->type
!= EXPR_STRING
)
501 int is_static(struct expression
*expr
)
507 name
= expr_to_str_sym(expr
, &sym
);
511 if (sym
->ctype
.modifiers
& MOD_STATIC
)
518 int is_local_variable(struct expression
*expr
)
523 name
= expr_to_var_sym(expr
, &sym
);
525 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
|| !cur_func_sym
)
527 if (cmp_pos(sym
->scope
->token
->pos
, cur_func_sym
->pos
) < 0)
534 int types_equiv(struct symbol
*one
, struct symbol
*two
)
540 if (one
->type
!= two
->type
)
542 if (one
->type
== SYM_PTR
)
543 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
544 if (type_positive_bits(one
) != type_positive_bits(two
))
551 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
554 const char *global_static(void)
556 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
562 struct symbol
*cur_func_return_type(void)
566 sym
= get_real_base_type(cur_func_sym
);
567 if (!sym
|| sym
->type
!= SYM_FN
)
569 sym
= get_real_base_type(sym
);
573 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
575 struct symbol
*fn_type
;
577 struct symbol
*arg_type
;
580 fn_type
= get_type(fn
);
583 if (fn_type
->type
== SYM_PTR
)
584 fn_type
= get_real_base_type(fn_type
);
585 if (fn_type
->type
!= SYM_FN
)
589 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
590 arg_type
= get_real_base_type(tmp
);
595 } END_FOR_EACH_PTR(tmp
);
600 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
602 struct symbol
*tmp
, *sub
;
605 if (strncmp(name
, ".", 1) == 0)
607 if (strncmp(name
, "->", 2) == 0)
610 FOR_EACH_PTR(symbol_list
, tmp
) {
612 sub
= get_real_base_type(tmp
);
613 sub
= get_member_from_string(sub
->symbol_list
, name
);
619 if (strcmp(tmp
->ident
->name
, name
) == 0)
622 chunk_len
= strlen(tmp
->ident
->name
);
623 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
624 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
625 sub
= get_real_base_type(tmp
);
626 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
629 } END_FOR_EACH_PTR(tmp
);
634 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
638 if (strcmp(key
, "$") == 0)
639 return get_type(expr
);
641 if (strcmp(key
, "*$") == 0) {
642 sym
= get_type(expr
);
643 if (!sym
|| sym
->type
!= SYM_PTR
)
645 return get_real_base_type(sym
);
648 sym
= get_type(expr
);
651 if (sym
->type
== SYM_PTR
)
652 sym
= get_real_base_type(sym
);
655 sym
= get_member_from_string(sym
->symbol_list
, key
);
658 return get_real_base_type(sym
);
661 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
)
667 if (strcmp(key
, "$") == 0)
668 return get_arg_type(fn
, param
);
669 if (strcmp(key
, "*$") == 0) {
670 type
= get_arg_type(fn
, param
);
671 if (!type
|| type
->type
!= SYM_PTR
)
673 return get_real_base_type(type
);
675 return get_member_type_from_key(arg
, key
);
678 int is_struct(struct expression
*expr
)
682 type
= get_type(expr
);
683 if (type
&& type
->type
== SYM_STRUCT
)
692 {&bool_ctype
, "bool"},
693 {&void_ctype
, "void"},
694 {&type_ctype
, "type"},
695 {&char_ctype
, "char"},
696 {&schar_ctype
, "schar"},
697 {&uchar_ctype
, "uchar"},
698 {&short_ctype
, "short"},
699 {&sshort_ctype
, "sshort"},
700 {&ushort_ctype
, "ushort"},
702 {&sint_ctype
, "sint"},
703 {&uint_ctype
, "uint"},
704 {&long_ctype
, "long"},
705 {&slong_ctype
, "slong"},
706 {&ulong_ctype
, "ulong"},
707 {&llong_ctype
, "llong"},
708 {&sllong_ctype
, "sllong"},
709 {&ullong_ctype
, "ullong"},
710 {&lllong_ctype
, "lllong"},
711 {&slllong_ctype
, "slllong"},
712 {&ulllong_ctype
, "ulllong"},
713 {&float_ctype
, "float"},
714 {&double_ctype
, "double"},
715 {&ldouble_ctype
, "ldouble"},
716 {&string_ctype
, "string"},
718 {&lazy_ptr_ctype
, "lazy_ptr"},
719 {&incomplete_ctype
, "incomplete"},
720 {&label_ctype
, "label"},
722 {&null_ctype
, "null"},
725 static const char *base_type_str(struct symbol
*sym
)
729 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
730 if (sym
== base_types
[i
].sym
)
731 return base_types
[i
].name
;
736 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
741 return snprintf(buf
, size
, "<unknown>");
743 if (type
->type
== SYM_BASETYPE
) {
744 return snprintf(buf
, size
, base_type_str(type
));
745 } else if (type
->type
== SYM_PTR
) {
746 type
= get_real_base_type(type
);
747 n
= type_str_helper(buf
, size
, type
);
750 return n
+ snprintf(buf
+ n
, size
- n
, "*");
751 } else if (type
->type
== SYM_ARRAY
) {
752 type
= get_real_base_type(type
);
753 n
= type_str_helper(buf
, size
, type
);
756 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
757 } else if (type
->type
== SYM_STRUCT
) {
758 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
759 } else if (type
->type
== SYM_UNION
) {
761 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
763 return snprintf(buf
, size
, "anonymous union");
764 } else if (type
->type
== SYM_FN
) {
765 struct symbol
*arg
, *return_type
, *arg_type
;
768 return_type
= get_real_base_type(type
);
769 n
= type_str_helper(buf
, size
, return_type
);
772 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
777 FOR_EACH_PTR(type
->arguments
, arg
) {
779 n
+= snprintf(buf
+ n
, size
- n
, ", ");
782 arg_type
= get_real_base_type(arg
);
783 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
786 } END_FOR_EACH_PTR(arg
);
788 return n
+ snprintf(buf
+ n
, size
- n
, ")");
789 } else if (type
->type
== SYM_NODE
) {
790 n
= snprintf(buf
, size
, "node {");
793 type
= get_real_base_type(type
);
794 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
797 return n
+ snprintf(buf
+ n
, size
- n
, "}");
799 return snprintf(buf
, size
, "<type %d>", type
->type
);
803 char *type_to_str(struct symbol
*type
)
805 static char buf
[256];
808 type_str_helper(buf
, sizeof(buf
), type
);