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
);
32 if (ret
&& ret
->type
== SYM_RESTRICT
)
33 return get_real_base_type(ret
);
37 int type_bits(struct symbol
*type
)
41 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
42 return bits_in_pointer
;
44 examine_symbol_type(type
);
45 return type
->bit_size
;
48 int type_bytes(struct symbol
*type
)
50 int bits
= type_bits(type
);
54 return bits_to_bytes(bits
);
57 int type_positive_bits(struct symbol
*type
)
61 if (type_unsigned(type
))
62 return type_bits(type
);
63 return type_bits(type
) - 1;
66 static struct symbol
*get_binop_type(struct expression
*expr
)
68 struct symbol
*left
, *right
;
70 left
= get_type(expr
->left
);
71 right
= get_type(expr
->right
);
76 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
78 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
81 if (expr
->op
== SPECIAL_LEFTSHIFT
||
82 expr
->op
== SPECIAL_RIGHTSHIFT
) {
83 if (type_positive_bits(left
) < 31)
88 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
91 if (type_positive_bits(left
) > type_positive_bits(right
))
96 static struct symbol
*get_type_symbol(struct expression
*expr
)
98 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
101 return get_real_base_type(expr
->symbol
);
104 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
106 struct symbol
*tmp
, *sub
;
108 FOR_EACH_PTR(symbol_list
, tmp
) {
110 sub
= get_real_base_type(tmp
);
111 sub
= get_member_symbol(sub
->symbol_list
, member
);
116 if (tmp
->ident
== member
)
118 } END_FOR_EACH_PTR(tmp
);
123 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
125 struct ident
*member
;
128 if (!expr
|| expr
->type
!= EXPR_DEREF
)
131 member
= expr
->member
;
132 sym
= get_type(expr
->deref
);
134 // sm_msg("could not find struct type");
137 if (sym
->type
== SYM_PTR
)
138 sym
= get_real_base_type(sym
);
139 sym
= get_member_symbol(sym
->symbol_list
, member
);
142 return get_real_base_type(sym
);
145 static struct symbol
*get_return_type(struct expression
*expr
)
149 tmp
= get_type(expr
->fn
);
152 return get_real_base_type(tmp
);
155 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
157 if (stmt
->type
!= STMT_COMPOUND
)
159 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
160 if (!stmt
|| 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 struct symbol
*get_type(struct expression
*expr
)
218 expr
= strip_parens(expr
);
220 switch (expr
->type
) {
222 return &string_ctype
;
224 return get_type_symbol(expr
);
226 return get_symbol_from_deref(expr
);
230 return fake_pointer_sym(expr
);
232 return get_pointer_type(expr
->unop
);
233 return get_type(expr
->unop
);
234 case EXPR_ASSIGNMENT
:
235 return get_type(expr
->left
);
237 case EXPR_FORCE_CAST
:
238 case EXPR_IMPLIED_CAST
:
239 return get_real_base_type(expr
->cast_type
);
242 return get_binop_type(expr
);
244 return get_return_type(expr
);
246 return get_expr_stmt_type(expr
->statement
);
247 case EXPR_CONDITIONAL
:
249 return get_select_type(expr
);
255 // sm_msg("unhandled type %d", expr->type);
261 int type_unsigned(struct symbol
*base_type
)
265 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
270 int type_signed(struct symbol
*base_type
)
274 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
279 int expr_unsigned(struct expression
*expr
)
283 sym
= get_type(expr
);
286 if (type_unsigned(sym
))
291 int returns_unsigned(struct symbol
*sym
)
295 sym
= get_base_type(sym
);
296 if (!sym
|| sym
->type
!= SYM_FN
)
298 sym
= get_base_type(sym
);
299 return type_unsigned(sym
);
302 int is_pointer(struct expression
*expr
)
306 sym
= get_type(expr
);
309 if (sym
== &string_ctype
)
311 if (sym
->type
== SYM_PTR
)
316 int returns_pointer(struct symbol
*sym
)
320 sym
= get_base_type(sym
);
321 if (!sym
|| sym
->type
!= SYM_FN
)
323 sym
= get_base_type(sym
);
324 if (sym
->type
== SYM_PTR
)
329 sval_t
sval_type_max(struct symbol
*base_type
)
333 ret
.value
= (~0ULL) >> 1;
334 ret
.type
= base_type
;
336 if (!base_type
|| !type_bits(base_type
))
339 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
343 sval_t
sval_type_min(struct symbol
*base_type
)
347 if (!base_type
|| !type_bits(base_type
))
348 base_type
= &llong_ctype
;
349 ret
.type
= base_type
;
351 if (type_unsigned(base_type
)) {
356 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
361 int nr_bits(struct expression
*expr
)
365 type
= get_type(expr
);
368 return type_bits(type
);
371 int is_void_pointer(struct expression
*expr
)
375 type
= get_type(expr
);
376 if (!type
|| type
->type
!= SYM_PTR
)
378 type
= get_real_base_type(type
);
379 if (type
== &void_ctype
)
384 int is_char_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
== &char_ctype
)
397 int is_string(struct expression
*expr
)
399 expr
= strip_expr(expr
);
400 if (!expr
|| expr
->type
!= EXPR_STRING
)
407 int is_static(struct expression
*expr
)
413 name
= expr_to_str_sym(expr
, &sym
);
417 if (sym
->ctype
.modifiers
& MOD_STATIC
)
424 int types_equiv(struct symbol
*one
, struct symbol
*two
)
430 if (one
->type
!= two
->type
)
432 if (one
->type
== SYM_PTR
)
433 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
434 if (type_positive_bits(one
) != type_positive_bits(two
))
441 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
444 const char *global_static(void)
446 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
452 struct symbol
*cur_func_return_type(void)
456 sym
= get_real_base_type(cur_func_sym
);
457 if (!sym
|| sym
->type
!= SYM_FN
)
459 sym
= get_real_base_type(sym
);
463 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
465 struct symbol
*fn_type
;
467 struct symbol
*arg_type
;
470 fn_type
= get_type(fn
);
473 if (fn_type
->type
== SYM_PTR
)
474 fn_type
= get_real_base_type(fn_type
);
475 if (fn_type
->type
!= SYM_FN
)
479 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
480 arg_type
= get_real_base_type(tmp
);
485 } END_FOR_EACH_PTR(tmp
);
490 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
492 struct symbol
*tmp
, *sub
;
495 if (strncmp(name
, ".", 1) == 0)
497 if (strncmp(name
, "->", 2) == 0)
500 FOR_EACH_PTR(symbol_list
, tmp
) {
502 sub
= get_real_base_type(tmp
);
503 sub
= get_member_from_string(sub
->symbol_list
, name
);
509 if (strcmp(tmp
->ident
->name
, name
) == 0)
512 chunk_len
= strlen(tmp
->ident
->name
);
513 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
514 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
515 sub
= get_real_base_type(tmp
);
516 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
519 } END_FOR_EACH_PTR(tmp
);
524 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
529 if (strcmp(key
, "$") == 0)
530 return get_type(expr
);
532 if (strcmp(key
, "*$") == 0) {
533 sym
= get_type(expr
);
534 if (!sym
|| sym
->type
!= SYM_PTR
)
536 return get_real_base_type(sym
);
539 name
= expr_to_str_sym(expr
, &sym
);
543 sym
= get_real_base_type(sym
);
544 if (sym
->type
== SYM_PTR
)
545 sym
= get_real_base_type(sym
);
548 sym
= get_member_from_string(sym
->symbol_list
, key
);
551 return get_real_base_type(sym
);
554 int is_struct(struct expression
*expr
)
558 type
= get_type(expr
);
559 if (type
&& type
->type
== SYM_STRUCT
)
568 {&bool_ctype
, "bool"},
569 {&void_ctype
, "void"},
570 {&type_ctype
, "type"},
571 {&char_ctype
, "char"},
572 {&schar_ctype
, "schar"},
573 {&uchar_ctype
, "uchar"},
574 {&short_ctype
, "short"},
575 {&sshort_ctype
, "sshort"},
576 {&ushort_ctype
, "ushort"},
578 {&sint_ctype
, "sint"},
579 {&uint_ctype
, "uint"},
580 {&long_ctype
, "long"},
581 {&slong_ctype
, "slong"},
582 {&ulong_ctype
, "ulong"},
583 {&llong_ctype
, "llong"},
584 {&sllong_ctype
, "sllong"},
585 {&ullong_ctype
, "ullong"},
586 {&lllong_ctype
, "lllong"},
587 {&slllong_ctype
, "slllong"},
588 {&ulllong_ctype
, "ulllong"},
589 {&float_ctype
, "float"},
590 {&double_ctype
, "double"},
591 {&ldouble_ctype
, "ldouble"},
592 {&string_ctype
, "string"},
594 {&lazy_ptr_ctype
, "lazy_ptr"},
595 {&incomplete_ctype
, "incomplete"},
596 {&label_ctype
, "label"},
598 {&null_ctype
, "null"},
601 static const char *base_type_str(struct symbol
*sym
)
605 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
606 if (sym
== base_types
[i
].sym
)
607 return base_types
[i
].name
;
612 static int type_str_helper(char *buf
, int size
, struct symbol
*type
)
617 return snprintf(buf
, size
, "<unknown>");
619 if (type
->type
== SYM_BASETYPE
) {
620 return snprintf(buf
, size
, base_type_str(type
));
621 } else if (type
->type
== SYM_PTR
) {
622 type
= get_real_base_type(type
);
623 n
= type_str_helper(buf
, size
, type
);
626 return n
+ snprintf(buf
+ n
, size
- n
, "*");
627 } else if (type
->type
== SYM_STRUCT
) {
628 return snprintf(buf
, size
, "struct %s", type
->ident
? type
->ident
->name
: "");
629 } else if (type
->type
== SYM_FN
) {
630 struct symbol
*arg
, *return_type
, *arg_type
;
633 return_type
= get_real_base_type(type
);
634 n
= type_str_helper(buf
, size
, return_type
);
637 n
+= snprintf(buf
+ n
, size
- n
, "(*)(");
642 FOR_EACH_PTR(type
->arguments
, arg
) {
644 n
+= snprintf(buf
+ n
, size
- n
, ", ");
647 arg_type
= get_real_base_type(arg
);
648 n
+= type_str_helper(buf
+ n
, size
- n
, arg_type
);
651 } END_FOR_EACH_PTR(arg
);
653 return n
+ snprintf(buf
+ n
, size
- n
, ")");
654 } else if (type
->type
== SYM_NODE
) {
655 n
= snprintf(buf
, size
, "node {");
658 type
= get_real_base_type(type
);
659 n
+= type_str_helper(buf
+ n
, size
- n
, type
);
662 return n
+ snprintf(buf
+ n
, size
- n
, "}");
664 return snprintf(buf
, size
, "<type %d>", type
->type
);
668 char *type_to_str(struct symbol
*type
)
670 static char buf
[256];
673 type_str_helper(buf
, sizeof(buf
), type
);