added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / ForStatement.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
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.*;
19
20 public class ForStatement extends Statement {
21         
22         public Statement[] initializations;
23         public Expression condition;
24         public Statement[] increments;
25         public Statement action;
26
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;
31
32         private Label breakLabel, continueLabel;
33
34         // for local variables table attributes
35         int preCondInitStateIndex = -1;
36         int condIfTrueInitStateIndex = -1;
37         int mergedInitStateIndex = -1;
38
39         public ForStatement(
40                 Statement[] initializations,
41                 Expression condition,
42                 Statement[] increments,
43                 Statement action,
44                 boolean neededScope,
45                 int s,
46                 int e) {
47
48                 this.sourceStart = s;
49                 this.sourceEnd = e;
50                 this.initializations = initializations;
51                 this.condition = condition;
52                 this.increments = increments;
53                 this.action = action;
54                 // remember useful empty statement
55                 if (action instanceof EmptyStatement) action.bits |= IsUsefulEmptyStatementMASK;
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                         for (int i = 0, count = initializations.length; i < count; i++) {
70                                 flowInfo = initializations[i].analyseCode(scope, flowContext, flowInfo);
71                         }
72                 }
73                 preCondInitStateIndex =
74                         currentScope.methodScope().recordInitializationStates(flowInfo);
75
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);
79
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);
83                 
84                 // process the condition
85                 LoopingFlowContext condLoopContext = null;
86                 FlowInfo condInfo = flowInfo.copy().unconditionalInits().discardNullRelatedInitializations();
87                 if (condition != null) {
88                         if (!isConditionTrue) {
89                                 condInfo =
90                                         condition.analyseCode(
91                                                 scope,
92                                                 (condLoopContext =
93                                                         new LoopingFlowContext(flowContext, this, null, null, scope)),
94                                                 condInfo);
95                         }
96                 }
97
98                 // process the action
99                 LoopingFlowContext loopingContext;
100                 FlowInfo actionInfo;
101                 if (action == null 
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;
107                         } else {
108                                 if (isConditionFalse){
109                                         continueLabel = null; // for(;false;p());
110                                 }
111                                 actionInfo = condInfo.initsWhenTrue().copy().unconditionalInits().discardNullRelatedInitializations();
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 = condInfo.initsWhenTrue();
119                         condIfTrueInitStateIndex =
120                                 currentScope.methodScope().recordInitializationStates(initsWhenTrue);
121
122                                 if (isConditionFalse) {
123                                         actionInfo = FlowInfo.DEAD_END;
124                                 } else {
125                                         actionInfo = initsWhenTrue.copy().unconditionalInits().discardNullRelatedInitializations();
126                                         if (isConditionOptimizedFalse){
127                                                 actionInfo.setReachMode(FlowInfo.UNREACHABLE);
128                                         }
129                                 }
130                         if (!this.action.complainIfUnreachable(actionInfo, 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.complainOnDeferredChecks(scope, condInfo);
140                                 actionInfo = actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
141                                 loopingContext.complainOnDeferredChecks(scope, actionInfo);
142                         }
143                 }
144                 // for increments
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);
153                                 }
154                                 loopContext.complainOnDeferredChecks(scope, actionInfo);
155                         }
156                         exitBranch.addPotentialInitializationsFrom(actionInfo.unconditionalInits());
157                 }
158
159                 //end of loop
160                 FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranches(
161                                 loopingContext.initsOnBreak, 
162                                 isConditionOptimizedTrue, 
163                                 exitBranch, 
164                                 isConditionOptimizedFalse, 
165                                 !isConditionTrue /*for(;;){}while(true); unreachable(); */);
166                 mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
167                 return mergedInfo;
168         }
169
170         /**
171          * For statement code generation
172          *
173          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
174          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
175          */
176         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
177
178                 if ((bits & IsReachableMASK) == 0) {
179                         return;
180                 }
181                 int pc = codeStream.position;
182
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);
187                         }
188                 }
189
190                 // label management
191                 Label actionLabel = new Label(codeStream);
192                 Label conditionLabel = new Label(codeStream);
193                 breakLabel.initialize(codeStream);
194                 if (continueLabel != null) {
195                         continueLabel.initialize(codeStream);
196                 }
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);
204                 }
205                 // generate the loop action
206                 actionLabel.place();
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(
212                                         currentScope,
213                                         condIfTrueInitStateIndex);
214                         }
215                         action.generateCode(scope, codeStream);
216                 }
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);
224                                 }
225                         }
226                 }
227
228                 // May loose some local variable initializations : affecting the local variable attributes
229                 if (preCondInitStateIndex != -1) {
230                         codeStream.removeNotDefinitelyAssignedVariables(currentScope, preCondInitStateIndex);
231                 }
232
233                 // generate the condition
234                 conditionLabel.place();
235                 if ((condition != null) && (condition.constant == NotAConstant)) {
236                         condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
237                 } else {
238                         if (continueLabel != null) {
239                                 codeStream.goto_(actionLabel);
240                         }
241                 }
242                 breakLabel.place();
243
244                 // May loose some local variable initializations : affecting the local variable attributes
245                 if (neededScope) {
246                         codeStream.exitUserScope(scope);
247                 }
248                 if (mergedInitStateIndex != -1) {
249                         codeStream.removeNotDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
250                         codeStream.addDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
251                 }
252                 codeStream.recordPositionsFrom(pc, this.sourceStart);
253         }
254
255         public StringBuffer printStatement(int tab, StringBuffer output) {
256
257                 printIndent(tab, output).append("for ("); //$NON-NLS-1$
258                 //inits
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);
264                         }
265                 }
266                 output.append("; "); //$NON-NLS-1$
267                 //cond
268                 if (condition != null) condition.printExpression(0, output);
269                 output.append("; "); //$NON-NLS-1$
270                 //updates
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);
275                         }
276                 }
277                 output.append(") "); //$NON-NLS-1$
278                 //block
279                 if (action == null)
280                         output.append(';');
281                 else {
282                         output.append('\n');
283                         action.printStatement(tab + 1, output); //$NON-NLS-1$
284                 }
285                 return output.append(';');
286         }
287
288         public void resolve(BlockScope upperScope) {
289
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);
298                 }
299                 if (increments != null)
300                         for (int i = 0, length = increments.length; i < length; i++)
301                                 increments[i].resolve(scope);
302                 if (action != null)
303                         action.resolve(scope);
304         }
305         
306         public void traverse(
307                 ASTVisitor visitor,
308                 BlockScope blockScope) {
309
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);
315                         }
316
317                         if (condition != null)
318                                 condition.traverse(visitor, scope);
319
320                         if (increments != null) {
321                                 int incrementsLength = increments.length;
322                                 for (int i = 0; i < incrementsLength; i++)
323                                         increments[i].traverse(visitor, scope);
324                         }
325
326                         if (action != null)
327                                 action.traverse(visitor, scope);
328                 }
329                 visitor.endVisit(this, blockScope);
330         }
331 }