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
);
150 if (types_equiv(one
, two
))
155 struct symbol
*get_pointer_type(struct expression
*expr
)
159 sym
= get_type(expr
);
160 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
162 return get_real_base_type(sym
);
165 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
170 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
172 base
= get_type(expr
);
175 sym
->ctype
.base_type
= base
;
179 struct symbol
*get_type(struct expression
*expr
)
183 expr
= strip_parens(expr
);
185 switch (expr
->type
) {
187 return get_type_symbol(expr
);
189 return get_symbol_from_deref(expr
);
193 return fake_pointer_sym(expr
);
195 return get_pointer_type(expr
->unop
);
196 return get_type(expr
->unop
);
197 case EXPR_ASSIGNMENT
:
198 return get_type(expr
->left
);
200 case EXPR_FORCE_CAST
:
201 case EXPR_IMPLIED_CAST
:
202 return get_real_base_type(expr
->cast_type
);
205 return get_binop_type(expr
);
207 return get_return_type(expr
);
209 return get_expr_stmt_type(expr
->statement
);
210 case EXPR_CONDITIONAL
:
212 return get_select_type(expr
);
218 // sm_msg("unhandled type %d", expr->type);
224 int type_unsigned(struct symbol
*base_type
)
228 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
233 int type_signed(struct symbol
*base_type
)
237 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
242 int expr_unsigned(struct expression
*expr
)
246 sym
= get_type(expr
);
249 if (type_unsigned(sym
))
254 int returns_unsigned(struct symbol
*sym
)
258 sym
= get_base_type(sym
);
259 if (!sym
|| sym
->type
!= SYM_FN
)
261 sym
= get_base_type(sym
);
262 return type_unsigned(sym
);
265 int is_pointer(struct expression
*expr
)
269 sym
= get_type(expr
);
272 if (sym
->type
== SYM_PTR
)
277 int returns_pointer(struct symbol
*sym
)
281 sym
= get_base_type(sym
);
282 if (!sym
|| sym
->type
!= SYM_FN
)
284 sym
= get_base_type(sym
);
285 if (sym
->type
== SYM_PTR
)
290 sval_t
sval_type_max(struct symbol
*base_type
)
294 ret
.value
= (~0ULL) >> 1;
295 ret
.type
= base_type
;
297 if (!base_type
|| !base_type
->bit_size
)
300 if (type_unsigned(base_type
))
301 ret
.value
= (~0ULL) >> (64 - base_type
->bit_size
);
303 ret
.value
= (~0ULL) >> (64 - (base_type
->bit_size
- 1));
308 sval_t
sval_type_min(struct symbol
*base_type
)
312 if (!base_type
|| !base_type
->bit_size
)
313 base_type
= &llong_ctype
;
314 ret
.type
= base_type
;
316 if (type_unsigned(base_type
)) {
321 ret
.value
= (~0ULL) << (base_type
->bit_size
- 1);
326 int nr_bits(struct expression
*expr
)
330 type
= get_type(expr
);
333 return type
->bit_size
;
336 int is_static(struct expression
*expr
)
342 name
= get_variable_from_expr_complex(expr
, &sym
);
346 if (sym
->ctype
.modifiers
& MOD_STATIC
)
353 int types_equiv(struct symbol
*one
, struct symbol
*two
)
359 if (one
->type
!= two
->type
)
361 if (one
->type
== SYM_PTR
)
362 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
363 if (type_positive_bits(one
) != type_positive_bits(two
))
368 const char *global_static()
370 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
376 struct symbol
*cur_func_return_type(void)
380 sym
= get_real_base_type(cur_func_sym
);
381 if (!sym
|| sym
->type
!= SYM_FN
)
383 sym
= get_real_base_type(sym
);