buf_size: store buffer sizes in db (turned off by default)
[smatch.git] / smatch_type.c
blobcf9a031190d6ae3c6863868e6ff7b622a8601a41
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_type_symbol(struct expression *expr)
29 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
30 return NULL;
32 return get_real_base_type(expr->symbol);
35 static struct symbol *get_symbol_from_deref(struct expression *expr)
37 struct ident *member;
38 struct symbol *struct_sym;
39 struct symbol *tmp;
41 if (!expr || expr->type != EXPR_DEREF)
42 return NULL;
44 member = expr->member;
45 struct_sym = get_type(expr->deref);
46 if (!struct_sym) {
47 // sm_msg("could not find struct type");
48 return NULL;
50 if (struct_sym->type == SYM_PTR)
51 struct_sym = get_real_base_type(struct_sym);
52 FOR_EACH_PTR(struct_sym->symbol_list, tmp) {
53 if (tmp->ident == member)
54 return get_real_base_type(tmp);
55 } END_FOR_EACH_PTR(tmp);
56 return NULL;
59 static struct symbol *get_return_type(struct expression *expr)
61 struct symbol *tmp;
63 tmp = get_type(expr->fn);
64 if (!tmp)
65 return NULL;
66 return get_real_base_type(tmp);
69 struct symbol *get_pointer_type(struct expression *expr)
71 struct symbol *sym;
73 sym = get_type(expr);
74 if (!sym || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
75 return NULL;
76 return get_real_base_type(sym);
79 static struct symbol *fake_pointer_sym(struct expression *expr)
81 struct symbol *sym;
82 struct symbol *base;
84 sym = alloc_symbol(expr->pos, SYM_PTR);
85 expr = expr->unop;
86 base = get_type(expr);
87 if (!base)
88 return NULL;
89 sym->ctype.base_type = base;
90 return sym;
93 struct symbol *get_type(struct expression *expr)
95 struct symbol *tmp;
97 if (!expr)
98 return NULL;
99 expr = strip_parens(expr);
101 switch (expr->type) {
102 case EXPR_SYMBOL:
103 return get_type_symbol(expr);
104 case EXPR_DEREF:
105 return get_symbol_from_deref(expr);
106 case EXPR_PREOP:
107 if (expr->op == '&')
108 return fake_pointer_sym(expr);
109 if (expr->op == '*')
110 return get_pointer_type(expr->unop);
111 return get_type(expr->unop);
112 case EXPR_CAST:
113 case EXPR_FORCE_CAST:
114 case EXPR_IMPLIED_CAST:
115 return get_real_base_type(expr->cast_type);
116 case EXPR_BINOP:
117 if (expr->op != '+')
118 return NULL;
119 tmp = get_type(expr->left);
120 return tmp;
121 case EXPR_CALL:
122 return get_return_type(expr);
123 case EXPR_SIZEOF:
124 return &ulong_ctype;
125 default:
126 return expr->ctype;
127 // sm_msg("unhandled type %d", expr->type);
131 return NULL;
134 int type_unsigned(struct symbol *base_type)
136 if (!base_type)
137 return 0;
138 if (base_type->ctype.modifiers & MOD_UNSIGNED)
139 return 1;
140 return 0;
143 int expr_unsigned(struct expression *expr)
145 struct symbol *sym;
147 sym = get_type(expr);
148 if (!sym)
149 return 0;
150 if (type_unsigned(sym))
151 return 1;
152 return 0;
155 int returns_unsigned(struct symbol *sym)
157 if (!sym)
158 return 0;
159 sym = get_base_type(sym);
160 if (!sym || sym->type != SYM_FN)
161 return 0;
162 sym = get_base_type(sym);
163 return type_unsigned(sym);
166 int returns_pointer(struct symbol *sym)
168 if (!sym)
169 return 0;
170 sym = get_base_type(sym);
171 if (!sym || sym->type != SYM_FN)
172 return 0;
173 sym = get_base_type(sym);
174 if (sym->type == SYM_PTR)
175 return 1;
176 return 0;
179 long long type_max(struct symbol *base_type)
181 long long ret = whole_range.max;
182 int bits;
184 if (!base_type || !base_type->bit_size)
185 return ret;
186 bits = base_type->bit_size;
187 if (bits == 64)
188 return ret;
189 if (!type_unsigned(base_type))
190 bits--;
191 ret >>= (63 - bits);
192 return ret;
195 long long type_min(struct symbol *base_type)
197 long long ret = whole_range.min;
198 int bits;
200 if (!base_type || !base_type->bit_size)
201 return ret;
202 if (type_unsigned(base_type))
203 return 0;
204 ret = whole_range.max;
205 bits = base_type->bit_size - 1;
206 ret >>= (63 - bits);
207 return -(ret + 1);
210 int nr_bits(struct expression *expr)
212 struct symbol *type;
214 type = get_type(expr);
215 if (!type)
216 return 0;
217 return type->bit_size;