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