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_bits(struct symbol
*type
)
44 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
45 return bits_in_pointer
;
47 examine_symbol_type(type
);
48 return type
->bit_size
;
51 int type_bytes(struct symbol
*type
)
53 int bits
= type_bits(type
);
57 return bits_to_bytes(bits
);
60 int type_positive_bits(struct symbol
*type
)
64 if (type_unsigned(type
))
65 return type_bits(type
);
66 return type_bits(type
) - 1;
69 static struct symbol
*get_binop_type(struct expression
*expr
)
71 struct symbol
*left
, *right
;
73 left
= get_type(expr
->left
);
77 if (expr
->op
== SPECIAL_LEFTSHIFT
||
78 expr
->op
== SPECIAL_RIGHTSHIFT
) {
79 if (type_positive_bits(left
) < 31)
83 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
86 right
= get_type(expr
->right
);
90 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
93 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
96 if (type_positive_bits(left
) > type_positive_bits(right
))
101 static struct symbol
*get_type_symbol(struct expression
*expr
)
103 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
106 return get_real_base_type(expr
->symbol
);
109 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
111 struct symbol
*tmp
, *sub
;
113 FOR_EACH_PTR(symbol_list
, tmp
) {
115 sub
= get_real_base_type(tmp
);
116 sub
= get_member_symbol(sub
->symbol_list
, member
);
121 if (tmp
->ident
== member
)
123 } END_FOR_EACH_PTR(tmp
);
128 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
130 struct ident
*member
;
133 if (!expr
|| expr
->type
!= EXPR_DEREF
)
136 member
= expr
->member
;
137 sym
= get_type(expr
->deref
);
139 // sm_msg("could not find struct type");
142 if (sym
->type
== SYM_PTR
)
143 sym
= get_real_base_type(sym
);
144 sym
= get_member_symbol(sym
->symbol_list
, member
);
147 return get_real_base_type(sym
);
150 static struct symbol
*get_return_type(struct expression
*expr
)
154 tmp
= get_type(expr
->fn
);
157 return get_real_base_type(tmp
);
160 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
162 if (stmt
->type
!= STMT_COMPOUND
)
164 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
165 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
167 return get_type(stmt
->expression
);
170 static struct symbol
*get_select_type(struct expression
*expr
)
172 struct symbol
*one
, *two
;
174 one
= get_type(expr
->cond_true
);
175 two
= get_type(expr
->cond_false
);
179 * This is a hack. If the types are not equiv then we
180 * really don't know the type. But I think guessing is
183 if (type_positive_bits(one
) > type_positive_bits(two
))
188 struct symbol
*get_pointer_type(struct expression
*expr
)
192 sym
= get_type(expr
);
195 if (sym
->type
== SYM_NODE
) {
196 sym
= get_real_base_type(sym
);
200 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
202 return get_real_base_type(sym
);
205 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
210 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
212 base
= get_type(expr
);
215 sym
->ctype
.base_type
= base
;
219 struct symbol
*get_type(struct expression
*expr
)
223 expr
= strip_parens(expr
);
230 switch (expr
->type
) {
235 ret
= get_type_symbol(expr
);
238 ret
= get_symbol_from_deref(expr
);
243 ret
= fake_pointer_sym(expr
);
244 else if (expr
->op
== '*')
245 ret
= get_pointer_type(expr
->unop
);
247 ret
= get_type(expr
->unop
);
249 case EXPR_ASSIGNMENT
:
250 ret
= get_type(expr
->left
);
253 case EXPR_FORCE_CAST
:
254 case EXPR_IMPLIED_CAST
:
255 ret
= get_real_base_type(expr
->cast_type
);
259 ret
= get_binop_type(expr
);
262 ret
= get_return_type(expr
);
265 ret
= get_expr_stmt_type(expr
->statement
);
267 case EXPR_CONDITIONAL
:
269 ret
= get_select_type(expr
);
285 int type_unsigned(struct symbol
*base_type
)
289 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
294 int type_signed(struct symbol
*base_type
)
298 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
303 int expr_unsigned(struct expression
*expr
)
307 sym
= get_type(expr
);
310 if (type_unsigned(sym
))
315 int expr_signed(struct expression
*expr
)
319 sym
= get_type(expr
);
322 if (type_signed(sym
))
327 int returns_unsigned(struct symbol
*sym
)
331 sym
= get_base_type(sym
);
332 if (!sym
|| sym
->type
!= SYM_FN
)
334 sym
= get_base_type(sym
);
335 return type_unsigned(sym
);
338 int is_pointer(struct expression
*expr
)
342 sym
= get_type(expr
);
345 if (sym
== &string_ctype
)
347 if (sym
->type
== SYM_PTR
)
352 int returns_pointer(struct symbol
*sym
)
356 sym
= get_base_type(sym
);
357 if (!sym
|| sym
->type
!= SYM_FN
)
359 sym
= get_base_type(sym
);
360 if (sym
->type
== SYM_PTR
)
365 sval_t
sval_type_max(struct symbol
*base_type
)
369 if (!base_type
|| !type_bits(base_type
))
370 base_type
= &llong_ctype
;
371 ret
.type
= base_type
;
373 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
377 sval_t
sval_type_min(struct symbol
*base_type
)
381 if (!base_type
|| !type_bits(base_type
))
382 base_type
= &llong_ctype
;
383 ret
.type
= base_type
;
385 if (type_unsigned(base_type
)) {
390 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
395 int nr_bits(struct expression
*expr
)
399 type
= get_type(expr
);
402 return type_bits(type
);
405 int is_void_pointer(struct expression
*expr
)
409 type
= get_type(expr
);
410 if (!type
|| type
->type
!= SYM_PTR
)
412 type
= get_real_base_type(type
);
413 if (type
== &void_ctype
)
418 int is_char_pointer(struct expression
*expr
)
422 type
= get_type(expr
);
423 if (!type
|| type
->type
!= SYM_PTR
)
425 type
= get_real_base_type(type
);
426 if (type
== &char_ctype
)
431 int is_string(struct expression
*expr
)
433 expr
= strip_expr(expr
);
434 if (!expr
|| expr
->type
!= EXPR_STRING
)
441 int is_static(struct expression
*expr
)
447 name
= expr_to_str_sym(expr
, &sym
);
451 if (sym
->ctype
.modifiers
& MOD_STATIC
)
458 int types_equiv(struct symbol
*one
, struct symbol
*two
)
464 if (one
->type
!= two
->type
)
466 if (one
->type
== SYM_PTR
)
467 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
468 if (type_positive_bits(one
) != type_positive_bits(two
))
475 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
478 const char *global_static(void)
480 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
486 struct symbol
*cur_func_return_type(void)
490 sym
= get_real_base_type(cur_func_sym
);
491 if (!sym
|| sym
->type
!= SYM_FN
)
493 sym
= get_real_base_type(sym
);
497 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
499 struct symbol
*fn_type
;
501 struct symbol
*arg_type
;
504 fn_type
= get_type(fn
);
507 if (fn_type
->type
== SYM_PTR
)
508 fn_type
= get_real_base_type(fn_type
);
509 if (fn_type
->type
!= SYM_FN
)
513 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
514 arg_type
= get_real_base_type(tmp
);
519 } END_FOR_EACH_PTR(tmp
);
524 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
526 struct symbol
*tmp
, *sub
;
529 if (strncmp(name
, ".", 1) == 0)
531 if (strncmp(name
, "->", 2) == 0)
534 FOR_EACH_PTR(symbol_list
, tmp
) {
536 sub
= get_real_base_type(tmp
);
537 sub
= get_member_from_string(sub
->symbol_list
, name
);
543 if (strcmp(tmp
->ident
->name
, name
) == 0)
546 chunk_len
= strlen(tmp
->ident
->name
);
547 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
548 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
549 sub
= get_real_base_type(tmp
);
550 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
553 } END_FOR_EACH_PTR(tmp
);
558 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
563 if (strcmp(key
, "$") == 0)
564 return get_type(expr
);
566 if (strcmp(key
, "*$") == 0) {
567 sym
= get_type(expr
);
568 if (!sym
|| sym
->type
!= SYM_PTR
)
570 return get_real_base_type(sym
);
573 name
= expr_to_str_sym(expr
, &sym
);
577 sym
= get_real_base_type(sym
);
578 if (sym
->type
== SYM_PTR
)
579 sym
= get_real_base_type(sym
);
582 sym
= get_member_from_string(sym
->symbol_list
, key
);
585 return get_real_base_type(sym
);
588 int is_struct(struct expression
*expr
)
592 type
= get_type(expr
);
593 if (type
&& type
->type
== SYM_STRUCT
)
602 {&bool_ctype
, "bool"},
603 {&void_ctype
, "void"},
604 {&type_ctype
, "type"},
605 {&char_ctype
, "char"},
606 {&schar_ctype
, "schar"},
607 {&uchar_ctype
, "uchar"},
608 {&short_ctype
, "short"},
609 {&sshort_ctype
, "sshort"},
610 {&ushort_ctype
, "ushort"},
612 {&sint_ctype
, "sint"},
613 {&uint_ctype
, "uint"},
614 {&long_ctype
, "long"},
615 {&slong_ctype
, "slong"},
616 {&ulong_ctype
, "ulong"},
617 {&llong_ctype
, "llong"},
618 {&sllong_ctype
, "sllong"},
619 {&ullong_ctype
, "ullong"},
620 {&lllong_ctype
, "lllong"},
621 {&slllong_ctype
, "slllong"},
622 {&ulllong_ctype
, "ulllong"},
623 {&float_ctype
, "float"},
624 {&double_ctype
, "double"},
625 {&ldouble_ctype
, "ldouble"},
626 {&string_ctype
, "string"},
628 {&lazy_ptr_ctype
, "lazy_ptr"},
629 {&incomplete_ctype
, "incomplete"},
630 {&label_ctype
, "label"},
632 {&null_ctype
, "null"},
635 static const char *base_type_str(struct symbol
*sym
)
639 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
640 if (sym
== base_types
[i
].sym
)
641 return base_types
[i
].name
;
646 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
651 return snprintf(buf
, size
, "<unknown>");
653 if (type
->type
== SYM_BASETYPE
) {
654 return snprintf(buf
, size
, base_type_str(type
));
655 } else if (type
->type
== SYM_PTR
) {
656 type
= get_real_base_type(type
);
657 n
= type_str_helper(buf
, size
, type
);
660 return n
+ snprintf(buf
+ n
, size
- n
, "*");
661 } else if (type
->type
== SYM_STRUCT
) {
662 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
663 } else if (type
->type
== SYM_UNION
) {
665 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
667 return snprintf(buf
, size
, "anonymous union");
668 } else if (type
->type
== SYM_FN
) {
669 struct symbol
*arg
, *return_type
, *arg_type
;
672 return_type
= get_real_base_type(type
);
673 n
= type_str_helper(buf
, size
, return_type
);
676 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
681 FOR_EACH_PTR(type
->arguments
, arg
) {
683 n
+= snprintf(buf
+ n
, size
- n
, ", ");
686 arg_type
= get_real_base_type(arg
);
687 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
690 } END_FOR_EACH_PTR(arg
);
692 return n
+ snprintf(buf
+ n
, size
- n
, ")");
693 } else if (type
->type
== SYM_NODE
) {
694 n
= snprintf(buf
, size
, "node {");
697 type
= get_real_base_type(type
);
698 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
701 return n
+ snprintf(buf
+ n
, size
- n
, "}");
703 return snprintf(buf
, size
, "<type %d>", type
->type
);
707 char *type_to_str(struct symbol
*type
)
709 static char buf
[256];
712 type_str_helper(buf
, sizeof(buf
), type
);