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.
25 struct symbol
*get_real_base_type(struct symbol
*sym
)
31 ret
= get_base_type(sym
);
34 if (ret
->type
== SYM_RESTRICT
|| ret
->type
== SYM_NODE
)
35 return get_real_base_type(ret
);
39 int type_bits(struct symbol
*type
)
43 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
44 return bits_in_pointer
;
46 examine_symbol_type(type
);
47 return type
->bit_size
;
50 int type_bytes(struct symbol
*type
)
52 int bits
= type_bits(type
);
56 return bits_to_bytes(bits
);
59 int type_positive_bits(struct symbol
*type
)
63 if (type_unsigned(type
))
64 return type_bits(type
);
65 return type_bits(type
) - 1;
68 static struct symbol
*get_binop_type(struct expression
*expr
)
70 struct symbol
*left
, *right
;
72 left
= get_type(expr
->left
);
73 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 (expr
->op
== SPECIAL_LEFTSHIFT
||
84 expr
->op
== SPECIAL_RIGHTSHIFT
) {
85 if (type_positive_bits(left
) < 31)
90 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
93 if (type_positive_bits(left
) > type_positive_bits(right
))
98 static struct symbol
*get_type_symbol(struct expression
*expr
)
100 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
103 return get_real_base_type(expr
->symbol
);
106 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
108 struct symbol
*tmp
, *sub
;
110 FOR_EACH_PTR(symbol_list
, tmp
) {
112 sub
= get_real_base_type(tmp
);
113 sub
= get_member_symbol(sub
->symbol_list
, member
);
118 if (tmp
->ident
== member
)
120 } END_FOR_EACH_PTR(tmp
);
125 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
127 struct ident
*member
;
130 if (!expr
|| expr
->type
!= EXPR_DEREF
)
133 member
= expr
->member
;
134 sym
= get_type(expr
->deref
);
136 // sm_msg("could not find struct type");
139 if (sym
->type
== SYM_PTR
)
140 sym
= get_real_base_type(sym
);
141 sym
= get_member_symbol(sym
->symbol_list
, member
);
144 return get_real_base_type(sym
);
147 static struct symbol
*get_return_type(struct expression
*expr
)
151 tmp
= get_type(expr
->fn
);
154 return get_real_base_type(tmp
);
157 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
159 if (stmt
->type
!= STMT_COMPOUND
)
161 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
162 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
164 return get_type(stmt
->expression
);
167 static struct symbol
*get_select_type(struct expression
*expr
)
169 struct symbol
*one
, *two
;
171 one
= get_type(expr
->cond_true
);
172 two
= get_type(expr
->cond_false
);
176 * This is a hack. If the types are not equiv then we
177 * really don't know the type. But I think guessing is
180 if (type_positive_bits(one
) > type_positive_bits(two
))
185 struct symbol
*get_pointer_type(struct expression
*expr
)
189 sym
= get_type(expr
);
192 if (sym
->type
== SYM_NODE
) {
193 sym
= get_real_base_type(sym
);
197 if (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
199 return get_real_base_type(sym
);
202 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
207 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
209 base
= get_type(expr
);
212 sym
->ctype
.base_type
= base
;
216 struct symbol
*get_type(struct expression
*expr
)
220 expr
= strip_parens(expr
);
222 switch (expr
->type
) {
224 return &string_ctype
;
226 return get_type_symbol(expr
);
228 return get_symbol_from_deref(expr
);
232 return fake_pointer_sym(expr
);
234 return get_pointer_type(expr
->unop
);
235 return get_type(expr
->unop
);
236 case EXPR_ASSIGNMENT
:
237 return get_type(expr
->left
);
239 case EXPR_FORCE_CAST
:
240 case EXPR_IMPLIED_CAST
:
241 return get_real_base_type(expr
->cast_type
);
244 return get_binop_type(expr
);
246 return get_return_type(expr
);
248 return get_expr_stmt_type(expr
->statement
);
249 case EXPR_CONDITIONAL
:
251 return get_select_type(expr
);
257 // sm_msg("unhandled type %d", expr->type);
263 int type_unsigned(struct symbol
*base_type
)
267 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
272 int type_signed(struct symbol
*base_type
)
276 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
281 int expr_unsigned(struct expression
*expr
)
285 sym
= get_type(expr
);
288 if (type_unsigned(sym
))
293 int expr_signed(struct expression
*expr
)
297 sym
= get_type(expr
);
300 if (type_signed(sym
))
305 int returns_unsigned(struct symbol
*sym
)
309 sym
= get_base_type(sym
);
310 if (!sym
|| sym
->type
!= SYM_FN
)
312 sym
= get_base_type(sym
);
313 return type_unsigned(sym
);
316 int is_pointer(struct expression
*expr
)
320 sym
= get_type(expr
);
323 if (sym
== &string_ctype
)
325 if (sym
->type
== SYM_PTR
)
330 int returns_pointer(struct symbol
*sym
)
334 sym
= get_base_type(sym
);
335 if (!sym
|| sym
->type
!= SYM_FN
)
337 sym
= get_base_type(sym
);
338 if (sym
->type
== SYM_PTR
)
343 sval_t
sval_type_max(struct symbol
*base_type
)
347 if (!base_type
|| !type_bits(base_type
))
348 base_type
= &llong_ctype
;
349 ret
.type
= base_type
;
351 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
355 sval_t
sval_type_min(struct symbol
*base_type
)
359 if (!base_type
|| !type_bits(base_type
))
360 base_type
= &llong_ctype
;
361 ret
.type
= base_type
;
363 if (type_unsigned(base_type
)) {
368 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
373 int nr_bits(struct expression
*expr
)
377 type
= get_type(expr
);
380 return type_bits(type
);
383 int is_void_pointer(struct expression
*expr
)
387 type
= get_type(expr
);
388 if (!type
|| type
->type
!= SYM_PTR
)
390 type
= get_real_base_type(type
);
391 if (type
== &void_ctype
)
396 int is_char_pointer(struct expression
*expr
)
400 type
= get_type(expr
);
401 if (!type
|| type
->type
!= SYM_PTR
)
403 type
= get_real_base_type(type
);
404 if (type
== &char_ctype
)
409 int is_string(struct expression
*expr
)
411 expr
= strip_expr(expr
);
412 if (!expr
|| expr
->type
!= EXPR_STRING
)
419 int is_static(struct expression
*expr
)
425 name
= expr_to_str_sym(expr
, &sym
);
429 if (sym
->ctype
.modifiers
& MOD_STATIC
)
436 int types_equiv(struct symbol
*one
, struct symbol
*two
)
442 if (one
->type
!= two
->type
)
444 if (one
->type
== SYM_PTR
)
445 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
446 if (type_positive_bits(one
) != type_positive_bits(two
))
453 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
456 const char *global_static(void)
458 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
464 struct symbol
*cur_func_return_type(void)
468 sym
= get_real_base_type(cur_func_sym
);
469 if (!sym
|| sym
->type
!= SYM_FN
)
471 sym
= get_real_base_type(sym
);
475 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
477 struct symbol
*fn_type
;
479 struct symbol
*arg_type
;
482 fn_type
= get_type(fn
);
485 if (fn_type
->type
== SYM_PTR
)
486 fn_type
= get_real_base_type(fn_type
);
487 if (fn_type
->type
!= SYM_FN
)
491 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
492 arg_type
= get_real_base_type(tmp
);
497 } END_FOR_EACH_PTR(tmp
);
502 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
504 struct symbol
*tmp
, *sub
;
507 if (strncmp(name
, ".", 1) == 0)
509 if (strncmp(name
, "->", 2) == 0)
512 FOR_EACH_PTR(symbol_list
, tmp
) {
514 sub
= get_real_base_type(tmp
);
515 sub
= get_member_from_string(sub
->symbol_list
, name
);
521 if (strcmp(tmp
->ident
->name
, name
) == 0)
524 chunk_len
= strlen(tmp
->ident
->name
);
525 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
526 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
527 sub
= get_real_base_type(tmp
);
528 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
531 } END_FOR_EACH_PTR(tmp
);
536 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
541 if (strcmp(key
, "$") == 0)
542 return get_type(expr
);
544 if (strcmp(key
, "*$") == 0) {
545 sym
= get_type(expr
);
546 if (!sym
|| sym
->type
!= SYM_PTR
)
548 return get_real_base_type(sym
);
551 name
= expr_to_str_sym(expr
, &sym
);
555 sym
= get_real_base_type(sym
);
556 if (sym
->type
== SYM_PTR
)
557 sym
= get_real_base_type(sym
);
560 sym
= get_member_from_string(sym
->symbol_list
, key
);
563 return get_real_base_type(sym
);
566 int is_struct(struct expression
*expr
)
570 type
= get_type(expr
);
571 if (type
&& type
->type
== SYM_STRUCT
)
580 {&bool_ctype
, "bool"},
581 {&void_ctype
, "void"},
582 {&type_ctype
, "type"},
583 {&char_ctype
, "char"},
584 {&schar_ctype
, "schar"},
585 {&uchar_ctype
, "uchar"},
586 {&short_ctype
, "short"},
587 {&sshort_ctype
, "sshort"},
588 {&ushort_ctype
, "ushort"},
590 {&sint_ctype
, "sint"},
591 {&uint_ctype
, "uint"},
592 {&long_ctype
, "long"},
593 {&slong_ctype
, "slong"},
594 {&ulong_ctype
, "ulong"},
595 {&llong_ctype
, "llong"},
596 {&sllong_ctype
, "sllong"},
597 {&ullong_ctype
, "ullong"},
598 {&lllong_ctype
, "lllong"},
599 {&slllong_ctype
, "slllong"},
600 {&ulllong_ctype
, "ulllong"},
601 {&float_ctype
, "float"},
602 {&double_ctype
, "double"},
603 {&ldouble_ctype
, "ldouble"},
604 {&string_ctype
, "string"},
606 {&lazy_ptr_ctype
, "lazy_ptr"},
607 {&incomplete_ctype
, "incomplete"},
608 {&label_ctype
, "label"},
610 {&null_ctype
, "null"},
613 static const char *base_type_str(struct symbol
*sym
)
617 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
618 if (sym
== base_types
[i
].sym
)
619 return base_types
[i
].name
;
624 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
629 return snprintf(buf
, size
, "<unknown>");
631 if (type
->type
== SYM_BASETYPE
) {
632 return snprintf(buf
, size
, base_type_str(type
));
633 } else if (type
->type
== SYM_PTR
) {
634 type
= get_real_base_type(type
);
635 n
= type_str_helper(buf
, size
, type
);
638 return n
+ snprintf(buf
+ n
, size
- n
, "*");
639 } else if (type
->type
== SYM_STRUCT
) {
640 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
641 } else if (type
->type
== SYM_UNION
) {
643 return snprintf(buf
, size
, "union %s", type
->ident
->name
);
645 return snprintf(buf
, size
, "anonymous union");
646 } else if (type
->type
== SYM_FN
) {
647 struct symbol
*arg
, *return_type
, *arg_type
;
650 return_type
= get_real_base_type(type
);
651 n
= type_str_helper(buf
, size
, return_type
);
654 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
659 FOR_EACH_PTR(type
->arguments
, arg
) {
661 n
+= snprintf(buf
+ n
, size
- n
, ", ");
664 arg_type
= get_real_base_type(arg
);
665 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
668 } END_FOR_EACH_PTR(arg
);
670 return n
+ snprintf(buf
+ n
, size
- n
, ")");
671 } else if (type
->type
== SYM_NODE
) {
672 n
= snprintf(buf
, size
, "node {");
675 type
= get_real_base_type(type
);
676 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
679 return n
+ snprintf(buf
+ n
, size
- n
, "}");
681 return snprintf(buf
, size
, "<type %d>", type
->type
);
685 char *type_to_str(struct symbol
*type
)
687 static char buf
[256];
690 type_str_helper(buf
, sizeof(buf
), type
);
691 return alloc_sname(buf
);