- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / acpi / parser / pswalk.c
blob04a75917d6ffe7d3fea0380947dbb3b149be5d1a
1 /******************************************************************************
3 * Module Name: pswalk - Parser routines to walk parsed op tree(s)
4 * $Revision: 50 $
6 *****************************************************************************/
8 /*
9 * Copyright (C) 2000 R. Byron Moore
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "acpi.h"
28 #include "amlcode.h"
29 #include "acparser.h"
30 #include "acdispat.h"
31 #include "acnamesp.h"
32 #include "acinterp.h"
34 #define _COMPONENT PARSER
35 MODULE_NAME ("pswalk")
38 /*******************************************************************************
40 * FUNCTION: Acpi_ps_get_next_walk_op
42 * PARAMETERS: Walk_state - Current state of the walk
43 * Op - Current Op to be walked
44 * Ascending_callback - Procedure called when Op is complete
46 * RETURN: Status
48 * DESCRIPTION: Get the next Op in a walk of the parse tree.
50 ******************************************************************************/
52 ACPI_STATUS
53 acpi_ps_get_next_walk_op (
54 ACPI_WALK_STATE *walk_state,
55 ACPI_PARSE_OBJECT *op,
56 ACPI_PARSE_UPWARDS ascending_callback)
58 ACPI_PARSE_OBJECT *next;
59 ACPI_PARSE_OBJECT *parent;
60 ACPI_PARSE_OBJECT *grand_parent;
61 ACPI_STATUS status;
64 /* Check for a argument only if we are descending in the tree */
66 if (walk_state->next_op_info != NEXT_OP_UPWARD) {
67 /* Look for an argument or child of the current op */
69 next = acpi_ps_get_arg (op, 0);
70 if (next) {
71 /* Still going downward in tree (Op is not completed yet) */
73 walk_state->prev_op = op;
74 walk_state->next_op = next;
75 walk_state->next_op_info = NEXT_OP_DOWNWARD;
77 return (AE_OK);
82 * No more children, this Op is complete. Save Next and Parent
83 * in case the Op object gets deleted by the callback routine
86 next = op->next;
87 parent = op->parent;
89 status = ascending_callback (walk_state, op);
92 * If we are back to the starting point, the walk is complete.
94 if (op == walk_state->origin) {
95 /* Reached the point of origin, the walk is complete */
97 walk_state->prev_op = op;
98 walk_state->next_op = NULL;
100 return (status);
104 * Check for a sibling to the current op. A sibling means
105 * we are still going "downward" in the tree.
108 if (next) {
109 /* There is a sibling, it will be next */
111 walk_state->prev_op = op;
112 walk_state->next_op = next;
113 walk_state->next_op_info = NEXT_OP_DOWNWARD;
115 /* Continue downward */
117 return (status);
122 * Drop into the loop below because we are moving upwards in
123 * the tree
127 else {
129 * We are resuming a walk, and we were (are) going upward in the tree.
130 * So, we want to drop into the parent loop below.
133 parent = op;
138 * Look for a sibling of the current Op's parent
139 * Continue moving up the tree until we find a node that has not been
140 * visited, or we get back to where we started.
142 while (parent) {
143 /* We are moving up the tree, therefore this parent Op is complete */
145 grand_parent = parent->parent;
146 next = parent->next;
148 status = ascending_callback (walk_state, parent);
151 * If we are back to the starting point, the walk is complete.
153 if (parent == walk_state->origin) {
154 /* Reached the point of origin, the walk is complete */
156 walk_state->prev_op = parent;
157 walk_state->next_op = NULL;
159 return (status);
163 * If there is a sibling to this parent (it is not the starting point
164 * Op), then we will visit it.
166 if (next) {
167 /* found sibling of parent */
169 walk_state->prev_op = parent;
170 walk_state->next_op = next;
171 walk_state->next_op_info = NEXT_OP_DOWNWARD;
173 return (status);
176 /* No siblings, no errors, just move up one more level in the tree */
178 op = parent;
179 parent = grand_parent;
180 walk_state->prev_op = op;
184 /* Got all the way to the top of the tree, we must be done! */
185 /* However, the code should have terminated in the loop above */
187 walk_state->next_op = NULL;
189 return (AE_OK);
193 /*******************************************************************************
195 * FUNCTION: Acpi_ps_delete_completed_op
197 * PARAMETERS: State - Walk state
198 * Op - Completed op
200 * RETURN: AE_OK
202 * DESCRIPTION: Callback function for Acpi_ps_get_next_walk_op(). Used during
203 * Acpi_ps_delete_parse tree to delete Op objects when all sub-objects
204 * have been visited (and deleted.)
206 ******************************************************************************/
208 static ACPI_STATUS
209 acpi_ps_delete_completed_op (
210 ACPI_WALK_STATE *state,
211 ACPI_PARSE_OBJECT *op)
214 acpi_ps_free_op (op);
215 return (AE_OK);
219 /*******************************************************************************
221 * FUNCTION: Acpi_ps_delete_parse_tree
223 * PARAMETERS: Subtree_root - Root of tree (or subtree) to delete
225 * RETURN: None
227 * DESCRIPTION: Delete a portion of or an entire parse tree.
229 ******************************************************************************/
231 void
232 acpi_ps_delete_parse_tree (
233 ACPI_PARSE_OBJECT *subtree_root)
235 ACPI_WALK_STATE *walk_state;
236 ACPI_WALK_LIST walk_list;
239 if (!subtree_root) {
240 return;
243 /* Create and initialize a new walk list */
245 walk_list.walk_state = NULL;
246 walk_state = acpi_ds_create_walk_state (TABLE_ID_DSDT, NULL, NULL, &walk_list);
247 if (!walk_state) {
248 return;
251 walk_state->parser_state = NULL;
252 walk_state->parse_flags = 0;
253 walk_state->descending_callback = NULL;
254 walk_state->ascending_callback = NULL;
257 walk_state->origin = subtree_root;
258 walk_state->next_op = subtree_root;
261 /* Head downward in the tree */
263 walk_state->next_op_info = NEXT_OP_DOWNWARD;
265 /* Visit all nodes in the subtree */
267 while (walk_state->next_op) {
268 acpi_ps_get_next_walk_op (walk_state, walk_state->next_op,
269 acpi_ps_delete_completed_op);
272 /* We are done with this walk */
274 acpi_ds_delete_walk_state (walk_state);
276 return;