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 (""))) tree stmt_list_cache;
37 alloc_stmt_list (void)
39 tree list = stmt_list_cache;
42 stmt_list_cache = TREE_CHAIN (list);
43 gcc_assert (stmt_list_cache != list);
44 memset (list, 0, sizeof(struct tree_common));
45 TREE_SET_CODE (list, STATEMENT_LIST);
48 list = make_node (STATEMENT_LIST);
49 TREE_TYPE (list) = void_type_node;
54 free_stmt_list (tree t)
56 gcc_assert (!STATEMENT_LIST_HEAD (t));
57 gcc_assert (!STATEMENT_LIST_TAIL (t));
58 /* If this triggers, it's a sign that the same list is being freed
60 gcc_assert (t != stmt_list_cache || stmt_list_cache == NULL);
61 TREE_CHAIN (t) = stmt_list_cache;
65 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
68 append_to_statement_list_1 (tree t, tree *list_p)
75 if (t && TREE_CODE (t) == STATEMENT_LIST)
80 *list_p = list = alloc_stmt_list ();
84 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
87 /* Add T to the end of the list container pointed to by LIST_P.
88 If T is an expression with no effects, it is ignored. */
91 append_to_statement_list (tree t, tree *list_p)
93 if (t && TREE_SIDE_EFFECTS (t))
94 append_to_statement_list_1 (t, list_p);
97 /* Similar, but the statement is always added, regardless of side effects. */
100 append_to_statement_list_force (tree t, tree *list_p)
103 append_to_statement_list_1 (t, list_p);
106 /* Links a statement, or a chain of statements, before the current stmt. */
109 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
111 struct tree_statement_list_node *head, *tail, *cur;
113 /* Die on looping. */
114 gcc_assert (t != i->container);
116 if (TREE_CODE (t) == STATEMENT_LIST)
118 head = STATEMENT_LIST_HEAD (t);
119 tail = STATEMENT_LIST_TAIL (t);
120 STATEMENT_LIST_HEAD (t) = NULL;
121 STATEMENT_LIST_TAIL (t) = NULL;
125 /* Empty statement lists need no work. */
128 gcc_assert (head == tail);
134 head = ggc_alloc_tree_statement_list_node ();
141 TREE_SIDE_EFFECTS (i->container) = 1;
145 /* Link it into the list. */
148 head->prev = cur->prev;
150 head->prev->next = head;
152 STATEMENT_LIST_HEAD (i->container) = head;
158 head->prev = STATEMENT_LIST_TAIL (i->container);
160 head->prev->next = head;
162 STATEMENT_LIST_HEAD (i->container) = head;
163 STATEMENT_LIST_TAIL (i->container) = tail;
166 /* Update the iterator, if requested. */
170 case TSI_CONTINUE_LINKING:
171 case TSI_CHAIN_START:
182 /* Links a statement, or a chain of statements, after the current stmt. */
185 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
187 struct tree_statement_list_node *head, *tail, *cur;
189 /* Die on looping. */
190 gcc_assert (t != i->container);
192 if (TREE_CODE (t) == STATEMENT_LIST)
194 head = STATEMENT_LIST_HEAD (t);
195 tail = STATEMENT_LIST_TAIL (t);
196 STATEMENT_LIST_HEAD (t) = NULL;
197 STATEMENT_LIST_TAIL (t) = NULL;
201 /* Empty statement lists need no work. */
204 gcc_assert (head == tail);
210 head = ggc_alloc_tree_statement_list_node ();
217 TREE_SIDE_EFFECTS (i->container) = 1;
221 /* Link it into the list. */
224 tail->next = cur->next;
226 tail->next->prev = tail;
228 STATEMENT_LIST_TAIL (i->container) = tail;
234 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
235 STATEMENT_LIST_HEAD (i->container) = head;
236 STATEMENT_LIST_TAIL (i->container) = tail;
239 /* Update the iterator, if requested. */
243 case TSI_CHAIN_START:
246 case TSI_CONTINUE_LINKING:
256 /* Remove a stmt from the tree list. The iterator is updated to point to
260 tsi_delink (tree_stmt_iterator *i)
262 struct tree_statement_list_node *cur, *next, *prev;
271 STATEMENT_LIST_HEAD (i->container) = next;
275 STATEMENT_LIST_TAIL (i->container) = prev;
278 TREE_SIDE_EFFECTS (i->container) = 0;
283 /* Return the first expression in a sequence of COMPOUND_EXPRs,
284 or in a STATEMENT_LIST. */
287 expr_first (tree expr)
289 if (expr == NULL_TREE)
292 if (TREE_CODE (expr) == STATEMENT_LIST)
294 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
295 return n ? n->stmt : NULL_TREE;
298 while (TREE_CODE (expr) == COMPOUND_EXPR)
299 expr = TREE_OPERAND (expr, 0);
304 /* Return the last expression in a sequence of COMPOUND_EXPRs,
305 or in a STATEMENT_LIST. */
308 expr_last (tree expr)
310 if (expr == NULL_TREE)
313 if (TREE_CODE (expr) == STATEMENT_LIST)
315 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
316 return n ? n->stmt : NULL_TREE;
319 while (TREE_CODE (expr) == COMPOUND_EXPR)
320 expr = TREE_OPERAND (expr, 1);
325 #include "gt-tree-iterator.h"