1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009 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 (""))) tree stmt_list_cache
;
36 alloc_stmt_list (void)
38 tree list
= stmt_list_cache
;
41 stmt_list_cache
= TREE_CHAIN (list
);
42 gcc_assert (stmt_list_cache
!= list
);
43 memset (list
, 0, sizeof(struct tree_common
));
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 /* If this triggers, it's a sign that the same list is being freed
59 gcc_assert (t
!= stmt_list_cache
|| stmt_list_cache
== NULL
);
60 TREE_CHAIN (t
) = stmt_list_cache
;
64 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
67 append_to_statement_list_1 (tree t
, tree
*list_p
)
74 if (t
&& TREE_CODE (t
) == STATEMENT_LIST
)
79 *list_p
= list
= alloc_stmt_list ();
83 tsi_link_after (&i
, t
, TSI_CONTINUE_LINKING
);
86 /* Add T to the end of the list container pointed to by LIST_P.
87 If T is an expression with no effects, it is ignored. */
90 append_to_statement_list (tree t
, tree
*list_p
)
92 if (t
&& TREE_SIDE_EFFECTS (t
))
93 append_to_statement_list_1 (t
, list_p
);
96 /* Similar, but the statement is always added, regardless of side effects. */
99 append_to_statement_list_force (tree t
, tree
*list_p
)
102 append_to_statement_list_1 (t
, list_p
);
105 /* Links a statement, or a chain of statements, before the current stmt. */
108 tsi_link_before (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
110 struct tree_statement_list_node
*head
, *tail
, *cur
;
112 /* Die on looping. */
113 gcc_assert (t
!= i
->container
);
115 if (TREE_CODE (t
) == STATEMENT_LIST
)
117 head
= STATEMENT_LIST_HEAD (t
);
118 tail
= STATEMENT_LIST_TAIL (t
);
119 STATEMENT_LIST_HEAD (t
) = NULL
;
120 STATEMENT_LIST_TAIL (t
) = NULL
;
124 /* Empty statement lists need no work. */
127 gcc_assert (head
== tail
);
133 head
= ggc_alloc_tree_statement_list_node ();
140 TREE_SIDE_EFFECTS (i
->container
) = 1;
144 /* Link it into the list. */
147 head
->prev
= cur
->prev
;
149 head
->prev
->next
= head
;
151 STATEMENT_LIST_HEAD (i
->container
) = head
;
157 head
->prev
= STATEMENT_LIST_TAIL (i
->container
);
159 head
->prev
->next
= head
;
161 STATEMENT_LIST_HEAD (i
->container
) = head
;
162 STATEMENT_LIST_TAIL (i
->container
) = tail
;
165 /* Update the iterator, if requested. */
169 case TSI_CONTINUE_LINKING
:
170 case TSI_CHAIN_START
:
181 /* Links a statement, or a chain of statements, after the current stmt. */
184 tsi_link_after (tree_stmt_iterator
*i
, tree t
, enum tsi_iterator_update mode
)
186 struct tree_statement_list_node
*head
, *tail
, *cur
;
188 /* Die on looping. */
189 gcc_assert (t
!= i
->container
);
191 if (TREE_CODE (t
) == STATEMENT_LIST
)
193 head
= STATEMENT_LIST_HEAD (t
);
194 tail
= STATEMENT_LIST_TAIL (t
);
195 STATEMENT_LIST_HEAD (t
) = NULL
;
196 STATEMENT_LIST_TAIL (t
) = NULL
;
200 /* Empty statement lists need no work. */
203 gcc_assert (head
== tail
);
209 head
= ggc_alloc_tree_statement_list_node ();
216 TREE_SIDE_EFFECTS (i
->container
) = 1;
220 /* Link it into the list. */
223 tail
->next
= cur
->next
;
225 tail
->next
->prev
= tail
;
227 STATEMENT_LIST_TAIL (i
->container
) = tail
;
233 gcc_assert (!STATEMENT_LIST_TAIL (i
->container
));
234 STATEMENT_LIST_HEAD (i
->container
) = head
;
235 STATEMENT_LIST_TAIL (i
->container
) = tail
;
238 /* Update the iterator, if requested. */
242 case TSI_CHAIN_START
:
245 case TSI_CONTINUE_LINKING
:
255 /* Remove a stmt from the tree list. The iterator is updated to point to
259 tsi_delink (tree_stmt_iterator
*i
)
261 struct tree_statement_list_node
*cur
, *next
, *prev
;
270 STATEMENT_LIST_HEAD (i
->container
) = next
;
274 STATEMENT_LIST_TAIL (i
->container
) = prev
;
277 TREE_SIDE_EFFECTS (i
->container
) = 0;
282 /* Return the first expression in a sequence of COMPOUND_EXPRs,
283 or in a STATEMENT_LIST. */
286 expr_first (tree expr
)
288 if (expr
== NULL_TREE
)
291 if (TREE_CODE (expr
) == STATEMENT_LIST
)
293 struct tree_statement_list_node
*n
= STATEMENT_LIST_HEAD (expr
);
294 return n
? n
->stmt
: NULL_TREE
;
297 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
298 expr
= TREE_OPERAND (expr
, 0);
303 /* Return the last expression in a sequence of COMPOUND_EXPRs,
304 or in a STATEMENT_LIST. */
307 expr_last (tree expr
)
309 if (expr
== NULL_TREE
)
312 if (TREE_CODE (expr
) == STATEMENT_LIST
)
314 struct tree_statement_list_node
*n
= STATEMENT_LIST_TAIL (expr
);
315 return n
? n
->stmt
: NULL_TREE
;
318 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
319 expr
= TREE_OPERAND (expr
, 1);
324 #include "gt-tree-iterator.h"