1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003-2015 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"
27 #include "double-int.h"
35 #include "tree-iterator.h"
39 /* This is a cache of STATEMENT_LIST nodes. We create and destroy them
40 fairly often during gimplification. */
42 static GTY ((deletable (""))) vec<tree, va_gc> *stmt_list_cache;
45 alloc_stmt_list (void)
48 if (!vec_safe_is_empty (stmt_list_cache))
50 list = stmt_list_cache->pop ();
51 memset (list, 0, sizeof (struct tree_base));
52 TREE_SET_CODE (list, STATEMENT_LIST);
55 list = make_node (STATEMENT_LIST);
56 TREE_TYPE (list) = void_type_node;
61 free_stmt_list (tree t)
63 gcc_assert (!STATEMENT_LIST_HEAD (t));
64 gcc_assert (!STATEMENT_LIST_TAIL (t));
65 vec_safe_push (stmt_list_cache, t);
68 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
71 append_to_statement_list_1 (tree t, tree *list_p)
78 if (t && TREE_CODE (t) == STATEMENT_LIST)
83 *list_p = list = alloc_stmt_list ();
85 else if (TREE_CODE (list) != STATEMENT_LIST)
88 *list_p = list = alloc_stmt_list ();
90 tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
94 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
97 /* Add T to the end of the list container pointed to by LIST_P.
98 If T is an expression with no effects, it is ignored. */
101 append_to_statement_list (tree t, tree *list_p)
103 if (t && TREE_SIDE_EFFECTS (t))
104 append_to_statement_list_1 (t, list_p);
107 /* Similar, but the statement is always added, regardless of side effects. */
110 append_to_statement_list_force (tree t, tree *list_p)
113 append_to_statement_list_1 (t, list_p);
116 /* Links a statement, or a chain of statements, before the current stmt. */
119 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
121 struct tree_statement_list_node *head, *tail, *cur;
123 /* Die on looping. */
124 gcc_assert (t != i->container);
126 if (TREE_CODE (t) == STATEMENT_LIST)
128 head = STATEMENT_LIST_HEAD (t);
129 tail = STATEMENT_LIST_TAIL (t);
130 STATEMENT_LIST_HEAD (t) = NULL;
131 STATEMENT_LIST_TAIL (t) = NULL;
135 /* Empty statement lists need no work. */
138 gcc_assert (head == tail);
144 head = ggc_alloc<tree_statement_list_node> ();
151 TREE_SIDE_EFFECTS (i->container) = 1;
155 /* Link it into the list. */
158 head->prev = cur->prev;
160 head->prev->next = head;
162 STATEMENT_LIST_HEAD (i->container) = head;
168 head->prev = STATEMENT_LIST_TAIL (i->container);
170 head->prev->next = head;
172 STATEMENT_LIST_HEAD (i->container) = head;
173 STATEMENT_LIST_TAIL (i->container) = tail;
176 /* Update the iterator, if requested. */
180 case TSI_CONTINUE_LINKING:
181 case TSI_CHAIN_START:
192 /* Links a statement, or a chain of statements, after the current stmt. */
195 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
197 struct tree_statement_list_node *head, *tail, *cur;
199 /* Die on looping. */
200 gcc_assert (t != i->container);
202 if (TREE_CODE (t) == STATEMENT_LIST)
204 head = STATEMENT_LIST_HEAD (t);
205 tail = STATEMENT_LIST_TAIL (t);
206 STATEMENT_LIST_HEAD (t) = NULL;
207 STATEMENT_LIST_TAIL (t) = NULL;
211 /* Empty statement lists need no work. */
214 gcc_assert (head == tail);
220 head = ggc_alloc<tree_statement_list_node> ();
227 TREE_SIDE_EFFECTS (i->container) = 1;
231 /* Link it into the list. */
234 tail->next = cur->next;
236 tail->next->prev = tail;
238 STATEMENT_LIST_TAIL (i->container) = tail;
244 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
245 STATEMENT_LIST_HEAD (i->container) = head;
246 STATEMENT_LIST_TAIL (i->container) = tail;
249 /* Update the iterator, if requested. */
253 case TSI_CHAIN_START:
256 case TSI_CONTINUE_LINKING:
266 /* Remove a stmt from the tree list. The iterator is updated to point to
270 tsi_delink (tree_stmt_iterator *i)
272 struct tree_statement_list_node *cur, *next, *prev;
281 STATEMENT_LIST_HEAD (i->container) = next;
285 STATEMENT_LIST_TAIL (i->container) = prev;
288 TREE_SIDE_EFFECTS (i->container) = 0;
293 /* Return the first expression in a sequence of COMPOUND_EXPRs,
294 or in a STATEMENT_LIST. */
297 expr_first (tree expr)
299 if (expr == NULL_TREE)
302 if (TREE_CODE (expr) == STATEMENT_LIST)
304 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
305 return n ? n->stmt : NULL_TREE;
308 while (TREE_CODE (expr) == COMPOUND_EXPR)
309 expr = TREE_OPERAND (expr, 0);
314 /* Return the last expression in a sequence of COMPOUND_EXPRs,
315 or in a STATEMENT_LIST. */
318 expr_last (tree expr)
320 if (expr == NULL_TREE)
323 if (TREE_CODE (expr) == STATEMENT_LIST)
325 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
326 return n ? n->stmt : NULL_TREE;
329 while (TREE_CODE (expr) == COMPOUND_EXPR)
330 expr = TREE_OPERAND (expr, 1);
335 #include "gt-tree-iterator.h"