return_to_param: delete debug code
[smatch.git] / smatch_expressions.c
blobd4cc44f223fd87e4d9998661b0672c99be2454c3
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 struct expression *gen_expression_from_name_sym(const char *name, struct symbol *sym)
228 struct expression *base;
229 int skip = 0;
230 struct expression *ret;
232 if (!name || !sym)
233 return NULL;
235 base = symbol_expression(sym);
236 while (name[skip] != '\0' && name[skip] != '.' && name[skip] != '-')
237 skip++;
239 ret = get_expression_from_base_and_str(base, name + skip);
240 if (ret) {
241 char *new = expr_to_str(ret);
244 * FIXME: this sometimes changes "foo->bar.a.b->c" into
245 * "foo->bar.a.b.c". I don't know why... :(
248 if (!new || strcmp(name, new) != 0)
249 return NULL;
251 return ret;
254 struct expression *gen_expression_from_key(struct expression *arg, const char *key)
256 struct expression *ret;
257 struct token *token, *prev, *end;
258 const char *p = key;
259 char buf[4095];
260 char *alloc;
261 size_t len;
263 /* The idea is that we can parse either $0->foo or $->foo */
264 if (key[0] != '$')
265 return NULL;
266 p++;
267 while (*p >= '0' && *p <= '9')
268 p++;
269 len = snprintf(buf, sizeof(buf), "%s\n", p);
270 alloc = alloc_string(buf);
272 token = tokenize_buffer(alloc, len, &end);
273 if (!token)
274 return NULL;
275 if (token_type(token) != TOKEN_STREAMBEGIN)
276 return NULL;
277 token = token->next;
279 ret = arg;
280 while (token_type(token) == TOKEN_SPECIAL &&
281 (token->special == SPECIAL_DEREFERENCE || token->special == '.')) {
282 prev = token;
283 token = token->next;
284 if (token_type(token) != TOKEN_IDENT)
285 return NULL;
286 ret = deref_expression(ret);
287 ret = member_expression(ret,
288 (prev->special == SPECIAL_DEREFERENCE) ? '*' : '.',
289 token->ident);
290 token = token->next;
293 if (token_type(token) != TOKEN_STREAMEND)
294 return NULL;
296 return ret;
299 struct expression *fake_variable(struct symbol *type, const char *name)
301 struct symbol *sym, *node;
302 struct expression *ret;
303 struct ident *ident;
305 if (!type)
306 type = &llong_ctype;
308 ident = alloc_ident(name, strlen(name));
310 sym = alloc_symbol(get_cur_pos(), type->type);
311 sym->ident = ident;
312 sym->ctype.base_type = type;
313 sym->ctype.modifiers |= MOD_AUTO;
315 node = alloc_symbol(get_cur_pos(), SYM_NODE);
316 node->ident = ident;
317 node->ctype.base_type = type;
318 node->ctype.modifiers |= MOD_AUTO;
320 ret = symbol_expression(node);
321 ret->smatch_flags |= Fake;
323 return ret;
326 void expr_set_parent_expr(struct expression *expr, struct expression *parent)
328 if (!expr)
329 return;
330 if (parent && parent->smatch_flags & Tmp)
331 return;
333 expr->parent = (unsigned long)parent | 0x1UL;
336 void expr_set_parent_stmt(struct expression *expr, struct statement *parent)
338 if (!expr)
339 return;
340 expr->parent = (unsigned long)parent;
343 struct expression *expr_get_parent_expr(struct expression *expr)
345 struct expression *parent;
347 if (!expr)
348 return NULL;
349 if (!(expr->parent & 0x1UL))
350 return NULL;
352 parent = (struct expression *)(expr->parent & ~0x1UL);
353 if (parent && (parent->smatch_flags & Fake))
354 return expr_get_parent_expr(parent);
355 return parent;
358 struct expression *expr_get_fake_parent_expr(struct expression *expr)
360 struct expression *parent;
362 if (!expr)
363 return NULL;
364 if (!(expr->parent & 0x1UL))
365 return NULL;
367 parent = (struct expression *)(expr->parent & ~0x1UL);
368 if (parent && (parent->smatch_flags & Fake))
369 return parent;
370 return NULL;
373 struct statement *expr_get_parent_stmt(struct expression *expr)
375 if (!expr)
376 return NULL;
377 if (expr->parent & 0x1UL)
378 return NULL;
379 return (struct statement *)expr->parent;