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
)
42 int bits
= type_bits(type
);
46 return bits_to_bytes(bits
);
49 static struct symbol
*get_binop_type(struct expression
*expr
)
51 struct symbol
*left
, *right
;
53 left
= get_type(expr
->left
);
57 if (expr
->op
== SPECIAL_LEFTSHIFT
||
58 expr
->op
== SPECIAL_RIGHTSHIFT
) {
59 if (type_positive_bits(left
) < 31)
63 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
66 right
= get_type(expr
->right
);
70 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
73 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
76 if (type_positive_bits(left
) > type_positive_bits(right
))
81 static struct symbol
*get_type_symbol(struct expression
*expr
)
83 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
86 return get_real_base_type(expr
->symbol
);
89 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
91 struct symbol
*tmp
, *sub
;
93 FOR_EACH_PTR(symbol_list
, tmp
) {
95 sub
= get_real_base_type(tmp
);
96 sub
= get_member_symbol(sub
->symbol_list
, member
);
101 if (tmp
->ident
== member
)
103 } END_FOR_EACH_PTR(tmp
);
108 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
110 struct ident
*member
;
113 if (!expr
|| expr
->type
!= EXPR_DEREF
)
116 member
= expr
->member
;
117 sym
= get_type(expr
->deref
);
119 // sm_msg("could not find struct type");
122 if (sym
->type
== SYM_PTR
)
123 sym
= get_real_base_type(sym
);
124 sym
= get_member_symbol(sym
->symbol_list
, member
);
127 return get_real_base_type(sym
);
130 static struct symbol
*get_return_type(struct expression
*expr
)
134 tmp
= get_type(expr
->fn
);
137 /* this is to handle __builtin_constant_p() */
138 if (tmp
->type
!= SYM_FN
)
139 tmp
= get_base_type(tmp
);
140 return get_real_base_type(tmp
);
143 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
145 if (stmt
->type
!= STMT_COMPOUND
)
147 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
148 if (stmt
->type
== STMT_LABEL
)
149 stmt
= stmt
->label_statement
;
150 if (stmt
->type
!= STMT_EXPRESSION
)
152 return get_type(stmt
->expression
);
155 static struct symbol
*get_select_type(struct expression
*expr
)
157 struct symbol
*one
, *two
;
159 one
= get_type(expr
->cond_true
);
160 two
= get_type(expr
->cond_false
);
164 * This is a hack. If the types are not equiv then we
165 * really don't know the type. But I think guessing is
168 if (type_positive_bits(one
) > type_positive_bits(two
))
173 struct symbol
*get_pointer_type(struct expression
*expr
)
177 sym
= get_type(expr
);
180 if (sym
->type
== SYM_NODE
) {
181 sym
= get_real_base_type(sym
);
185 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
187 return get_real_base_type(sym
);
190 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
195 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
197 base
= get_type(expr
);
200 sym
->ctype
.base_type
= base
;
204 struct symbol
*get_type(struct expression
*expr
)
208 expr
= strip_parens(expr
);
215 switch (expr
->type
) {
220 ret
= get_type_symbol(expr
);
223 ret
= get_symbol_from_deref(expr
);
228 ret
= fake_pointer_sym(expr
);
229 else if (expr
->op
== '*')
230 ret
= get_pointer_type(expr
->unop
);
232 ret
= get_type(expr
->unop
);
234 case EXPR_ASSIGNMENT
:
235 ret
= get_type(expr
->left
);
238 case EXPR_FORCE_CAST
:
239 case EXPR_IMPLIED_CAST
:
240 ret
= get_real_base_type(expr
->cast_type
);
244 ret
= get_binop_type(expr
);
247 ret
= get_return_type(expr
);
250 ret
= get_expr_stmt_type(expr
->statement
);
252 case EXPR_CONDITIONAL
:
254 ret
= get_select_type(expr
);
266 if (ret
&& ret
->type
== SYM_TYPEOF
)
267 ret
= get_type(ret
->initializer
);
273 int type_signed(struct symbol
*base_type
)
277 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
282 int expr_unsigned(struct expression
*expr
)
286 sym
= get_type(expr
);
289 if (type_unsigned(sym
))
294 int expr_signed(struct expression
*expr
)
298 sym
= get_type(expr
);
301 if (type_signed(sym
))
306 int returns_unsigned(struct symbol
*sym
)
310 sym
= get_base_type(sym
);
311 if (!sym
|| sym
->type
!= SYM_FN
)
313 sym
= get_base_type(sym
);
314 return type_unsigned(sym
);
317 int is_pointer(struct expression
*expr
)
321 sym
= get_type(expr
);
324 if (sym
== &string_ctype
)
326 if (sym
->type
== SYM_PTR
)
331 int returns_pointer(struct symbol
*sym
)
335 sym
= get_base_type(sym
);
336 if (!sym
|| sym
->type
!= SYM_FN
)
338 sym
= get_base_type(sym
);
339 if (sym
->type
== SYM_PTR
)
344 sval_t
sval_type_max(struct symbol
*base_type
)
348 if (!base_type
|| !type_bits(base_type
))
349 base_type
= &llong_ctype
;
350 ret
.type
= base_type
;
352 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
356 sval_t
sval_type_min(struct symbol
*base_type
)
360 if (!base_type
|| !type_bits(base_type
))
361 base_type
= &llong_ctype
;
362 ret
.type
= base_type
;
364 if (type_unsigned(base_type
)) {
369 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
374 int nr_bits(struct expression
*expr
)
378 type
= get_type(expr
);
381 return type_bits(type
);
384 int is_void_pointer(struct expression
*expr
)
388 type
= get_type(expr
);
389 if (!type
|| type
->type
!= SYM_PTR
)
391 type
= get_real_base_type(type
);
392 if (type
== &void_ctype
)
397 int is_char_pointer(struct expression
*expr
)
401 type
= get_type(expr
);
402 if (!type
|| type
->type
!= SYM_PTR
)
404 type
= get_real_base_type(type
);
405 if (type
== &char_ctype
)
410 int is_string(struct expression
*expr
)
412 expr
= strip_expr(expr
);
413 if (!expr
|| expr
->type
!= EXPR_STRING
)
420 int is_static(struct expression
*expr
)
426 name
= expr_to_str_sym(expr
, &sym
);
430 if (sym
->ctype
.modifiers
& MOD_STATIC
)
437 int is_local_variable(struct expression
*expr
)
442 name
= expr_to_var_sym(expr
, &sym
);
444 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
)
446 if (cmp_pos(sym
->scope
->token
->pos
, cur_func_sym
->pos
) < 0)
453 int types_equiv(struct symbol
*one
, struct symbol
*two
)
459 if (one
->type
!= two
->type
)
461 if (one
->type
== SYM_PTR
)
462 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
463 if (type_positive_bits(one
) != type_positive_bits(two
))
470 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
473 const char *global_static(void)
475 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
481 struct symbol
*cur_func_return_type(void)
485 sym
= get_real_base_type(cur_func_sym
);
486 if (!sym
|| sym
->type
!= SYM_FN
)
488 sym
= get_real_base_type(sym
);
492 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
494 struct symbol
*fn_type
;
496 struct symbol
*arg_type
;
499 fn_type
= get_type(fn
);
502 if (fn_type
->type
== SYM_PTR
)
503 fn_type
= get_real_base_type(fn_type
);
504 if (fn_type
->type
!= SYM_FN
)
508 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
509 arg_type
= get_real_base_type(tmp
);
514 } END_FOR_EACH_PTR(tmp
);
519 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
521 struct symbol
*tmp
, *sub
;
524 if (strncmp(name
, ".", 1) == 0)
526 if (strncmp(name
, "->", 2) == 0)
529 FOR_EACH_PTR(symbol_list
, tmp
) {
531 sub
= get_real_base_type(tmp
);
532 sub
= get_member_from_string(sub
->symbol_list
, name
);
538 if (strcmp(tmp
->ident
->name
, name
) == 0)
541 chunk_len
= strlen(tmp
->ident
->name
);
542 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
543 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
544 sub
= get_real_base_type(tmp
);
545 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
548 } END_FOR_EACH_PTR(tmp
);
553 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
557 if (strcmp(key
, "$") == 0)
558 return get_type(expr
);
560 if (strcmp(key
, "*$") == 0) {
561 sym
= get_type(expr
);
562 if (!sym
|| sym
->type
!= SYM_PTR
)
564 return get_real_base_type(sym
);
567 sym
= get_type(expr
);
570 if (sym
->type
== SYM_PTR
)
571 sym
= get_real_base_type(sym
);
574 sym
= get_member_from_string(sym
->symbol_list
, key
);
577 return get_real_base_type(sym
);
580 int is_struct(struct expression
*expr
)
584 type
= get_type(expr
);
585 if (type
&& type
->type
== SYM_STRUCT
)
594 {&bool_ctype
, "bool"},
595 {&void_ctype
, "void"},
596 {&type_ctype
, "type"},
597 {&char_ctype
, "char"},
598 {&schar_ctype
, "schar"},
599 {&uchar_ctype
, "uchar"},
600 {&short_ctype
, "short"},
601 {&sshort_ctype
, "sshort"},
602 {&ushort_ctype
, "ushort"},
604 {&sint_ctype
, "sint"},
605 {&uint_ctype
, "uint"},
606 {&long_ctype
, "long"},
607 {&slong_ctype
, "slong"},
608 {&ulong_ctype
, "ulong"},
609 {&llong_ctype
, "llong"},
610 {&sllong_ctype
, "sllong"},
611 {&ullong_ctype
, "ullong"},
612 {&lllong_ctype
, "lllong"},
613 {&slllong_ctype
, "slllong"},
614 {&ulllong_ctype
, "ulllong"},
615 {&float_ctype
, "float"},
616 {&double_ctype
, "double"},
617 {&ldouble_ctype
, "ldouble"},
618 {&string_ctype
, "string"},
620 {&lazy_ptr_ctype
, "lazy_ptr"},
621 {&incomplete_ctype
, "incomplete"},
622 {&label_ctype
, "label"},
624 {&null_ctype
, "null"},
627 static const char *base_type_str(struct symbol
*sym
)
631 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
632 if (sym
== base_types
[i
].sym
)
633 return base_types
[i
].name
;
638 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
643 return snprintf(buf
, size
, "<unknown>");
645 if (type
->type
== SYM_BASETYPE
) {
646 return snprintf(buf
, size
, base_type_str(type
));
647 } else if (type
->type
== SYM_PTR
) {
648 type
= get_real_base_type(type
);
649 n
= type_str_helper(buf
, size
, type
);
652 return n
+ snprintf(buf
+ n
, size
- n
, "*");
653 } else if (type
->type
== SYM_ARRAY
) {
654 type
= get_real_base_type(type
);
655 n
= type_str_helper(buf
, size
, type
);
658 return n
+ snprintf(buf
+ n
, size
- n
, "[]");
659 } else if (type
->type
== SYM_STRUCT
) {
660 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
661 } else if (type
->type
== SYM_UNION
) {
663 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
665 return snprintf(buf
, size
, "anonymous union");
666 } else if (type
->type
== SYM_FN
) {
667 struct symbol
*arg
, *return_type
, *arg_type
;
670 return_type
= get_real_base_type(type
);
671 n
= type_str_helper(buf
, size
, return_type
);
674 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
679 FOR_EACH_PTR(type
->arguments
, arg
) {
681 n
+= snprintf(buf
+ n
, size
- n
, ", ");
684 arg_type
= get_real_base_type(arg
);
685 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
688 } END_FOR_EACH_PTR(arg
);
690 return n
+ snprintf(buf
+ n
, size
- n
, ")");
691 } else if (type
->type
== SYM_NODE
) {
692 n
= snprintf(buf
, size
, "node {");
695 type
= get_real_base_type(type
);
696 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
699 return n
+ snprintf(buf
+ n
, size
- n
, "}");
701 return snprintf(buf
, size
, "<type %d>", type
->type
);
705 char *type_to_str(struct symbol
*type
)
707 static char buf
[256];
710 type_str_helper(buf
, sizeof(buf
), type
);