1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003-2013 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 (""))) vec<tree, va_gc> *stmt_list_cache;
36 alloc_stmt_list (void)
39 if (!vec_safe_is_empty (stmt_list_cache))
41 list = stmt_list_cache->pop ();
42 memset (list, 0, sizeof(struct tree_base));
43 TREE_SET_CODE (list, STATEMENT_LIST);
46 list = make_node (STATEMENT_LIST);
47 TREE_TYPE (list) = void_type_node;
52 free_stmt_list (tree t)
54 gcc_assert (!STATEMENT_LIST_HEAD (t));
55 gcc_assert (!STATEMENT_LIST_TAIL (t));
56 vec_safe_push (stmt_list_cache, t);
59 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
62 append_to_statement_list_1 (tree t, tree *list_p)
69 if (t && TREE_CODE (t) == STATEMENT_LIST)
74 *list_p = list = alloc_stmt_list ();
76 else if (TREE_CODE (list) != STATEMENT_LIST)
79 *list_p = list = alloc_stmt_list ();
81 tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
85 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
88 /* Add T to the end of the list container pointed to by LIST_P.
89 If T is an expression with no effects, it is ignored. */
92 append_to_statement_list (tree t, tree *list_p)
94 if (t && TREE_SIDE_EFFECTS (t))
95 append_to_statement_list_1 (t, list_p);
98 /* Similar, but the statement is always added, regardless of side effects. */
101 append_to_statement_list_force (tree t, tree *list_p)
104 append_to_statement_list_1 (t, list_p);
107 /* Links a statement, or a chain of statements, before the current stmt. */
110 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
112 struct tree_statement_list_node *head, *tail, *cur;
114 /* Die on looping. */
115 gcc_assert (t != i->container);
117 if (TREE_CODE (t) == STATEMENT_LIST)
119 head = STATEMENT_LIST_HEAD (t);
120 tail = STATEMENT_LIST_TAIL (t);
121 STATEMENT_LIST_HEAD (t) = NULL;
122 STATEMENT_LIST_TAIL (t) = NULL;
126 /* Empty statement lists need no work. */
129 gcc_assert (head == tail);
135 head = ggc_alloc_tree_statement_list_node ();
142 TREE_SIDE_EFFECTS (i->container) = 1;
146 /* Link it into the list. */
149 head->prev = cur->prev;
151 head->prev->next = head;
153 STATEMENT_LIST_HEAD (i->container) = head;
159 head->prev = STATEMENT_LIST_TAIL (i->container);
161 head->prev->next = head;
163 STATEMENT_LIST_HEAD (i->container) = head;
164 STATEMENT_LIST_TAIL (i->container) = tail;
167 /* Update the iterator, if requested. */
171 case TSI_CONTINUE_LINKING:
172 case TSI_CHAIN_START:
183 /* Links a statement, or a chain of statements, after the current stmt. */
186 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
188 struct tree_statement_list_node *head, *tail, *cur;
190 /* Die on looping. */
191 gcc_assert (t != i->container);
193 if (TREE_CODE (t) == STATEMENT_LIST)
195 head = STATEMENT_LIST_HEAD (t);
196 tail = STATEMENT_LIST_TAIL (t);
197 STATEMENT_LIST_HEAD (t) = NULL;
198 STATEMENT_LIST_TAIL (t) = NULL;
202 /* Empty statement lists need no work. */
205 gcc_assert (head == tail);
211 head = ggc_alloc_tree_statement_list_node ();
218 TREE_SIDE_EFFECTS (i->container) = 1;
222 /* Link it into the list. */
225 tail->next = cur->next;
227 tail->next->prev = tail;
229 STATEMENT_LIST_TAIL (i->container) = tail;
235 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
236 STATEMENT_LIST_HEAD (i->container) = head;
237 STATEMENT_LIST_TAIL (i->container) = tail;
240 /* Update the iterator, if requested. */
244 case TSI_CHAIN_START:
247 case TSI_CONTINUE_LINKING:
257 /* Remove a stmt from the tree list. The iterator is updated to point to
261 tsi_delink (tree_stmt_iterator *i)
263 struct tree_statement_list_node *cur, *next, *prev;
272 STATEMENT_LIST_HEAD (i->container) = next;
276 STATEMENT_LIST_TAIL (i->container) = prev;
279 TREE_SIDE_EFFECTS (i->container) = 0;
284 /* Return the first expression in a sequence of COMPOUND_EXPRs,
285 or in a STATEMENT_LIST. */
288 expr_first (tree expr)
290 if (expr == NULL_TREE)
293 if (TREE_CODE (expr) == STATEMENT_LIST)
295 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
296 return n ? n->stmt : NULL_TREE;
299 while (TREE_CODE (expr) == COMPOUND_EXPR)
300 expr = TREE_OPERAND (expr, 0);
305 /* Return the last expression in a sequence of COMPOUND_EXPRs,
306 or in a STATEMENT_LIST. */
309 expr_last (tree expr)
311 if (expr == NULL_TREE)
314 if (TREE_CODE (expr) == STATEMENT_LIST)
316 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
317 return n ? n->stmt : NULL_TREE;
320 while (TREE_CODE (expr) == COMPOUND_EXPR)
321 expr = TREE_OPERAND (expr, 1);
326 #include "gt-tree-iterator.h"