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
|
/*******************************************************************************
* 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.impl.*;
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.lookup.*;
public class WhileStatement extends Statement {
public Expression condition;
public Statement action;
private BranchLabel breakLabel, continueLabel;
int preCondInitStateIndex = -1;
int condIfTrueInitStateIndex = -1;
int mergedInitStateIndex = -1;
public WhileStatement(Expression condition, Statement action, int s, int e) {
this.condition = condition;
this.action = action;
// remember useful empty statement
if (action instanceof EmptyStatement) action.bits |= IsUsefulEmptyStatement;
this.sourceStart = s;
this.sourceEnd = e;
}
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;
Constant cst = this.condition.constant;
boolean isConditionTrue = cst != Constant.NotAConstant && cst.booleanValue() == true;
boolean isConditionFalse = cst != Constant.NotAConstant && cst.booleanValue() == false;
cst = this.condition.optimizedBooleanConstant();
boolean isConditionOptimizedTrue = cst != Constant.NotAConstant && cst.booleanValue() == true;
boolean isConditionOptimizedFalse = cst != Constant.NotAConstant && cst.booleanValue() == false;
this.preCondInitStateIndex = currentScope.methodScope().recordInitializationStates(flowInfo);
LoopingFlowContext condLoopContext;
FlowInfo condInfo = flowInfo.nullInfoLessUnconditionalCopy();
// we need to collect the contribution to nulls of the coming paths through the
// loop, be they falling through normally or branched to break, continue labels
// or catch blocks
condInfo = this.condition.analyseCode(
currentScope,
(condLoopContext =
new LoopingFlowContext(flowContext, flowInfo, this, null,
null, currentScope)),
condInfo);
if ((this.condition.implicitConversion & TypeIds.UNBOXING) != 0) {
this.condition.checkNPE(currentScope, flowContext, flowInfo);
}
LoopingFlowContext loopingContext;
FlowInfo actionInfo;
FlowInfo exitBranch;
if (this.action == null
|| (this.action.isEmptyBlock() && currentScope.compilerOptions().complianceLevel <= ClassFileConstants.JDK1_3)) {
condLoopContext.complainOnDeferredFinalChecks(currentScope,
condInfo);
condLoopContext.complainOnDeferredNullChecks(currentScope,
condInfo.unconditionalInits());
if (isConditionTrue) {
return FlowInfo.DEAD_END;
} else {
FlowInfo mergedInfo = flowInfo.copy().addInitializationsFrom(condInfo.initsWhenFalse());
if (isConditionOptimizedTrue){
mergedInfo.setReachMode(FlowInfo.UNREACHABLE_OR_DEAD);
}
this.mergedInitStateIndex =
currentScope.methodScope().recordInitializationStates(mergedInfo);
return mergedInfo;
}
} else {
// in case the condition was inlined to false, record the fact that there is no way to reach any
// statement inside the looping action
loopingContext =
new LoopingFlowContext(
flowContext,
flowInfo,
this,
this.breakLabel,
this.continueLabel,
currentScope);
if (isConditionFalse) {
actionInfo = FlowInfo.DEAD_END;
} else {
actionInfo = condInfo.initsWhenTrue().copy();
if (isConditionOptimizedFalse){
actionInfo.setReachMode(FlowInfo.UNREACHABLE_OR_DEAD);
}
}
// for computing local var attributes
this.condIfTrueInitStateIndex =
currentScope.methodScope().recordInitializationStates(
condInfo.initsWhenTrue());
if (this.action.complainIfUnreachable(actionInfo, currentScope, initialComplaintLevel, true) < Statement.COMPLAINED_UNREACHABLE) {
actionInfo = this.action.analyseCode(currentScope, loopingContext, actionInfo);
}
// code generation can be optimized when no need to continue in the loop
exitBranch = flowInfo.copy();
// need to start over from flowInfo so as to get null inits
int combinedTagBits = actionInfo.tagBits & loopingContext.initsOnContinue.tagBits;
if ((combinedTagBits & FlowInfo.UNREACHABLE) != 0) {
if ((combinedTagBits & FlowInfo.UNREACHABLE_OR_DEAD) != 0)
this.continueLabel = null;
exitBranch.addInitializationsFrom(condInfo.initsWhenFalse());
} else {
condLoopContext.complainOnDeferredFinalChecks(currentScope,
condInfo);
actionInfo = actionInfo.mergedWith(loopingContext.initsOnContinue.unconditionalInits());
condLoopContext.complainOnDeferredNullChecks(currentScope,
actionInfo);
loopingContext.complainOnDeferredFinalChecks(currentScope,
actionInfo);
loopingContext.complainOnDeferredNullChecks(currentScope,
actionInfo);
exitBranch.
addPotentialInitializationsFrom(
actionInfo.unconditionalInits()).
addInitializationsFrom(condInfo.initsWhenFalse());
}
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 /*while(true); unreachable(); */);
this.mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
return mergedInfo;
}
/**
* While 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;
Constant cst = this.condition.optimizedBooleanConstant();
boolean isConditionOptimizedFalse = cst != Constant.NotAConstant && cst.booleanValue() == false;
if (isConditionOptimizedFalse) {
this.condition.generateCode(currentScope, codeStream, false);
// May loose some local variable initializations : affecting the local variable attributes
if (this.mergedInitStateIndex != -1) {
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
codeStream.addDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
}
codeStream.recordPositionsFrom(pc, this.sourceStart);
return;
}
this.breakLabel.initialize(codeStream);
// generate condition
if (this.continueLabel == null) {
// no need to reverse condition
if (this.condition.constant == Constant.NotAConstant) {
this.condition.generateOptimizedBoolean(
currentScope,
codeStream,
null,
this.breakLabel,
true);
}
} else {
this.continueLabel.initialize(codeStream);
if (!(((this.condition.constant != Constant.NotAConstant)
&& (this.condition.constant.booleanValue() == true))
|| (this.action == null)
|| this.action.isEmptyBlock())) {
int jumpPC = codeStream.position;
codeStream.goto_(this.continueLabel);
codeStream.recordPositionsFrom(jumpPC, this.condition.sourceStart);
}
}
// generate the action
BranchLabel actionLabel = new BranchLabel(codeStream);
if (this.action != null) {
actionLabel.tagBits |= BranchLabel.USED;
// 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(currentScope, codeStream);
// May loose some local variable initializations : affecting the local variable attributes
if (this.preCondInitStateIndex != -1) {
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.preCondInitStateIndex);
}
} else {
actionLabel.place();
}
// output condition and branch back to the beginning of the repeated action
if (this.continueLabel != null) {
this.continueLabel.place();
this.condition.generateOptimizedBoolean(
currentScope,
codeStream,
actionLabel,
null,
true);
}
// May loose some local variable initializations : affecting the local variable attributes
if (this.mergedInitStateIndex != -1) {
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
codeStream.addDefinitelyAssignedVariables(currentScope, this.mergedInitStateIndex);
}
this.breakLabel.place();
codeStream.recordPositionsFrom(pc, this.sourceStart);
}
public void resolve(BlockScope scope) {
TypeBinding type = this.condition.resolveTypeExpecting(scope, TypeBinding.BOOLEAN);
this.condition.computeConversion(scope, type, type);
if (this.action != null)
this.action.resolve(scope);
}
public StringBuffer printStatement(int tab, StringBuffer output) {
printIndent(tab, output).append("while ("); //$NON-NLS-1$
this.condition.printExpression(0, output).append(')');
if (this.action == null)
output.append(';');
else
this.action.printStatement(tab + 1, output);
return output;
}
public void traverse(
ASTVisitor visitor,
BlockScope blockScope) {
if (visitor.visit(this, blockScope)) {
this.condition.traverse(visitor, blockScope);
if (this.action != null)
this.action.traverse(visitor, blockScope);
}
visitor.endVisit(this, blockScope);
}
}
|