1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andrew MacLeod <amacleod@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.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 (""))) VEC(tree,gc) *stmt_list_cache;
37 alloc_stmt_list (void)
40 if (!VEC_empty (tree, stmt_list_cache))
42 list = VEC_pop (tree, stmt_list_cache);
43 memset (list, 0, sizeof(struct tree_base));
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 VEC_safe_push (tree, gc, stmt_list_cache, t);
60 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
63 append_to_statement_list_1 (tree t, tree *list_p)
70 if (t && TREE_CODE (t) == STATEMENT_LIST)
75 *list_p = list = alloc_stmt_list ();
77 else if (TREE_CODE (list) != STATEMENT_LIST)
80 *list_p = list = alloc_stmt_list ();
82 tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
86 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
89 /* Add T to the end of the list container pointed to by LIST_P.
90 If T is an expression with no effects, it is ignored. */
93 append_to_statement_list (tree t, tree *list_p)
95 if (t && TREE_SIDE_EFFECTS (t))
96 append_to_statement_list_1 (t, list_p);
99 /* Similar, but the statement is always added, regardless of side effects. */
102 append_to_statement_list_force (tree t, tree *list_p)
105 append_to_statement_list_1 (t, list_p);
108 /* Links a statement, or a chain of statements, before the current stmt. */
111 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
113 struct tree_statement_list_node *head, *tail, *cur;
115 /* Die on looping. */
116 gcc_assert (t != i->container);
118 if (TREE_CODE (t) == STATEMENT_LIST)
120 head = STATEMENT_LIST_HEAD (t);
121 tail = STATEMENT_LIST_TAIL (t);
122 STATEMENT_LIST_HEAD (t) = NULL;
123 STATEMENT_LIST_TAIL (t) = NULL;
127 /* Empty statement lists need no work. */
130 gcc_assert (head == tail);
136 head = ggc_alloc_tree_statement_list_node ();
143 TREE_SIDE_EFFECTS (i->container) = 1;
147 /* Link it into the list. */
150 head->prev = cur->prev;
152 head->prev->next = head;
154 STATEMENT_LIST_HEAD (i->container) = head;
160 head->prev = STATEMENT_LIST_TAIL (i->container);
162 head->prev->next = head;
164 STATEMENT_LIST_HEAD (i->container) = head;
165 STATEMENT_LIST_TAIL (i->container) = tail;
168 /* Update the iterator, if requested. */
172 case TSI_CONTINUE_LINKING:
173 case TSI_CHAIN_START:
184 /* Links a statement, or a chain of statements, after the current stmt. */
187 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
189 struct tree_statement_list_node *head, *tail, *cur;
191 /* Die on looping. */
192 gcc_assert (t != i->container);
194 if (TREE_CODE (t) == STATEMENT_LIST)
196 head = STATEMENT_LIST_HEAD (t);
197 tail = STATEMENT_LIST_TAIL (t);
198 STATEMENT_LIST_HEAD (t) = NULL;
199 STATEMENT_LIST_TAIL (t) = NULL;
203 /* Empty statement lists need no work. */
206 gcc_assert (head == tail);
212 head = ggc_alloc_tree_statement_list_node ();
219 TREE_SIDE_EFFECTS (i->container) = 1;
223 /* Link it into the list. */
226 tail->next = cur->next;
228 tail->next->prev = tail;
230 STATEMENT_LIST_TAIL (i->container) = tail;
236 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
237 STATEMENT_LIST_HEAD (i->container) = head;
238 STATEMENT_LIST_TAIL (i->container) = tail;
241 /* Update the iterator, if requested. */
245 case TSI_CHAIN_START:
248 case TSI_CONTINUE_LINKING:
258 /* Remove a stmt from the tree list. The iterator is updated to point to
262 tsi_delink (tree_stmt_iterator *i)
264 struct tree_statement_list_node *cur, *next, *prev;
273 STATEMENT_LIST_HEAD (i->container) = next;
277 STATEMENT_LIST_TAIL (i->container) = prev;
280 TREE_SIDE_EFFECTS (i->container) = 0;
285 /* Return the first expression in a sequence of COMPOUND_EXPRs,
286 or in a STATEMENT_LIST. */
289 expr_first (tree expr)
291 if (expr == NULL_TREE)
294 if (TREE_CODE (expr) == STATEMENT_LIST)
296 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
297 return n ? n->stmt : NULL_TREE;
300 while (TREE_CODE (expr) == COMPOUND_EXPR)
301 expr = TREE_OPERAND (expr, 0);
306 /* Return the last expression in a sequence of COMPOUND_EXPRs,
307 or in a STATEMENT_LIST. */
310 expr_last (tree expr)
312 if (expr == NULL_TREE)
315 if (TREE_CODE (expr) == STATEMENT_LIST)
317 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
318 return n ? n->stmt : NULL_TREE;
321 while (TREE_CODE (expr) == COMPOUND_EXPR)
322 expr = TREE_OPERAND (expr, 1);
327 #include "gt-tree-iterator.h"