sval: type: get_type() should handle expression statements
[smatch.git] / smatch_type.c
blob3429124210fb1350b85d079900d251bf90d6a249
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 static struct symbol *get_expr_stmt_type(struct statement *stmt)
108 if (stmt->type != STMT_COMPOUND)
109 return NULL;
110 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
111 if (!stmt || stmt->type != STMT_EXPRESSION)
112 return NULL;
113 return get_type(stmt->expression);
116 struct symbol *get_pointer_type(struct expression *expr)
118 struct symbol *sym;
120 sym = get_type(expr);
121 if (!sym || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
122 return NULL;
123 return get_real_base_type(sym);
126 static struct symbol *fake_pointer_sym(struct expression *expr)
128 struct symbol *sym;
129 struct symbol *base;
131 sym = alloc_symbol(expr->pos, SYM_PTR);
132 expr = expr->unop;
133 base = get_type(expr);
134 if (!base)
135 return NULL;
136 sym->ctype.base_type = base;
137 return sym;
140 struct symbol *get_type(struct expression *expr)
142 if (!expr)
143 return NULL;
144 expr = strip_parens(expr);
146 switch (expr->type) {
147 case EXPR_SYMBOL:
148 return get_type_symbol(expr);
149 case EXPR_DEREF:
150 return get_symbol_from_deref(expr);
151 case EXPR_PREOP:
152 case EXPR_POSTOP:
153 if (expr->op == '&')
154 return fake_pointer_sym(expr);
155 if (expr->op == '*')
156 return get_pointer_type(expr->unop);
157 return get_type(expr->unop);
158 case EXPR_ASSIGNMENT:
159 return get_type(expr->left);
160 case EXPR_CAST:
161 case EXPR_FORCE_CAST:
162 case EXPR_IMPLIED_CAST:
163 return get_real_base_type(expr->cast_type);
164 case EXPR_COMPARE:
165 case EXPR_BINOP:
166 return get_binop_type(expr);
167 case EXPR_CALL:
168 return get_return_type(expr);
169 case EXPR_STATEMENT:
170 return get_expr_stmt_type(expr->statement);
171 case EXPR_SIZEOF:
172 return &ulong_ctype;
173 case EXPR_LOGICAL:
174 return &int_ctype;
175 default:
176 // sm_msg("unhandled type %d", expr->type);
177 return expr->ctype;
179 return NULL;
182 int type_unsigned(struct symbol *base_type)
184 if (!base_type)
185 return 0;
186 if (base_type->ctype.modifiers & MOD_UNSIGNED)
187 return 1;
188 return 0;
191 int type_signed(struct symbol *base_type)
193 if (!base_type)
194 return 0;
195 if (base_type->ctype.modifiers & MOD_UNSIGNED)
196 return 0;
197 return 1;
200 int expr_unsigned(struct expression *expr)
202 struct symbol *sym;
204 sym = get_type(expr);
205 if (!sym)
206 return 0;
207 if (type_unsigned(sym))
208 return 1;
209 return 0;
212 int returns_unsigned(struct symbol *sym)
214 if (!sym)
215 return 0;
216 sym = get_base_type(sym);
217 if (!sym || sym->type != SYM_FN)
218 return 0;
219 sym = get_base_type(sym);
220 return type_unsigned(sym);
223 int is_pointer(struct expression *expr)
225 struct symbol *sym;
227 sym = get_type(expr);
228 if (!sym)
229 return 0;
230 if (sym->type == SYM_PTR)
231 return 1;
232 return 0;
235 int returns_pointer(struct symbol *sym)
237 if (!sym)
238 return 0;
239 sym = get_base_type(sym);
240 if (!sym || sym->type != SYM_FN)
241 return 0;
242 sym = get_base_type(sym);
243 if (sym->type == SYM_PTR)
244 return 1;
245 return 0;
248 sval_t sval_type_max(struct symbol *base_type)
250 sval_t ret;
252 ret.value = (~0ULL) >> 1;
253 ret.type = base_type;
255 if (!base_type || !base_type->bit_size)
256 return ret;
258 if (type_unsigned(base_type))
259 ret.value = (~0ULL) >> (64 - base_type->bit_size);
260 else
261 ret.value = (~0ULL) >> (64 - (base_type->bit_size - 1));
263 return ret;
266 sval_t sval_type_min(struct symbol *base_type)
268 sval_t ret;
270 if (!base_type || !base_type->bit_size)
271 base_type = &llong_ctype;
272 ret.type = base_type;
274 if (type_unsigned(base_type)) {
275 ret.value = 0;
276 return ret;
279 ret.value = (~0ULL) << (base_type->bit_size - 1);
281 return ret;
284 int nr_bits(struct expression *expr)
286 struct symbol *type;
288 type = get_type(expr);
289 if (!type)
290 return 0;
291 return type->bit_size;
294 int is_static(struct expression *expr)
296 char *name;
297 struct symbol *sym;
298 int ret = 0;
300 name = get_variable_from_expr_complex(expr, &sym);
301 if (!name || !sym)
302 goto free;
304 if (sym->ctype.modifiers & MOD_STATIC)
305 ret = 1;
306 free:
307 free_string(name);
308 return ret;
311 int types_equiv(struct symbol *one, struct symbol *two)
313 if (!one && !two)
314 return 1;
315 if (!one || !two)
316 return 0;
317 if (one->type != two->type)
318 return 0;
319 if (one->type == SYM_PTR)
320 return types_equiv(get_real_base_type(one), get_real_base_type(two));
321 if (type_positive_bits(one) != type_positive_bits(two))
322 return 0;
323 return 1;
326 const char *global_static()
328 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
329 return "static";
330 else
331 return "global";
334 struct symbol *cur_func_return_type(void)
336 struct symbol *sym;
338 sym = get_real_base_type(cur_func_sym);
339 if (!sym || sym->type != SYM_FN)
340 return NULL;
341 sym = get_real_base_type(sym);
342 return sym;