sval: fix sval_cmp() a bit
[smatch.git] / smatch_type.c
blobd4fd1a73234508b194e635628e34e5d1ecad5087
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 sval_t sval_type_max(struct symbol *base_type)
229 sval_t ret;
231 ret.value = (~0ULL) >> 1;
232 ret.type = base_type;
234 if (!base_type || !base_type->bit_size)
235 return ret;
237 if (type_unsigned(base_type))
238 ret.value = (~0ULL) >> (64 - base_type->bit_size);
239 else
240 ret.value = (~0ULL) >> (64 - (base_type->bit_size - 1));
242 return ret;
245 sval_t sval_type_min(struct symbol *base_type)
247 sval_t ret;
249 if (!base_type || !base_type->bit_size)
250 base_type = &llong_ctype;
251 ret.type = base_type;
253 if (type_unsigned(base_type)) {
254 ret.value = 0;
255 return ret;
258 ret.value = (~0ULL) << (base_type->bit_size - 1);
260 return ret;
263 int nr_bits(struct expression *expr)
265 struct symbol *type;
267 type = get_type(expr);
268 if (!type)
269 return 0;
270 return type->bit_size;
273 int is_static(struct expression *expr)
275 char *name;
276 struct symbol *sym;
277 int ret = 0;
279 name = get_variable_from_expr_complex(expr, &sym);
280 if (!name || !sym)
281 goto free;
283 if (sym->ctype.modifiers & MOD_STATIC)
284 ret = 1;
285 free:
286 free_string(name);
287 return ret;
290 const char *global_static()
292 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
293 return "static";
294 else
295 return "global";
298 struct symbol *cur_func_return_type(void)
300 struct symbol *sym;
302 sym = get_real_base_type(cur_func_sym);
303 if (!sym || sym->type != SYM_FN)
304 return NULL;
305 sym = get_real_base_type(sym);
306 return sym;