sval: type: use type_bits() through out.
[smatch.git] / smatch_type.c
blob614235c9f9380299b65fc10def1758b28823c96c
1 /*
2 * sparse/smatch_types.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * The idea here is that you have an expression and you
12 * want to know what the type is for that.
15 #include "smatch.h"
17 struct symbol *get_real_base_type(struct symbol *sym)
19 struct symbol *ret;
21 ret = get_base_type(sym);
22 if (ret && ret->type == SYM_RESTRICT)
23 return get_real_base_type(ret);
24 return ret;
27 int type_bits(struct symbol *type)
29 if (!type)
30 return 0;
31 return type->bit_size;
34 int type_positive_bits(struct symbol *type)
36 if (!type)
37 return 0;
38 if (type_unsigned(type))
39 return type_bits(type);
40 return type_bits(type) - 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);
50 if (!left || !right)
51 return NULL;
53 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
54 return left;
55 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
56 return right;
58 if (expr->op == SPECIAL_LEFTSHIFT ||
59 expr->op == SPECIAL_RIGHTSHIFT) {
60 if (type_positive_bits(left) < 31)
61 return &int_ctype;
62 return left;
65 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
66 return &int_ctype;
68 if (type_positive_bits(left) > type_positive_bits(right))
69 return left;
70 return right;
73 static struct symbol *get_type_symbol(struct expression *expr)
75 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
76 return NULL;
78 return get_real_base_type(expr->symbol);
81 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
83 struct symbol *tmp;
85 FOR_EACH_PTR(symbol_list, tmp) {
86 if (!tmp->ident) {
87 tmp = get_real_base_type(tmp);
88 tmp = get_member_symbol(tmp->symbol_list, member);
89 if (tmp)
90 return tmp;
91 continue;
93 if (tmp->ident == member)
94 return tmp;
95 } END_FOR_EACH_PTR(tmp);
97 return NULL;
100 static struct symbol *get_symbol_from_deref(struct expression *expr)
102 struct ident *member;
103 struct symbol *sym;
105 if (!expr || expr->type != EXPR_DEREF)
106 return NULL;
108 member = expr->member;
109 sym = get_type(expr->deref);
110 if (!sym) {
111 // sm_msg("could not find struct type");
112 return NULL;
114 if (sym->type == SYM_PTR)
115 sym = get_real_base_type(sym);
116 sym = get_member_symbol(sym->symbol_list, member);
117 if (!sym)
118 return NULL;
119 return get_real_base_type(sym);
122 static struct symbol *get_return_type(struct expression *expr)
124 struct symbol *tmp;
126 tmp = get_type(expr->fn);
127 if (!tmp)
128 return NULL;
129 return get_real_base_type(tmp);
132 static struct symbol *get_expr_stmt_type(struct statement *stmt)
134 if (stmt->type != STMT_COMPOUND)
135 return NULL;
136 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
137 if (!stmt || stmt->type != STMT_EXPRESSION)
138 return NULL;
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);
148 if (!one || !two)
149 return NULL;
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
153 * probably Ok here.
155 if (type_positive_bits(one) > type_positive_bits(two))
156 return one;
157 return two;
160 struct symbol *get_pointer_type(struct expression *expr)
162 struct symbol *sym;
164 sym = get_type(expr);
165 if (!sym || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
166 return NULL;
167 return get_real_base_type(sym);
170 static struct symbol *fake_pointer_sym(struct expression *expr)
172 struct symbol *sym;
173 struct symbol *base;
175 sym = alloc_symbol(expr->pos, SYM_PTR);
176 expr = expr->unop;
177 base = get_type(expr);
178 if (!base)
179 return NULL;
180 sym->ctype.base_type = base;
181 return sym;
184 struct symbol *get_type(struct expression *expr)
186 if (!expr)
187 return NULL;
188 expr = strip_parens(expr);
190 switch (expr->type) {
191 case EXPR_SYMBOL:
192 return get_type_symbol(expr);
193 case EXPR_DEREF:
194 return get_symbol_from_deref(expr);
195 case EXPR_PREOP:
196 case EXPR_POSTOP:
197 if (expr->op == '&')
198 return fake_pointer_sym(expr);
199 if (expr->op == '*')
200 return get_pointer_type(expr->unop);
201 return get_type(expr->unop);
202 case EXPR_ASSIGNMENT:
203 return get_type(expr->left);
204 case EXPR_CAST:
205 case EXPR_FORCE_CAST:
206 case EXPR_IMPLIED_CAST:
207 return get_real_base_type(expr->cast_type);
208 case EXPR_COMPARE:
209 case EXPR_BINOP:
210 return get_binop_type(expr);
211 case EXPR_CALL:
212 return get_return_type(expr);
213 case EXPR_STATEMENT:
214 return get_expr_stmt_type(expr->statement);
215 case EXPR_CONDITIONAL:
216 case EXPR_SELECT:
217 return get_select_type(expr);
218 case EXPR_SIZEOF:
219 return &ulong_ctype;
220 case EXPR_LOGICAL:
221 return &int_ctype;
222 default:
223 // sm_msg("unhandled type %d", expr->type);
224 return expr->ctype;
226 return NULL;
229 int type_unsigned(struct symbol *base_type)
231 if (!base_type)
232 return 0;
233 if (base_type->ctype.modifiers & MOD_UNSIGNED)
234 return 1;
235 return 0;
238 int type_signed(struct symbol *base_type)
240 if (!base_type)
241 return 0;
242 if (base_type->ctype.modifiers & MOD_UNSIGNED)
243 return 0;
244 return 1;
247 int expr_unsigned(struct expression *expr)
249 struct symbol *sym;
251 sym = get_type(expr);
252 if (!sym)
253 return 0;
254 if (type_unsigned(sym))
255 return 1;
256 return 0;
259 int returns_unsigned(struct symbol *sym)
261 if (!sym)
262 return 0;
263 sym = get_base_type(sym);
264 if (!sym || sym->type != SYM_FN)
265 return 0;
266 sym = get_base_type(sym);
267 return type_unsigned(sym);
270 int is_pointer(struct expression *expr)
272 struct symbol *sym;
274 sym = get_type(expr);
275 if (!sym)
276 return 0;
277 if (sym->type == SYM_PTR)
278 return 1;
279 return 0;
282 int returns_pointer(struct symbol *sym)
284 if (!sym)
285 return 0;
286 sym = get_base_type(sym);
287 if (!sym || sym->type != SYM_FN)
288 return 0;
289 sym = get_base_type(sym);
290 if (sym->type == SYM_PTR)
291 return 1;
292 return 0;
295 sval_t sval_type_max(struct symbol *base_type)
297 sval_t ret;
299 ret.value = (~0ULL) >> 1;
300 ret.type = base_type;
302 if (!base_type || !base_type->bit_size)
303 return ret;
305 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
306 return ret;
309 sval_t sval_type_min(struct symbol *base_type)
311 sval_t ret;
313 if (!base_type || !base_type->bit_size)
314 base_type = &llong_ctype;
315 ret.type = base_type;
317 if (type_unsigned(base_type)) {
318 ret.value = 0;
319 return ret;
322 ret.value = (~0ULL) << type_positive_bits(base_type);
324 return ret;
327 int nr_bits(struct expression *expr)
329 struct symbol *type;
331 type = get_type(expr);
332 if (!type)
333 return 0;
334 return type_bits(type);
337 int is_static(struct expression *expr)
339 char *name;
340 struct symbol *sym;
341 int ret = 0;
343 name = get_variable_from_expr_complex(expr, &sym);
344 if (!name || !sym)
345 goto free;
347 if (sym->ctype.modifiers & MOD_STATIC)
348 ret = 1;
349 free:
350 free_string(name);
351 return ret;
354 int types_equiv(struct symbol *one, struct symbol *two)
356 if (!one && !two)
357 return 1;
358 if (!one || !two)
359 return 0;
360 if (one->type != two->type)
361 return 0;
362 if (one->type == SYM_PTR)
363 return types_equiv(get_real_base_type(one), get_real_base_type(two));
364 if (type_positive_bits(one) != type_positive_bits(two))
365 return 0;
366 return 1;
369 const char *global_static()
371 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
372 return "static";
373 else
374 return "global";
377 struct symbol *cur_func_return_type(void)
379 struct symbol *sym;
381 sym = get_real_base_type(cur_func_sym);
382 if (!sym || sym->type != SYM_FN)
383 return NULL;
384 sym = get_real_base_type(sym);
385 return sym;