sval: introduce sval_cmp_t()
[smatch.git] / smatch_type.c
blobddbd814a34db00dd3a57e458d2d61a8f1f364844
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_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);
41 if (!left || !right)
42 return NULL;
44 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
45 return left;
46 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
47 return right;
49 if (expr->op == SPECIAL_LEFTSHIFT ||
50 expr->op == SPECIAL_RIGHTSHIFT) {
51 if (type_positive_bits(left) < 31)
52 return &int_ctype;
53 return left;
56 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
57 return &int_ctype;
59 if (type_positive_bits(left) > type_positive_bits(right))
60 return left;
61 return right;
64 static struct symbol *get_type_symbol(struct expression *expr)
66 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
67 return NULL;
69 return get_real_base_type(expr->symbol);
72 static struct symbol *get_symbol_from_deref(struct expression *expr)
74 struct ident *member;
75 struct symbol *struct_sym;
76 struct symbol *tmp;
78 if (!expr || expr->type != EXPR_DEREF)
79 return NULL;
81 member = expr->member;
82 struct_sym = get_type(expr->deref);
83 if (!struct_sym) {
84 // sm_msg("could not find struct type");
85 return NULL;
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);
93 return NULL;
96 static struct symbol *get_return_type(struct expression *expr)
98 struct symbol *tmp;
100 tmp = get_type(expr->fn);
101 if (!tmp)
102 return NULL;
103 return get_real_base_type(tmp);
106 struct symbol *get_pointer_type(struct expression *expr)
108 struct symbol *sym;
110 sym = get_type(expr);
111 if (!sym || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
112 return NULL;
113 return get_real_base_type(sym);
116 static struct symbol *fake_pointer_sym(struct expression *expr)
118 struct symbol *sym;
119 struct symbol *base;
121 sym = alloc_symbol(expr->pos, SYM_PTR);
122 expr = expr->unop;
123 base = get_type(expr);
124 if (!base)
125 return NULL;
126 sym->ctype.base_type = base;
127 return sym;
130 struct symbol *get_type(struct expression *expr)
132 if (!expr)
133 return NULL;
134 expr = strip_parens(expr);
136 switch (expr->type) {
137 case EXPR_SYMBOL:
138 return get_type_symbol(expr);
139 case EXPR_DEREF:
140 return get_symbol_from_deref(expr);
141 case EXPR_PREOP:
142 case EXPR_POSTOP:
143 if (expr->op == '&')
144 return fake_pointer_sym(expr);
145 if (expr->op == '*')
146 return get_pointer_type(expr->unop);
147 return get_type(expr->unop);
148 case EXPR_ASSIGNMENT:
149 return get_type(expr->left);
150 case EXPR_CAST:
151 case EXPR_FORCE_CAST:
152 case EXPR_IMPLIED_CAST:
153 return get_real_base_type(expr->cast_type);
154 case EXPR_COMPARE:
155 case EXPR_BINOP:
156 return get_binop_type(expr);
157 case EXPR_CALL:
158 return get_return_type(expr);
159 case EXPR_SIZEOF:
160 return &ulong_ctype;
161 case EXPR_LOGICAL:
162 return &int_ctype;
163 default:
164 // sm_msg("unhandled type %d", expr->type);
165 return expr->ctype;
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 type_signed(struct symbol *base_type)
181 if (!base_type)
182 return 0;
183 if (base_type->ctype.modifiers & MOD_UNSIGNED)
184 return 0;
185 return 1;
188 int expr_unsigned(struct expression *expr)
190 struct symbol *sym;
192 sym = get_type(expr);
193 if (!sym)
194 return 0;
195 if (type_unsigned(sym))
196 return 1;
197 return 0;
200 int returns_unsigned(struct symbol *sym)
202 if (!sym)
203 return 0;
204 sym = get_base_type(sym);
205 if (!sym || sym->type != SYM_FN)
206 return 0;
207 sym = get_base_type(sym);
208 return type_unsigned(sym);
211 int is_pointer(struct expression *expr)
213 struct symbol *sym;
215 sym = get_type(expr);
216 if (!sym)
217 return 0;
218 if (sym->type == SYM_PTR)
219 return 1;
220 return 0;
223 int returns_pointer(struct symbol *sym)
225 if (!sym)
226 return 0;
227 sym = get_base_type(sym);
228 if (!sym || sym->type != SYM_FN)
229 return 0;
230 sym = get_base_type(sym);
231 if (sym->type == SYM_PTR)
232 return 1;
233 return 0;
236 sval_t sval_type_max(struct symbol *base_type)
238 sval_t ret;
240 ret.value = (~0ULL) >> 1;
241 ret.type = base_type;
243 if (!base_type || !base_type->bit_size)
244 return ret;
246 if (type_unsigned(base_type))
247 ret.value = (~0ULL) >> (64 - base_type->bit_size);
248 else
249 ret.value = (~0ULL) >> (64 - (base_type->bit_size - 1));
251 return ret;
254 sval_t sval_type_min(struct symbol *base_type)
256 sval_t ret;
258 if (!base_type || !base_type->bit_size)
259 base_type = &llong_ctype;
260 ret.type = base_type;
262 if (type_unsigned(base_type)) {
263 ret.value = 0;
264 return ret;
267 ret.value = (~0ULL) << (base_type->bit_size - 1);
269 return ret;
272 int nr_bits(struct expression *expr)
274 struct symbol *type;
276 type = get_type(expr);
277 if (!type)
278 return 0;
279 return type->bit_size;
282 int is_static(struct expression *expr)
284 char *name;
285 struct symbol *sym;
286 int ret = 0;
288 name = get_variable_from_expr_complex(expr, &sym);
289 if (!name || !sym)
290 goto free;
292 if (sym->ctype.modifiers & MOD_STATIC)
293 ret = 1;
294 free:
295 free_string(name);
296 return ret;
299 const char *global_static()
301 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
302 return "static";
303 else
304 return "global";
307 struct symbol *cur_func_return_type(void)
309 struct symbol *sym;
311 sym = get_real_base_type(cur_func_sym);
312 if (!sym || sym->type != SYM_FN)
313 return NULL;
314 sym = get_real_base_type(sym);
315 return sym;