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
|
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Stephan Herrmann - Contributions for
* Bug 185682 - Increment/decrement operators mark local variables as read
* Bug 458396 - NPE in CodeStream.invoke()
*******************************************************************************/
package org.eclipse.jdt.internal.eval;
import org.eclipse.jdt.internal.compiler.ast.Assignment;
import org.eclipse.jdt.internal.compiler.ast.CompoundAssignment;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
import org.eclipse.jdt.internal.compiler.ast.IntLiteral;
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
import org.eclipse.jdt.internal.compiler.codegen.Opcodes;
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemFieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
public class CodeSnippetFieldReference extends FieldReference implements ProblemReasons, EvaluationConstants {
EvaluationContext evaluationContext;
FieldBinding delegateThis;
/**
* CodeSnippetFieldReference constructor comment.
* @param source char[]
* @param pos long
*/
public CodeSnippetFieldReference(char[] source, long pos, EvaluationContext evaluationContext) {
super(source, pos);
this.evaluationContext = evaluationContext;
}
@Override
public void generateAssignment(BlockScope currentScope, CodeStream codeStream, Assignment assignment, boolean valueRequired) {
FieldBinding codegenBinding = this.binding.original();
if (codegenBinding.canBeSeenBy(this.actualReceiverType, this, currentScope)) {
this.receiver.generateCode(currentScope, codeStream, !codegenBinding.isStatic());
assignment.expression.generateCode(currentScope, codeStream, true);
fieldStore(currentScope, codeStream, codegenBinding, null, this.actualReceiverType, this.receiver.isImplicitThis(), valueRequired);
} else {
codeStream.generateEmulationForField(codegenBinding);
this.receiver.generateCode(currentScope, codeStream, !codegenBinding.isStatic());
if (codegenBinding.isStatic()) { // need a receiver?
codeStream.aconst_null();
}
assignment.expression.generateCode(currentScope, codeStream, true);
if (valueRequired) {
switch (codegenBinding.type.id) {
case TypeIds.T_long :
case TypeIds.T_double :
codeStream.dup2_x2();
break;
default :
codeStream.dup_x2();
break;
}
}
codeStream.generateEmulatedWriteAccessForField(codegenBinding);
}
if (valueRequired){
codeStream.generateImplicitConversion(assignment.implicitConversion);
}
}
/**
* Field reference code generation
*
* @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
* @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
* @param valueRequired boolean
*/
@Override
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
int pc = codeStream.position;
if (this.constant != Constant.NotAConstant) {
if (valueRequired) {
codeStream.generateConstant(this.constant, this.implicitConversion);
}
} else {
FieldBinding codegenBinding = this.binding.original();
boolean isStatic = codegenBinding.isStatic();
this.receiver.generateCode(currentScope, codeStream, !isStatic);
if (valueRequired) {
Constant fieldConstant = codegenBinding.constant();
if (fieldConstant == Constant.NotAConstant) {
if (codegenBinding.declaringClass == null) { // array length
codeStream.arraylength();
} else {
if (codegenBinding.canBeSeenBy(this.actualReceiverType, this, currentScope)) {
TypeBinding constantPoolDeclaringClass = CodeStream.getConstantPoolDeclaringClass(currentScope, codegenBinding, this.actualReceiverType, this.receiver.isImplicitThis());
if (isStatic) {
codeStream.fieldAccess(Opcodes.OPC_getstatic , codegenBinding, constantPoolDeclaringClass);
} else {
codeStream.fieldAccess(Opcodes.OPC_getfield, codegenBinding, constantPoolDeclaringClass);
}
} else {
if (isStatic) {
// we need a null on the stack to use the reflect emulation
codeStream.aconst_null();
}
codeStream.generateEmulatedReadAccessForField(codegenBinding);
}
}
codeStream.generateImplicitConversion(this.implicitConversion);
} else {
if (!isStatic) {
codeStream.invokeObjectGetClass(); // perform null check
codeStream.pop();
}
codeStream.generateConstant(fieldConstant, this.implicitConversion);
}
} else {
if (!isStatic){
codeStream.invokeObjectGetClass(); // perform null check
codeStream.pop();
}
}
}
codeStream.recordPositionsFrom(pc, this.sourceStart);
}
@Override
public void generateCompoundAssignment(BlockScope currentScope, CodeStream codeStream, Expression expression, int operator, int assignmentImplicitConversion, boolean valueRequired) {
boolean isStatic;
FieldBinding codegenBinding = this.binding.original();
if (codegenBinding.canBeSeenBy(this.actualReceiverType, this, currentScope)) {
this.receiver.generateCode(currentScope, codeStream, !(isStatic = codegenBinding.isStatic()));
TypeBinding constantPoolDeclaringClass = CodeStream.getConstantPoolDeclaringClass(currentScope, codegenBinding, this.actualReceiverType, this.receiver.isImplicitThis());
if (isStatic) {
codeStream.fieldAccess(Opcodes.OPC_getstatic, codegenBinding, constantPoolDeclaringClass);
} else {
codeStream.dup();
codeStream.fieldAccess(Opcodes.OPC_getfield, codegenBinding, constantPoolDeclaringClass);
}
int operationTypeID;
switch(operationTypeID = (this.implicitConversion & IMPLICIT_CONVERSION_MASK) >> 4) {
case T_JavaLangString :
case T_JavaLangObject :
case T_undefined :
codeStream.generateStringConcatenationAppend(currentScope, null, expression);
break;
default :
// promote the array reference to the suitable operation type
codeStream.generateImplicitConversion(this.implicitConversion);
// generate the increment value (will by itself be promoted to the operation value)
if (expression == IntLiteral.One){ // prefix operation
codeStream.generateConstant(expression.constant, this.implicitConversion);
} else {
expression.generateCode(currentScope, codeStream, true);
}
// perform the operation
codeStream.sendOperator(operator, operationTypeID);
// cast the value back to the array reference type
codeStream.generateImplicitConversion(assignmentImplicitConversion);
}
fieldStore(currentScope, codeStream, codegenBinding, null, this.actualReceiverType, this.receiver.isImplicitThis(), valueRequired);
} else {
this.receiver.generateCode(currentScope, codeStream, !(isStatic = codegenBinding.isStatic()));
if (isStatic) {
// used to store the value
codeStream.generateEmulationForField(codegenBinding);
codeStream.aconst_null();
// used to retrieve the actual value
codeStream.aconst_null();
codeStream.generateEmulatedReadAccessForField(codegenBinding);
} else {
// used to store the value
codeStream.generateEmulationForField(this.binding);
this.receiver.generateCode(currentScope, codeStream, !isStatic);
// used to retrieve the actual value
codeStream.dup();
codeStream.generateEmulatedReadAccessForField(codegenBinding);
}
int operationTypeID;
if ((operationTypeID = (this.implicitConversion & IMPLICIT_CONVERSION_MASK) >> 4) == T_JavaLangString) {
codeStream.generateStringConcatenationAppend(currentScope, null, expression);
} else {
// promote the array reference to the suitable operation type
codeStream.generateImplicitConversion(this.implicitConversion);
// generate the increment value (will by itself be promoted to the operation value)
if (expression == IntLiteral.One){ // prefix operation
codeStream.generateConstant(expression.constant, this.implicitConversion);
} else {
expression.generateCode(currentScope, codeStream, true);
}
// perform the operation
codeStream.sendOperator(operator, operationTypeID);
// cast the value back to the array reference type
codeStream.generateImplicitConversion(assignmentImplicitConversion);
}
// current stack is:
// field receiver value
if (valueRequired) {
if ((TypeBinding.equalsEquals(codegenBinding.type, TypeBinding.LONG)) || (TypeBinding.equalsEquals(codegenBinding.type, TypeBinding.DOUBLE))) {
codeStream.dup2_x2();
} else {
codeStream.dup_x2();
}
}
// current stack is:
// value field receiver value
codeStream.generateEmulatedWriteAccessForField(codegenBinding);
}
}
@Override
public void generatePostIncrement(BlockScope currentScope, CodeStream codeStream, CompoundAssignment postIncrement, boolean valueRequired) {
boolean isStatic;
FieldBinding codegenBinding = this.binding.original();
if (codegenBinding.canBeSeenBy(this.actualReceiverType, this, currentScope)) {
super.generatePostIncrement(currentScope, codeStream, postIncrement, valueRequired);
} else {
this.receiver.generateCode(currentScope, codeStream, !(isStatic = codegenBinding.isStatic()));
if (isStatic) {
codeStream.aconst_null();
}
// the actual stack is: receiver
codeStream.dup();
// the actual stack is: receiver receiver
codeStream.generateEmulatedReadAccessForField(codegenBinding);
// the actual stack is: receiver value
// receiver value
// value receiver value dup_x1 or dup2_x1 if value required
// value value receiver value dup_x1 or dup2_x1
// value value receiver pop or pop2
// value value receiver field generateEmulationForField
// value value field receiver swap
// value field receiver value field receiver dup2_x1 or dup2_x2
// value field receiver value pop2
// value field receiver newvalue generate constant + op
// value store
int typeID;
switch (typeID = codegenBinding.type.id) {
case TypeIds.T_long :
case TypeIds.T_double :
if (valueRequired) {
codeStream.dup2_x1();
}
codeStream.dup2_x1();
codeStream.pop2();
break;
default :
if (valueRequired) {
codeStream.dup_x1();
}
codeStream.dup_x1();
codeStream.pop();
break;
}
codeStream.generateEmulationForField(codegenBinding);
codeStream.swap();
switch (typeID) {
case TypeIds.T_long :
case TypeIds.T_double :
codeStream.dup2_x2();
break;
default :
codeStream.dup2_x1();
break;
}
codeStream.pop2();
codeStream.generateConstant(postIncrement.expression.constant, this.implicitConversion);
codeStream.sendOperator(postIncrement.operator, codegenBinding.type.id);
codeStream.generateImplicitConversion(postIncrement.preAssignImplicitConversion);
codeStream.generateEmulatedWriteAccessForField(codegenBinding);
}
}
/*
* No need to emulate access to protected fields since not implicitly accessed
*/
@Override
public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo, boolean isReadAccess){
// The private access will be managed through the code generation
if ((flowInfo.tagBits & FlowInfo.UNREACHABLE_OR_DEAD) != 0) return;
}
@Override
public TypeBinding resolveType(BlockScope scope) {
// Answer the signature type of the field.
// constants are propaged when the field is final
// and initialized with a (compile time) constant
// regular receiver reference
this.actualReceiverType = this.receiver.resolveType(scope);
if (this.actualReceiverType == null){
this.constant = Constant.NotAConstant;
return null;
}
// the case receiverType.isArrayType and token = 'length' is handled by the scope API
this.binding = scope.getField(this.actualReceiverType, this.token, this);
FieldBinding firstAttempt = this.binding;
boolean isNotVisible = false;
if (!this.binding.isValidBinding()) {
if (this.binding instanceof ProblemFieldBinding
&& ((ProblemFieldBinding) this.binding).problemId() == NotVisible) {
isNotVisible = true;
if (this.evaluationContext.declaringTypeName != null) {
this.delegateThis = scope.getField(scope.enclosingSourceType(), DELEGATE_THIS, this);
if (this.delegateThis == null){ // if not found then internal error, field should have been found
this.constant = Constant.NotAConstant;
scope.problemReporter().invalidField(this, this.actualReceiverType);
return null;
}
this.actualReceiverType = this.delegateThis.type;
} else {
this.constant = Constant.NotAConstant;
scope.problemReporter().invalidField(this, this.actualReceiverType);
return null;
}
CodeSnippetScope localScope = new CodeSnippetScope(scope);
this.binding = localScope.getFieldForCodeSnippet(this.delegateThis.type, this.token, this);
}
}
if (!this.binding.isValidBinding()) {
this.constant = Constant.NotAConstant;
if (isNotVisible) {
this.binding = firstAttempt;
}
scope.problemReporter().invalidField(this, this.actualReceiverType);
return null;
}
if (isFieldUseDeprecated(this.binding, scope, this.bits)) {
scope.problemReporter().deprecatedField(this.binding, this);
}
// check for this.x in static is done in the resolution of the receiver
this.constant = this.receiver.isImplicitThis() ? this.binding.constant(scope) : Constant.NotAConstant;
if (!this.receiver.isThis()) { // TODO need to check if shouldn't be isImplicitThis check (and then removed)
this.constant = Constant.NotAConstant;
}
return this.resolvedType = this.binding.type;
}
}
|