1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.impl.Constant;
18 import org.eclipse.jdt.internal.compiler.lookup.*;
20 public class ForStatement extends Statement {
22 public Statement[] initializations;
23 public Expression condition;
24 public Statement[] increments;
25 public Statement action;
27 //when there is no local declaration, there is no need of a new scope
28 //scope is positionned either to a new scope, or to the "upper"scope (see resolveType)
29 public boolean neededScope;
30 public BlockScope scope;
32 private Label breakLabel, continueLabel;
34 // for local variables table attributes
35 int preCondInitStateIndex = -1;
36 int condIfTrueInitStateIndex = -1;
37 int mergedInitStateIndex = -1;
40 Statement[] initializations,
42 Statement[] increments,
50 this.initializations = initializations;
51 this.condition = condition;
52 this.increments = increments;
54 // remember useful empty statement
55 if (action instanceof EmptyStatement) action.bits |= IsUsefulEmptyStatementMASK;
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 for (int i = 0, count = initializations.length; i < count; i++) {
70 flowInfo = initializations[i].analyseCode(scope, flowContext, flowInfo);
73 preCondInitStateIndex =
74 currentScope.methodScope().recordInitializationStates(flowInfo);
76 Constant cst = this.condition == null ? null : this.condition.constant;
77 boolean isConditionTrue = cst == null || (cst != NotAConstant && cst.booleanValue() == true);
78 boolean isConditionFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
80 cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
81 boolean isConditionOptimizedTrue = cst == null || (cst != NotAConstant && cst.booleanValue() == true);
82 boolean isConditionOptimizedFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
84 // process the condition
85 LoopingFlowContext condLoopContext = null;
86 if (condition != null) {
87 if (!isConditionTrue) {
89 condition.analyseCode(
92 new LoopingFlowContext(flowContext, this, null, null, scope)),
98 LoopingFlowContext loopingContext;
101 || (action.isEmptyBlock() && currentScope.environment().options.complianceLevel <= ClassFileConstants.JDK1_3)) {
102 if (condLoopContext != null)
103 condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
104 if (isConditionTrue) {
105 return FlowInfo.DEAD_END;
107 if (isConditionFalse){
108 continueLabel = null; // for(;false;p());
110 actionInfo = flowInfo.initsWhenTrue().copy();
112 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
116 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
117 FlowInfo initsWhenTrue = flowInfo.initsWhenTrue();
118 condIfTrueInitStateIndex =
119 currentScope.methodScope().recordInitializationStates(initsWhenTrue);
121 if (isConditionFalse) {
122 actionInfo = FlowInfo.DEAD_END;
124 actionInfo = initsWhenTrue.copy();
125 if (isConditionOptimizedFalse){
126 actionInfo.setReachMode(FlowInfo.UNREACHABLE);
129 if (!this.action.complainIfUnreachable(actionInfo, scope, false)) {
130 actionInfo = action.analyseCode(scope, loopingContext, actionInfo);
133 // code generation can be optimized when no need to continue in the loop
134 if (!actionInfo.isReachable() && !loopingContext.initsOnContinue.isReachable()) {
135 continueLabel = null;
137 if (condLoopContext != null)
138 condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
139 actionInfo = actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
140 loopingContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
144 if ((continueLabel != null) && (increments != null)) {
145 LoopingFlowContext loopContext =
146 new LoopingFlowContext(flowContext, this, null, null, scope);
147 for (int i = 0, count = increments.length; i < count; i++) {
148 actionInfo = increments[i].analyseCode(scope, loopContext, actionInfo);
150 loopContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
154 FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranches(
155 loopingContext.initsOnBreak,
156 isConditionOptimizedTrue,
157 flowInfo.initsWhenFalse(),
158 isConditionOptimizedFalse,
159 !isConditionTrue /*for(;;){}while(true); unreachable(); */);
160 mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
165 * For statement code generation
167 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
168 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
170 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
172 if ((bits & IsReachableMASK) == 0) {
175 int pc = codeStream.position;
177 // generate the initializations
178 if (initializations != null) {
179 for (int i = 0, max = initializations.length; i < max; i++) {
180 initializations[i].generateCode(scope, codeStream);
185 Label actionLabel = new Label(codeStream);
186 Label conditionLabel = new Label(codeStream);
187 breakLabel.initialize(codeStream);
188 if (continueLabel != null) {
189 continueLabel.initialize(codeStream);
191 // jump over the actionBlock
192 if ((condition != null)
193 && (condition.constant == NotAConstant)
194 && !((action == null || action.isEmptyBlock()) && (increments == null))) {
195 int jumpPC = codeStream.position;
196 codeStream.goto_(conditionLabel);
197 codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
199 // generate the loop action
201 if (action != null) {
202 // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
203 if (condIfTrueInitStateIndex != -1) {
204 // insert all locals initialized inside the condition into the action generated prior to the condition
205 codeStream.addDefinitelyAssignedVariables(
207 condIfTrueInitStateIndex);
209 action.generateCode(scope, codeStream);
211 // continuation point
212 if (continueLabel != null) {
213 continueLabel.place();
214 // generate the increments for next iteration
215 if (increments != null) {
216 for (int i = 0, max = increments.length; i < max; i++) {
217 increments[i].generateCode(scope, codeStream);
222 // May loose some local variable initializations : affecting the local variable attributes
223 if (preCondInitStateIndex != -1) {
224 codeStream.removeNotDefinitelyAssignedVariables(
226 preCondInitStateIndex);
229 // generate the condition
230 conditionLabel.place();
231 if ((condition != null) && (condition.constant == NotAConstant)) {
232 condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
234 if (continueLabel != null) {
235 codeStream.goto_(actionLabel);
240 // May loose some local variable initializations : affecting the local variable attributes
242 codeStream.exitUserScope(scope);
244 if (mergedInitStateIndex != -1) {
245 codeStream.removeNotDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
246 codeStream.addDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
248 codeStream.recordPositionsFrom(pc, this.sourceStart);
251 public StringBuffer printStatement(int tab, StringBuffer output) {
253 printIndent(tab, output).append("for ("); //$NON-NLS-1$
255 if (initializations != null) {
256 for (int i = 0; i < initializations.length; i++) {
257 //nice only with expressions
258 if (i > 0) output.append(", "); //$NON-NLS-1$
259 initializations[i].print(0, output);
262 output.append("; "); //$NON-NLS-1$
264 if (condition != null) condition.printExpression(0, output);
265 output.append("; "); //$NON-NLS-1$
267 if (increments != null) {
268 for (int i = 0; i < increments.length; i++) {
269 if (i > 0) output.append(", "); //$NON-NLS-1$
270 increments[i].print(0, output);
273 output.append(") "); //$NON-NLS-1$
279 action.printStatement(tab + 1, output); //$NON-NLS-1$
281 return output.append(';');
284 public void resolve(BlockScope upperScope) {
286 // use the scope that will hold the init declarations
287 scope = neededScope ? new BlockScope(upperScope) : upperScope;
288 if (initializations != null)
289 for (int i = 0, length = initializations.length; i < length; i++)
290 initializations[i].resolve(scope);
291 if (condition != null) {
292 TypeBinding type = condition.resolveTypeExpecting(scope, BooleanBinding);
293 condition.implicitWidening(type, type);
295 if (increments != null)
296 for (int i = 0, length = increments.length; i < length; i++)
297 increments[i].resolve(scope);
299 action.resolve(scope);
302 public void traverse(
304 BlockScope blockScope) {
306 if (visitor.visit(this, blockScope)) {
307 if (initializations != null) {
308 int initializationsLength = initializations.length;
309 for (int i = 0; i < initializationsLength; i++)
310 initializations[i].traverse(visitor, scope);
313 if (condition != null)
314 condition.traverse(visitor, scope);
316 if (increments != null) {
317 int incrementsLength = increments.length;
318 for (int i = 0; i < incrementsLength; i++)
319 increments[i].traverse(visitor, scope);
323 action.traverse(visitor, scope);
325 visitor.endVisit(this, blockScope);