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 struct symbol
*get_pointer_type(struct expression
*expr
)
110 sym
= get_type(expr
);
111 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
113 return get_real_base_type(sym
);
116 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
121 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
123 base
= get_type(expr
);
126 sym
->ctype
.base_type
= base
;
130 struct symbol
*get_type(struct expression
*expr
)
134 expr
= strip_parens(expr
);
136 switch (expr
->type
) {
138 return get_type_symbol(expr
);
140 return get_symbol_from_deref(expr
);
144 return fake_pointer_sym(expr
);
146 return get_pointer_type(expr
->unop
);
147 return get_type(expr
->unop
);
148 case EXPR_ASSIGNMENT
:
149 return get_type(expr
->left
);
151 case EXPR_FORCE_CAST
:
152 case EXPR_IMPLIED_CAST
:
153 return get_real_base_type(expr
->cast_type
);
156 return get_binop_type(expr
);
158 return get_return_type(expr
);
164 // sm_msg("unhandled type %d", expr->type);
170 int type_unsigned(struct symbol
*base_type
)
174 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
179 int type_signed(struct symbol
*base_type
)
183 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
188 int expr_unsigned(struct expression
*expr
)
192 sym
= get_type(expr
);
195 if (type_unsigned(sym
))
200 int returns_unsigned(struct symbol
*sym
)
204 sym
= get_base_type(sym
);
205 if (!sym
|| sym
->type
!= SYM_FN
)
207 sym
= get_base_type(sym
);
208 return type_unsigned(sym
);
211 int is_pointer(struct expression
*expr
)
215 sym
= get_type(expr
);
218 if (sym
->type
== SYM_PTR
)
223 int returns_pointer(struct symbol
*sym
)
227 sym
= get_base_type(sym
);
228 if (!sym
|| sym
->type
!= SYM_FN
)
230 sym
= get_base_type(sym
);
231 if (sym
->type
== SYM_PTR
)
236 sval_t
sval_type_max(struct symbol
*base_type
)
240 ret
.value
= (~0ULL) >> 1;
241 ret
.type
= base_type
;
243 if (!base_type
|| !base_type
->bit_size
)
246 if (type_unsigned(base_type
))
247 ret
.value
= (~0ULL) >> (64 - base_type
->bit_size
);
249 ret
.value
= (~0ULL) >> (64 - (base_type
->bit_size
- 1));
254 sval_t
sval_type_min(struct symbol
*base_type
)
258 if (!base_type
|| !base_type
->bit_size
)
259 base_type
= &llong_ctype
;
260 ret
.type
= base_type
;
262 if (type_unsigned(base_type
)) {
267 ret
.value
= (~0ULL) << (base_type
->bit_size
- 1);
272 int nr_bits(struct expression
*expr
)
276 type
= get_type(expr
);
279 return type
->bit_size
;
282 int is_static(struct expression
*expr
)
288 name
= get_variable_from_expr_complex(expr
, &sym
);
292 if (sym
->ctype
.modifiers
& MOD_STATIC
)
299 const char *global_static()
301 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
307 struct symbol
*cur_func_return_type(void)
311 sym
= get_real_base_type(cur_func_sym
);
312 if (!sym
|| sym
->type
!= SYM_FN
)
314 sym
= get_real_base_type(sym
);