new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / internal / compiler / ast / ForStatement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.flow.LoopingFlowContext;
18 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
19 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
21
22 public class ForStatement extends Statement {
23         
24         public Statement[] initializations;
25         public Expression condition;
26         public Statement[] increments;
27         public Statement action;
28
29         //when there is no local declaration, there is no need of a new scope
30         //scope is positionned either to a new scope, or to the "upper"scope (see resolveType)
31         public boolean neededScope;
32         public BlockScope scope;
33
34         private Label breakLabel, continueLabel;
35
36         // for local variables table attributes
37         int preCondInitStateIndex = -1;
38         int condIfTrueInitStateIndex = -1;
39         int mergedInitStateIndex = -1;
40
41         public ForStatement(
42                 Statement[] initializations,
43                 Expression condition,
44                 Statement[] increments,
45                 Statement action,
46                 boolean neededScope,
47                 int s,
48                 int e) {
49
50                 this.sourceStart = s;
51                 this.sourceEnd = e;
52                 this.initializations = initializations;
53                 this.condition = condition;
54                 this.increments = increments;
55                 this.action = action;
56                 this.neededScope = neededScope;
57         }
58
59         public FlowInfo analyseCode(
60                 BlockScope currentScope,
61                 FlowContext flowContext,
62                 FlowInfo flowInfo) {
63                         
64                 breakLabel = new Label();
65                 continueLabel = new Label();
66
67                 // process the initializations
68                 if (initializations != null) {
69                         int count = initializations.length, i = 0;
70                         while (i < count) {
71                                 flowInfo = initializations[i++].analyseCode(scope, flowContext, flowInfo);
72                         }
73                 }
74                 preCondInitStateIndex =
75                         currentScope.methodScope().recordInitializationStates(flowInfo);
76
77                 Constant cst = this.condition == null ? null : this.condition.constant;
78                 boolean isConditionTrue = cst == null || (cst != NotAConstant && cst.booleanValue() == true);
79                 boolean isConditionFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
80
81                 cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
82                 boolean isConditionOptimizedTrue = cst == null ||  (cst != NotAConstant && cst.booleanValue() == true);
83                 boolean isConditionOptimizedFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
84                 
85                 // process the condition
86                 LoopingFlowContext condLoopContext = null;
87                 if (condition != null) {
88                         if (!isConditionTrue) {
89                                 flowInfo =
90                                         condition.analyseCode(
91                                                 scope,
92                                                 (condLoopContext =
93                                                         new LoopingFlowContext(flowContext, this, null, null, scope)),
94                                                 flowInfo);
95                         }
96                 }
97
98                 // process the action
99                 LoopingFlowContext loopingContext;
100                 FlowInfo actionInfo;
101                 if (action == null ){
102 //                      || (action.isEmptyBlock() && currentScope.environment().options.complianceLevel <= CompilerOptions.JDK1_3)) {
103                         if (condLoopContext != null)
104                                 condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
105                         if (isConditionTrue) {
106                                 return FlowInfo.DEAD_END;
107                         } else {
108                                 if (isConditionFalse){
109                                         continueLabel = null; // for(;false;p());
110                                 }
111                                 actionInfo = flowInfo.initsWhenTrue().copy();
112                                 loopingContext =
113                                         new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
114                         }
115                 } else {
116                         loopingContext =
117                                 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
118                         FlowInfo initsWhenTrue = flowInfo.initsWhenTrue();
119                         condIfTrueInitStateIndex =
120                                 currentScope.methodScope().recordInitializationStates(initsWhenTrue);
121
122                                 if (isConditionFalse) {
123                                         actionInfo = FlowInfo.DEAD_END;
124                                 } else {
125                                         actionInfo = initsWhenTrue.copy();
126                                         if (isConditionOptimizedFalse){
127                                                 actionInfo.setReachMode(FlowInfo.UNREACHABLE);
128                                         }
129                                 }
130                         if (!actionInfo.complainIfUnreachable(action, scope, false)) {
131                                 actionInfo = action.analyseCode(scope, loopingContext, actionInfo);
132                         }
133
134                         // code generation can be optimized when no need to continue in the loop
135                         if (!actionInfo.isReachable() && !loopingContext.initsOnContinue.isReachable()) {
136                                 continueLabel = null;
137                         } else {
138                                 if (condLoopContext != null)
139                                         condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
140                                 loopingContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
141                                 actionInfo =
142                                         actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
143                                 // for increments
144                         }
145                 }
146                 if ((continueLabel != null) && (increments != null)) {
147                         LoopingFlowContext loopContext =
148                                 new LoopingFlowContext(flowContext, this, null, null, scope);
149                         int i = 0, count = increments.length;
150                         while (i < count)
151                                 actionInfo = increments[i++].analyseCode(scope, loopContext, actionInfo);
152                         loopContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
153                 }
154
155                 // infinite loop
156                 FlowInfo mergedInfo;
157                 if (isConditionOptimizedTrue) {
158                         mergedInitStateIndex =
159                                 currentScope.methodScope().recordInitializationStates(
160                                         mergedInfo = loopingContext.initsOnBreak);
161                         return mergedInfo;
162                 }
163
164                 //end of loop: either condition false or break
165                 mergedInfo =
166                         flowInfo.initsWhenFalse().unconditionalInits().mergedWith(
167                                 loopingContext.initsOnBreak.unconditionalInits());
168                 if (isConditionOptimizedTrue && continueLabel == null){
169                         mergedInfo.setReachMode(FlowInfo.UNREACHABLE);
170                 }
171                 mergedInitStateIndex =
172                         currentScope.methodScope().recordInitializationStates(mergedInfo);
173                 return mergedInfo;
174         }
175
176         /**
177          * For statement code generation
178          *
179          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
180          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
181          */
182 //      public void generateCode(BlockScope currentScope, CodeStream codeStream) {
183 //
184 //              if ((bits & IsReachableMASK) == 0) {
185 //                      return;
186 //              }
187 //              int pc = codeStream.position;
188 //
189 //              // generate the initializations
190 //              if (initializations != null) {
191 //                      for (int i = 0, max = initializations.length; i < max; i++) {
192 //                              initializations[i].generateCode(scope, codeStream);
193 //                      }
194 //              }
195 //
196 //              // label management
197 //              Label actionLabel = new Label(codeStream);
198 //              Label conditionLabel = new Label(codeStream);
199 //              breakLabel.codeStream = codeStream;
200 //              if (continueLabel != null) {
201 //                      continueLabel.codeStream = codeStream;
202 //              }
203 //              // jump over the actionBlock
204 //              if ((condition != null)
205 //                      && (condition.constant == NotAConstant)
206 //                      && !((action == null || action.isEmptyBlock()) && (increments == null))) {
207 //                      int jumpPC = codeStream.position;
208 //                      codeStream.goto_(conditionLabel);
209 //                      codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
210 //              }
211 //              // generate the loop action
212 //              actionLabel.place();
213 //              if (action != null) {
214 //                      // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
215 //                      if (condIfTrueInitStateIndex != -1) {
216 //                              // insert all locals initialized inside the condition into the action generated prior to the condition
217 //                              codeStream.addDefinitelyAssignedVariables(
218 //                                      currentScope,
219 //                                      condIfTrueInitStateIndex);
220 //                      }
221 //                      action.generateCode(scope, codeStream);
222 //              }
223 //              // continuation point
224 //              if (continueLabel != null) {
225 //                      continueLabel.place();
226 //                      // generate the increments for next iteration
227 //                      if (increments != null) {
228 //                              for (int i = 0, max = increments.length; i < max; i++) {
229 //                                      increments[i].generateCode(scope, codeStream);
230 //                              }
231 //                      }
232 //              }
233 //
234 //              // May loose some local variable initializations : affecting the local variable attributes
235 //              if (preCondInitStateIndex != -1) {
236 //                      codeStream.removeNotDefinitelyAssignedVariables(
237 //                              currentScope,
238 //                              preCondInitStateIndex);
239 //              }
240 //
241 //              // generate the condition
242 //              conditionLabel.place();
243 //              if ((condition != null) && (condition.constant == NotAConstant)) {
244 //                      condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
245 //              } else {
246 //                      if (continueLabel != null) {
247 //                              codeStream.goto_(actionLabel);
248 //                      }
249 //              }
250 //              breakLabel.place();
251 //
252 //              // May loose some local variable initializations : affecting the local variable attributes
253 //              if (neededScope) {
254 //                      codeStream.exitUserScope(scope);
255 //              }
256 //              if (mergedInitStateIndex != -1) {
257 //                      codeStream.removeNotDefinitelyAssignedVariables(
258 //                              currentScope,
259 //                              mergedInitStateIndex);
260 //              }
261 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
262 //      }
263
264         public void resetStateForCodeGeneration() {
265                 if (this.breakLabel != null) {
266                         this.breakLabel.resetStateForCodeGeneration();
267                 }
268                 if (this.continueLabel != null) {
269                         this.continueLabel.resetStateForCodeGeneration();
270                 }
271         }
272
273         public void resolve(BlockScope upperScope) {
274
275                 // use the scope that will hold the init declarations
276                 scope = neededScope ? new BlockScope(upperScope) : upperScope;
277                 if (initializations != null)
278                         for (int i = 0, length = initializations.length; i < length; i++)
279                                 initializations[i].resolve(scope);
280                 if (condition != null) {
281                         TypeBinding type = condition.resolveTypeExpecting(scope, BooleanBinding);
282                         condition.implicitWidening(type, type);
283                 }
284                 if (increments != null)
285                         for (int i = 0, length = increments.length; i < length; i++)
286                                 increments[i].resolve(scope);
287                 if (action != null)
288                         action.resolve(scope);
289         }
290
291         public String toString(int tab) {
292
293                 String s = tabString(tab) + "for ("; //$NON-NLS-1$
294                 if (!neededScope)
295                         s = s + " //--NO upperscope scope needed\n" + tabString(tab) + "     "; //$NON-NLS-2$ //$NON-NLS-1$
296                 //inits
297                 if (initializations != null) {
298                         for (int i = 0; i < initializations.length; i++) {
299                                 //nice only with expressions
300                                 s = s + initializations[i].toString(0);
301                                 if (i != (initializations.length - 1))
302                                         s = s + " , "; //$NON-NLS-1$
303                         }
304                 }; 
305                 s = s + "; "; //$NON-NLS-1$
306                 //cond
307                 if (condition != null)
308                         s = s + condition.toStringExpression();
309                 s = s + "; "; //$NON-NLS-1$
310                 //updates
311                 if (increments != null) {
312                         for (int i = 0; i < increments.length; i++) {
313                                 //nice only with expressions
314                                 s = s + increments[i].toString(0);
315                                 if (i != (increments.length - 1))
316                                         s = s + " , "; //$NON-NLS-1$
317                         }
318                 }; 
319                 s = s + ") "; //$NON-NLS-1$
320                 //block
321                 if (action == null)
322                         s = s + "{}"; //$NON-NLS-1$
323                 else
324                         s = s + "\n" + action.toString(tab + 1); //$NON-NLS-1$
325                 return s;
326         }
327         
328         public void traverse(
329                 IAbstractSyntaxTreeVisitor visitor,
330                 BlockScope blockScope) {
331
332                 if (visitor.visit(this, blockScope)) {
333                         if (initializations != null) {
334                                 int initializationsLength = initializations.length;
335                                 for (int i = 0; i < initializationsLength; i++)
336                                         initializations[i].traverse(visitor, scope);
337                         }
338
339                         if (condition != null)
340                                 condition.traverse(visitor, scope);
341
342                         if (increments != null) {
343                                 int incrementsLength = increments.length;
344                                 for (int i = 0; i < incrementsLength; i++)
345                                         increments[i].traverse(visitor, scope);
346                         }
347
348                         if (action != null)
349                                 action.traverse(visitor, scope);
350                 }
351                 visitor.endVisit(this, blockScope);
352         }
353 }