1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
26 #include "tree-gimple.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 /* Links a statement, or a chain of statements, before the current stmt. */
68 tsi_link_before (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
70 struct tree_statement_list_node
*head
, *tail
, *cur
;
73 gcc_assert (t
!= i
->container
);
75 if (TREE_CODE (t
) == STATEMENT_LIST
)
77 head
= STATEMENT_LIST_HEAD (t
);
78 tail
= STATEMENT_LIST_TAIL (t
);
79 STATEMENT_LIST_HEAD (t
) = NULL
;
80 STATEMENT_LIST_TAIL (t
) = NULL
;
84 /* Empty statement lists need no work. */
87 gcc_assert (head
== tail
);
93 head
= ggc_alloc (sizeof (*head
));
100 TREE_SIDE_EFFECTS (i
->container
) = 1;
104 /* Link it into the list. */
107 head
->prev
= cur
->prev
;
109 head
->prev
->next
= head
;
111 STATEMENT_LIST_HEAD (i
->container
) = head
;
117 head
->prev
= STATEMENT_LIST_TAIL (i
->container
);
119 head
->prev
->next
= head
;
121 STATEMENT_LIST_HEAD (i
->container
) = head
;
122 STATEMENT_LIST_TAIL (i
->container
) = tail
;
125 /* Update the iterator, if requested. */
129 case TSI_CONTINUE_LINKING
:
130 case TSI_CHAIN_START
:
141 /* Links a statement, or a chain of statements, after the current stmt. */
144 tsi_link_after (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
146 struct tree_statement_list_node
*head
, *tail
, *cur
;
148 /* Die on looping. */
149 gcc_assert (t
!= i
->container
);
151 if (TREE_CODE (t
) == STATEMENT_LIST
)
153 head
= STATEMENT_LIST_HEAD (t
);
154 tail
= STATEMENT_LIST_TAIL (t
);
155 STATEMENT_LIST_HEAD (t
) = NULL
;
156 STATEMENT_LIST_TAIL (t
) = NULL
;
160 /* Empty statement lists need no work. */
163 gcc_assert (head
== tail
);
169 head
= ggc_alloc (sizeof (*head
));
176 TREE_SIDE_EFFECTS (i
->container
) = 1;
180 /* Link it into the list. */
183 tail
->next
= cur
->next
;
185 tail
->next
->prev
= tail
;
187 STATEMENT_LIST_TAIL (i
->container
) = tail
;
193 gcc_assert (!STATEMENT_LIST_TAIL (i
->container
));
194 STATEMENT_LIST_HEAD (i
->container
) = head
;
195 STATEMENT_LIST_TAIL (i
->container
) = tail
;
198 /* Update the iterator, if requested. */
202 case TSI_CHAIN_START
:
205 case TSI_CONTINUE_LINKING
:
215 /* Remove a stmt from the tree list. The iterator is updated to point to
219 tsi_delink (tree_stmt_iterator
*i
)
221 struct tree_statement_list_node
*cur
, *next
, *prev
;
230 STATEMENT_LIST_HEAD (i
->container
) = next
;
234 STATEMENT_LIST_TAIL (i
->container
) = prev
;
237 TREE_SIDE_EFFECTS (i
->container
) = 0;
242 /* Move all statements in the statement list after I to a new
243 statement list. I itself is unchanged. */
246 tsi_split_statement_list_after (const tree_stmt_iterator
*i
)
248 struct tree_statement_list_node
*cur
, *next
;
252 /* How can we possibly split after the end, or before the beginning? */
256 old_sl
= i
->container
;
257 new_sl
= alloc_stmt_list ();
258 TREE_SIDE_EFFECTS (new_sl
) = 1;
260 STATEMENT_LIST_HEAD (new_sl
) = next
;
261 STATEMENT_LIST_TAIL (new_sl
) = STATEMENT_LIST_TAIL (old_sl
);
262 STATEMENT_LIST_TAIL (old_sl
) = cur
;
269 /* Move all statements in the statement list before I to a new
270 statement list. I is set to the head of the new list. */
273 tsi_split_statement_list_before (tree_stmt_iterator
*i
)
275 struct tree_statement_list_node
*cur
, *prev
;
279 /* How can we possibly split after the end, or before the beginning? */
283 old_sl
= i
->container
;
284 new_sl
= alloc_stmt_list ();
285 TREE_SIDE_EFFECTS (new_sl
) = 1;
286 i
->container
= new_sl
;
288 STATEMENT_LIST_HEAD (new_sl
) = cur
;
289 STATEMENT_LIST_TAIL (new_sl
) = STATEMENT_LIST_TAIL (old_sl
);
290 STATEMENT_LIST_TAIL (old_sl
) = prev
;
295 STATEMENT_LIST_HEAD (old_sl
) = NULL
;
300 /* Return the first expression in a sequence of COMPOUND_EXPRs,
301 or in a STATEMENT_LIST. */
304 expr_first (tree expr
)
306 if (expr
== NULL_TREE
)
309 if (TREE_CODE (expr
) == STATEMENT_LIST
)
311 struct tree_statement_list_node
*n
= STATEMENT_LIST_HEAD (expr
);
312 return n
? n
->stmt
: NULL_TREE
;
315 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
316 expr
= TREE_OPERAND (expr
, 0);
320 /* Return the last expression in a sequence of COMPOUND_EXPRs,
321 or in a STATEMENT_LIST. */
324 expr_last (tree expr
)
326 if (expr
== NULL_TREE
)
329 if (TREE_CODE (expr
) == STATEMENT_LIST
)
331 struct tree_statement_list_node
*n
= STATEMENT_LIST_TAIL (expr
);
332 return n
? n
->stmt
: NULL_TREE
;
335 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
336 expr
= TREE_OPERAND (expr
, 1);
340 /* If EXPR is a single statement return it. If EXPR is a
341 STATEMENT_LIST containing exactly one statement S, return S.
342 Otherwise, return NULL. */
345 expr_only (tree expr
)
347 if (expr
== NULL_TREE
)
350 if (TREE_CODE (expr
) == STATEMENT_LIST
)
352 struct tree_statement_list_node
*n
= STATEMENT_LIST_TAIL (expr
);
353 if (n
&& STATEMENT_LIST_HEAD (expr
) == n
)
359 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
365 #include "gt-tree-iterator.h"