sval: change type_min/max() => sval_type_min/max()
[smatch.git] / smatch_type.c
blob09534e4e79f1164d527b881e249db65dcc83eb39
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 static struct symbol *get_binop_type(struct expression *expr)
29 struct symbol *left, *right;
30 sval_t left_max, right_max, int_max;
32 left = get_type(expr->left);
33 right = get_type(expr->right);
35 if (!left || !right)
36 return NULL;
38 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
39 return left;
40 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
41 return right;
43 left_max = sval_type_max(left);
44 right_max = sval_type_max(right);
45 int_max = sval_type_max(&int_ctype);
47 if (expr->op == SPECIAL_LEFTSHIFT ||
48 expr->op == SPECIAL_RIGHTSHIFT) {
49 if (sval_cmp(left_max, int_max) < 0)
50 return &int_ctype;
51 return left;
54 if (sval_cmp(left_max, int_max) < 0 &&
55 sval_cmp(right_max, int_max) < 0)
56 return &int_ctype;
58 if (sval_cmp(right_max, left_max) > 0)
59 return right;
60 return left;
63 static struct symbol *get_type_symbol(struct expression *expr)
65 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
66 return NULL;
68 return get_real_base_type(expr->symbol);
71 static struct symbol *get_symbol_from_deref(struct expression *expr)
73 struct ident *member;
74 struct symbol *struct_sym;
75 struct symbol *tmp;
77 if (!expr || expr->type != EXPR_DEREF)
78 return NULL;
80 member = expr->member;
81 struct_sym = get_type(expr->deref);
82 if (!struct_sym) {
83 // sm_msg("could not find struct type");
84 return NULL;
86 if (struct_sym->type == SYM_PTR)
87 struct_sym = get_real_base_type(struct_sym);
88 FOR_EACH_PTR(struct_sym->symbol_list, tmp) {
89 if (tmp->ident == member)
90 return get_real_base_type(tmp);
91 } END_FOR_EACH_PTR(tmp);
92 return NULL;
95 static struct symbol *get_return_type(struct expression *expr)
97 struct symbol *tmp;
99 tmp = get_type(expr->fn);
100 if (!tmp)
101 return NULL;
102 return get_real_base_type(tmp);
105 struct symbol *get_pointer_type(struct expression *expr)
107 struct symbol *sym;
109 sym = get_type(expr);
110 if (!sym || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
111 return NULL;
112 return get_real_base_type(sym);
115 static struct symbol *fake_pointer_sym(struct expression *expr)
117 struct symbol *sym;
118 struct symbol *base;
120 sym = alloc_symbol(expr->pos, SYM_PTR);
121 expr = expr->unop;
122 base = get_type(expr);
123 if (!base)
124 return NULL;
125 sym->ctype.base_type = base;
126 return sym;
129 struct symbol *get_type(struct expression *expr)
131 if (!expr)
132 return NULL;
133 expr = strip_parens(expr);
135 switch (expr->type) {
136 case EXPR_SYMBOL:
137 return get_type_symbol(expr);
138 case EXPR_DEREF:
139 return get_symbol_from_deref(expr);
140 case EXPR_PREOP:
141 if (expr->op == '&')
142 return fake_pointer_sym(expr);
143 if (expr->op == '*')
144 return get_pointer_type(expr->unop);
145 return get_type(expr->unop);
146 case EXPR_ASSIGNMENT:
147 return get_type(expr->left);
148 case EXPR_CAST:
149 case EXPR_FORCE_CAST:
150 case EXPR_IMPLIED_CAST:
151 return get_real_base_type(expr->cast_type);
152 case EXPR_BINOP:
153 return get_binop_type(expr);
154 case EXPR_CALL:
155 return get_return_type(expr);
156 case EXPR_SIZEOF:
157 return &ulong_ctype;
158 case EXPR_COMPARE:
159 case EXPR_LOGICAL:
160 return &int_ctype;
161 default:
162 return expr->ctype;
163 // sm_msg("unhandled type %d", expr->type);
167 return NULL;
170 int type_unsigned(struct symbol *base_type)
172 if (!base_type)
173 return 0;
174 if (base_type->ctype.modifiers & MOD_UNSIGNED)
175 return 1;
176 return 0;
179 int expr_unsigned(struct expression *expr)
181 struct symbol *sym;
183 sym = get_type(expr);
184 if (!sym)
185 return 0;
186 if (type_unsigned(sym))
187 return 1;
188 return 0;
191 int returns_unsigned(struct symbol *sym)
193 if (!sym)
194 return 0;
195 sym = get_base_type(sym);
196 if (!sym || sym->type != SYM_FN)
197 return 0;
198 sym = get_base_type(sym);
199 return type_unsigned(sym);
202 int is_pointer(struct expression *expr)
204 struct symbol *sym;
206 sym = get_type(expr);
207 if (!sym)
208 return 0;
209 if (sym->type == SYM_PTR)
210 return 1;
211 return 0;
214 int returns_pointer(struct symbol *sym)
216 if (!sym)
217 return 0;
218 sym = get_base_type(sym);
219 if (!sym || sym->type != SYM_FN)
220 return 0;
221 sym = get_base_type(sym);
222 if (sym->type == SYM_PTR)
223 return 1;
224 return 0;
227 long long type_max(struct symbol *base_type)
229 long long ret = whole_range.max;
230 int bits;
232 if (!base_type || !base_type->bit_size)
233 return ret;
234 bits = base_type->bit_size;
235 if (bits == 64)
236 return ret;
237 if (!type_unsigned(base_type))
238 bits--;
239 ret >>= (63 - bits);
240 return ret;
243 sval_t sval_type_max(struct symbol *base_type)
245 sval_t ret;
247 ret.value = (~0ULL) >> 1;
248 ret.type = base_type;
250 if (!base_type || !base_type->bit_size)
251 return ret;
253 if (type_unsigned(base_type))
254 ret.value = (~0ULL) >> (64 - base_type->bit_size);
255 else
256 ret.value = (~0ULL) >> (64 - (base_type->bit_size - 1));
258 return ret;
261 long long type_min(struct symbol *base_type)
263 long long ret = whole_range.min;
264 int bits;
266 if (!base_type || !base_type->bit_size)
267 return ret;
268 if (type_unsigned(base_type))
269 return 0;
270 ret = whole_range.max;
271 bits = base_type->bit_size - 1;
272 ret >>= (63 - bits);
273 return -(ret + 1);
276 sval_t sval_type_min(struct symbol *base_type)
278 sval_t ret;
280 if (!base_type || !base_type->bit_size)
281 base_type = &llong_ctype;
282 ret.type = base_type;
284 if (type_unsigned(base_type)) {
285 ret.value = 0;
286 return ret;
289 ret.value = (~0ULL) << (base_type->bit_size - 1);
291 return ret;
294 int nr_bits(struct expression *expr)
296 struct symbol *type;
298 type = get_type(expr);
299 if (!type)
300 return 0;
301 return type->bit_size;
304 int is_static(struct expression *expr)
306 char *name;
307 struct symbol *sym;
308 int ret = 0;
310 name = get_variable_from_expr_complex(expr, &sym);
311 if (!name || !sym)
312 goto free;
314 if (sym->ctype.modifiers & MOD_STATIC)
315 ret = 1;
316 free:
317 free_string(name);
318 return ret;
321 const char *global_static()
323 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
324 return "static";
325 else
326 return "global";