[PATCH] line-splicing fixes in sparse
[smatch.git] / inline.c
blob5d4dce4a0962f3531e7514e55f0a0b3a285ef23f
1 /*
2 * Sparse - a semantic source parser.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
13 #include "lib.h"
14 #include "token.h"
15 #include "parse.h"
16 #include "symbol.h"
17 #include "expression.h"
19 static struct expression * dup_expression(struct expression *expr)
21 struct expression *dup = alloc_expression(expr->pos, expr->type);
22 *dup = *expr;
23 return dup;
26 static struct statement * dup_statement(struct statement *stmt)
28 struct statement *dup = alloc_statement(stmt->pos, stmt->type);
29 *dup = *stmt;
30 return dup;
33 static struct symbol *copy_symbol(struct position pos, struct symbol *sym)
35 if (!sym)
36 return sym;
37 if (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN | MOD_TOPLEVEL | MOD_INLINE))
38 return sym;
39 if (!sym->replace) {
40 warn(pos, "unreplaced symbol '%s'", show_ident(sym->ident));
41 return sym;
43 return sym->replace;
46 static struct symbol_list *copy_symbol_list(struct symbol_list *src)
48 struct symbol_list *dst = NULL;
49 struct symbol *sym;
51 FOR_EACH_PTR(src, sym) {
52 struct symbol *newsym = copy_symbol(sym->pos, sym);
53 add_symbol(&dst, newsym);
54 } END_FOR_EACH_PTR;
55 return dst;
58 static struct expression * copy_expression(struct expression *expr)
60 if (!expr)
61 return NULL;
63 switch (expr->type) {
65 * EXPR_SYMBOL is the interesting case, we may need to replace the
66 * symbol to the new copy.
68 case EXPR_SYMBOL: {
69 struct symbol *sym = copy_symbol(expr->pos, expr->symbol);
70 if (sym == expr->symbol)
71 break;
72 expr = dup_expression(expr);
73 expr->symbol = sym;
74 break;
77 /* Atomics, never change, just return the expression directly */
78 case EXPR_VALUE:
79 case EXPR_STRING:
80 break;
82 /* Unops: check if the subexpression is unique */
83 case EXPR_PREOP:
84 case EXPR_POSTOP: {
85 struct expression *unop = copy_expression(expr->unop);
86 if (expr->unop == unop)
87 break;
88 expr = dup_expression(expr);
89 expr->unop = unop;
90 break;
93 /* Binops: copy left/right expressions */
94 case EXPR_BINOP:
95 case EXPR_COMMA:
96 case EXPR_COMPARE:
97 case EXPR_LOGICAL:
98 case EXPR_ASSIGNMENT: {
99 struct expression *left = copy_expression(expr->left);
100 struct expression *right = copy_expression(expr->right);
101 if (left == expr->left && right == expr->right)
102 break;
103 expr = dup_expression(expr);
104 expr->left = left;
105 expr->right = right;
106 break;
109 /* Dereference */
110 case EXPR_DEREF: {
111 struct expression *deref = copy_expression(expr->deref);
112 if (deref == expr->deref)
113 break;
114 expr = dup_expression(expr);
115 expr->deref = deref;
116 break;
119 /* Cast/sizeof */
120 case EXPR_CAST:
121 case EXPR_SIZEOF: {
122 struct expression *cast = copy_expression(expr->cast_expression);
123 if (cast == expr->cast_expression)
124 break;
125 expr = dup_expression(expr);
126 expr->cast_expression = cast;
127 break;
130 /* Conditional expression */
131 case EXPR_CONDITIONAL: {
132 struct expression *cond = copy_expression(expr->conditional);
133 struct expression *true = copy_expression(expr->cond_true);
134 struct expression *false = copy_expression(expr->cond_false);
135 if (cond == expr->conditional && true == expr->cond_true && false == expr->cond_false)
136 break;
137 expr = dup_expression(expr);
138 expr->conditional = cond;
139 expr->cond_true = true;
140 expr->cond_false = false;
141 break;
144 /* Statement expression */
145 case EXPR_STATEMENT: {
146 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
147 copy_statement(expr->statement, stmt);
148 expr = dup_expression(expr);
149 expr->statement = stmt;
150 break;
153 /* Call expression */
154 case EXPR_CALL: {
155 struct expression *fn = copy_expression(expr->fn);
156 struct expression_list *list = expr->args;
157 struct expression *arg;
159 expr = dup_expression(expr);
160 expr->fn = fn;
161 expr->args = NULL;
162 FOR_EACH_PTR(list, arg) {
163 add_expression(&expr->args, copy_expression(arg));
164 } END_FOR_EACH_PTR;
165 break;
168 /* Initializer list statement */
169 case EXPR_INITIALIZER: {
170 struct expression_list *list = expr->expr_list;
171 struct expression *entry;
172 expr = dup_expression(expr);
173 expr->expr_list = NULL;
174 FOR_EACH_PTR(list, entry) {
175 add_expression(&expr->expr_list, copy_expression(entry));
176 } END_FOR_EACH_PTR;
177 break;
180 /* Label in inline function - hmm. */
181 case EXPR_LABEL: {
182 struct symbol *label_symbol = copy_symbol(expr->pos, expr->label_symbol);
183 expr = dup_expression(expr);
184 expr->label_symbol = label_symbol;
185 break;
188 /* Identifier in member dereference is unchanged across a fn copy */
189 case EXPR_IDENTIFIER:
190 break;
192 /* Position in initializer.. */
193 case EXPR_POS: {
194 struct expression *val = copy_expression(expr->init_expr);
195 if (val == expr->init_expr)
196 break;
197 expr = dup_expression(expr);
198 expr->init_expr = val;
199 break;
202 default:
203 warn(expr->pos, "trying to copy expression type %d", expr->type);
205 return expr;
208 void set_replace(struct symbol *old, struct symbol *new)
210 new->replace = old;
211 old->replace = new;
214 void unset_replace(struct symbol *sym)
216 struct symbol *r = sym->replace;
217 if (!r) {
218 warn(sym->pos, "symbol '%s' not replaced?", show_ident(sym->ident));
219 return;
221 r->replace = NULL;
222 sym->replace = NULL;
225 static void unset_replace_list(struct symbol_list *list)
227 struct symbol *sym;
228 FOR_EACH_PTR(list, sym) {
229 unset_replace(sym);
230 } END_FOR_EACH_PTR;
233 static struct statement *copy_one_statement(struct statement *stmt)
235 if (!stmt)
236 return NULL;
237 switch(stmt->type) {
238 case STMT_NONE:
239 break;
240 case STMT_EXPRESSION: {
241 struct expression *expr = copy_expression(stmt->expression);
242 if (expr == stmt->expression)
243 break;
244 stmt = dup_statement(stmt);
245 stmt->expression = expr;
246 break;
248 case STMT_COMPOUND: {
249 struct statement *new = alloc_statement(stmt->pos, STMT_COMPOUND);
250 copy_statement(stmt, new);
251 stmt = new;
252 break;
254 case STMT_IF: {
255 struct expression *cond = stmt->if_conditional;
256 struct statement *true = stmt->if_true;
257 struct statement *false = stmt->if_false;
259 cond = copy_expression(cond);
260 true = copy_one_statement(true);
261 false = copy_one_statement(false);
262 if (stmt->if_conditional == cond &&
263 stmt->if_true == true &&
264 stmt->if_false == false)
265 break;
266 stmt = dup_statement(stmt);
267 stmt->if_conditional = cond;
268 stmt->if_true = true;
269 stmt->if_false = false;
270 break;
272 case STMT_RETURN: {
273 struct expression *retval = copy_expression(stmt->ret_value);
274 struct symbol *sym = copy_symbol(stmt->pos, stmt->ret_target);
276 stmt = dup_statement(stmt);
277 stmt->ret_value = retval;
278 stmt->ret_target = sym;
279 break;
281 case STMT_CASE: {
282 stmt = dup_statement(stmt);
283 stmt->case_label = copy_symbol(stmt->pos, stmt->case_label);
284 stmt->case_expression = copy_expression(stmt->case_expression);
285 stmt->case_to = copy_expression(stmt->case_to);
286 stmt->case_statement = copy_one_statement(stmt->case_statement);
287 break;
289 case STMT_SWITCH: {
290 struct symbol *switch_break = copy_symbol(stmt->pos, stmt->switch_break);
291 struct symbol *switch_case = copy_symbol(stmt->pos, stmt->switch_case);
292 struct expression *expr = copy_expression(stmt->switch_expression);
293 struct statement *switch_stmt = copy_one_statement(stmt->switch_statement);
294 stmt = dup_statement(stmt);
295 stmt->switch_break = switch_break;
296 stmt->switch_case = switch_case;
297 stmt->switch_expression = expr;
298 stmt->switch_statement = switch_stmt;
299 break;
301 case STMT_ITERATOR: {
302 stmt = dup_statement(stmt);
303 stmt->iterator_break = copy_symbol(stmt->pos, stmt->iterator_break);
304 stmt->iterator_continue = copy_symbol(stmt->pos, stmt->iterator_continue);
305 stmt->iterator_syms = copy_symbol_list(stmt->iterator_syms);
307 stmt->iterator_pre_statement = copy_one_statement(stmt->iterator_pre_statement);
308 stmt->iterator_pre_condition = copy_expression(stmt->iterator_pre_condition);
310 stmt->iterator_statement = copy_one_statement(stmt->iterator_statement);
312 stmt->iterator_post_statement = copy_one_statement(stmt->iterator_post_statement);
313 stmt->iterator_post_condition = copy_expression(stmt->iterator_post_condition);
314 break;
316 case STMT_LABEL: {
317 stmt = dup_statement(stmt);
318 stmt->label_identifier = copy_symbol(stmt->pos, stmt->label_identifier);
319 stmt->label_statement = copy_one_statement(stmt->label_statement);
320 break;
322 case STMT_GOTO: {
323 stmt = dup_statement(stmt);
324 stmt->goto_label = copy_symbol(stmt->pos, stmt->goto_label);
325 stmt->goto_expression = copy_expression(stmt->goto_expression);
326 break;
328 case STMT_ASM: {
329 /* FIXME! */
330 break;
332 default:
333 warn(stmt->pos, "trying to copy statement type %d", stmt->type);
334 break;
336 return stmt;
340 * Copy a stateemnt tree from 'src' to 'dst', where both
341 * source and destination are of type STMT_COMPOUND.
343 * We do this for the tree-level inliner.
345 * This doesn't do the symbol replacement right: it's not
346 * re-entrant.
348 void copy_statement(struct statement *src, struct statement *dst)
350 struct statement *stmt;
351 struct symbol *sym;
353 FOR_EACH_PTR(src->syms, sym) {
354 struct symbol *newsym = copy_symbol(src->pos, sym);
355 newsym->initializer = copy_expression(sym->initializer);
356 add_symbol(&dst->syms, newsym);
357 } END_FOR_EACH_PTR;
359 FOR_EACH_PTR(src->stmts, stmt) {
360 add_statement(&dst->stmts, copy_one_statement(stmt));
361 } END_FOR_EACH_PTR;
363 dst->ret = copy_symbol(src->pos, src->ret);
366 static struct symbol *create_copy_symbol(struct symbol *orig)
368 struct symbol *sym = orig;
369 if (orig) {
370 sym = alloc_symbol(orig->pos, orig->type);
371 sym->ident = orig->ident;
372 sym->ctype = orig->ctype;
373 sym->initializer = NULL;
374 set_replace(orig, sym);
376 return orig;
379 static struct symbol_list *create_symbol_list(struct symbol_list *src)
381 struct symbol_list *dst = NULL;
382 struct symbol *sym;
384 FOR_EACH_PTR(src, sym) {
385 struct symbol *newsym = create_copy_symbol(sym);
386 add_symbol(&dst, newsym);
387 } END_FOR_EACH_PTR;
388 return dst;
391 int inline_function(struct expression *expr, struct symbol *sym)
393 struct symbol_list * fn_symbol_list;
394 struct symbol *fn = sym->ctype.base_type;
395 struct expression_list *arg_list = expr->args;
396 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
397 struct symbol_list *name_list;
398 struct symbol *name;
399 struct expression *arg;
401 if (!fn->stmt) {
402 warn(fn->pos, "marked inline, but without a definition");
403 return 0;
405 name_list = fn->arguments;
407 stmt = alloc_statement(expr->pos, STMT_COMPOUND);
409 expr->type = EXPR_STATEMENT;
410 expr->statement = stmt;
411 expr->ctype = fn->ctype.base_type;
413 fn_symbol_list = create_symbol_list(sym->symbol_list);
415 PREPARE_PTR_LIST(name_list, name);
416 FOR_EACH_PTR(arg_list, arg) {
417 struct symbol *a = alloc_symbol(arg->pos, SYM_NODE);
419 a->ctype.base_type = arg->ctype;
420 if (name) {
421 a->ident = name->ident;
422 a->ctype = name->ctype;
423 set_replace(name, a);
424 add_symbol(&fn_symbol_list, a);
426 a->initializer = arg;
427 add_symbol(&stmt->syms, a);
429 NEXT_PTR_LIST(name);
430 } END_FOR_EACH_PTR;
431 FINISH_PTR_LIST(name);
433 copy_statement(fn->stmt, stmt);
435 unset_replace_list(fn_symbol_list);
437 evaluate_statement(stmt);
439 return 1;