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 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
77 right
= get_type(expr
->right
);
81 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
84 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
87 if (type_positive_bits(left
) > type_positive_bits(right
))
92 static struct symbol
*get_type_symbol(struct expression
*expr
)
94 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
97 return get_real_base_type(expr
->symbol
);
100 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
102 struct symbol
*tmp
, *sub
;
104 FOR_EACH_PTR(symbol_list
, tmp
) {
106 sub
= get_real_base_type(tmp
);
107 sub
= get_member_symbol(sub
->symbol_list
, member
);
112 if (tmp
->ident
== member
)
114 } END_FOR_EACH_PTR(tmp
);
119 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
121 struct ident
*member
;
124 if (!expr
|| expr
->type
!= EXPR_DEREF
)
127 member
= expr
->member
;
128 sym
= get_type(expr
->deref
);
130 // sm_msg("could not find struct type");
133 if (sym
->type
== SYM_PTR
)
134 sym
= get_real_base_type(sym
);
135 sym
= get_member_symbol(sym
->symbol_list
, member
);
138 return get_real_base_type(sym
);
141 static struct symbol
*get_return_type(struct expression
*expr
)
145 tmp
= get_type(expr
->fn
);
148 /* this is to handle __builtin_constant_p() */
149 if (tmp
->type
!= SYM_FN
)
150 tmp
= get_base_type(tmp
);
151 return get_real_base_type(tmp
);
154 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
156 if (stmt
->type
!= STMT_COMPOUND
)
158 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
159 if (stmt
->type
== STMT_LABEL
)
160 stmt
= stmt
->label_statement
;
161 if (stmt
->type
!= STMT_EXPRESSION
)
163 return get_type(stmt
->expression
);
166 static struct symbol
*get_select_type(struct expression
*expr
)
168 struct symbol
*one
, *two
;
170 one
= get_type(expr
->cond_true
);
171 two
= get_type(expr
->cond_false
);
175 * This is a hack. If the types are not equiv then we
176 * really don't know the type. But I think guessing is
179 if (type_positive_bits(one
) > type_positive_bits(two
))
184 struct symbol
*get_pointer_type(struct expression
*expr
)
188 sym
= get_type(expr
);
191 if (sym
->type
== SYM_NODE
) {
192 sym
= get_real_base_type(sym
);
196 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
198 return get_real_base_type(sym
);
201 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
206 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
208 base
= get_type(expr
);
211 sym
->ctype
.base_type
= base
;
215 struct symbol
*get_type(struct expression
*expr
)
219 expr
= strip_parens(expr
);
226 switch (expr
->type
) {
231 ret
= get_type_symbol(expr
);
234 ret
= get_symbol_from_deref(expr
);
239 ret
= fake_pointer_sym(expr
);
240 else if (expr
->op
== '*')
241 ret
= get_pointer_type(expr
->unop
);
243 ret
= get_type(expr
->unop
);
245 case EXPR_ASSIGNMENT
:
246 ret
= get_type(expr
->left
);
249 case EXPR_FORCE_CAST
:
250 case EXPR_IMPLIED_CAST
:
251 ret
= get_real_base_type(expr
->cast_type
);
255 ret
= get_binop_type(expr
);
258 ret
= get_return_type(expr
);
261 ret
= get_expr_stmt_type(expr
->statement
);
263 case EXPR_CONDITIONAL
:
265 ret
= get_select_type(expr
);
277 if (ret
&& ret
->type
== SYM_TYPEOF
)
278 ret
= get_type(ret
->initializer
);
284 int type_signed(struct symbol
*base_type
)
288 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
293 int expr_unsigned(struct expression
*expr
)
297 sym
= get_type(expr
);
300 if (type_unsigned(sym
))
305 int expr_signed(struct expression
*expr
)
309 sym
= get_type(expr
);
312 if (type_signed(sym
))
317 int returns_unsigned(struct symbol
*sym
)
321 sym
= get_base_type(sym
);
322 if (!sym
|| sym
->type
!= SYM_FN
)
324 sym
= get_base_type(sym
);
325 return type_unsigned(sym
);
328 int is_pointer(struct expression
*expr
)
332 sym
= get_type(expr
);
335 if (sym
== &string_ctype
)
337 if (sym
->type
== SYM_PTR
)
342 int returns_pointer(struct symbol
*sym
)
346 sym
= get_base_type(sym
);
347 if (!sym
|| sym
->type
!= SYM_FN
)
349 sym
= get_base_type(sym
);
350 if (sym
->type
== SYM_PTR
)
355 sval_t
sval_type_max(struct symbol
*base_type
)
359 if (!base_type
|| !type_bits(base_type
))
360 base_type
= &llong_ctype
;
361 ret
.type
= base_type
;
363 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
367 sval_t
sval_type_min(struct symbol
*base_type
)
371 if (!base_type
|| !type_bits(base_type
))
372 base_type
= &llong_ctype
;
373 ret
.type
= base_type
;
375 if (type_unsigned(base_type
)) {
380 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
385 int nr_bits(struct expression
*expr
)
389 type
= get_type(expr
);
392 return type_bits(type
);
395 int is_void_pointer(struct expression
*expr
)
399 type
= get_type(expr
);
400 if (!type
|| type
->type
!= SYM_PTR
)
402 type
= get_real_base_type(type
);
403 if (type
== &void_ctype
)
408 int is_char_pointer(struct expression
*expr
)
412 type
= get_type(expr
);
413 if (!type
|| type
->type
!= SYM_PTR
)
415 type
= get_real_base_type(type
);
416 if (type
== &char_ctype
)
421 int is_string(struct expression
*expr
)
423 expr
= strip_expr(expr
);
424 if (!expr
|| expr
->type
!= EXPR_STRING
)
431 int is_static(struct expression
*expr
)
437 name
= expr_to_str_sym(expr
, &sym
);
441 if (sym
->ctype
.modifiers
& MOD_STATIC
)
448 int is_local_variable(struct expression
*expr
)
453 name
= expr_to_var_sym(expr
, &sym
);
455 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
)
457 if (cmp_pos(sym
->scope
->token
->pos
, cur_func_sym
->pos
) < 0)
464 int types_equiv(struct symbol
*one
, struct symbol
*two
)
470 if (one
->type
!= two
->type
)
472 if (one
->type
== SYM_PTR
)
473 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
474 if (type_positive_bits(one
) != type_positive_bits(two
))
481 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
484 const char *global_static(void)
486 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
492 struct symbol
*cur_func_return_type(void)
496 sym
= get_real_base_type(cur_func_sym
);
497 if (!sym
|| sym
->type
!= SYM_FN
)
499 sym
= get_real_base_type(sym
);
503 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
505 struct symbol
*fn_type
;
507 struct symbol
*arg_type
;
510 fn_type
= get_type(fn
);
513 if (fn_type
->type
== SYM_PTR
)
514 fn_type
= get_real_base_type(fn_type
);
515 if (fn_type
->type
!= SYM_FN
)
519 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
520 arg_type
= get_real_base_type(tmp
);
525 } END_FOR_EACH_PTR(tmp
);
530 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
532 struct symbol
*tmp
, *sub
;
535 if (strncmp(name
, ".", 1) == 0)
537 if (strncmp(name
, "->", 2) == 0)
540 FOR_EACH_PTR(symbol_list
, tmp
) {
542 sub
= get_real_base_type(tmp
);
543 sub
= get_member_from_string(sub
->symbol_list
, name
);
549 if (strcmp(tmp
->ident
->name
, name
) == 0)
552 chunk_len
= strlen(tmp
->ident
->name
);
553 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
554 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
555 sub
= get_real_base_type(tmp
);
556 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
559 } END_FOR_EACH_PTR(tmp
);
564 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
568 if (strcmp(key
, "$") == 0)
569 return get_type(expr
);
571 if (strcmp(key
, "*$") == 0) {
572 sym
= get_type(expr
);
573 if (!sym
|| sym
->type
!= SYM_PTR
)
575 return get_real_base_type(sym
);
578 sym
= get_type(expr
);
581 if (sym
->type
== SYM_PTR
)
582 sym
= get_real_base_type(sym
);
585 sym
= get_member_from_string(sym
->symbol_list
, key
);
588 return get_real_base_type(sym
);
591 int is_struct(struct expression
*expr
)
595 type
= get_type(expr
);
596 if (type
&& type
->type
== SYM_STRUCT
)
605 {&bool_ctype
, "bool"},
606 {&void_ctype
, "void"},
607 {&type_ctype
, "type"},
608 {&char_ctype
, "char"},
609 {&schar_ctype
, "schar"},
610 {&uchar_ctype
, "uchar"},
611 {&short_ctype
, "short"},
612 {&sshort_ctype
, "sshort"},
613 {&ushort_ctype
, "ushort"},
615 {&sint_ctype
, "sint"},
616 {&uint_ctype
, "uint"},
617 {&long_ctype
, "long"},
618 {&slong_ctype
, "slong"},
619 {&ulong_ctype
, "ulong"},
620 {&llong_ctype
, "llong"},
621 {&sllong_ctype
, "sllong"},
622 {&ullong_ctype
, "ullong"},
623 {&lllong_ctype
, "lllong"},
624 {&slllong_ctype
, "slllong"},
625 {&ulllong_ctype
, "ulllong"},
626 {&float_ctype
, "float"},
627 {&double_ctype
, "double"},
628 {&ldouble_ctype
, "ldouble"},
629 {&string_ctype
, "string"},
631 {&lazy_ptr_ctype
, "lazy_ptr"},
632 {&incomplete_ctype
, "incomplete"},
633 {&label_ctype
, "label"},
635 {&null_ctype
, "null"},
638 static const char *base_type_str(struct symbol
*sym
)
642 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
643 if (sym
== base_types
[i
].sym
)
644 return base_types
[i
].name
;
649 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
654 return snprintf(buf
, size
, "<unknown>");
656 if (type
->type
== SYM_BASETYPE
) {
657 return snprintf(buf
, size
, base_type_str(type
));
658 } else if (type
->type
== SYM_PTR
) {
659 type
= get_real_base_type(type
);
660 n
= type_str_helper(buf
, size
, type
);
663 return n
+ snprintf(buf
+ n
, size
- n
, "*");
664 } else if (type
->type
== SYM_ARRAY
) {
665 type
= get_real_base_type(type
);
666 n
= type_str_helper(buf
, size
, type
);
669 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
670 } else if (type
->type
== SYM_STRUCT
) {
671 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
672 } else if (type
->type
== SYM_UNION
) {
674 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
676 return snprintf(buf
, size
, "anonymous union");
677 } else if (type
->type
== SYM_FN
) {
678 struct symbol
*arg
, *return_type
, *arg_type
;
681 return_type
= get_real_base_type(type
);
682 n
= type_str_helper(buf
, size
, return_type
);
685 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
690 FOR_EACH_PTR(type
->arguments
, arg
) {
692 n
+= snprintf(buf
+ n
, size
- n
, ", ");
695 arg_type
= get_real_base_type(arg
);
696 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
699 } END_FOR_EACH_PTR(arg
);
701 return n
+ snprintf(buf
+ n
, size
- n
, ")");
702 } else if (type
->type
== SYM_NODE
) {
703 n
= snprintf(buf
, size
, "node {");
706 type
= get_real_base_type(type
);
707 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
710 return n
+ snprintf(buf
+ n
, size
- n
, "}");
712 return snprintf(buf
, size
, "<type %d>", type
->type
);
716 char *type_to_str(struct symbol
*type
)
718 static char buf
[256];
721 type_str_helper(buf
, sizeof(buf
), type
);