removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / ForStatement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * For statement AST node type.
19  *
20  * <pre>
21  * ForStatement:
22  *    <b>for</b> <b>(</b>
23  *                      [ ForInit ]<b>;</b>
24  *                      [ Expression ] <b>;</b>
25  *                      [ ForUpdate ] <b>)</b>
26  *                      Statement
27  * ForInit:
28  *              Expression { <b>,</b> Expression }
29  * ForUpdate:
30  *              Expression { <b>,</b> Expression }
31  * </pre>
32  * <p>
33  * Note: When variables are declared in the initializer
34  * of a for statement such as "<code>for (int a=1, b=2;;);</code>",
35  * they should be represented as a single
36  * <code>VariableDeclarationExpression</code>
37  * with two fragments, rather than being split up into a pair
38  * of expressions.
39  * </p>
40  * 
41  * @since 2.0
42  * @noinstantiate This class is not intended to be instantiated by clients.
43  */
44 public class ForStatement extends Statement {
45         
46         /**
47          * The "initializers" structural property of this node type.
48          * @since 3.0
49          */
50         public static final ChildListPropertyDescriptor INITIALIZERS_PROPERTY = 
51                 new ChildListPropertyDescriptor(ForStatement.class, "initializers", Expression.class, CYCLE_RISK); //$NON-NLS-1$
52
53         /**
54          * The "expression" structural property of this node type.
55          * @since 3.0
56          */
57         public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = 
58                 new ChildPropertyDescriptor(ForStatement.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
59
60         /**
61          * The "updaters" structural property of this node type.
62          * @since 3.0
63          */
64         public static final ChildListPropertyDescriptor UPDATERS_PROPERTY = 
65                 new ChildListPropertyDescriptor(ForStatement.class, "updaters", Expression.class, CYCLE_RISK); //$NON-NLS-1$
66
67         /**
68          * The "body" structural property of this node type.
69          * @since 3.0
70          */
71         public static final ChildPropertyDescriptor BODY_PROPERTY = 
72                 new ChildPropertyDescriptor(ForStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
73
74         /**
75          * A list of property descriptors (element type: 
76          * {@link StructuralPropertyDescriptor}),
77          * or null if uninitialized.
78          */
79         private static final List PROPERTY_DESCRIPTORS;
80         
81         static {
82                 List properyList = new ArrayList(5);
83                 createPropertyList(ForStatement.class, properyList);
84                 addProperty(INITIALIZERS_PROPERTY, properyList);
85                 addProperty(EXPRESSION_PROPERTY, properyList);
86                 addProperty(UPDATERS_PROPERTY, properyList);
87                 addProperty(BODY_PROPERTY, properyList);
88                 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
89         }
90
91         /**
92          * Returns a list of structural property descriptors for this node type.
93          * Clients must not modify the result.
94          * 
95          * @param apiLevel the API level; one of the
96          * <code>AST.JLS*</code> constants
97
98          * @return a list of property descriptors (element type: 
99          * {@link StructuralPropertyDescriptor})
100          * @since 3.0
101          */
102         public static List propertyDescriptors(int apiLevel) {
103                 return PROPERTY_DESCRIPTORS;
104         }
105                         
106         /**
107          * The list of initializer expressions (element type: 
108          * <code>Expression</code>). Defaults to an empty list.
109          */
110         private ASTNode.NodeList initializers =
111                 new ASTNode.NodeList(INITIALIZERS_PROPERTY);
112
113         /**
114          * The condition expression; <code>null</code> for none; defaults to none.
115          */
116         private Expression optionalConditionExpression = null;
117         
118         /**
119          * The list of update expressions (element type: 
120          * <code>Expression</code>). Defaults to an empty list.
121          */
122         private ASTNode.NodeList updaters =
123                 new ASTNode.NodeList(UPDATERS_PROPERTY);
124
125         /**
126          * The body statement; lazily initialized; defaults to an empty block
127          * statement.
128          */
129         private Statement body = null;
130                         
131         /**
132          * Creates a new AST node for a for statement owned by the given AST. 
133          * By default, there are no initializers, no condition expression, 
134          * no updaters, and the body is an empty block.
135          * 
136          * @param ast the AST that is to own this node
137          */
138         ForStatement(AST ast) {
139                 super(ast);
140         }
141
142         /* (omit javadoc for this method)
143          * Method declared on ASTNode.
144          */
145         final List internalStructuralPropertiesForType(int apiLevel) {
146                 return propertyDescriptors(apiLevel);
147         }
148         
149
150         /* (omit javadoc for this method)
151          * Method declared on ASTNode.
152          */
153         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
154                 if (property == EXPRESSION_PROPERTY) {
155                         if (get) {
156                                 return getExpression();
157                         } else {
158                                 setExpression((Expression) child);
159                                 return null;
160                         }
161                 }
162                 if (property == BODY_PROPERTY) {
163                         if (get) {
164                                 return getBody();
165                         } else {
166                                 setBody((Statement) child);
167                                 return null;
168                         }
169                 }
170                 // allow default implementation to flag the error
171                 return super.internalGetSetChildProperty(property, get, child);
172         }
173         
174         /* (omit javadoc for this method)
175          * Method declared on ASTNode.
176          */
177         final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
178                 if (property == INITIALIZERS_PROPERTY) {
179                         return initializers();
180                 }
181                 if (property == UPDATERS_PROPERTY) {
182                         return updaters();
183                 }
184                 // allow default implementation to flag the error
185                 return super.internalGetChildListProperty(property);
186         }
187
188         /* (omit javadoc for this method)
189          * Method declared on ASTNode.
190          */
191         final int getNodeType0() {
192                 return FOR_STATEMENT;
193         }
194
195         /* (omit javadoc for this method)
196          * Method declared on ASTNode.
197          */
198         ASTNode clone0(AST target) {
199                 ForStatement result = new ForStatement(target);
200                 result.setSourceRange(this.getStartPosition(), this.getLength());
201                 result.copyLeadingComment(this);
202                 result.initializers().addAll(ASTNode.copySubtrees(target, initializers()));
203                 result.setExpression(
204                         (Expression) ASTNode.copySubtree(target, getExpression()));
205                 result.updaters().addAll(ASTNode.copySubtrees(target, updaters()));
206                 result.setBody(
207                         (Statement) ASTNode.copySubtree(target, getBody()));
208                 return result;
209         }
210
211         /* (omit javadoc for this method)
212          * Method declared on ASTNode.
213          */
214         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
215                 // dispatch to correct overloaded match method
216                 return matcher.match(this, other);
217         }
218
219         /* (omit javadoc for this method)
220          * Method declared on ASTNode.
221          */
222         void accept0(ASTVisitor visitor) {
223                 boolean visitChildren = visitor.visit(this);
224                 if (visitChildren) {
225                         // visit children in normal left to right reading order
226                         acceptChildren(visitor, this.initializers);
227                         acceptChild(visitor, getExpression());
228                         acceptChildren(visitor, this.updaters);
229                         acceptChild(visitor, getBody());
230                 }
231                 visitor.endVisit(this);
232         }
233         
234         /**
235          * Returns the live ordered list of initializer expressions in this for
236          * statement.
237          * <p>
238          * The list should consist of either a list of so called statement 
239          * expressions (JLS2, 14.8), or a single <code>VariableDeclarationExpression</code>. 
240          * Otherwise, the for statement would have no Java source equivalent.
241          * </p>
242          * 
243          * @return the live list of initializer expressions 
244          *    (element type: <code>Expression</code>)
245          */ 
246         public List initializers() {
247                 return this.initializers;
248         }
249         
250         /**
251          * Returns the condition expression of this for statement, or 
252          * <code>null</code> if there is none.
253          * 
254          * @return the condition expression node, or <code>null</code> if 
255          *     there is none
256          */ 
257         public Expression getExpression() {
258                 return this.optionalConditionExpression;
259         }
260         
261         /**
262          * Sets or clears the condition expression of this return statement.
263          * 
264          * @param expression the condition expression node, or <code>null</code>
265          *    if there is none
266          * @exception IllegalArgumentException if:
267          * <ul>
268          * <li>the node belongs to a different AST</li>
269          * <li>the node already has a parent</li>
270          * <li>a cycle in would be created</li>
271          * </ul>
272          */ 
273         public void setExpression(Expression expression) {
274                 ASTNode oldChild = this.optionalConditionExpression;
275                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
276                 this.optionalConditionExpression = expression;
277                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
278         }
279
280         /**
281          * Returns the live ordered list of update expressions in this for
282          * statement.
283          * <p>
284          * The list should consist of so called statement expressions. Otherwise,
285          * the for statement would have no Java source equivalent.
286          * </p>
287          * 
288          * @return the live list of update expressions 
289          *    (element type: <code>Expression</code>)
290          */ 
291         public List updaters() {
292                 return this.updaters;
293         }
294         
295         /**
296          * Returns the body of this for statement.
297          * 
298          * @return the body statement node
299          */ 
300         public Statement getBody() {
301                 if (this.body == null) {
302                         // lazy init must be thread-safe for readers
303                         synchronized (this) {
304                                 if (this.body == null) {
305                                         preLazyInit();
306                                         this.body = new Block(this.ast);
307                                         postLazyInit(this.body, BODY_PROPERTY);
308                                 }
309                         }
310                 }
311                 return this.body;
312         }
313         
314         /**
315          * Sets the body of this for statement.
316          * <p>
317          * Special note: The Java language does not allow a local variable declaration
318          * to appear as the body of a for statement (they may only appear within a
319          * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
320          * as the body of a <code>ForStatement</code>. To get something that will
321          * compile, be sure to embed the <code>VariableDeclarationStatement</code>
322          * inside a <code>Block</code>.
323          * </p>
324          * 
325          * @param statement the body statement node
326          * @exception IllegalArgumentException if:
327          * <ul>
328          * <li>the node belongs to a different AST</li>
329          * <li>the node already has a parent</li>
330          * <li>a cycle in would be created</li>
331          * </ul>
332          */ 
333         public void setBody(Statement statement) {
334                 if (statement == null) {
335                         throw new IllegalArgumentException();
336                 }
337                 ASTNode oldChild = this.body;
338                 preReplaceChild(oldChild, statement, BODY_PROPERTY);
339                 this.body = statement;
340                 postReplaceChild(oldChild, statement, BODY_PROPERTY);
341         }
342         
343         /* (omit javadoc for this method)
344          * Method declared on ASTNode.
345          */
346         int memSize() {
347                 return super.memSize() + 4 * 4;
348         }
349         
350         /* (omit javadoc for this method)
351          * Method declared on ASTNode.
352          */
353         int treeSize() {
354                 return
355                         memSize()
356                         + this.initializers.listSize()
357                         + this.updaters.listSize()
358                         + (this.optionalConditionExpression == null ? 0 : getExpression().treeSize())
359                         + (this.body == null ? 0 : getBody().treeSize());
360         }
361 }