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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
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;
22 public class ForStatement extends Statement {
24 public Statement[] initializations;
25 public Expression condition;
26 public Statement[] increments;
27 public Statement action;
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;
34 private Label breakLabel, continueLabel;
36 // for local variables table attributes
37 int preCondInitStateIndex = -1;
38 int condIfTrueInitStateIndex = -1;
39 int mergedInitStateIndex = -1;
42 Statement[] initializations,
44 Statement[] increments,
52 this.initializations = initializations;
53 this.condition = condition;
54 this.increments = increments;
56 this.neededScope = neededScope;
59 public FlowInfo analyseCode(
60 BlockScope currentScope,
61 FlowContext flowContext,
64 breakLabel = new Label();
65 continueLabel = new Label();
67 // process the initializations
68 if (initializations != null) {
69 int count = initializations.length, i = 0;
71 flowInfo = initializations[i++].analyseCode(scope, flowContext, flowInfo);
74 preCondInitStateIndex =
75 currentScope.methodScope().recordInitializationStates(flowInfo);
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);
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);
85 // process the condition
86 LoopingFlowContext condLoopContext = null;
87 if (condition != null) {
88 if (!isConditionTrue) {
90 condition.analyseCode(
93 new LoopingFlowContext(flowContext, this, null, null, scope)),
99 LoopingFlowContext loopingContext;
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;
108 if (isConditionFalse){
109 continueLabel = null; // for(;false;p());
111 actionInfo = flowInfo.initsWhenTrue().copy();
113 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
117 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
118 FlowInfo initsWhenTrue = flowInfo.initsWhenTrue();
119 condIfTrueInitStateIndex =
120 currentScope.methodScope().recordInitializationStates(initsWhenTrue);
122 if (isConditionFalse) {
123 actionInfo = FlowInfo.DEAD_END;
125 actionInfo = initsWhenTrue.copy();
126 if (isConditionOptimizedFalse){
127 actionInfo.setReachMode(FlowInfo.UNREACHABLE);
130 if (!actionInfo.complainIfUnreachable(action, scope, false)) {
131 actionInfo = action.analyseCode(scope, loopingContext, actionInfo);
134 // code generation can be optimized when no need to continue in the loop
135 if (!actionInfo.isReachable() && !loopingContext.initsOnContinue.isReachable()) {
136 continueLabel = null;
138 if (condLoopContext != null)
139 condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
140 loopingContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
142 actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
146 if ((continueLabel != null) && (increments != null)) {
147 LoopingFlowContext loopContext =
148 new LoopingFlowContext(flowContext, this, null, null, scope);
149 int i = 0, count = increments.length;
151 actionInfo = increments[i++].analyseCode(scope, loopContext, actionInfo);
152 loopContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
157 if (isConditionOptimizedTrue) {
158 mergedInitStateIndex =
159 currentScope.methodScope().recordInitializationStates(
160 mergedInfo = loopingContext.initsOnBreak);
164 //end of loop: either condition false or break
166 flowInfo.initsWhenFalse().unconditionalInits().mergedWith(
167 loopingContext.initsOnBreak.unconditionalInits());
168 if (isConditionOptimizedTrue && continueLabel == null){
169 mergedInfo.setReachMode(FlowInfo.UNREACHABLE);
171 mergedInitStateIndex =
172 currentScope.methodScope().recordInitializationStates(mergedInfo);
177 * For statement code generation
179 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
180 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
182 // public void generateCode(BlockScope currentScope, CodeStream codeStream) {
184 // if ((bits & IsReachableMASK) == 0) {
187 // int pc = codeStream.position;
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);
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;
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);
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(
219 // condIfTrueInitStateIndex);
221 // action.generateCode(scope, codeStream);
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);
234 // // May loose some local variable initializations : affecting the local variable attributes
235 // if (preCondInitStateIndex != -1) {
236 // codeStream.removeNotDefinitelyAssignedVariables(
238 // preCondInitStateIndex);
241 // // generate the condition
242 // conditionLabel.place();
243 // if ((condition != null) && (condition.constant == NotAConstant)) {
244 // condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
246 // if (continueLabel != null) {
247 // codeStream.goto_(actionLabel);
250 // breakLabel.place();
252 // // May loose some local variable initializations : affecting the local variable attributes
253 // if (neededScope) {
254 // codeStream.exitUserScope(scope);
256 // if (mergedInitStateIndex != -1) {
257 // codeStream.removeNotDefinitelyAssignedVariables(
259 // mergedInitStateIndex);
261 // codeStream.recordPositionsFrom(pc, this.sourceStart);
264 public void resetStateForCodeGeneration() {
265 if (this.breakLabel != null) {
266 this.breakLabel.resetStateForCodeGeneration();
268 if (this.continueLabel != null) {
269 this.continueLabel.resetStateForCodeGeneration();
273 public void resolve(BlockScope upperScope) {
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);
284 if (increments != null)
285 for (int i = 0, length = increments.length; i < length; i++)
286 increments[i].resolve(scope);
288 action.resolve(scope);
291 public String toString(int tab) {
293 String s = tabString(tab) + "for ("; //$NON-NLS-1$
295 s = s + " //--NO upperscope scope needed\n" + tabString(tab) + " "; //$NON-NLS-2$ //$NON-NLS-1$
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$
305 s = s + "; "; //$NON-NLS-1$
307 if (condition != null)
308 s = s + condition.toStringExpression();
309 s = s + "; "; //$NON-NLS-1$
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$
319 s = s + ") "; //$NON-NLS-1$
322 s = s + "{}"; //$NON-NLS-1$
324 s = s + "\n" + action.toString(tab + 1); //$NON-NLS-1$
328 public void traverse(
329 IAbstractSyntaxTreeVisitor visitor,
330 BlockScope blockScope) {
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);
339 if (condition != null)
340 condition.traverse(visitor, scope);
342 if (increments != null) {
343 int incrementsLength = increments.length;
344 for (int i = 0; i < incrementsLength; i++)
345 increments[i].traverse(visitor, scope);
349 action.traverse(visitor, scope);
351 visitor.endVisit(this, blockScope);