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_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
76 FOR_EACH_PTR(symbol_list
, tmp
) {
78 tmp
= get_real_base_type(tmp
);
79 tmp
= get_member_symbol(tmp
->symbol_list
, member
);
84 if (tmp
->ident
== member
)
86 } END_FOR_EACH_PTR(tmp
);
91 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
96 if (!expr
|| expr
->type
!= EXPR_DEREF
)
99 member
= expr
->member
;
100 sym
= get_type(expr
->deref
);
102 // sm_msg("could not find struct type");
105 if (sym
->type
== SYM_PTR
)
106 sym
= get_real_base_type(sym
);
107 sym
= get_member_symbol(sym
->symbol_list
, member
);
110 return get_real_base_type(sym
);
113 static struct symbol
*get_return_type(struct expression
*expr
)
117 tmp
= get_type(expr
->fn
);
120 return get_real_base_type(tmp
);
123 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
125 if (stmt
->type
!= STMT_COMPOUND
)
127 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
128 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
130 return get_type(stmt
->expression
);
133 static struct symbol
*get_select_type(struct expression
*expr
)
135 struct symbol
*one
, *two
;
137 one
= get_type(expr
->cond_true
);
138 two
= get_type(expr
->cond_false
);
141 if (types_equiv(one
, two
))
146 struct symbol
*get_pointer_type(struct expression
*expr
)
150 sym
= get_type(expr
);
151 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
153 return get_real_base_type(sym
);
156 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
161 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
163 base
= get_type(expr
);
166 sym
->ctype
.base_type
= base
;
170 struct symbol
*get_type(struct expression
*expr
)
174 expr
= strip_parens(expr
);
176 switch (expr
->type
) {
178 return get_type_symbol(expr
);
180 return get_symbol_from_deref(expr
);
184 return fake_pointer_sym(expr
);
186 return get_pointer_type(expr
->unop
);
187 return get_type(expr
->unop
);
188 case EXPR_ASSIGNMENT
:
189 return get_type(expr
->left
);
191 case EXPR_FORCE_CAST
:
192 case EXPR_IMPLIED_CAST
:
193 return get_real_base_type(expr
->cast_type
);
196 return get_binop_type(expr
);
198 return get_return_type(expr
);
200 return get_expr_stmt_type(expr
->statement
);
201 case EXPR_CONDITIONAL
:
203 return get_select_type(expr
);
209 // sm_msg("unhandled type %d", expr->type);
215 int type_unsigned(struct symbol
*base_type
)
219 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
224 int type_signed(struct symbol
*base_type
)
228 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
233 int expr_unsigned(struct expression
*expr
)
237 sym
= get_type(expr
);
240 if (type_unsigned(sym
))
245 int returns_unsigned(struct symbol
*sym
)
249 sym
= get_base_type(sym
);
250 if (!sym
|| sym
->type
!= SYM_FN
)
252 sym
= get_base_type(sym
);
253 return type_unsigned(sym
);
256 int is_pointer(struct expression
*expr
)
260 sym
= get_type(expr
);
263 if (sym
->type
== SYM_PTR
)
268 int returns_pointer(struct symbol
*sym
)
272 sym
= get_base_type(sym
);
273 if (!sym
|| sym
->type
!= SYM_FN
)
275 sym
= get_base_type(sym
);
276 if (sym
->type
== SYM_PTR
)
281 sval_t
sval_type_max(struct symbol
*base_type
)
285 ret
.value
= (~0ULL) >> 1;
286 ret
.type
= base_type
;
288 if (!base_type
|| !base_type
->bit_size
)
291 if (type_unsigned(base_type
))
292 ret
.value
= (~0ULL) >> (64 - base_type
->bit_size
);
294 ret
.value
= (~0ULL) >> (64 - (base_type
->bit_size
- 1));
299 sval_t
sval_type_min(struct symbol
*base_type
)
303 if (!base_type
|| !base_type
->bit_size
)
304 base_type
= &llong_ctype
;
305 ret
.type
= base_type
;
307 if (type_unsigned(base_type
)) {
312 ret
.value
= (~0ULL) << (base_type
->bit_size
- 1);
317 int nr_bits(struct expression
*expr
)
321 type
= get_type(expr
);
324 return type
->bit_size
;
327 int is_static(struct expression
*expr
)
333 name
= get_variable_from_expr_complex(expr
, &sym
);
337 if (sym
->ctype
.modifiers
& MOD_STATIC
)
344 int types_equiv(struct symbol
*one
, struct symbol
*two
)
350 if (one
->type
!= two
->type
)
352 if (one
->type
== SYM_PTR
)
353 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
354 if (type_positive_bits(one
) != type_positive_bits(two
))
359 const char *global_static()
361 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
367 struct symbol
*cur_func_return_type(void)
371 sym
= get_real_base_type(cur_func_sym
);
372 if (!sym
|| sym
->type
!= SYM_FN
)
374 sym
= get_real_base_type(sym
);