1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
26 #include "tree-gimple.h"
27 #include "tree-iterator.h"
31 /* This is a cache of STATEMENT_LIST nodes. We create and destroy them
32 fairly often during gimplification. */
34 static GTY ((deletable (""))) tree stmt_list_cache
;
37 alloc_stmt_list (void)
39 tree list
= stmt_list_cache
;
42 stmt_list_cache
= TREE_CHAIN (list
);
43 memset (list
, 0, sizeof(struct tree_common
));
44 TREE_SET_CODE (list
, STATEMENT_LIST
);
47 list
= make_node (STATEMENT_LIST
);
48 TREE_TYPE (list
) = void_type_node
;
53 free_stmt_list (tree t
)
55 gcc_assert (!STATEMENT_LIST_HEAD (t
));
56 gcc_assert (!STATEMENT_LIST_TAIL (t
));
57 TREE_CHAIN (t
) = stmt_list_cache
;
61 /* Links a statement, or a chain of statements, before the current stmt. */
64 tsi_link_before (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
66 struct tree_statement_list_node
*head
, *tail
, *cur
;
69 gcc_assert (t
!= i
->container
);
71 if (TREE_CODE (t
) == STATEMENT_LIST
)
73 head
= STATEMENT_LIST_HEAD (t
);
74 tail
= STATEMENT_LIST_TAIL (t
);
75 STATEMENT_LIST_HEAD (t
) = NULL
;
76 STATEMENT_LIST_TAIL (t
) = NULL
;
80 /* Empty statement lists need no work. */
83 gcc_assert (head
== tail
);
89 head
= ggc_alloc (sizeof (*head
));
96 TREE_SIDE_EFFECTS (i
->container
) = 1;
100 /* Link it into the list. */
103 head
->prev
= cur
->prev
;
105 head
->prev
->next
= head
;
107 STATEMENT_LIST_HEAD (i
->container
) = head
;
113 gcc_assert (!STATEMENT_LIST_TAIL (i
->container
));
114 STATEMENT_LIST_HEAD (i
->container
) = head
;
115 STATEMENT_LIST_TAIL (i
->container
) = tail
;
118 /* Update the iterator, if requested. */
122 case TSI_CONTINUE_LINKING
:
123 case TSI_CHAIN_START
:
135 /* Links a statement, or a chain of statements, after the current stmt. */
138 tsi_link_after (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
140 struct tree_statement_list_node
*head
, *tail
, *cur
;
142 /* Die on looping. */
143 gcc_assert (t
!= i
->container
);
145 if (TREE_CODE (t
) == STATEMENT_LIST
)
147 head
= STATEMENT_LIST_HEAD (t
);
148 tail
= STATEMENT_LIST_TAIL (t
);
149 STATEMENT_LIST_HEAD (t
) = NULL
;
150 STATEMENT_LIST_TAIL (t
) = NULL
;
154 /* Empty statement lists need no work. */
157 gcc_assert (head
== tail
);
163 head
= ggc_alloc (sizeof (*head
));
170 TREE_SIDE_EFFECTS (i
->container
) = 1;
174 /* Link it into the list. */
177 tail
->next
= cur
->next
;
179 tail
->next
->prev
= tail
;
181 STATEMENT_LIST_TAIL (i
->container
) = tail
;
187 gcc_assert (!STATEMENT_LIST_TAIL (i
->container
));
188 STATEMENT_LIST_HEAD (i
->container
) = head
;
189 STATEMENT_LIST_TAIL (i
->container
) = tail
;
192 /* Update the iterator, if requested. */
196 case TSI_CHAIN_START
:
199 case TSI_CONTINUE_LINKING
:
209 /* Remove a stmt from the tree list. The iterator is updated to point to
213 tsi_delink (tree_stmt_iterator
*i
)
215 struct tree_statement_list_node
*cur
, *next
, *prev
;
224 STATEMENT_LIST_HEAD (i
->container
) = next
;
228 STATEMENT_LIST_TAIL (i
->container
) = prev
;
231 TREE_SIDE_EFFECTS (i
->container
) = 0;
236 /* Move all statements in the statement list after I to a new
237 statement list. I itself is unchanged. */
240 tsi_split_statement_list_after (const tree_stmt_iterator
*i
)
242 struct tree_statement_list_node
*cur
, *next
;
246 /* How can we possibly split after the end, or before the beginning? */
250 old_sl
= i
->container
;
251 new_sl
= alloc_stmt_list ();
252 TREE_SIDE_EFFECTS (new_sl
) = 1;
254 STATEMENT_LIST_HEAD (new_sl
) = next
;
255 STATEMENT_LIST_TAIL (new_sl
) = STATEMENT_LIST_TAIL (old_sl
);
256 STATEMENT_LIST_TAIL (old_sl
) = cur
;
263 /* Move all statements in the statement list before I to a new
264 statement list. I is set to the head of the new list. */
267 tsi_split_statement_list_before (tree_stmt_iterator
*i
)
269 struct tree_statement_list_node
*cur
, *prev
;
273 /* How can we possibly split after the end, or before the beginning? */
277 old_sl
= i
->container
;
278 new_sl
= alloc_stmt_list ();
279 TREE_SIDE_EFFECTS (new_sl
) = 1;
280 i
->container
= new_sl
;
282 STATEMENT_LIST_HEAD (new_sl
) = cur
;
283 STATEMENT_LIST_TAIL (new_sl
) = STATEMENT_LIST_TAIL (old_sl
);
284 STATEMENT_LIST_TAIL (old_sl
) = prev
;
291 /* Return the first expression in a sequence of COMPOUND_EXPRs,
292 or in a STATEMENT_LIST. */
295 expr_first (tree expr
)
297 if (expr
== NULL_TREE
)
300 if (TREE_CODE (expr
) == STATEMENT_LIST
)
302 struct tree_statement_list_node
*n
= STATEMENT_LIST_HEAD (expr
);
303 return n
? n
->stmt
: NULL_TREE
;
306 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
307 expr
= TREE_OPERAND (expr
, 0);
311 /* Return the last expression in a sequence of COMPOUND_EXPRs,
312 or in a STATEMENT_LIST. */
315 expr_last (tree expr
)
317 if (expr
== NULL_TREE
)
320 if (TREE_CODE (expr
) == STATEMENT_LIST
)
322 struct tree_statement_list_node
*n
= STATEMENT_LIST_TAIL (expr
);
323 return n
? n
->stmt
: NULL_TREE
;
326 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
327 expr
= TREE_OPERAND (expr
, 1);
331 /* If EXPR is a single statement, naked or in a STATEMENT_LIST, then
332 return it. Otherwise return NULL. */
335 expr_only (tree expr
)
337 if (expr
== NULL_TREE
)
340 if (TREE_CODE (expr
) == STATEMENT_LIST
)
342 struct tree_statement_list_node
*n
= STATEMENT_LIST_TAIL (expr
);
343 if (n
&& STATEMENT_LIST_HEAD (expr
) == n
)
349 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
355 #include "gt-tree-iterator.h"