Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ForStatement.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import java.util.List;
4
5 /**
6  * A For statement.
7  * for(initializations;condition;increments) action
8  * @author Matthieu Casanova
9  */
10 public class ForStatement extends Statement {
11
12   /** the initializations. */
13   public Expression[] initializations;
14
15   /** the condition. */
16   public Expression condition;
17   /** the increments. */
18   public Expression[] increments;
19
20   public Statement action;
21
22   /**
23    * a for statement
24    * @param initializations the initializations expressions
25    * @param condition the condition when the for get out
26    * @param increments the increments statements
27    * @param action the action (a statement, a block ...)
28    * @param sourceStart the beginning sources
29    * @param sourceEnd the ending sources
30    */
31   public ForStatement(final Expression[] initializations,
32                       final Expression condition,
33                       final Expression[] increments,
34                       final Statement action,
35                       final int sourceStart,
36                       final int sourceEnd) {
37     super(sourceStart, sourceEnd);
38     this.initializations = initializations;
39     this.condition = condition;
40     this.increments = increments;
41     this.action = action;
42   }
43
44   public String toString(final int tab) {
45     final StringBuffer buff = new StringBuffer(tabString(tab));
46     buff.append("for (");  //$NON-NLS-1$
47     //inits
48     if (initializations != null) {
49       for (int i = 0; i < initializations.length; i++) {
50         buff.append(initializations[i].toStringExpression());
51         if (i != (initializations.length - 1))
52           buff.append(" , "); //$NON-NLS-1$
53       }
54     }
55     buff.append("; "); //$NON-NLS-1$
56     //cond
57     if (condition != null) {
58       buff.append(condition.toStringExpression());
59     }
60     buff.append("; "); //$NON-NLS-1$
61     //updates
62     if (increments != null) {
63       for (int i = 0; i < increments.length; i++) {
64         //nice only with expressions
65         buff.append(increments[i].toStringExpression());
66         if (i != (increments.length - 1))
67           buff.append(" , "); //$NON-NLS-1$
68       }
69     }
70     buff.append(") "); //$NON-NLS-1$
71     //block
72     if (action == null)
73       buff.append("{}"); //$NON-NLS-1$
74     else
75       buff.append(action.toString(tab + 1)); //$NON-NLS-1$
76     return buff.toString();
77   }
78
79   /**
80    * Get the variables from outside (parameters, globals ...)
81    */
82   public void getOutsideVariable(final List list) {
83     if (condition != null) {
84       condition.getOutsideVariable(list);
85     }
86     if (action != null) {
87       action.getOutsideVariable(list);
88     }
89     if (initializations != null) {
90       for (int i = 0; i < initializations.length; i++) {
91         initializations[i].getOutsideVariable(list);
92       }
93     }
94     if (increments != null) {
95       for (int i = 0; i < increments.length; i++) {
96         increments[i].getOutsideVariable(list);
97       }
98     }
99   }
100
101   /**
102    * get the modified variables.
103    */
104   public void getModifiedVariable(final List list) {
105     if (condition != null) {
106       condition.getModifiedVariable(list);
107     }
108     if (action != null) {
109       action.getModifiedVariable(list);
110     }
111     if (initializations != null) {
112       for (int i = 0; i < initializations.length; i++) {
113         initializations[i].getModifiedVariable(list);
114       }
115     }
116     if (increments != null) {
117       for (int i = 0; i < increments.length; i++) {
118         increments[i].getModifiedVariable(list);
119       }
120     }
121   }
122
123   /**
124    * Get the variables used.
125    */
126   public void getUsedVariable(final List list) {
127     if (condition != null) {
128       condition.getUsedVariable(list);
129     }
130     if (action != null) {
131       action.getUsedVariable(list);
132     }
133     if (initializations != null) {
134       for (int i = 0; i < initializations.length; i++) {
135         initializations[i].getUsedVariable(list);
136       }
137     }
138     if (increments != null) {
139       for (int i = 0; i < increments.length; i++) {
140         increments[i].getUsedVariable(list);
141       }
142     }
143   }
144 }