* g++.dg/debug/dwarf2/pr44641.C: Revert line number change. Remove
[official-gcc.git] / gcc / tree-iterator.c
blob4dfbd177f44b5abce37a3deea77bda4af9190f7a
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "alias.h"
25 #include "tree.h"
26 #include "options.h"
27 #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;
35 tree
36 alloc_stmt_list (void)
38 tree list;
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);
45 else
46 list = make_node (STATEMENT_LIST);
47 TREE_TYPE (list) = void_type_node;
48 return list;
51 void
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. */
61 static void
62 append_to_statement_list_1 (tree t, tree *list_p)
64 tree list = *list_p;
65 tree_stmt_iterator i;
67 if (!list)
69 if (t && TREE_CODE (t) == STATEMENT_LIST)
71 *list_p = t;
72 return;
74 *list_p = list = alloc_stmt_list ();
76 else if (TREE_CODE (list) != STATEMENT_LIST)
78 tree first = list;
79 *list_p = list = alloc_stmt_list ();
80 i = tsi_last (list);
81 tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
84 i = tsi_last (list);
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. */
91 void
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. */
100 void
101 append_to_statement_list_force (tree t, tree *list_p)
103 if (t != NULL_TREE)
104 append_to_statement_list_1 (t, list_p);
107 /* Links a statement, or a chain of statements, before the current stmt. */
109 void
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;
124 free_stmt_list (t);
126 /* Empty statement lists need no work. */
127 if (!head || !tail)
129 gcc_assert (head == tail);
130 return;
133 else
135 head = ggc_alloc<tree_statement_list_node> ();
136 head->prev = NULL;
137 head->next = NULL;
138 head->stmt = t;
139 tail = head;
142 TREE_SIDE_EFFECTS (i->container) = 1;
144 cur = i->ptr;
146 /* Link it into the list. */
147 if (cur)
149 head->prev = cur->prev;
150 if (head->prev)
151 head->prev->next = head;
152 else
153 STATEMENT_LIST_HEAD (i->container) = head;
154 tail->next = cur;
155 cur->prev = tail;
157 else
159 head->prev = STATEMENT_LIST_TAIL (i->container);
160 if (head->prev)
161 head->prev->next = head;
162 else
163 STATEMENT_LIST_HEAD (i->container) = head;
164 STATEMENT_LIST_TAIL (i->container) = tail;
167 /* Update the iterator, if requested. */
168 switch (mode)
170 case TSI_NEW_STMT:
171 case TSI_CONTINUE_LINKING:
172 case TSI_CHAIN_START:
173 i->ptr = head;
174 break;
175 case TSI_CHAIN_END:
176 i->ptr = tail;
177 break;
178 case TSI_SAME_STMT:
179 break;
183 /* Links a statement, or a chain of statements, after the current stmt. */
185 void
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;
200 free_stmt_list (t);
202 /* Empty statement lists need no work. */
203 if (!head || !tail)
205 gcc_assert (head == tail);
206 return;
209 else
211 head = ggc_alloc<tree_statement_list_node> ();
212 head->prev = NULL;
213 head->next = NULL;
214 head->stmt = t;
215 tail = head;
218 TREE_SIDE_EFFECTS (i->container) = 1;
220 cur = i->ptr;
222 /* Link it into the list. */
223 if (cur)
225 tail->next = cur->next;
226 if (tail->next)
227 tail->next->prev = tail;
228 else
229 STATEMENT_LIST_TAIL (i->container) = tail;
230 head->prev = cur;
231 cur->next = head;
233 else
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. */
241 switch (mode)
243 case TSI_NEW_STMT:
244 case TSI_CHAIN_START:
245 i->ptr = head;
246 break;
247 case TSI_CONTINUE_LINKING:
248 case TSI_CHAIN_END:
249 i->ptr = tail;
250 break;
251 case TSI_SAME_STMT:
252 gcc_assert (cur);
253 break;
257 /* Remove a stmt from the tree list. The iterator is updated to point to
258 the next stmt. */
260 void
261 tsi_delink (tree_stmt_iterator *i)
263 struct tree_statement_list_node *cur, *next, *prev;
265 cur = i->ptr;
266 next = cur->next;
267 prev = cur->prev;
269 if (prev)
270 prev->next = next;
271 else
272 STATEMENT_LIST_HEAD (i->container) = next;
273 if (next)
274 next->prev = prev;
275 else
276 STATEMENT_LIST_TAIL (i->container) = prev;
278 if (!next && !prev)
279 TREE_SIDE_EFFECTS (i->container) = 0;
281 i->ptr = next;
284 /* Return the first expression in a sequence of COMPOUND_EXPRs,
285 or in a STATEMENT_LIST. */
287 tree
288 expr_first (tree expr)
290 if (expr == NULL_TREE)
291 return expr;
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);
302 return expr;
305 /* Return the last expression in a sequence of COMPOUND_EXPRs,
306 or in a STATEMENT_LIST. */
308 tree
309 expr_last (tree expr)
311 if (expr == NULL_TREE)
312 return expr;
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);
323 return expr;
326 #include "gt-tree-iterator.h"