1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004, 2007, 2008 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "tree-iterator.h"
30 /* This is a cache of STATEMENT_LIST nodes. We create and destroy them
31 fairly often during gimplification. */
33 static GTY ((deletable (""))) tree stmt_list_cache;
36 alloc_stmt_list (void)
38 tree list = stmt_list_cache;
41 stmt_list_cache = TREE_CHAIN (list);
42 gcc_assert (stmt_list_cache != 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 /* If this triggers, it's a sign that the same list is being freed
59 gcc_assert (t != stmt_list_cache || stmt_list_cache == NULL);
60 TREE_CHAIN (t) = stmt_list_cache;
64 /* Links a statement, or a chain of statements, before the current stmt. */
67 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
69 struct tree_statement_list_node *head, *tail, *cur;
72 gcc_assert (t != i->container);
74 if (TREE_CODE (t) == STATEMENT_LIST)
76 head = STATEMENT_LIST_HEAD (t);
77 tail = STATEMENT_LIST_TAIL (t);
78 STATEMENT_LIST_HEAD (t) = NULL;
79 STATEMENT_LIST_TAIL (t) = NULL;
83 /* Empty statement lists need no work. */
86 gcc_assert (head == tail);
92 head = GGC_NEW (struct tree_statement_list_node);
99 TREE_SIDE_EFFECTS (i->container) = 1;
103 /* Link it into the list. */
106 head->prev = cur->prev;
108 head->prev->next = head;
110 STATEMENT_LIST_HEAD (i->container) = head;
116 head->prev = STATEMENT_LIST_TAIL (i->container);
118 head->prev->next = head;
120 STATEMENT_LIST_HEAD (i->container) = head;
121 STATEMENT_LIST_TAIL (i->container) = tail;
124 /* Update the iterator, if requested. */
128 case TSI_CONTINUE_LINKING:
129 case TSI_CHAIN_START:
140 /* Links a statement, or a chain of statements, after the current stmt. */
143 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
145 struct tree_statement_list_node *head, *tail, *cur;
147 /* Die on looping. */
148 gcc_assert (t != i->container);
150 if (TREE_CODE (t) == STATEMENT_LIST)
152 head = STATEMENT_LIST_HEAD (t);
153 tail = STATEMENT_LIST_TAIL (t);
154 STATEMENT_LIST_HEAD (t) = NULL;
155 STATEMENT_LIST_TAIL (t) = NULL;
159 /* Empty statement lists need no work. */
162 gcc_assert (head == tail);
168 head = GGC_NEW (struct tree_statement_list_node);
175 TREE_SIDE_EFFECTS (i->container) = 1;
179 /* Link it into the list. */
182 tail->next = cur->next;
184 tail->next->prev = tail;
186 STATEMENT_LIST_TAIL (i->container) = tail;
192 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
193 STATEMENT_LIST_HEAD (i->container) = head;
194 STATEMENT_LIST_TAIL (i->container) = tail;
197 /* Update the iterator, if requested. */
201 case TSI_CHAIN_START:
204 case TSI_CONTINUE_LINKING:
214 /* Remove a stmt from the tree list. The iterator is updated to point to
218 tsi_delink (tree_stmt_iterator *i)
220 struct tree_statement_list_node *cur, *next, *prev;
229 STATEMENT_LIST_HEAD (i->container) = next;
233 STATEMENT_LIST_TAIL (i->container) = prev;
236 TREE_SIDE_EFFECTS (i->container) = 0;
241 /* Return the first expression in a sequence of COMPOUND_EXPRs,
242 or in a STATEMENT_LIST. */
245 expr_first (tree expr)
247 if (expr == NULL_TREE)
250 if (TREE_CODE (expr) == STATEMENT_LIST)
252 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
253 return n ? n->stmt : NULL_TREE;
256 while (TREE_CODE (expr) == COMPOUND_EXPR)
257 expr = TREE_OPERAND (expr, 0);
262 /* Return the last expression in a sequence of COMPOUND_EXPRs,
263 or in a STATEMENT_LIST. */
266 expr_last (tree expr)
268 if (expr == NULL_TREE)
271 if (TREE_CODE (expr) == STATEMENT_LIST)
273 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
274 return n ? n->stmt : NULL_TREE;
277 while (TREE_CODE (expr) == COMPOUND_EXPR)
278 expr = TREE_OPERAND (expr, 1);
283 #include "gt-tree-iterator.h"