import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
public class ForStatement extends Statement {
-
+
public Statement[] initializations;
+
public Expression condition;
+
public Statement[] increments;
+
public Statement action;
- //when there is no local declaration, there is no need of a new scope
- //scope is positionned either to a new scope, or to the "upper"scope (see resolveType)
+ // when there is no local declaration, there is no need of a new scope
+ // scope is positionned either to a new scope, or to the "upper"scope (see
+ // resolveType)
public boolean neededScope;
+
public BlockScope scope;
private Label breakLabel, continueLabel;
// for local variables table attributes
int preCondInitStateIndex = -1;
+
int condIfTrueInitStateIndex = -1;
+
int mergedInitStateIndex = -1;
- public ForStatement(
- Statement[] initializations,
- Expression condition,
- Statement[] increments,
- Statement action,
- boolean neededScope,
- int s,
- int e) {
+ public ForStatement(Statement[] initializations, Expression condition,
+ Statement[] increments, Statement action, boolean neededScope,
+ int s, int e) {
this.sourceStart = s;
this.sourceEnd = e;
this.neededScope = neededScope;
}
- public FlowInfo analyseCode(
- BlockScope currentScope,
- FlowContext flowContext,
- FlowInfo flowInfo) {
-
+ public FlowInfo analyseCode(BlockScope currentScope,
+ FlowContext flowContext, FlowInfo flowInfo) {
+
breakLabel = new Label();
continueLabel = new Label();
if (initializations != null) {
int count = initializations.length, i = 0;
while (i < count) {
- flowInfo = initializations[i++].analyseCode(scope, flowContext, flowInfo);
+ flowInfo = initializations[i++].analyseCode(scope, flowContext,
+ flowInfo);
}
}
- preCondInitStateIndex =
- currentScope.methodScope().recordInitializationStates(flowInfo);
+ preCondInitStateIndex = currentScope.methodScope()
+ .recordInitializationStates(flowInfo);
Constant cst = this.condition == null ? null : this.condition.constant;
- boolean isConditionTrue = cst == null || (cst != NotAConstant && cst.booleanValue() == true);
- boolean isConditionFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
+ boolean isConditionTrue = cst == null
+ || (cst != NotAConstant && cst.booleanValue() == true);
+ boolean isConditionFalse = cst != null
+ && (cst != NotAConstant && cst.booleanValue() == false);
+
+ cst = this.condition == null ? null : this.condition
+ .optimizedBooleanConstant();
+ boolean isConditionOptimizedTrue = cst == null
+ || (cst != NotAConstant && cst.booleanValue() == true);
+ boolean isConditionOptimizedFalse = cst != null
+ && (cst != NotAConstant && cst.booleanValue() == false);
- cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
- boolean isConditionOptimizedTrue = cst == null || (cst != NotAConstant && cst.booleanValue() == true);
- boolean isConditionOptimizedFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
-
// process the condition
LoopingFlowContext condLoopContext = null;
if (condition != null) {
if (!isConditionTrue) {
- flowInfo =
- condition.analyseCode(
- scope,
- (condLoopContext =
- new LoopingFlowContext(flowContext, this, null, null, scope)),
- flowInfo);
+ flowInfo = condition.analyseCode(scope,
+ (condLoopContext = new LoopingFlowContext(flowContext,
+ this, null, null, scope)), flowInfo);
}
}
// process the action
LoopingFlowContext loopingContext;
FlowInfo actionInfo;
- if (action == null ){
-// || (action.isEmptyBlock() && currentScope.environment().options.complianceLevel <= CompilerOptions.JDK1_3)) {
+ if (action == null) {
+ // || (action.isEmptyBlock() &&
+ // currentScope.environment().options.complianceLevel <=
+ // CompilerOptions.JDK1_3)) {
if (condLoopContext != null)
- condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
+ condLoopContext.complainOnFinalAssignmentsInLoop(scope,
+ flowInfo);
if (isConditionTrue) {
return FlowInfo.DEAD_END;
} else {
- if (isConditionFalse){
+ if (isConditionFalse) {
continueLabel = null; // for(;false;p());
}
actionInfo = flowInfo.initsWhenTrue().copy();
- loopingContext =
- new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
+ loopingContext = new LoopingFlowContext(flowContext, this,
+ breakLabel, continueLabel, scope);
}
} else {
- loopingContext =
- new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
+ loopingContext = new LoopingFlowContext(flowContext, this,
+ breakLabel, continueLabel, scope);
FlowInfo initsWhenTrue = flowInfo.initsWhenTrue();
- condIfTrueInitStateIndex =
- currentScope.methodScope().recordInitializationStates(initsWhenTrue);
+ condIfTrueInitStateIndex = currentScope.methodScope()
+ .recordInitializationStates(initsWhenTrue);
- if (isConditionFalse) {
- actionInfo = FlowInfo.DEAD_END;
- } else {
- actionInfo = initsWhenTrue.copy();
- if (isConditionOptimizedFalse){
- actionInfo.setReachMode(FlowInfo.UNREACHABLE);
- }
+ if (isConditionFalse) {
+ actionInfo = FlowInfo.DEAD_END;
+ } else {
+ actionInfo = initsWhenTrue.copy();
+ if (isConditionOptimizedFalse) {
+ actionInfo.setReachMode(FlowInfo.UNREACHABLE);
}
+ }
if (!actionInfo.complainIfUnreachable(action, scope, false)) {
- actionInfo = action.analyseCode(scope, loopingContext, actionInfo);
+ actionInfo = action.analyseCode(scope, loopingContext,
+ actionInfo);
}
- // code generation can be optimized when no need to continue in the loop
- if (!actionInfo.isReachable() && !loopingContext.initsOnContinue.isReachable()) {
+ // code generation can be optimized when no need to continue in the
+ // loop
+ if (!actionInfo.isReachable()
+ && !loopingContext.initsOnContinue.isReachable()) {
continueLabel = null;
} else {
if (condLoopContext != null)
- condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
- loopingContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
- actionInfo =
- actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
+ condLoopContext.complainOnFinalAssignmentsInLoop(scope,
+ flowInfo);
+ loopingContext.complainOnFinalAssignmentsInLoop(scope,
+ actionInfo);
+ actionInfo = actionInfo
+ .mergedWith(loopingContext.initsOnContinue
+ .unconditionalInits());
// for increments
}
}
if ((continueLabel != null) && (increments != null)) {
- LoopingFlowContext loopContext =
- new LoopingFlowContext(flowContext, this, null, null, scope);
+ LoopingFlowContext loopContext = new LoopingFlowContext(
+ flowContext, this, null, null, scope);
int i = 0, count = increments.length;
while (i < count)
- actionInfo = increments[i++].analyseCode(scope, loopContext, actionInfo);
+ actionInfo = increments[i++].analyseCode(scope, loopContext,
+ actionInfo);
loopContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
}
// infinite loop
FlowInfo mergedInfo;
if (isConditionOptimizedTrue) {
- mergedInitStateIndex =
- currentScope.methodScope().recordInitializationStates(
- mergedInfo = loopingContext.initsOnBreak);
+ mergedInitStateIndex = currentScope.methodScope()
+ .recordInitializationStates(
+ mergedInfo = loopingContext.initsOnBreak);
return mergedInfo;
}
- //end of loop: either condition false or break
- mergedInfo =
- flowInfo.initsWhenFalse().unconditionalInits().mergedWith(
+ // end of loop: either condition false or break
+ mergedInfo = flowInfo.initsWhenFalse().unconditionalInits().mergedWith(
loopingContext.initsOnBreak.unconditionalInits());
- if (isConditionOptimizedTrue && continueLabel == null){
+ if (isConditionOptimizedTrue && continueLabel == null) {
mergedInfo.setReachMode(FlowInfo.UNREACHABLE);
}
- mergedInitStateIndex =
- currentScope.methodScope().recordInitializationStates(mergedInfo);
+ mergedInitStateIndex = currentScope.methodScope()
+ .recordInitializationStates(mergedInfo);
return mergedInfo;
}
/**
* For statement code generation
- *
- * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
- * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
+ *
+ * @param currentScope
+ * net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
+ * @param codeStream
+ * net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
*/
-// public void generateCode(BlockScope currentScope, CodeStream codeStream) {
-//
-// if ((bits & IsReachableMASK) == 0) {
-// return;
-// }
-// int pc = codeStream.position;
-//
-// // generate the initializations
-// if (initializations != null) {
-// for (int i = 0, max = initializations.length; i < max; i++) {
-// initializations[i].generateCode(scope, codeStream);
-// }
-// }
-//
-// // label management
-// Label actionLabel = new Label(codeStream);
-// Label conditionLabel = new Label(codeStream);
-// breakLabel.codeStream = codeStream;
-// if (continueLabel != null) {
-// continueLabel.codeStream = codeStream;
-// }
-// // jump over the actionBlock
-// if ((condition != null)
-// && (condition.constant == NotAConstant)
-// && !((action == null || action.isEmptyBlock()) && (increments == null))) {
-// int jumpPC = codeStream.position;
-// codeStream.goto_(conditionLabel);
-// codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
-// }
-// // generate the loop action
-// actionLabel.place();
-// if (action != null) {
-// // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
-// if (condIfTrueInitStateIndex != -1) {
-// // insert all locals initialized inside the condition into the action generated prior to the condition
-// codeStream.addDefinitelyAssignedVariables(
-// currentScope,
-// condIfTrueInitStateIndex);
-// }
-// action.generateCode(scope, codeStream);
-// }
-// // continuation point
-// if (continueLabel != null) {
-// continueLabel.place();
-// // generate the increments for next iteration
-// if (increments != null) {
-// for (int i = 0, max = increments.length; i < max; i++) {
-// increments[i].generateCode(scope, codeStream);
-// }
-// }
-// }
-//
-// // May loose some local variable initializations : affecting the local variable attributes
-// if (preCondInitStateIndex != -1) {
-// codeStream.removeNotDefinitelyAssignedVariables(
-// currentScope,
-// preCondInitStateIndex);
-// }
-//
-// // generate the condition
-// conditionLabel.place();
-// if ((condition != null) && (condition.constant == NotAConstant)) {
-// condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
-// } else {
-// if (continueLabel != null) {
-// codeStream.goto_(actionLabel);
-// }
-// }
-// breakLabel.place();
-//
-// // May loose some local variable initializations : affecting the local variable attributes
-// if (neededScope) {
-// codeStream.exitUserScope(scope);
-// }
-// if (mergedInitStateIndex != -1) {
-// codeStream.removeNotDefinitelyAssignedVariables(
-// currentScope,
-// mergedInitStateIndex);
-// }
-// codeStream.recordPositionsFrom(pc, this.sourceStart);
-// }
-
+ // public void generateCode(BlockScope currentScope, CodeStream codeStream)
+ // {
+ //
+ // if ((bits & IsReachableMASK) == 0) {
+ // return;
+ // }
+ // int pc = codeStream.position;
+ //
+ // // generate the initializations
+ // if (initializations != null) {
+ // for (int i = 0, max = initializations.length; i < max; i++) {
+ // initializations[i].generateCode(scope, codeStream);
+ // }
+ // }
+ //
+ // // label management
+ // Label actionLabel = new Label(codeStream);
+ // Label conditionLabel = new Label(codeStream);
+ // breakLabel.codeStream = codeStream;
+ // if (continueLabel != null) {
+ // continueLabel.codeStream = codeStream;
+ // }
+ // // jump over the actionBlock
+ // if ((condition != null)
+ // && (condition.constant == NotAConstant)
+ // && !((action == null || action.isEmptyBlock()) && (increments == null)))
+ // {
+ // int jumpPC = codeStream.position;
+ // codeStream.goto_(conditionLabel);
+ // codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
+ // }
+ // // generate the loop action
+ // actionLabel.place();
+ // if (action != null) {
+ // // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for
+ // method appears incorrect
+ // if (condIfTrueInitStateIndex != -1) {
+ // // insert all locals initialized inside the condition into the action
+ // generated prior to the condition
+ // codeStream.addDefinitelyAssignedVariables(
+ // currentScope,
+ // condIfTrueInitStateIndex);
+ // }
+ // action.generateCode(scope, codeStream);
+ // }
+ // // continuation point
+ // if (continueLabel != null) {
+ // continueLabel.place();
+ // // generate the increments for next iteration
+ // if (increments != null) {
+ // for (int i = 0, max = increments.length; i < max; i++) {
+ // increments[i].generateCode(scope, codeStream);
+ // }
+ // }
+ // }
+ //
+ // // May loose some local variable initializations : affecting the local
+ // variable attributes
+ // if (preCondInitStateIndex != -1) {
+ // codeStream.removeNotDefinitelyAssignedVariables(
+ // currentScope,
+ // preCondInitStateIndex);
+ // }
+ //
+ // // generate the condition
+ // conditionLabel.place();
+ // if ((condition != null) && (condition.constant == NotAConstant)) {
+ // condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null,
+ // true);
+ // } else {
+ // if (continueLabel != null) {
+ // codeStream.goto_(actionLabel);
+ // }
+ // }
+ // breakLabel.place();
+ //
+ // // May loose some local variable initializations : affecting the local
+ // variable attributes
+ // if (neededScope) {
+ // codeStream.exitUserScope(scope);
+ // }
+ // if (mergedInitStateIndex != -1) {
+ // codeStream.removeNotDefinitelyAssignedVariables(
+ // currentScope,
+ // mergedInitStateIndex);
+ // }
+ // codeStream.recordPositionsFrom(pc, this.sourceStart);
+ // }
public void resetStateForCodeGeneration() {
if (this.breakLabel != null) {
this.breakLabel.resetStateForCodeGeneration();
this.continueLabel.resetStateForCodeGeneration();
}
}
+
public StringBuffer printStatement(int tab, StringBuffer output) {
printIndent(tab, output).append("for ("); //$NON-NLS-1$
- //inits
+ // inits
if (initializations != null) {
for (int i = 0; i < initializations.length; i++) {
- //nice only with expressions
- if (i > 0) output.append(", "); //$NON-NLS-1$
+ // nice only with expressions
+ if (i > 0)
+ output.append(", "); //$NON-NLS-1$
initializations[i].print(0, output);
}
}
output.append("; "); //$NON-NLS-1$
- //cond
- if (condition != null) condition.printExpression(0, output);
+ // cond
+ if (condition != null)
+ condition.printExpression(0, output);
output.append("; "); //$NON-NLS-1$
- //updates
+ // updates
if (increments != null) {
for (int i = 0; i < increments.length; i++) {
- if (i > 0) output.append(", "); //$NON-NLS-1$
+ if (i > 0)
+ output.append(", "); //$NON-NLS-1$
increments[i].print(0, output);
}
}
output.append(") "); //$NON-NLS-1$
- //block
+ // block
if (action == null)
output.append(';');
else {
}
return output.append(';');
}
+
public void resolve(BlockScope upperScope) {
// use the scope that will hold the init declarations
for (int i = 0, length = initializations.length; i < length; i++)
initializations[i].resolve(scope);
if (condition != null) {
- TypeBinding type = condition.resolveTypeExpecting(scope, BooleanBinding);
+ TypeBinding type = condition.resolveTypeExpecting(scope,
+ BooleanBinding);
condition.implicitWidening(type, type);
}
if (increments != null)
String s = tabString(tab) + "for ("; //$NON-NLS-1$
if (!neededScope)
- s = s + " //--NO upperscope scope needed\n" + tabString(tab) + " "; //$NON-NLS-2$ //$NON-NLS-1$
- //inits
+ s = s
+ + " //--NO upperscope scope needed\n" + tabString(tab) + " "; //$NON-NLS-2$ //$NON-NLS-1$
+ // inits
if (initializations != null) {
for (int i = 0; i < initializations.length; i++) {
- //nice only with expressions
+ // nice only with expressions
s = s + initializations[i].toString(0);
if (i != (initializations.length - 1))
s = s + " , "; //$NON-NLS-1$
}
- };
+ }
+ ;
s = s + "; "; //$NON-NLS-1$
- //cond
+ // cond
if (condition != null)
s = s + condition.toStringExpression();
s = s + "; "; //$NON-NLS-1$
- //updates
+ // updates
if (increments != null) {
for (int i = 0; i < increments.length; i++) {
- //nice only with expressions
+ // nice only with expressions
s = s + increments[i].toString(0);
if (i != (increments.length - 1))
s = s + " , "; //$NON-NLS-1$
}
- };
+ }
+ ;
s = s + ") "; //$NON-NLS-1$
- //block
+ // block
if (action == null)
s = s + "{}"; //$NON-NLS-1$
else
s = s + "\n" + action.toString(tab + 1); //$NON-NLS-1$
return s;
}
-
- public void traverse(
- ASTVisitor visitor,
- BlockScope blockScope) {
+
+ public void traverse(ASTVisitor visitor, BlockScope blockScope) {
if (visitor.visit(this, blockScope)) {
if (initializations != null) {