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 FlowInfo condInfo = flowInfo.copy().unconditionalInits().discardNullRelatedInitializations();
87 if (condition != null) {
88 if (!isConditionTrue) {
90 condition.analyseCode(
93 new LoopingFlowContext(flowContext, this, null, null, scope)),
99 LoopingFlowContext loopingContext;
102 || (action.isEmptyBlock() && currentScope.environment().options.complianceLevel <= ClassFileConstants.JDK1_3)) {
103 if (condLoopContext != null)
104 condLoopContext.complainOnDeferredChecks(scope, condInfo);
105 if (isConditionTrue) {
106 return FlowInfo.DEAD_END;
108 if (isConditionFalse){
109 continueLabel = null; // for(;false;p());
111 actionInfo = condInfo.initsWhenTrue().copy().unconditionalInits().discardNullRelatedInitializations();
113 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
117 new LoopingFlowContext(flowContext, this, breakLabel, continueLabel, scope);
118 FlowInfo initsWhenTrue = condInfo.initsWhenTrue();
119 condIfTrueInitStateIndex =
120 currentScope.methodScope().recordInitializationStates(initsWhenTrue);
122 if (isConditionFalse) {
123 actionInfo = FlowInfo.DEAD_END;
125 actionInfo = initsWhenTrue.copy().unconditionalInits().discardNullRelatedInitializations();
126 if (isConditionOptimizedFalse){
127 actionInfo.setReachMode(FlowInfo.UNREACHABLE);
130 if (!this.action.complainIfUnreachable(actionInfo, 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.complainOnDeferredChecks(scope, condInfo);
140 actionInfo = actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
141 loopingContext.complainOnDeferredChecks(scope, actionInfo);
145 FlowInfo exitBranch = condInfo.initsWhenFalse();
146 exitBranch.addInitializationsFrom(flowInfo); // recover null inits from before condition analysis
147 if (continueLabel != null) {
148 if (increments != null) {
149 LoopingFlowContext loopContext =
150 new LoopingFlowContext(flowContext, this, null, null, scope);
151 for (int i = 0, count = increments.length; i < count; i++) {
152 actionInfo = increments[i].analyseCode(scope, loopContext, actionInfo);
154 loopContext.complainOnDeferredChecks(scope, actionInfo);
156 exitBranch.addPotentialInitializationsFrom(actionInfo.unconditionalInits());
160 FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranches(
161 loopingContext.initsOnBreak,
162 isConditionOptimizedTrue,
164 isConditionOptimizedFalse,
165 !isConditionTrue /*for(;;){}while(true); unreachable(); */);
166 mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
171 * For statement code generation
173 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
174 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
176 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
178 if ((bits & IsReachableMASK) == 0) {
181 int pc = codeStream.position;
183 // generate the initializations
184 if (initializations != null) {
185 for (int i = 0, max = initializations.length; i < max; i++) {
186 initializations[i].generateCode(scope, codeStream);
191 Label actionLabel = new Label(codeStream);
192 Label conditionLabel = new Label(codeStream);
193 breakLabel.initialize(codeStream);
194 if (continueLabel != null) {
195 continueLabel.initialize(codeStream);
197 // jump over the actionBlock
198 if ((condition != null)
199 && (condition.constant == NotAConstant)
200 && !((action == null || action.isEmptyBlock()) && (increments == null))) {
201 int jumpPC = codeStream.position;
202 codeStream.goto_(conditionLabel);
203 codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
205 // generate the loop action
207 if (action != null) {
208 // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
209 if (condIfTrueInitStateIndex != -1) {
210 // insert all locals initialized inside the condition into the action generated prior to the condition
211 codeStream.addDefinitelyAssignedVariables(
213 condIfTrueInitStateIndex);
215 action.generateCode(scope, codeStream);
217 // continuation point
218 if (continueLabel != null) {
219 continueLabel.place();
220 // generate the increments for next iteration
221 if (increments != null) {
222 for (int i = 0, max = increments.length; i < max; i++) {
223 increments[i].generateCode(scope, codeStream);
228 // May loose some local variable initializations : affecting the local variable attributes
229 if (preCondInitStateIndex != -1) {
230 codeStream.removeNotDefinitelyAssignedVariables(currentScope, preCondInitStateIndex);
233 // generate the condition
234 conditionLabel.place();
235 if ((condition != null) && (condition.constant == NotAConstant)) {
236 condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
238 if (continueLabel != null) {
239 codeStream.goto_(actionLabel);
244 // May loose some local variable initializations : affecting the local variable attributes
246 codeStream.exitUserScope(scope);
248 if (mergedInitStateIndex != -1) {
249 codeStream.removeNotDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
250 codeStream.addDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
252 codeStream.recordPositionsFrom(pc, this.sourceStart);
255 public StringBuffer printStatement(int tab, StringBuffer output) {
257 printIndent(tab, output).append("for ("); //$NON-NLS-1$
259 if (initializations != null) {
260 for (int i = 0; i < initializations.length; i++) {
261 //nice only with expressions
262 if (i > 0) output.append(", "); //$NON-NLS-1$
263 initializations[i].print(0, output);
266 output.append("; "); //$NON-NLS-1$
268 if (condition != null) condition.printExpression(0, output);
269 output.append("; "); //$NON-NLS-1$
271 if (increments != null) {
272 for (int i = 0; i < increments.length; i++) {
273 if (i > 0) output.append(", "); //$NON-NLS-1$
274 increments[i].print(0, output);
277 output.append(") "); //$NON-NLS-1$
283 action.printStatement(tab + 1, output); //$NON-NLS-1$
285 return output.append(';');
288 public void resolve(BlockScope upperScope) {
290 // use the scope that will hold the init declarations
291 scope = neededScope ? new BlockScope(upperScope) : upperScope;
292 if (initializations != null)
293 for (int i = 0, length = initializations.length; i < length; i++)
294 initializations[i].resolve(scope);
295 if (condition != null) {
296 TypeBinding type = condition.resolveTypeExpecting(scope, BooleanBinding);
297 condition.computeConversion(scope, type, type);
299 if (increments != null)
300 for (int i = 0, length = increments.length; i < length; i++)
301 increments[i].resolve(scope);
303 action.resolve(scope);
306 public void traverse(
308 BlockScope blockScope) {
310 if (visitor.visit(this, blockScope)) {
311 if (initializations != null) {
312 int initializationsLength = initializations.length;
313 for (int i = 0; i < initializationsLength; i++)
314 initializations[i].traverse(visitor, scope);
317 if (condition != null)
318 condition.traverse(visitor, scope);
320 if (increments != null) {
321 int incrementsLength = increments.length;
322 for (int i = 0; i < incrementsLength; i++)
323 increments[i].traverse(visitor, scope);
327 action.traverse(visitor, scope);
329 visitor.endVisit(this, blockScope);