db/smdb.py: add a try catch block printing the return states
[smatch.git] / smatch_expressions.c
blob5e204182d9310cd09b77b8eecb9a17cb75d4c572
1 #include "smatch.h"
2 #include "smatch_extra.h"
4 DECLARE_ALLOCATOR(sname);
5 __ALLOCATOR(struct expression, "temporary expr", tmp_expression);
7 static struct position get_cur_pos(void)
9 static struct position pos;
10 static struct position none;
11 struct expression *expr;
12 struct statement *stmt;
14 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
15 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
16 if (expr)
17 pos = expr->pos;
18 else if (stmt)
19 pos = stmt->pos;
20 else
21 pos = none;
22 return pos;
25 struct expression *alloc_tmp_expression(struct position pos, int type)
27 struct expression *expr;
29 expr = __alloc_tmp_expression(0);
30 expr->smatch_flags |= Tmp;
31 expr->type = type;
32 expr->pos = pos;
33 return expr;
36 void free_tmp_expressions(void)
38 clear_tmp_expression_alloc();
41 struct expression *zero_expr(void)
43 struct expression *zero;
45 zero = alloc_tmp_expression(get_cur_pos(), EXPR_VALUE);
46 zero->value = 0;
47 zero->ctype = &int_ctype;
48 return zero;
51 struct expression *value_expr(long long val)
53 struct expression *expr;
55 if (!val)
56 return zero_expr();
58 expr = alloc_tmp_expression(get_cur_pos(), EXPR_VALUE);
59 expr->value = val;
60 expr->ctype = &llong_ctype;
61 return expr;
64 struct expression *member_expression(struct expression *deref, int op, struct ident *member)
66 struct expression *expr;
68 expr = alloc_tmp_expression(deref->pos, EXPR_DEREF);
69 expr->op = op;
70 expr->deref = deref;
71 expr->member = member;
72 expr->member_offset = -1;
73 return expr;
76 struct expression *preop_expression(struct expression *expr, int op)
78 struct expression *preop;
80 preop = alloc_tmp_expression(expr->pos, EXPR_PREOP);
81 preop->unop = expr;
82 preop->op = op;
83 return preop;
86 struct expression *deref_expression(struct expression *expr)
88 if (expr->type == EXPR_BINOP)
89 expr = preop_expression(expr, '(');
90 return preop_expression(expr, '*');
93 struct expression *assign_expression(struct expression *left, int op, struct expression *right)
95 struct expression *expr;
97 if (!right)
98 return NULL;
100 /* FIXME: make this a tmp expression. */
101 expr = alloc_expression(right->pos, EXPR_ASSIGNMENT);
102 expr->op = op;
103 expr->left = left;
104 expr->right = right;
105 return expr;
108 struct expression *binop_expression(struct expression *left, int op, struct expression *right)
110 struct expression *expr;
112 expr = alloc_tmp_expression(right->pos, EXPR_BINOP);
113 expr->op = op;
114 expr->left = left;
115 expr->right = right;
116 return expr;
119 struct expression *array_element_expression(struct expression *array, struct expression *offset)
121 struct expression *expr;
123 expr = binop_expression(array, '+', offset);
124 return deref_expression(expr);
127 struct expression *symbol_expression(struct symbol *sym)
129 struct expression *expr;
131 expr = alloc_tmp_expression(sym->pos, EXPR_SYMBOL);
132 expr->symbol = sym;
133 expr->symbol_name = sym->ident;
134 return expr;
137 struct expression *compare_expression(struct expression *left, int op, struct expression *right)
139 struct expression *expr;
141 expr = alloc_tmp_expression(get_cur_pos(), EXPR_COMPARE);
142 expr->op = op;
143 expr->left = left;
144 expr->right = right;
145 return expr;
148 struct expression *gen_string_expression(char *str)
150 struct expression *ret;
151 struct string *string;
152 int len;
154 len = strlen(str) + 1;
155 string = (void *)__alloc_sname(4 + len);
156 string->length = len;
157 string->immutable = 0;
158 memcpy(string->data, str, len);
160 ret = alloc_tmp_expression(get_cur_pos(), EXPR_STRING);
161 ret->wide = 0;
162 ret->string = string;
164 return ret;
167 struct expression *call_expression(struct expression *fn, struct expression_list *args)
169 struct expression *expr;
171 expr = alloc_tmp_expression(fn->pos, EXPR_CALL);
172 expr->fn = fn;
173 expr->args = args;
175 return expr;
178 static struct expression *get_expression_from_base_and_str(struct expression *base, const char *addition)
180 struct expression *ret = NULL;
181 struct token *token, *prev, *end;
182 char *alloc;
184 if (addition[0] == '\0')
185 return base;
187 alloc = alloc_string_newline(addition);
189 token = tokenize_buffer(alloc, strlen(alloc), &end);
190 if (!token)
191 goto free;
192 if (token_type(token) != TOKEN_STREAMBEGIN)
193 goto free;
194 token = token->next;
196 ret = base;
197 while (token_type(token) == TOKEN_SPECIAL &&
198 (token->special == SPECIAL_DEREFERENCE || token->special == '.')) {
199 prev = token;
200 token = token->next;
201 if (token_type(token) != TOKEN_IDENT)
202 goto free;
203 switch (prev->special) {
204 case SPECIAL_DEREFERENCE:
205 ret = deref_expression(ret);
206 ret = member_expression(ret, '*', token->ident);
207 break;
208 case '.':
209 ret = member_expression(ret, '.', token->ident);
210 break;
211 default:
212 goto free;
214 token = token->next;
217 if (token_type(token) != TOKEN_STREAMEND)
218 goto free;
220 free:
221 free_string(alloc);
223 return ret;
226 static struct expression *gen_expression_from_name_sym_helper(const char *name, struct symbol *sym)
228 struct expression *ret;
229 int skip = 0;
231 if (name[0] == '&' ||
232 name[0] == '*' ||
233 name[0] == '(') {
234 ret = gen_expression_from_name_sym_helper(name + 1, sym);
235 return preop_expression(ret, name[0]);
237 while (name[skip] != '\0' && name[skip] != '.' && name[skip] != '-')
238 skip++;
240 return get_expression_from_base_and_str(symbol_expression(sym), name + skip);
243 struct expression *gen_expression_from_name_sym(const char *name, struct symbol *sym)
245 struct expression *ret;
247 ret = gen_expression_from_name_sym_helper(name, sym);
248 if (ret) {
249 char *new = expr_to_str(ret);
252 * FIXME: this sometimes changes "foo->bar.a.b->c" into
253 * "foo->bar.a.b.c". I don't know why... :(
256 if (!new || strcmp(name, new) != 0)
257 return NULL;
259 return ret;
262 struct expression *gen_expression_from_key(struct expression *arg, const char *key)
264 struct expression *ret;
265 struct token *token, *prev, *end;
266 const char *p = key;
267 char buf[4095];
268 char *alloc;
269 size_t len;
271 /* The idea is that we can parse either $0->foo or $->foo */
272 if (key[0] != '$')
273 return NULL;
274 p++;
275 while (*p >= '0' && *p <= '9')
276 p++;
277 len = snprintf(buf, sizeof(buf), "%s\n", p);
278 alloc = alloc_string(buf);
280 token = tokenize_buffer(alloc, len, &end);
281 if (!token)
282 return NULL;
283 if (token_type(token) != TOKEN_STREAMBEGIN)
284 return NULL;
285 token = token->next;
287 ret = arg;
288 while (token_type(token) == TOKEN_SPECIAL &&
289 (token->special == SPECIAL_DEREFERENCE || token->special == '.')) {
290 prev = token;
291 token = token->next;
292 if (token_type(token) != TOKEN_IDENT)
293 return NULL;
294 ret = deref_expression(ret);
295 ret = member_expression(ret,
296 (prev->special == SPECIAL_DEREFERENCE) ? '*' : '.',
297 token->ident);
298 token = token->next;
301 if (token_type(token) != TOKEN_STREAMEND)
302 return NULL;
304 return ret;
307 struct expression *gen_expr_from_param_key(struct expression *expr, int param, const char *key)
309 struct expression *ret = NULL;
310 struct symbol *sym;
311 char *name;
313 name = get_name_sym_from_key(expr, param, key, &sym);
314 if (!name || !sym)
315 goto free;
317 ret = gen_expression_from_name_sym(name, sym);
318 free:
319 free_string(name);
320 return ret;
323 bool is_fake_var(struct expression *expr)
325 if (expr && (expr->smatch_flags & Fake))
326 return true;
327 return false;
330 struct expression *fake_variable(struct symbol *type, const char *name)
332 struct symbol *sym, *node;
333 struct expression *ret;
334 struct ident *ident;
336 if (!type)
337 type = &llong_ctype;
339 ident = alloc_ident(name, strlen(name));
341 sym = alloc_symbol(get_cur_pos(), type->type);
342 sym->ident = ident;
343 sym->ctype.base_type = type;
344 sym->ctype.modifiers |= MOD_AUTO;
346 node = alloc_symbol(get_cur_pos(), SYM_NODE);
347 node->ident = ident;
348 node->ctype.base_type = type;
349 node->ctype.modifiers |= MOD_AUTO;
351 ret = symbol_expression(node);
352 ret->smatch_flags |= Fake;
354 return ret;
357 void expr_set_parent_expr(struct expression *expr, struct expression *parent)
359 if (!expr)
360 return;
361 if (parent && parent->smatch_flags & Tmp)
362 return;
364 expr->parent = (unsigned long)parent | 0x1UL;
367 void expr_set_parent_stmt(struct expression *expr, struct statement *parent)
369 if (!expr)
370 return;
371 expr->parent = (unsigned long)parent;
374 struct expression *expr_get_parent_expr(struct expression *expr)
376 struct expression *parent;
378 if (!expr)
379 return NULL;
380 if (!(expr->parent & 0x1UL))
381 return NULL;
383 parent = (struct expression *)(expr->parent & ~0x1UL);
384 if (parent && (parent->smatch_flags & Fake))
385 return expr_get_parent_expr(parent);
386 return parent;
389 struct expression *expr_get_fake_parent_expr(struct expression *expr)
391 struct expression *parent;
393 if (!expr)
394 return NULL;
395 if (!(expr->parent & 0x1UL))
396 return NULL;
398 parent = (struct expression *)(expr->parent & ~0x1UL);
399 if (parent && (parent->smatch_flags & Fake))
400 return parent;
401 return NULL;
404 struct statement *expr_get_parent_stmt(struct expression *expr)
406 if (!expr)
407 return NULL;
408 if (expr->parent & 0x1UL)
409 return NULL;
410 return (struct statement *)expr->parent;