db: store whether functions are global or static
[smatch.git] / smatch_type.c
blobf13068027360491bd197df8573c5d0b586778f57
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 is_pointer(struct expression *expr)
168 struct symbol *sym;
170 sym = get_type(expr);
171 if (!sym)
172 return 0;
173 if (sym->type == SYM_PTR)
174 return 1;
175 return 0;
178 int returns_pointer(struct symbol *sym)
180 if (!sym)
181 return 0;
182 sym = get_base_type(sym);
183 if (!sym || sym->type != SYM_FN)
184 return 0;
185 sym = get_base_type(sym);
186 if (sym->type == SYM_PTR)
187 return 1;
188 return 0;
191 long long type_max(struct symbol *base_type)
193 long long ret = whole_range.max;
194 int bits;
196 if (!base_type || !base_type->bit_size)
197 return ret;
198 bits = base_type->bit_size;
199 if (bits == 64)
200 return ret;
201 if (!type_unsigned(base_type))
202 bits--;
203 ret >>= (63 - bits);
204 return ret;
207 long long type_min(struct symbol *base_type)
209 long long ret = whole_range.min;
210 int bits;
212 if (!base_type || !base_type->bit_size)
213 return ret;
214 if (type_unsigned(base_type))
215 return 0;
216 ret = whole_range.max;
217 bits = base_type->bit_size - 1;
218 ret >>= (63 - bits);
219 return -(ret + 1);
222 int nr_bits(struct expression *expr)
224 struct symbol *type;
226 type = get_type(expr);
227 if (!type)
228 return 0;
229 return type->bit_size;
232 int is_static(struct expression *expr)
234 char *name;
235 struct symbol *sym;
236 int ret = 0;
238 name = get_variable_from_expr_complex(expr, &sym);
239 if (!name || !sym)
240 goto free;
242 if (sym->ctype.modifiers & MOD_STATIC)
243 ret = 1;
244 free:
245 free_string(name);
246 return ret;
249 const char *global_static()
251 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
252 return "static";
253 else
254 return "global";