dev_queue_xmit: turn on by default
[smatch.git] / smatch_type.c
blob0214166d6ae7d04f661ddfdbfcae6574865ce1d4
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;
31 left = get_type(expr->left);
32 right = get_type(expr->right);
34 if (!left || !right)
35 return NULL;
37 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
38 return left;
39 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
40 return right;
42 if (expr->op == SPECIAL_LEFTSHIFT ||
43 expr->op == SPECIAL_RIGHTSHIFT) {
44 if (type_max(left) < type_max(&int_ctype))
45 return &int_ctype;
46 return left;
49 if (type_max(left) < type_max(&int_ctype) &&
50 type_max(right) < type_max(&int_ctype))
51 return &int_ctype;
53 if (type_max(right) > type_max(left))
54 return right;
55 return left;
58 static struct symbol *get_type_symbol(struct expression *expr)
60 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
61 return NULL;
63 return get_real_base_type(expr->symbol);
66 static struct symbol *get_symbol_from_deref(struct expression *expr)
68 struct ident *member;
69 struct symbol *struct_sym;
70 struct symbol *tmp;
72 if (!expr || expr->type != EXPR_DEREF)
73 return NULL;
75 member = expr->member;
76 struct_sym = get_type(expr->deref);
77 if (!struct_sym) {
78 // sm_msg("could not find struct type");
79 return NULL;
81 if (struct_sym->type == SYM_PTR)
82 struct_sym = get_real_base_type(struct_sym);
83 FOR_EACH_PTR(struct_sym->symbol_list, tmp) {
84 if (tmp->ident == member)
85 return get_real_base_type(tmp);
86 } END_FOR_EACH_PTR(tmp);
87 return NULL;
90 static struct symbol *get_return_type(struct expression *expr)
92 struct symbol *tmp;
94 tmp = get_type(expr->fn);
95 if (!tmp)
96 return NULL;
97 return get_real_base_type(tmp);
100 struct symbol *get_pointer_type(struct expression *expr)
102 struct symbol *sym;
104 sym = get_type(expr);
105 if (!sym || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
106 return NULL;
107 return get_real_base_type(sym);
110 static struct symbol *fake_pointer_sym(struct expression *expr)
112 struct symbol *sym;
113 struct symbol *base;
115 sym = alloc_symbol(expr->pos, SYM_PTR);
116 expr = expr->unop;
117 base = get_type(expr);
118 if (!base)
119 return NULL;
120 sym->ctype.base_type = base;
121 return sym;
124 struct symbol *get_type(struct expression *expr)
126 if (!expr)
127 return NULL;
128 expr = strip_parens(expr);
130 switch (expr->type) {
131 case EXPR_SYMBOL:
132 return get_type_symbol(expr);
133 case EXPR_DEREF:
134 return get_symbol_from_deref(expr);
135 case EXPR_PREOP:
136 if (expr->op == '&')
137 return fake_pointer_sym(expr);
138 if (expr->op == '*')
139 return get_pointer_type(expr->unop);
140 return get_type(expr->unop);
141 case EXPR_CAST:
142 case EXPR_FORCE_CAST:
143 case EXPR_IMPLIED_CAST:
144 return get_real_base_type(expr->cast_type);
145 case EXPR_BINOP:
146 return get_binop_type(expr);
147 case EXPR_CALL:
148 return get_return_type(expr);
149 case EXPR_SIZEOF:
150 return &ulong_ctype;
151 default:
152 return expr->ctype;
153 // sm_msg("unhandled type %d", expr->type);
157 return NULL;
160 int type_unsigned(struct symbol *base_type)
162 if (!base_type)
163 return 0;
164 if (base_type->ctype.modifiers & MOD_UNSIGNED)
165 return 1;
166 return 0;
169 int expr_unsigned(struct expression *expr)
171 struct symbol *sym;
173 sym = get_type(expr);
174 if (!sym)
175 return 0;
176 if (type_unsigned(sym))
177 return 1;
178 return 0;
181 int returns_unsigned(struct symbol *sym)
183 if (!sym)
184 return 0;
185 sym = get_base_type(sym);
186 if (!sym || sym->type != SYM_FN)
187 return 0;
188 sym = get_base_type(sym);
189 return type_unsigned(sym);
192 int is_pointer(struct expression *expr)
194 struct symbol *sym;
196 sym = get_type(expr);
197 if (!sym)
198 return 0;
199 if (sym->type == SYM_PTR)
200 return 1;
201 return 0;
204 int returns_pointer(struct symbol *sym)
206 if (!sym)
207 return 0;
208 sym = get_base_type(sym);
209 if (!sym || sym->type != SYM_FN)
210 return 0;
211 sym = get_base_type(sym);
212 if (sym->type == SYM_PTR)
213 return 1;
214 return 0;
217 long long type_max(struct symbol *base_type)
219 long long ret = whole_range.max;
220 int bits;
222 if (!base_type || !base_type->bit_size)
223 return ret;
224 bits = base_type->bit_size;
225 if (bits == 64)
226 return ret;
227 if (!type_unsigned(base_type))
228 bits--;
229 ret >>= (63 - bits);
230 return ret;
233 long long type_min(struct symbol *base_type)
235 long long ret = whole_range.min;
236 int bits;
238 if (!base_type || !base_type->bit_size)
239 return ret;
240 if (type_unsigned(base_type))
241 return 0;
242 ret = whole_range.max;
243 bits = base_type->bit_size - 1;
244 ret >>= (63 - bits);
245 return -(ret + 1);
248 int nr_bits(struct expression *expr)
250 struct symbol *type;
252 type = get_type(expr);
253 if (!type)
254 return 0;
255 return type->bit_size;
258 int is_static(struct expression *expr)
260 char *name;
261 struct symbol *sym;
262 int ret = 0;
264 name = get_variable_from_expr_complex(expr, &sym);
265 if (!name || !sym)
266 goto free;
268 if (sym->ctype.modifiers & MOD_STATIC)
269 ret = 1;
270 free:
271 free_string(name);
272 return ret;
275 const char *global_static()
277 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
278 return "static";
279 else
280 return "global";