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_bits(struct symbol
*type
)
31 return type
->bit_size
;
34 int type_positive_bits(struct symbol
*type
)
38 if (type_unsigned(type
))
39 return type
->bit_size
;
40 return type
->bit_size
- 1;
43 static struct symbol
*get_binop_type(struct expression
*expr
)
45 struct symbol
*left
, *right
;
47 left
= get_type(expr
->left
);
48 right
= get_type(expr
->right
);
53 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
55 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
58 if (expr
->op
== SPECIAL_LEFTSHIFT
||
59 expr
->op
== SPECIAL_RIGHTSHIFT
) {
60 if (type_positive_bits(left
) < 31)
65 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
68 if (type_positive_bits(left
) > type_positive_bits(right
))
73 static struct symbol
*get_type_symbol(struct expression
*expr
)
75 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
78 return get_real_base_type(expr
->symbol
);
81 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
85 FOR_EACH_PTR(symbol_list
, tmp
) {
87 tmp
= get_real_base_type(tmp
);
88 tmp
= get_member_symbol(tmp
->symbol_list
, member
);
93 if (tmp
->ident
== member
)
95 } END_FOR_EACH_PTR(tmp
);
100 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
102 struct ident
*member
;
105 if (!expr
|| expr
->type
!= EXPR_DEREF
)
108 member
= expr
->member
;
109 sym
= get_type(expr
->deref
);
111 // sm_msg("could not find struct type");
114 if (sym
->type
== SYM_PTR
)
115 sym
= get_real_base_type(sym
);
116 sym
= get_member_symbol(sym
->symbol_list
, member
);
119 return get_real_base_type(sym
);
122 static struct symbol
*get_return_type(struct expression
*expr
)
126 tmp
= get_type(expr
->fn
);
129 return get_real_base_type(tmp
);
132 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
134 if (stmt
->type
!= STMT_COMPOUND
)
136 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
137 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
139 return get_type(stmt
->expression
);
142 static struct symbol
*get_select_type(struct expression
*expr
)
144 struct symbol
*one
, *two
;
146 one
= get_type(expr
->cond_true
);
147 two
= get_type(expr
->cond_false
);
151 * This is a hack. If the types are not equiv then we
152 * really don't know the type. But I think guessing is
155 if (type_positive_bits(one
) > type_positive_bits(two
))
160 struct symbol
*get_pointer_type(struct expression
*expr
)
164 sym
= get_type(expr
);
165 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
167 return get_real_base_type(sym
);
170 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
175 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
177 base
= get_type(expr
);
180 sym
->ctype
.base_type
= base
;
184 struct symbol
*get_type(struct expression
*expr
)
188 expr
= strip_parens(expr
);
190 switch (expr
->type
) {
192 return get_type_symbol(expr
);
194 return get_symbol_from_deref(expr
);
198 return fake_pointer_sym(expr
);
200 return get_pointer_type(expr
->unop
);
201 return get_type(expr
->unop
);
202 case EXPR_ASSIGNMENT
:
203 return get_type(expr
->left
);
205 case EXPR_FORCE_CAST
:
206 case EXPR_IMPLIED_CAST
:
207 return get_real_base_type(expr
->cast_type
);
210 return get_binop_type(expr
);
212 return get_return_type(expr
);
214 return get_expr_stmt_type(expr
->statement
);
215 case EXPR_CONDITIONAL
:
217 return get_select_type(expr
);
223 // sm_msg("unhandled type %d", expr->type);
229 int type_unsigned(struct symbol
*base_type
)
233 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
238 int type_signed(struct symbol
*base_type
)
242 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
247 int expr_unsigned(struct expression
*expr
)
251 sym
= get_type(expr
);
254 if (type_unsigned(sym
))
259 int returns_unsigned(struct symbol
*sym
)
263 sym
= get_base_type(sym
);
264 if (!sym
|| sym
->type
!= SYM_FN
)
266 sym
= get_base_type(sym
);
267 return type_unsigned(sym
);
270 int is_pointer(struct expression
*expr
)
274 sym
= get_type(expr
);
277 if (sym
->type
== SYM_PTR
)
282 int returns_pointer(struct symbol
*sym
)
286 sym
= get_base_type(sym
);
287 if (!sym
|| sym
->type
!= SYM_FN
)
289 sym
= get_base_type(sym
);
290 if (sym
->type
== SYM_PTR
)
295 sval_t
sval_type_max(struct symbol
*base_type
)
299 ret
.value
= (~0ULL) >> 1;
300 ret
.type
= base_type
;
302 if (!base_type
|| !base_type
->bit_size
)
305 if (type_unsigned(base_type
))
306 ret
.value
= (~0ULL) >> (64 - base_type
->bit_size
);
308 ret
.value
= (~0ULL) >> (64 - (base_type
->bit_size
- 1));
313 sval_t
sval_type_min(struct symbol
*base_type
)
317 if (!base_type
|| !base_type
->bit_size
)
318 base_type
= &llong_ctype
;
319 ret
.type
= base_type
;
321 if (type_unsigned(base_type
)) {
326 ret
.value
= (~0ULL) << (base_type
->bit_size
- 1);
331 int nr_bits(struct expression
*expr
)
335 type
= get_type(expr
);
338 return type
->bit_size
;
341 int is_static(struct expression
*expr
)
347 name
= get_variable_from_expr_complex(expr
, &sym
);
351 if (sym
->ctype
.modifiers
& MOD_STATIC
)
358 int types_equiv(struct symbol
*one
, struct symbol
*two
)
364 if (one
->type
!= two
->type
)
366 if (one
->type
== SYM_PTR
)
367 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
368 if (type_positive_bits(one
) != type_positive_bits(two
))
373 const char *global_static()
375 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
381 struct symbol
*cur_func_return_type(void)
385 sym
= get_real_base_type(cur_func_sym
);
386 if (!sym
|| sym
->type
!= SYM_FN
)
388 sym
= get_real_base_type(sym
);