2 * sparse/smatch_types.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * The idea here is that you have an expression and you
12 * want to know what the type is for that.
17 struct symbol
*get_real_base_type(struct symbol
*sym
)
21 ret
= get_base_type(sym
);
22 if (ret
&& ret
->type
== SYM_RESTRICT
)
23 return get_real_base_type(ret
);
27 int type_positive_bits(struct symbol
*type
)
29 if (type_unsigned(type
))
30 return type
->bit_size
;
31 return type
->bit_size
- 1;
34 static struct symbol
*get_binop_type(struct expression
*expr
)
36 struct symbol
*left
, *right
;
38 left
= get_type(expr
->left
);
39 right
= get_type(expr
->right
);
44 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
46 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
49 if (expr
->op
== SPECIAL_LEFTSHIFT
||
50 expr
->op
== SPECIAL_RIGHTSHIFT
) {
51 if (type_positive_bits(left
) < 31)
56 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
59 if (type_positive_bits(left
) > type_positive_bits(right
))
64 static struct symbol
*get_type_symbol(struct expression
*expr
)
66 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
69 return get_real_base_type(expr
->symbol
);
72 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
75 struct symbol
*struct_sym
;
78 if (!expr
|| expr
->type
!= EXPR_DEREF
)
81 member
= expr
->member
;
82 struct_sym
= get_type(expr
->deref
);
84 // sm_msg("could not find struct type");
87 if (struct_sym
->type
== SYM_PTR
)
88 struct_sym
= get_real_base_type(struct_sym
);
89 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
90 if (tmp
->ident
== member
)
91 return get_real_base_type(tmp
);
92 } END_FOR_EACH_PTR(tmp
);
96 static struct symbol
*get_return_type(struct expression
*expr
)
100 tmp
= get_type(expr
->fn
);
103 return get_real_base_type(tmp
);
106 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
108 if (stmt
->type
!= STMT_COMPOUND
)
110 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
111 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
113 return get_type(stmt
->expression
);
116 struct symbol
*get_pointer_type(struct expression
*expr
)
120 sym
= get_type(expr
);
121 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
123 return get_real_base_type(sym
);
126 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
131 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
133 base
= get_type(expr
);
136 sym
->ctype
.base_type
= base
;
140 struct symbol
*get_type(struct expression
*expr
)
144 expr
= strip_parens(expr
);
146 switch (expr
->type
) {
148 return get_type_symbol(expr
);
150 return get_symbol_from_deref(expr
);
154 return fake_pointer_sym(expr
);
156 return get_pointer_type(expr
->unop
);
157 return get_type(expr
->unop
);
158 case EXPR_ASSIGNMENT
:
159 return get_type(expr
->left
);
161 case EXPR_FORCE_CAST
:
162 case EXPR_IMPLIED_CAST
:
163 return get_real_base_type(expr
->cast_type
);
166 return get_binop_type(expr
);
168 return get_return_type(expr
);
170 return get_expr_stmt_type(expr
->statement
);
176 // sm_msg("unhandled type %d", expr->type);
182 int type_unsigned(struct symbol
*base_type
)
186 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
191 int type_signed(struct symbol
*base_type
)
195 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
200 int expr_unsigned(struct expression
*expr
)
204 sym
= get_type(expr
);
207 if (type_unsigned(sym
))
212 int returns_unsigned(struct symbol
*sym
)
216 sym
= get_base_type(sym
);
217 if (!sym
|| sym
->type
!= SYM_FN
)
219 sym
= get_base_type(sym
);
220 return type_unsigned(sym
);
223 int is_pointer(struct expression
*expr
)
227 sym
= get_type(expr
);
230 if (sym
->type
== SYM_PTR
)
235 int returns_pointer(struct symbol
*sym
)
239 sym
= get_base_type(sym
);
240 if (!sym
|| sym
->type
!= SYM_FN
)
242 sym
= get_base_type(sym
);
243 if (sym
->type
== SYM_PTR
)
248 sval_t
sval_type_max(struct symbol
*base_type
)
252 ret
.value
= (~0ULL) >> 1;
253 ret
.type
= base_type
;
255 if (!base_type
|| !base_type
->bit_size
)
258 if (type_unsigned(base_type
))
259 ret
.value
= (~0ULL) >> (64 - base_type
->bit_size
);
261 ret
.value
= (~0ULL) >> (64 - (base_type
->bit_size
- 1));
266 sval_t
sval_type_min(struct symbol
*base_type
)
270 if (!base_type
|| !base_type
->bit_size
)
271 base_type
= &llong_ctype
;
272 ret
.type
= base_type
;
274 if (type_unsigned(base_type
)) {
279 ret
.value
= (~0ULL) << (base_type
->bit_size
- 1);
284 int nr_bits(struct expression
*expr
)
288 type
= get_type(expr
);
291 return type
->bit_size
;
294 int is_static(struct expression
*expr
)
300 name
= get_variable_from_expr_complex(expr
, &sym
);
304 if (sym
->ctype
.modifiers
& MOD_STATIC
)
311 int types_equiv(struct symbol
*one
, struct symbol
*two
)
317 if (one
->type
!= two
->type
)
319 if (one
->type
== SYM_PTR
)
320 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
321 if (type_positive_bits(one
) != type_positive_bits(two
))
326 const char *global_static()
328 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
334 struct symbol
*cur_func_return_type(void)
338 sym
= get_real_base_type(cur_func_sym
);
339 if (!sym
|| sym
->type
!= SYM_FN
)
341 sym
= get_real_base_type(sym
);