Make #include handling do the right thing for absolute paths.
[smatch.git] / inline.c
blob8a07c57fae0536cbd7dc652eae45a6ef92c4383a
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/__alignof__ */
120 case EXPR_CAST:
121 case EXPR_SIZEOF:
122 case EXPR_ALIGNOF: {
123 struct expression *cast = copy_expression(expr->cast_expression);
124 if (cast == expr->cast_expression)
125 break;
126 expr = dup_expression(expr);
127 expr->cast_expression = cast;
128 break;
131 /* Conditional expression */
132 case EXPR_CONDITIONAL: {
133 struct expression *cond = copy_expression(expr->conditional);
134 struct expression *true = copy_expression(expr->cond_true);
135 struct expression *false = copy_expression(expr->cond_false);
136 if (cond == expr->conditional && true == expr->cond_true && false == expr->cond_false)
137 break;
138 expr = dup_expression(expr);
139 expr->conditional = cond;
140 expr->cond_true = true;
141 expr->cond_false = false;
142 break;
145 /* Statement expression */
146 case EXPR_STATEMENT: {
147 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
148 copy_statement(expr->statement, stmt);
149 expr = dup_expression(expr);
150 expr->statement = stmt;
151 break;
154 /* Call expression */
155 case EXPR_CALL: {
156 struct expression *fn = copy_expression(expr->fn);
157 struct expression_list *list = expr->args;
158 struct expression *arg;
160 expr = dup_expression(expr);
161 expr->fn = fn;
162 expr->args = NULL;
163 FOR_EACH_PTR(list, arg) {
164 add_expression(&expr->args, copy_expression(arg));
165 } END_FOR_EACH_PTR;
166 break;
169 /* Initializer list statement */
170 case EXPR_INITIALIZER: {
171 struct expression_list *list = expr->expr_list;
172 struct expression *entry;
173 expr = dup_expression(expr);
174 expr->expr_list = NULL;
175 FOR_EACH_PTR(list, entry) {
176 add_expression(&expr->expr_list, copy_expression(entry));
177 } END_FOR_EACH_PTR;
178 break;
181 /* Label in inline function - hmm. */
182 case EXPR_LABEL: {
183 struct symbol *label_symbol = copy_symbol(expr->pos, expr->label_symbol);
184 expr = dup_expression(expr);
185 expr->label_symbol = label_symbol;
186 break;
189 /* Identifier in member dereference is unchanged across a fn copy */
190 /* As is an array index expression */
191 case EXPR_INDEX:
192 case EXPR_IDENTIFIER:
193 break;
195 /* Position in initializer.. */
196 case EXPR_POS: {
197 struct expression *val = copy_expression(expr->init_expr);
198 if (val == expr->init_expr)
199 break;
200 expr = dup_expression(expr);
201 expr->init_expr = val;
202 break;
205 default:
206 warn(expr->pos, "trying to copy expression type %d", expr->type);
208 return expr;
211 void set_replace(struct symbol *old, struct symbol *new)
213 new->replace = old;
214 old->replace = new;
217 void unset_replace(struct symbol *sym)
219 struct symbol *r = sym->replace;
220 if (!r) {
221 warn(sym->pos, "symbol '%s' not replaced?", show_ident(sym->ident));
222 return;
224 r->replace = NULL;
225 sym->replace = NULL;
228 static void unset_replace_list(struct symbol_list *list)
230 struct symbol *sym;
231 FOR_EACH_PTR(list, sym) {
232 unset_replace(sym);
233 } END_FOR_EACH_PTR;
236 static struct statement *copy_one_statement(struct statement *stmt)
238 if (!stmt)
239 return NULL;
240 switch(stmt->type) {
241 case STMT_NONE:
242 break;
243 case STMT_EXPRESSION: {
244 struct expression *expr = copy_expression(stmt->expression);
245 if (expr == stmt->expression)
246 break;
247 stmt = dup_statement(stmt);
248 stmt->expression = expr;
249 break;
251 case STMT_COMPOUND: {
252 struct statement *new = alloc_statement(stmt->pos, STMT_COMPOUND);
253 copy_statement(stmt, new);
254 stmt = new;
255 break;
257 case STMT_IF: {
258 struct expression *cond = stmt->if_conditional;
259 struct statement *true = stmt->if_true;
260 struct statement *false = stmt->if_false;
262 cond = copy_expression(cond);
263 true = copy_one_statement(true);
264 false = copy_one_statement(false);
265 if (stmt->if_conditional == cond &&
266 stmt->if_true == true &&
267 stmt->if_false == false)
268 break;
269 stmt = dup_statement(stmt);
270 stmt->if_conditional = cond;
271 stmt->if_true = true;
272 stmt->if_false = false;
273 break;
275 case STMT_RETURN: {
276 struct expression *retval = copy_expression(stmt->ret_value);
277 struct symbol *sym = copy_symbol(stmt->pos, stmt->ret_target);
279 stmt = dup_statement(stmt);
280 stmt->ret_value = retval;
281 stmt->ret_target = sym;
282 break;
284 case STMT_CASE: {
285 stmt = dup_statement(stmt);
286 stmt->case_label = copy_symbol(stmt->pos, stmt->case_label);
287 stmt->case_expression = copy_expression(stmt->case_expression);
288 stmt->case_to = copy_expression(stmt->case_to);
289 stmt->case_statement = copy_one_statement(stmt->case_statement);
290 break;
292 case STMT_SWITCH: {
293 struct symbol *switch_break = copy_symbol(stmt->pos, stmt->switch_break);
294 struct symbol *switch_case = copy_symbol(stmt->pos, stmt->switch_case);
295 struct expression *expr = copy_expression(stmt->switch_expression);
296 struct statement *switch_stmt = copy_one_statement(stmt->switch_statement);
297 stmt = dup_statement(stmt);
298 stmt->switch_break = switch_break;
299 stmt->switch_case = switch_case;
300 stmt->switch_expression = expr;
301 stmt->switch_statement = switch_stmt;
302 break;
304 case STMT_ITERATOR: {
305 stmt = dup_statement(stmt);
306 stmt->iterator_break = copy_symbol(stmt->pos, stmt->iterator_break);
307 stmt->iterator_continue = copy_symbol(stmt->pos, stmt->iterator_continue);
308 stmt->iterator_syms = copy_symbol_list(stmt->iterator_syms);
310 stmt->iterator_pre_statement = copy_one_statement(stmt->iterator_pre_statement);
311 stmt->iterator_pre_condition = copy_expression(stmt->iterator_pre_condition);
313 stmt->iterator_statement = copy_one_statement(stmt->iterator_statement);
315 stmt->iterator_post_statement = copy_one_statement(stmt->iterator_post_statement);
316 stmt->iterator_post_condition = copy_expression(stmt->iterator_post_condition);
317 break;
319 case STMT_LABEL: {
320 stmt = dup_statement(stmt);
321 stmt->label_identifier = copy_symbol(stmt->pos, stmt->label_identifier);
322 stmt->label_statement = copy_one_statement(stmt->label_statement);
323 break;
325 case STMT_GOTO: {
326 stmt = dup_statement(stmt);
327 stmt->goto_label = copy_symbol(stmt->pos, stmt->goto_label);
328 stmt->goto_expression = copy_expression(stmt->goto_expression);
329 break;
331 case STMT_ASM: {
332 /* FIXME! */
333 break;
335 default:
336 warn(stmt->pos, "trying to copy statement type %d", stmt->type);
337 break;
339 return stmt;
343 * Copy a stateemnt tree from 'src' to 'dst', where both
344 * source and destination are of type STMT_COMPOUND.
346 * We do this for the tree-level inliner.
348 * This doesn't do the symbol replacement right: it's not
349 * re-entrant.
351 void copy_statement(struct statement *src, struct statement *dst)
353 struct statement *stmt;
354 struct symbol *sym;
356 FOR_EACH_PTR(src->syms, sym) {
357 struct symbol *newsym = copy_symbol(src->pos, sym);
358 newsym->initializer = copy_expression(sym->initializer);
359 add_symbol(&dst->syms, newsym);
360 } END_FOR_EACH_PTR;
362 FOR_EACH_PTR(src->stmts, stmt) {
363 add_statement(&dst->stmts, copy_one_statement(stmt));
364 } END_FOR_EACH_PTR;
366 dst->ret = copy_symbol(src->pos, src->ret);
369 static struct symbol *create_copy_symbol(struct symbol *orig)
371 struct symbol *sym = orig;
372 if (orig) {
373 sym = alloc_symbol(orig->pos, orig->type);
374 sym->ident = orig->ident;
375 sym->ctype = orig->ctype;
376 sym->initializer = NULL;
377 set_replace(orig, sym);
379 return orig;
382 static struct symbol_list *create_symbol_list(struct symbol_list *src)
384 struct symbol_list *dst = NULL;
385 struct symbol *sym;
387 FOR_EACH_PTR(src, sym) {
388 struct symbol *newsym = create_copy_symbol(sym);
389 add_symbol(&dst, newsym);
390 } END_FOR_EACH_PTR;
391 return dst;
394 int inline_function(struct expression *expr, struct symbol *sym)
396 struct symbol_list * fn_symbol_list;
397 struct symbol *fn = sym->ctype.base_type;
398 struct expression_list *arg_list = expr->args;
399 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
400 struct symbol_list *name_list;
401 struct symbol *name;
402 struct expression *arg;
404 if (!fn->stmt) {
405 warn(fn->pos, "marked inline, but without a definition");
406 return 0;
408 name_list = fn->arguments;
410 stmt = alloc_statement(expr->pos, STMT_COMPOUND);
412 expr->type = EXPR_STATEMENT;
413 expr->statement = stmt;
414 expr->ctype = fn->ctype.base_type;
416 fn_symbol_list = create_symbol_list(sym->symbol_list);
418 PREPARE_PTR_LIST(name_list, name);
419 FOR_EACH_PTR(arg_list, arg) {
420 struct symbol *a = alloc_symbol(arg->pos, SYM_NODE);
422 a->ctype.base_type = arg->ctype;
423 if (name) {
424 a->ident = name->ident;
425 a->ctype = name->ctype;
426 set_replace(name, a);
427 add_symbol(&fn_symbol_list, a);
429 a->initializer = arg;
430 add_symbol(&stmt->syms, a);
432 NEXT_PTR_LIST(name);
433 } END_FOR_EACH_PTR;
434 FINISH_PTR_LIST(name);
436 copy_statement(fn->stmt, stmt);
438 unset_replace_list(fn_symbol_list);
440 evaluate_statement(stmt);
442 return 1;