Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 498a81408cb1400a75fa6560b2c5eaa95cd30cb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Stephan Herrmann - Contributions for 
 *     							bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;

import org.eclipse.jdt.internal.compiler.ASTVisitor;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.codegen.*;
import org.eclipse.jdt.internal.compiler.flow.*;
import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.*;

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 positioned either to a new scope, or to the "upper"scope (see resolveType)
	public BlockScope scope;

	private BranchLabel breakLabel, continueLabel;

	// for local variables table attributes
	int preCondInitStateIndex = -1;
	int preIncrementsInitStateIndex = -1;
	int condIfTrueInitStateIndex = -1;
	int mergedInitStateIndex = -1;

	public ForStatement(
		Statement[] initializations,
		Expression condition,
		Statement[] increments,
		Statement action,
		boolean neededScope,
		int s,
		int e) {

		this.sourceStart = s;
		this.sourceEnd = e;
		this.initializations = initializations;
		this.condition = condition;
		this.increments = increments;
		this.action = action;
		// remember useful empty statement
		if (action instanceof EmptyStatement) action.bits |= ASTNode.IsUsefulEmptyStatement;
		if (neededScope) {
			this.bits |= ASTNode.NeededScope;
		}
	}

	public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
		this.breakLabel = new BranchLabel();
		this.continueLabel = new BranchLabel();
		int initialComplaintLevel = (flowInfo.reachMode() & FlowInfo.UNREACHABLE) != 0 ? Statement.COMPLAINED_FAKE_REACHABLE : Statement.NOT_COMPLAINED;

		// process the initializations
		if (this.initializations != null) {
			for (int i = 0, count = this.initializations.length; i < count; i++) {
				flowInfo = this.initializations[i].analyseCode(this.scope, flowContext, flowInfo);
			}
		}
		this.preCondInitStateIndex =
			currentScope.methodScope().recordInitializationStates(flowInfo);

		Constant cst = this.condition == null ? null : this.condition.constant;
		boolean isConditionTrue = cst == null || (cst != Constant.NotAConstant && cst.booleanValue() == true);
		boolean isConditionFalse = cst != null && (cst != Constant.NotAConstant && cst.booleanValue() == false);

		cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
		boolean isConditionOptimizedTrue = cst == null ||  (cst != Constant.NotAConstant && cst.booleanValue() == true);
		boolean isConditionOptimizedFalse = cst != null && (cst != Constant.NotAConstant && cst.booleanValue() == false);
		
		// process the condition
		LoopingFlowContext condLoopContext = null;
		FlowInfo condInfo = flowInfo.nullInfoLessUnconditionalCopy();
		if (this.condition != null) {
			if (!isConditionTrue) {
				condInfo =
					this.condition.analyseCode(
						this.scope,
						(condLoopContext =
							new LoopingFlowContext(flowContext, flowInfo, this, null,
								null, this.scope)),
						condInfo);
				if ((this.condition.implicitConversion & TypeIds.UNBOXING) != 0) {
					this.condition.checkNPE(currentScope, flowContext, flowInfo);
				}
			}
		}

		// process the action
		LoopingFlowContext loopingContext;
		UnconditionalFlowInfo actionInfo;
		if (this.action == null
			|| (this.action.isEmptyBlock() && currentScope.compilerOptions().complianceLevel <= ClassFileConstants.JDK1_3)) {
			if (condLoopContext != null)
				condLoopContext.complainOnDeferredFinalChecks(this.scope, condInfo);
			if (isConditionTrue) {
				if (condLoopContext != null) {
					condLoopContext.complainOnDeferredNullChecks(currentScope,
						condInfo);
				}
				return FlowInfo.DEAD_END;
			} else {
				if (isConditionFalse){
					this.continueLabel = null; // for(;false;p());
				}
				actionInfo = condInfo.initsWhenTrue().unconditionalCopy();
				loopingContext =
					new LoopingFlowContext(flowContext, flowInfo, this,
						this.breakLabel, this.continueLabel, this.scope);
			}
		}
		else {
			loopingContext =
				new LoopingFlowContext(flowContext, flowInfo, this, this.breakLabel,
					this.continueLabel, this.scope);
			FlowInfo initsWhenTrue = condInfo.initsWhenTrue();
			this.condIfTrueInitStateIndex =
				currentScope.methodScope().recordInitializationStates(initsWhenTrue);

				if (isConditionFalse) {
					actionInfo = FlowInfo.DEAD_END;
				} else {
					actionInfo = initsWhenTrue.unconditionalCopy();
					if (isConditionOptimizedFalse){
						actionInfo.setReachMode(FlowInfo.UNREACHABLE_OR_DEAD);
					}
				}
			if (this.action.complainIfUnreachable(actionInfo, this.scope, initialComplaintLevel, true) < Statement.COMPLAINED_UNREACHABLE) {
				actionInfo = this.action.analyseCode(this.scope, loopingContext, actionInfo).unconditionalInits();
			}

			// code generation can be optimized when no need to continue in the loop
			if ((actionInfo.tagBits &
					loopingContext.initsOnContinue.tagBits &
					FlowInfo.UNREACHABLE_OR_DEAD) != 0) {
				this.continueLabel = null;
			}
			else {
				if (condLoopContext != null) {
					condLoopContext.complainOnDeferredFinalChecks(this.scope,
							condInfo);
				}
				actionInfo = actionInfo.mergedWith(loopingContext.initsOnContinue);
				loopingContext.complainOnDeferredFinalChecks(this.scope,
						actionInfo);
			}
		}
		// for increments
		FlowInfo exitBranch = flowInfo.copy();
		// recover null inits from before condition analysis
		LoopingFlowContext incrementContext = null;
		if (this.continueLabel != null) {
			if (this.increments != null) {
				incrementContext =
					new LoopingFlowContext(flowContext, flowInfo, this, null,
						null, this.scope);
				FlowInfo incrementInfo = actionInfo;
				this.preIncrementsInitStateIndex =
					currentScope.methodScope().recordInitializationStates(incrementInfo);
				for (int i = 0, count = this.increments.length; i < count; i++) {
					incrementInfo = this.increments[i].
						analyseCode(this.scope, incrementContext, incrementInfo);
				}
				incrementContext.complainOnDeferredFinalChecks(this.scope,
						actionInfo = incrementInfo.unconditionalInits());
			}
			exitBranch.addPotentialInitializationsFrom(actionInfo).
				addInitializationsFrom(condInfo.initsWhenFalse());
		} else {
			exitBranch.addInitializationsFrom(condInfo.initsWhenFalse());
			if (this.increments != null) {
				if (initialComplaintLevel == Statement.NOT_COMPLAINED) {
					currentScope.problemReporter().fakeReachable(this.increments[0]);
				}
			}
		}
		// nulls checks
		if (condLoopContext != null) {
			condLoopContext.complainOnDeferredNullChecks(currentScope,
				actionInfo);
		}
		loopingContext.complainOnDeferredNullChecks(currentScope,
			actionInfo);
		if (incrementContext != null) {
			incrementContext.complainOnDeferredNullChecks(currentScope,
				actionInfo);
		}
		if (loopingContext.hasEscapingExceptions()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=321926
			FlowInfo loopbackFlowInfo = flowInfo.copy();
			if (this.continueLabel != null) {  // we do get to the bottom 
				loopbackFlowInfo.mergedWith(actionInfo.unconditionalCopy());
			}
			loopingContext.simulateThrowAfterLoopBack(loopbackFlowInfo);
		}
		//end of loop
		FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranches(
				(loopingContext.initsOnBreak.tagBits &
					FlowInfo.UNREACHABLE) != 0 ?
					loopingContext.initsOnBreak :
					flowInfo.addInitializationsFrom(loopingContext.initsOnBreak), // recover upstream null info
				isConditionOptimizedTrue,
				exitBranch,
				isConditionOptimizedFalse,
				!isConditionTrue /*for(;;){}while(true); unreachable(); */);
		// Variables initialized only for the purpose of the for loop can be removed for further flow info
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=359495
		if (this.initializations != null) {
			for (int i = 0; i < this.initializations.length; i++) {
				Statement init = this.initializations[i];
				if (init instanceof LocalDeclaration) {
					LocalVariableBinding binding = ((LocalDeclaration) init).binding;
					mergedInfo.resetAssignmentInfo(binding);
				}
			}
		}
		this.mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
		return mergedInfo;
	}

	/**
	 * For statement code generation
	 *
	 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
	 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
	 */
	public void generateCode(BlockScope currentScope, CodeStream codeStream) {

		if ((this.bits & IsReachable) == 0) {
			return;
		}
		int pc = codeStream.position;

		// generate the initializations
		if (this.initializations != null) {
			for (int i = 0, max = this.initializations.length; i < max; i++) {
				this.initializations[i].generateCode(this.scope, codeStream);
			}
		}
		Constant cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
		boolean isConditionOptimizedFalse = cst != null && (cst != Constant.NotAConstant && cst.booleanValue() == false);
		if (isConditionOptimizedFalse) {
			this.condition.generateCode(this.scope, codeStream, false);
			// May loose some local variable initializations : affecting the local variable attributes
			if ((this.bits & ASTNode.NeededScope) != 0) {
				codeStream.exitUserScope(this.scope);
			}
			if (this.mergedInitStateIndex != -1) {
				codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
				codeStream.addDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
			}
			codeStream.recordPositionsFrom(pc, this.sourceStart);
			return;
		}

		// label management
		BranchLabel actionLabel = new BranchLabel(codeStream);
		actionLabel.tagBits |= BranchLabel.USED;
		BranchLabel conditionLabel = new BranchLabel(codeStream);
		this.breakLabel.initialize(codeStream);
		if (this.continueLabel == null) {
			conditionLabel.place();
			if ((this.condition != null) && (this.condition.constant == Constant.NotAConstant)) {
				this.condition.generateOptimizedBoolean(this.scope, codeStream, null, this.breakLabel, true);
			}
		} else {
			this.continueLabel.initialize(codeStream);
			// jump over the actionBlock
			if ((this.condition != null)
				&& (this.condition.constant == Constant.NotAConstant)
				&& !((this.action == null || this.action.isEmptyBlock()) && (this.increments == null))) {
				conditionLabel.tagBits |= BranchLabel.USED;
				int jumpPC = codeStream.position;
				codeStream.goto_(conditionLabel);
				codeStream.recordPositionsFrom(jumpPC, this.condition.sourceStart);
			}
		}

		// generate the loop action
		if (this.action != null) {
			// Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
			if (this.condIfTrueInitStateIndex != -1) {
				// insert all locals initialized inside the condition into the action generated prior to the condition
				codeStream.addDefinitelyAssignedVariables(
					currentScope,
					this.condIfTrueInitStateIndex);
			}
			actionLabel.place();
			this.action.generateCode(this.scope, codeStream);
		} else {
			actionLabel.place();
		}
		if (this.preIncrementsInitStateIndex != -1) {
			codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.preIncrementsInitStateIndex);
			codeStream.addDefinitelyAssignedVariables(currentScope, this.preIncrementsInitStateIndex);
		}
		// continuation point
		if (this.continueLabel != null) {
			this.continueLabel.place();
			// generate the increments for next iteration
			if (this.increments != null) {
				for (int i = 0, max = this.increments.length; i < max; i++) {
					this.increments[i].generateCode(this.scope, codeStream);
				}
			}
			// May loose some local variable initializations : affecting the local variable attributes
			if (this.preCondInitStateIndex != -1) {
				codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.preCondInitStateIndex);
			}
			// generate the condition
			conditionLabel.place();
			if ((this.condition != null) && (this.condition.constant == Constant.NotAConstant)) {
				this.condition.generateOptimizedBoolean(this.scope, codeStream, actionLabel, null, true);
			} else {
				codeStream.goto_(actionLabel);
			}

		} else {
			// May loose some local variable initializations : affecting the local variable attributes
			if (this.preCondInitStateIndex != -1) {
				codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.preCondInitStateIndex);
			}
		}


		// May loose some local variable initializations : affecting the local variable attributes
		if ((this.bits & ASTNode.NeededScope) != 0) {
			codeStream.exitUserScope(this.scope);
		}
		if (this.mergedInitStateIndex != -1) {
			codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
			codeStream.addDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
		}
		this.breakLabel.place();
		codeStream.recordPositionsFrom(pc, this.sourceStart);
	}

	public StringBuffer printStatement(int tab, StringBuffer output) {

		printIndent(tab, output).append("for ("); //$NON-NLS-1$
		//inits
		if (this.initializations != null) {
			for (int i = 0; i < this.initializations.length; i++) {
				//nice only with expressions
				if (i > 0) output.append(", "); //$NON-NLS-1$
				this.initializations[i].print(0, output);
			}
		}
		output.append("; "); //$NON-NLS-1$
		//cond
		if (this.condition != null) this.condition.printExpression(0, output);
		output.append("; "); //$NON-NLS-1$
		//updates
		if (this.increments != null) {
			for (int i = 0; i < this.increments.length; i++) {
				if (i > 0) output.append(", "); //$NON-NLS-1$
				this.increments[i].print(0, output);
			}
		}
		output.append(") "); //$NON-NLS-1$
		//block
		if (this.action == null)
			output.append(';');
		else {
			output.append('\n');
			this.action.printStatement(tab + 1, output);
		}
		return output;
	}

	public void resolve(BlockScope upperScope) {

		// use the scope that will hold the init declarations
		this.scope = (this.bits & ASTNode.NeededScope) != 0 ? new BlockScope(upperScope) : upperScope;
		if (this.initializations != null)
			for (int i = 0, length = this.initializations.length; i < length; i++)
				this.initializations[i].resolve(this.scope);
		if (this.condition != null) {
			TypeBinding type = this.condition.resolveTypeExpecting(this.scope, TypeBinding.BOOLEAN);
			this.condition.computeConversion(this.scope, type, type);
		}
		if (this.increments != null)
			for (int i = 0, length = this.increments.length; i < length; i++)
				this.increments[i].resolve(this.scope);
		if (this.action != null)
			this.action.resolve(this.scope);
	}

	public void traverse(
		ASTVisitor visitor,
		BlockScope blockScope) {

		if (visitor.visit(this, blockScope)) {
			if (this.initializations != null) {
				int initializationsLength = this.initializations.length;
				for (int i = 0; i < initializationsLength; i++)
					this.initializations[i].traverse(visitor, this.scope);
			}

			if (this.condition != null)
				this.condition.traverse(visitor, this.scope);

			if (this.increments != null) {
				int incrementsLength = this.increments.length;
				for (int i = 0; i < incrementsLength; i++)
					this.increments[i].traverse(visitor, this.scope);
			}

			if (this.action != null)
				this.action.traverse(visitor, this.scope);
		}
		visitor.endVisit(this, blockScope);
	}
}

Back to the top