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.
304 switch (expr
->type
) {
308 struct symbol
*left
, *right
;
313 left
= get_type(expr
->left
);
314 right
= get_type(expr
->right
);
315 if (type_is_ptr(left
) && type_is_ptr(right
))
316 return ssize_t_ctype
;
323 struct symbol
*get_type(struct expression
*expr
)
325 return get_type_helper(expr
);
328 struct symbol
*get_final_type(struct expression
*expr
)
332 ret
= get_final_type_helper(expr
);
335 return get_type_helper(expr
);
338 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
)
340 struct symbol
*ret
= &int_ctype
;
342 if (type_positive_bits(left
) > type_positive_bits(ret
))
344 if (type_positive_bits(right
) > type_positive_bits(ret
))
347 if (type_is_ptr(left
))
349 if (type_is_ptr(right
))
355 int type_signed(struct symbol
*base_type
)
359 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
364 int expr_unsigned(struct expression
*expr
)
368 sym
= get_type(expr
);
371 if (type_unsigned(sym
))
376 int expr_signed(struct expression
*expr
)
380 sym
= get_type(expr
);
383 if (type_signed(sym
))
388 int returns_unsigned(struct symbol
*sym
)
392 sym
= get_base_type(sym
);
393 if (!sym
|| sym
->type
!= SYM_FN
)
395 sym
= get_base_type(sym
);
396 return type_unsigned(sym
);
399 int is_pointer(struct expression
*expr
)
403 sym
= get_type(expr
);
406 if (sym
== &string_ctype
)
408 if (sym
->type
== SYM_PTR
)
413 int returns_pointer(struct symbol
*sym
)
417 sym
= get_base_type(sym
);
418 if (!sym
|| sym
->type
!= SYM_FN
)
420 sym
= get_base_type(sym
);
421 if (sym
->type
== SYM_PTR
)
426 sval_t
sval_type_max(struct symbol
*base_type
)
430 if (!base_type
|| !type_bits(base_type
))
431 base_type
= &llong_ctype
;
432 ret
.type
= base_type
;
434 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
438 sval_t
sval_type_min(struct symbol
*base_type
)
442 if (!base_type
|| !type_bits(base_type
))
443 base_type
= &llong_ctype
;
444 ret
.type
= base_type
;
446 if (type_unsigned(base_type
)) {
451 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
456 int nr_bits(struct expression
*expr
)
460 type
= get_type(expr
);
463 return type_bits(type
);
466 int is_void_pointer(struct expression
*expr
)
470 type
= get_type(expr
);
471 if (!type
|| type
->type
!= SYM_PTR
)
473 type
= get_real_base_type(type
);
474 if (type
== &void_ctype
)
479 int is_char_pointer(struct expression
*expr
)
483 type
= get_type(expr
);
484 if (!type
|| type
->type
!= SYM_PTR
)
486 type
= get_real_base_type(type
);
487 if (type
== &char_ctype
)
492 int is_string(struct expression
*expr
)
494 expr
= strip_expr(expr
);
495 if (!expr
|| expr
->type
!= EXPR_STRING
)
502 int is_static(struct expression
*expr
)
508 name
= expr_to_str_sym(expr
, &sym
);
512 if (sym
->ctype
.modifiers
& MOD_STATIC
)
519 int is_local_variable(struct expression
*expr
)
524 name
= expr_to_var_sym(expr
, &sym
);
526 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
|| !cur_func_sym
)
528 if (cmp_pos(sym
->scope
->token
->pos
, cur_func_sym
->pos
) < 0)
535 int types_equiv(struct symbol
*one
, struct symbol
*two
)
541 if (one
->type
!= two
->type
)
543 if (one
->type
== SYM_PTR
)
544 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
545 if (type_positive_bits(one
) != type_positive_bits(two
))
552 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
555 const char *global_static(void)
557 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
563 struct symbol
*cur_func_return_type(void)
567 sym
= get_real_base_type(cur_func_sym
);
568 if (!sym
|| sym
->type
!= SYM_FN
)
570 sym
= get_real_base_type(sym
);
574 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
576 struct symbol
*fn_type
;
578 struct symbol
*arg_type
;
581 fn_type
= get_type(fn
);
584 if (fn_type
->type
== SYM_PTR
)
585 fn_type
= get_real_base_type(fn_type
);
586 if (fn_type
->type
!= SYM_FN
)
590 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
591 arg_type
= get_real_base_type(tmp
);
596 } END_FOR_EACH_PTR(tmp
);
601 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, const char *name
)
603 struct symbol
*tmp
, *sub
;
606 if (strncmp(name
, ".", 1) == 0)
608 if (strncmp(name
, "->", 2) == 0)
611 FOR_EACH_PTR(symbol_list
, tmp
) {
613 sub
= get_real_base_type(tmp
);
614 sub
= get_member_from_string(sub
->symbol_list
, name
);
620 if (strcmp(tmp
->ident
->name
, name
) == 0)
623 chunk_len
= strlen(tmp
->ident
->name
);
624 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
625 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
626 sub
= get_real_base_type(tmp
);
627 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
630 } END_FOR_EACH_PTR(tmp
);
635 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
)
639 if (strcmp(key
, "$") == 0)
640 return get_type(expr
);
642 if (strcmp(key
, "*$") == 0) {
643 sym
= get_type(expr
);
644 if (!sym
|| sym
->type
!= SYM_PTR
)
646 return get_real_base_type(sym
);
649 sym
= get_type(expr
);
652 if (sym
->type
== SYM_PTR
)
653 sym
= get_real_base_type(sym
);
656 sym
= get_member_from_string(sym
->symbol_list
, key
);
659 return get_real_base_type(sym
);
662 int is_struct(struct expression
*expr
)
666 type
= get_type(expr
);
667 if (type
&& type
->type
== SYM_STRUCT
)
676 {&bool_ctype
, "bool"},
677 {&void_ctype
, "void"},
678 {&type_ctype
, "type"},
679 {&char_ctype
, "char"},
680 {&schar_ctype
, "schar"},
681 {&uchar_ctype
, "uchar"},
682 {&short_ctype
, "short"},
683 {&sshort_ctype
, "sshort"},
684 {&ushort_ctype
, "ushort"},
686 {&sint_ctype
, "sint"},
687 {&uint_ctype
, "uint"},
688 {&long_ctype
, "long"},
689 {&slong_ctype
, "slong"},
690 {&ulong_ctype
, "ulong"},
691 {&llong_ctype
, "llong"},
692 {&sllong_ctype
, "sllong"},
693 {&ullong_ctype
, "ullong"},
694 {&lllong_ctype
, "lllong"},
695 {&slllong_ctype
, "slllong"},
696 {&ulllong_ctype
, "ulllong"},
697 {&float_ctype
, "float"},
698 {&double_ctype
, "double"},
699 {&ldouble_ctype
, "ldouble"},
700 {&string_ctype
, "string"},
702 {&lazy_ptr_ctype
, "lazy_ptr"},
703 {&incomplete_ctype
, "incomplete"},
704 {&label_ctype
, "label"},
706 {&null_ctype
, "null"},
709 static const char *base_type_str(struct symbol
*sym
)
713 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
714 if (sym
== base_types
[i
].sym
)
715 return base_types
[i
].name
;
720 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
725 return snprintf(buf
, size
, "<unknown>");
727 if (type
->type
== SYM_BASETYPE
) {
728 return snprintf(buf
, size
, base_type_str(type
));
729 } else if (type
->type
== SYM_PTR
) {
730 type
= get_real_base_type(type
);
731 n
= type_str_helper(buf
, size
, type
);
734 return n
+ snprintf(buf
+ n
, size
- n
, "*");
735 } else if (type
->type
== SYM_ARRAY
) {
736 type
= get_real_base_type(type
);
737 n
= type_str_helper(buf
, size
, type
);
740 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
741 } else if (type
->type
== SYM_STRUCT
) {
742 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
743 } else if (type
->type
== SYM_UNION
) {
745 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
747 return snprintf(buf
, size
, "anonymous union");
748 } else if (type
->type
== SYM_FN
) {
749 struct symbol
*arg
, *return_type
, *arg_type
;
752 return_type
= get_real_base_type(type
);
753 n
= type_str_helper(buf
, size
, return_type
);
756 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
761 FOR_EACH_PTR(type
->arguments
, arg
) {
763 n
+= snprintf(buf
+ n
, size
- n
, ", ");
766 arg_type
= get_real_base_type(arg
);
767 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
770 } END_FOR_EACH_PTR(arg
);
772 return n
+ snprintf(buf
+ n
, size
- n
, ")");
773 } else if (type
->type
== SYM_NODE
) {
774 n
= snprintf(buf
, size
, "node {");
777 type
= get_real_base_type(type
);
778 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
781 return n
+ snprintf(buf
+ n
, size
- n
, "}");
783 return snprintf(buf
, size
, "<type %d>", type
->type
);
787 char *type_to_str(struct symbol
*type
)
789 static char buf
[256];
792 type_str_helper(buf
, sizeof(buf
), type
);