1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andrew MacLeod <amacleod@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.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 (""))) VEC(tree
,gc
) *stmt_list_cache
;
37 alloc_stmt_list (void)
40 if (!VEC_empty (tree
, stmt_list_cache
))
42 list
= VEC_pop (tree
, stmt_list_cache
);
43 memset (list
, 0, sizeof(struct tree_base
));
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 VEC_safe_push (tree
, gc
, stmt_list_cache
, t
);
60 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
63 append_to_statement_list_1 (tree t
, tree
*list_p
)
70 if (t
&& TREE_CODE (t
) == STATEMENT_LIST
)
75 *list_p
= list
= alloc_stmt_list ();
79 tsi_link_after (&i
, t
, TSI_CONTINUE_LINKING
);
82 /* Add T to the end of the list container pointed to by LIST_P.
83 If T is an expression with no effects, it is ignored. */
86 append_to_statement_list (tree t
, tree
*list_p
)
88 if (t
&& TREE_SIDE_EFFECTS (t
))
89 append_to_statement_list_1 (t
, list_p
);
92 /* Similar, but the statement is always added, regardless of side effects. */
95 append_to_statement_list_force (tree t
, tree
*list_p
)
98 append_to_statement_list_1 (t
, list_p
);
101 /* Links a statement, or a chain of statements, before the current stmt. */
104 tsi_link_before (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
106 struct tree_statement_list_node
*head
, *tail
, *cur
;
108 /* Die on looping. */
109 gcc_assert (t
!= i
->container
);
111 if (TREE_CODE (t
) == STATEMENT_LIST
)
113 head
= STATEMENT_LIST_HEAD (t
);
114 tail
= STATEMENT_LIST_TAIL (t
);
115 STATEMENT_LIST_HEAD (t
) = NULL
;
116 STATEMENT_LIST_TAIL (t
) = NULL
;
120 /* Empty statement lists need no work. */
123 gcc_assert (head
== tail
);
129 head
= ggc_alloc_tree_statement_list_node ();
136 TREE_SIDE_EFFECTS (i
->container
) = 1;
140 /* Link it into the list. */
143 head
->prev
= cur
->prev
;
145 head
->prev
->next
= head
;
147 STATEMENT_LIST_HEAD (i
->container
) = head
;
153 head
->prev
= STATEMENT_LIST_TAIL (i
->container
);
155 head
->prev
->next
= head
;
157 STATEMENT_LIST_HEAD (i
->container
) = head
;
158 STATEMENT_LIST_TAIL (i
->container
) = tail
;
161 /* Update the iterator, if requested. */
165 case TSI_CONTINUE_LINKING
:
166 case TSI_CHAIN_START
:
177 /* Links a statement, or a chain of statements, after the current stmt. */
180 tsi_link_after (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
182 struct tree_statement_list_node
*head
, *tail
, *cur
;
184 /* Die on looping. */
185 gcc_assert (t
!= i
->container
);
187 if (TREE_CODE (t
) == STATEMENT_LIST
)
189 head
= STATEMENT_LIST_HEAD (t
);
190 tail
= STATEMENT_LIST_TAIL (t
);
191 STATEMENT_LIST_HEAD (t
) = NULL
;
192 STATEMENT_LIST_TAIL (t
) = NULL
;
196 /* Empty statement lists need no work. */
199 gcc_assert (head
== tail
);
205 head
= ggc_alloc_tree_statement_list_node ();
212 TREE_SIDE_EFFECTS (i
->container
) = 1;
216 /* Link it into the list. */
219 tail
->next
= cur
->next
;
221 tail
->next
->prev
= tail
;
223 STATEMENT_LIST_TAIL (i
->container
) = tail
;
229 gcc_assert (!STATEMENT_LIST_TAIL (i
->container
));
230 STATEMENT_LIST_HEAD (i
->container
) = head
;
231 STATEMENT_LIST_TAIL (i
->container
) = tail
;
234 /* Update the iterator, if requested. */
238 case TSI_CHAIN_START
:
241 case TSI_CONTINUE_LINKING
:
251 /* Remove a stmt from the tree list. The iterator is updated to point to
255 tsi_delink (tree_stmt_iterator
*i
)
257 struct tree_statement_list_node
*cur
, *next
, *prev
;
266 STATEMENT_LIST_HEAD (i
->container
) = next
;
270 STATEMENT_LIST_TAIL (i
->container
) = prev
;
273 TREE_SIDE_EFFECTS (i
->container
) = 0;
278 /* Return the first expression in a sequence of COMPOUND_EXPRs,
279 or in a STATEMENT_LIST. */
282 expr_first (tree expr
)
284 if (expr
== NULL_TREE
)
287 if (TREE_CODE (expr
) == STATEMENT_LIST
)
289 struct tree_statement_list_node
*n
= STATEMENT_LIST_HEAD (expr
);
290 return n
? n
->stmt
: NULL_TREE
;
293 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
294 expr
= TREE_OPERAND (expr
, 0);
299 /* Return the last expression in a sequence of COMPOUND_EXPRs,
300 or in a STATEMENT_LIST. */
303 expr_last (tree expr
)
305 if (expr
== NULL_TREE
)
308 if (TREE_CODE (expr
) == STATEMENT_LIST
)
310 struct tree_statement_list_node
*n
= STATEMENT_LIST_TAIL (expr
);
311 return n
? n
->stmt
: NULL_TREE
;
314 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
315 expr
= TREE_OPERAND (expr
, 1);
320 #include "gt-tree-iterator.h"