1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
18 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
19 import net.sourceforge.phpdt.internal.compiler.lookup.BindingIds;
20 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
22 import net.sourceforge.phpdt.internal.compiler.lookup.InvocationSite;
23 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
24 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
25 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
26 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
27 import net.sourceforge.phpdt.internal.compiler.lookup.SourceTypeBinding;
28 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
30 public class FieldReference extends Reference implements InvocationSite {
32 public Expression receiver;
34 public FieldBinding binding, codegenBinding;
35 public long nameSourcePosition; //(start<<32)+end
36 MethodBinding syntheticReadAccessor, syntheticWriteAccessor;
37 public TypeBinding receiverType;
39 public FieldReference(char[] source, long pos) {
42 nameSourcePosition = pos;
43 //by default the position are the one of the field (not true for super access)
44 sourceStart = (int) (pos >>> 32);
45 sourceEnd = (int) (pos & 0x00000000FFFFFFFFL);
46 bits |= BindingIds.FIELD;
50 public FlowInfo analyseAssignment(
51 BlockScope currentScope,
52 FlowContext flowContext,
54 Assignment assignment,
57 // compound assignment extra work
58 if (isCompound) { // check the variable part is initialized if blank final
61 && currentScope.allowBlankFinalFieldAssignment(binding)
62 && (!flowInfo.isDefinitelyAssigned(binding))) {
63 currentScope.problemReporter().uninitializedBlankFinalField(binding, this);
64 // we could improve error msg here telling "cannot use compound assignment on final blank field"
66 manageSyntheticReadAccessIfNecessary(currentScope);
68 if (assignment.expression != null) {
72 .analyseCode(currentScope, flowContext, flowInfo)
73 .unconditionalInits();
77 .analyseCode(currentScope, flowContext, flowInfo, !binding.isStatic())
78 .unconditionalInits();
79 manageSyntheticWriteAccessIfNecessary(currentScope);
81 // check if assigning a final field
82 if (binding.isFinal()) {
83 // in a context where it can be assigned?
85 && !(receiver instanceof QualifiedThisReference)
86 && currentScope.allowBlankFinalFieldAssignment(binding)) {
87 if (flowInfo.isPotentiallyAssigned(binding)) {
88 currentScope.problemReporter().duplicateInitializationOfBlankFinalField(
92 flowInfo.markAsDefinitelyAssigned(binding);
93 flowContext.recordSettingFinal(binding, this);
95 // assigning a final field outside an initializer or constructor
96 currentScope.problemReporter().cannotAssignToFinalField(binding, this);
102 public FlowInfo analyseCode(
103 BlockScope currentScope,
104 FlowContext flowContext,
107 return analyseCode(currentScope, flowContext, flowInfo, true);
110 public FlowInfo analyseCode(
111 BlockScope currentScope,
112 FlowContext flowContext,
114 boolean valueRequired) {
116 receiver.analyseCode(currentScope, flowContext, flowInfo, !binding.isStatic());
118 manageSyntheticReadAccessIfNecessary(currentScope);
123 public FieldBinding fieldBinding() {
128 public void generateAssignment(
129 BlockScope currentScope,
130 CodeStream codeStream,
131 Assignment assignment,
132 boolean valueRequired) {
134 receiver.generateCode(
137 !this.codegenBinding.isStatic());
138 assignment.expression.generateCode(currentScope, codeStream, true);
142 syntheticWriteAccessor,
145 codeStream.generateImplicitConversion(assignment.implicitConversion);
150 * Field reference code generation
152 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
153 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
154 * @param valueRequired boolean
156 public void generateCode(
157 BlockScope currentScope,
158 CodeStream codeStream,
159 boolean valueRequired) {
161 int pc = codeStream.position;
162 if (constant != NotAConstant) {
164 codeStream.generateConstant(constant, implicitConversion);
167 boolean isStatic = this.codegenBinding.isStatic();
168 receiver.generateCode(
171 valueRequired && (!isStatic) && (this.codegenBinding.constant == NotAConstant));
173 if (this.codegenBinding.constant == NotAConstant) {
174 if (this.codegenBinding.declaringClass == null) { // array length
175 codeStream.arraylength();
177 if (syntheticReadAccessor == null) {
179 codeStream.getstatic(this.codegenBinding);
181 codeStream.getfield(this.codegenBinding);
184 codeStream.invokestatic(syntheticReadAccessor);
187 codeStream.generateImplicitConversion(implicitConversion);
189 codeStream.generateConstant(this.codegenBinding.constant, implicitConversion);
193 codeStream.recordPositionsFrom(pc, this.sourceStart);
196 public void generateCompoundAssignment(
197 BlockScope currentScope,
198 CodeStream codeStream,
199 Expression expression,
201 int assignmentImplicitConversion,
202 boolean valueRequired) {
205 receiver.generateCode(
208 !(isStatic = this.codegenBinding.isStatic()));
210 if (syntheticReadAccessor == null) {
211 codeStream.getstatic(this.codegenBinding);
213 codeStream.invokestatic(syntheticReadAccessor);
217 if (syntheticReadAccessor == null) {
218 codeStream.getfield(this.codegenBinding);
220 codeStream.invokestatic(syntheticReadAccessor);
224 if ((operationTypeID = implicitConversion >> 4) == T_String) {
225 codeStream.generateStringAppend(currentScope, null, expression);
227 // promote the array reference to the suitable operation type
228 codeStream.generateImplicitConversion(implicitConversion);
229 // generate the increment value (will by itself be promoted to the operation value)
230 if (expression == IntLiteral.One) { // prefix operation
231 codeStream.generateConstant(expression.constant, implicitConversion);
233 expression.generateCode(currentScope, codeStream, true);
235 // perform the operation
236 codeStream.sendOperator(operator, operationTypeID);
237 // cast the value back to the array reference type
238 codeStream.generateImplicitConversion(assignmentImplicitConversion);
243 syntheticWriteAccessor,
247 public void generatePostIncrement(
248 BlockScope currentScope,
249 CodeStream codeStream,
250 CompoundAssignment postIncrement,
251 boolean valueRequired) {
254 receiver.generateCode(
257 !(isStatic = this.codegenBinding.isStatic()));
259 if (syntheticReadAccessor == null) {
260 codeStream.getstatic(this.codegenBinding);
262 codeStream.invokestatic(syntheticReadAccessor);
266 if (syntheticReadAccessor == null) {
267 codeStream.getfield(this.codegenBinding);
269 codeStream.invokestatic(syntheticReadAccessor);
274 if ((this.codegenBinding.type == LongBinding)
275 || (this.codegenBinding.type == DoubleBinding)) {
280 } else { // Stack: [owner][old field value] ---> [old field value][owner][old field value]
281 if ((this.codegenBinding.type == LongBinding)
282 || (this.codegenBinding.type == DoubleBinding)) {
283 codeStream.dup2_x1();
289 codeStream.generateConstant(
290 postIncrement.expression.constant,
292 codeStream.sendOperator(postIncrement.operator, this.codegenBinding.type.id);
293 codeStream.generateImplicitConversion(
294 postIncrement.assignmentImplicitConversion);
295 fieldStore(codeStream, this.codegenBinding, syntheticWriteAccessor, false);
298 public static final Constant getConstantFor(
299 FieldBinding binding,
300 boolean implicitReceiver,
302 Scope referenceScope,
303 int indexInQualification) {
305 //propagation of the constant.
307 //ref can be a FieldReference, a SingleNameReference or a QualifiedNameReference
308 //indexInQualification may have a value greater than zero only for QualifiednameReference
309 //if ref==null then indexInQualification==0 AND implicitReceiver == false. This case is a
310 //degenerated case where a fake reference field (null)
311 //is associted to a real FieldBinding in order
312 //to allow its constant computation using the regular path (i.e. find the fieldDeclaration
313 //and proceed to its type resolution). As implicitReceiver is false, no error reporting
314 //against ref will be used ==> no nullPointerException risk ....
316 //special treatment for langage-built-in field (their declaring class is null)
317 if (binding.declaringClass == null) {
318 //currently only one field "length" : the constant computation is never done
321 if (!binding.isFinal()) {
322 return binding.constant = NotAConstant;
324 if (binding.constant != null) {
325 if (indexInQualification == 0) {
326 return binding.constant;
328 //see previous comment for the (sould-always-be) valid cast
329 QualifiedNameReference qualifiedReference = (QualifiedNameReference) reference;
330 if (indexInQualification == (qualifiedReference.indexOfFirstFieldBinding - 1)) {
331 return binding.constant;
336 //The field has not been yet type checked.
337 //It also means that the field is not coming from a class that
338 //has already been compiled. It can only be from a class within
339 //compilation units to process. Thus the field is NOT from a BinaryTypeBinbing
341 SourceTypeBinding typeBinding = (SourceTypeBinding) binding.declaringClass;
342 TypeDeclaration typeDecl = typeBinding.scope.referenceContext;
343 FieldDeclaration fieldDecl = typeDecl.declarationOf(binding);
345 //what scope to use (depend on the staticness of the field binding)
346 MethodScope fieldScope =
348 ? typeDecl.staticInitializerScope
349 : typeDecl.initializerScope;
351 if (implicitReceiver) { //Determine if the ref is legal in the current class of the field
352 //i.e. not a forward reference .... (they are allowed when the receiver is explicit ! ... Please don't ask me why !...yet another java mystery...)
353 if (fieldScope.fieldDeclarationIndex == MethodScope.NotInFieldDecl) {
354 // no field is currently being analysed in typeDecl
355 fieldDecl.resolve(fieldScope); //side effect on binding :-) ...
356 return binding.constant;
358 //We are re-entering the same class fields analysing
359 if ((reference != null)
360 && (binding.declaringClass == referenceScope.enclosingSourceType()) // only complain for access inside same type
361 && (binding.id > fieldScope.fieldDeclarationIndex)) {
362 //forward reference. The declaration remains unresolved.
363 referenceScope.problemReporter().forwardReference(reference, indexInQualification, typeBinding);
366 fieldDecl.resolve(fieldScope); //side effect on binding :-) ...
367 return binding.constant;
369 //the field reference is explicity. It has to be a "simple" like field reference to get the
370 //constant propagation. For example in Packahe.Type.field1.field2 , field1 may have its
371 //constant having a propagation where field2 is always not propagating its
372 if (indexInQualification == 0) {
373 fieldDecl.resolve(fieldScope); //side effect on binding :-) ...
374 return binding.constant;
376 // Side-effect on the field binding may not be propagated out for the qualified reference
377 // unless it occurs in first place of the name sequence
378 fieldDecl.resolve(fieldScope); //side effect on binding :-) ...
379 //see previous comment for the cast that should always be valid
380 QualifiedNameReference qualifiedReference = (QualifiedNameReference) reference;
381 if (indexInQualification == (qualifiedReference.indexOfFirstFieldBinding - 1)) {
382 return binding.constant;
388 public boolean isSuperAccess() {
390 return receiver.isSuper();
393 public boolean isTypeAccess() {
395 return receiver != null && receiver.isTypeReference();
399 * No need to emulate access to protected fields since not implicitly accessed
401 public void manageSyntheticReadAccessIfNecessary(BlockScope currentScope) {
403 if (binding.isPrivate()) {
404 if ((currentScope.enclosingSourceType() != binding.declaringClass)
405 && (binding.constant == NotAConstant)) {
406 syntheticReadAccessor =
407 ((SourceTypeBinding) binding.declaringClass).addSyntheticMethod(binding, true);
408 currentScope.problemReporter().needToEmulateFieldReadAccess(binding, this);
412 } else if (receiver instanceof QualifiedSuperReference) { // qualified super
414 // qualified super need emulation always
415 SourceTypeBinding destinationType =
416 (SourceTypeBinding) (((QualifiedSuperReference) receiver)
417 .currentCompatibleType);
418 syntheticReadAccessor = destinationType.addSyntheticMethod(binding, true);
419 currentScope.problemReporter().needToEmulateFieldReadAccess(binding, this);
422 } else if (binding.isProtected()) {
424 SourceTypeBinding enclosingSourceType;
425 if (((bits & DepthMASK) != 0)
426 && binding.declaringClass.getPackage()
427 != (enclosingSourceType = currentScope.enclosingSourceType()).getPackage()) {
429 SourceTypeBinding currentCompatibleType =
430 (SourceTypeBinding) enclosingSourceType.enclosingTypeAt(
431 (bits & DepthMASK) >> DepthSHIFT);
432 syntheticReadAccessor = currentCompatibleType.addSyntheticMethod(binding, true);
433 currentScope.problemReporter().needToEmulateFieldReadAccess(binding, this);
437 // if the binding declaring class is not visible, need special action
438 // for runtime compatibility on 1.2 VMs : change the declaring class of the binding
439 // NOTE: from 1.4 on, field's declaring class is touched if any different from receiver type
440 if (binding.declaringClass != this.receiverType
441 && !this.receiverType.isArrayType()
442 && binding.declaringClass != null // array.length
443 && binding.constant == NotAConstant
444 && ((currentScope.environment().options.complianceLevel >= CompilerOptions.JDK1_4
445 && binding.declaringClass.id != T_Object)
446 //no change for Object fields (in case there was)
447 || !binding.declaringClass.canBeSeenBy(currentScope))) {
448 this.codegenBinding =
449 currentScope.enclosingSourceType().getUpdatedFieldBinding(
451 (ReferenceBinding) this.receiverType);
456 * No need to emulate access to protected fields since not implicitly accessed
458 public void manageSyntheticWriteAccessIfNecessary(BlockScope currentScope) {
460 if (binding.isPrivate()) {
461 if (currentScope.enclosingSourceType() != binding.declaringClass) {
462 syntheticWriteAccessor =
463 ((SourceTypeBinding) binding.declaringClass).addSyntheticMethod(binding, false);
464 currentScope.problemReporter().needToEmulateFieldWriteAccess(binding, this);
468 } else if (receiver instanceof QualifiedSuperReference) { // qualified super
470 // qualified super need emulation always
471 SourceTypeBinding destinationType =
472 (SourceTypeBinding) (((QualifiedSuperReference) receiver)
473 .currentCompatibleType);
474 syntheticWriteAccessor = destinationType.addSyntheticMethod(binding, false);
475 currentScope.problemReporter().needToEmulateFieldWriteAccess(binding, this);
478 } else if (binding.isProtected()) {
480 SourceTypeBinding enclosingSourceType;
481 if (((bits & DepthMASK) != 0)
482 && binding.declaringClass.getPackage()
483 != (enclosingSourceType = currentScope.enclosingSourceType()).getPackage()) {
485 SourceTypeBinding currentCompatibleType =
486 (SourceTypeBinding) enclosingSourceType.enclosingTypeAt(
487 (bits & DepthMASK) >> DepthSHIFT);
488 syntheticWriteAccessor =
489 currentCompatibleType.addSyntheticMethod(binding, false);
490 currentScope.problemReporter().needToEmulateFieldWriteAccess(binding, this);
494 // if the binding declaring class is not visible, need special action
495 // for runtime compatibility on 1.2 VMs : change the declaring class of the binding
496 // NOTE: from 1.4 on, field's declaring class is touched if any different from receiver type
497 if (binding.declaringClass != this.receiverType
498 && !this.receiverType.isArrayType()
499 && binding.declaringClass != null // array.length
500 && binding.constant == NotAConstant
501 && ((currentScope.environment().options.complianceLevel >= CompilerOptions.JDK1_4
502 && binding.declaringClass.id != T_Object)
503 //no change for Object fields (in case there was)
504 || !binding.declaringClass.canBeSeenBy(currentScope))) {
505 this.codegenBinding =
506 currentScope.enclosingSourceType().getUpdatedFieldBinding(
508 (ReferenceBinding) this.receiverType);
512 public TypeBinding resolveType(BlockScope scope) {
514 // Answer the signature type of the field.
515 // constants are propaged when the field is final
516 // and initialized with a (compile time) constant
518 // regular receiver reference
519 this.receiverType = receiver.resolveType(scope);
520 if (this.receiverType == null) {
521 constant = NotAConstant;
524 // the case receiverType.isArrayType and token = 'length' is handled by the scope API
525 this.codegenBinding =
526 this.binding = scope.getField(this.receiverType, token, this);
527 if (!binding.isValidBinding()) {
528 constant = NotAConstant;
529 scope.problemReporter().invalidField(this, this.receiverType);
533 if (isFieldUseDeprecated(binding, scope))
534 scope.problemReporter().deprecatedField(binding, this);
536 // check for this.x in static is done in the resolution of the receiver
538 FieldReference.getConstantFor(
540 receiver == ThisReference.ThisImplicit,
544 if (receiver != ThisReference.ThisImplicit)
545 constant = NotAConstant;
550 public void setActualReceiverType(ReferenceBinding receiverType) {
554 public void setDepth(int depth) {
557 bits &= ~DepthMASK; // flush previous depth if any
558 bits |= (depth & 0xFF) << DepthSHIFT; // encoded on 8 bits
562 public void setFieldIndex(int index) {
566 public String toStringExpression() {
568 return receiver.toString() + "." //$NON-NLS-1$
572 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
574 if (visitor.visit(this, scope)) {
575 receiver.traverse(visitor, scope);
577 visitor.endVisit(this, scope);