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
9 * IBM Corporation - initial API and implementation
10 * Genady Beriozkin - added support for reporting assignment with no effect
11 *******************************************************************************/
12 package org.eclipse.jdt.internal.compiler.ast;
14 import org.eclipse.jdt.internal.compiler.ASTVisitor;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
19 public class Assignment extends Expression {
21 public Expression lhs;
22 public Expression expression;
24 public Assignment(Expression lhs, Expression expression, int sourceEnd) {
25 //lhs is always a reference by construction ,
26 //but is build as an expression ==> the checkcast cannot fail
29 lhs.bits |= IsStrictlyAssignedMASK; // tag lhs as assigned
31 this.expression = expression;
33 this.sourceStart = lhs.sourceStart;
34 this.sourceEnd = sourceEnd;
37 public FlowInfo analyseCode(
38 BlockScope currentScope,
39 FlowContext flowContext,
41 // record setting a variable: various scenarii are possible, setting an array reference,
42 // a field reference, a blank final field reference, a field of an enclosing instance or
43 // just a local variable.
45 LocalVariableBinding local = this.lhs.localVariableBinding();
46 int nullStatus = this.expression.nullStatus(flowInfo);
47 if (local != null && nullStatus == FlowInfo.NULL) {
48 flowContext.recordUsingNullReference(currentScope, local, this.lhs, FlowInfo.NON_NULL, flowInfo);
50 flowInfo = ((Reference) lhs)
51 .analyseAssignment(currentScope, flowContext, flowInfo, this, false)
52 .unconditionalInits();
56 flowInfo.markAsDefinitelyNull(local);
58 case FlowInfo.NON_NULL :
59 flowInfo.markAsDefinitelyNonNull(local);
66 void checkAssignmentEffect(BlockScope scope) {
68 Binding left = getDirectBinding(this.lhs);
69 if (left != null && left == getDirectBinding(this.expression)) {
70 scope.problemReporter().assignmentHasNoEffect(this, left.shortReadableName());
71 this.bits |= IsAssignmentWithNoEffectMASK; // record assignment has no effect
75 void checkAssignment(BlockScope scope, TypeBinding lhsType, TypeBinding rhsType) {
77 FieldBinding leftField = getLastField(this.lhs);
78 if (leftField != null && rhsType != NullBinding && lhsType.isWildcard() && ((WildcardBinding)lhsType).kind != Wildcard.SUPER) {
79 scope.problemReporter().wildcardAssignment(lhsType, rhsType, this.expression);
80 } else if (leftField != null && leftField.declaringClass != null /*length pseudo field*/&& leftField.declaringClass.isRawType()
81 && (rhsType.isParameterizedType() || rhsType.isGenericType())) {
82 scope.problemReporter().unsafeRawFieldAssignment(leftField, rhsType, this.lhs);
83 } else if (rhsType.needsUncheckedConversion(lhsType)) {
84 scope.problemReporter().unsafeRawConversion(this.expression, rhsType, lhsType);
88 public void generateCode(
89 BlockScope currentScope,
90 CodeStream codeStream,
91 boolean valueRequired) {
93 // various scenarii are possible, setting an array reference,
94 // a field reference, a blank final field reference, a field of an enclosing instance or
95 // just a local variable.
97 int pc = codeStream.position;
98 if ((this.bits & IsAssignmentWithNoEffectMASK) != 0) {
100 this.expression.generateCode(currentScope, codeStream, true);
103 ((Reference) lhs).generateAssignment(currentScope, codeStream, this, valueRequired);
104 // variable may have been optimized out
105 // the lhs is responsible to perform the implicitConversion generation for the assignment since optimized for unused local assignment.
107 codeStream.recordPositionsFrom(pc, this.sourceStart);
110 Binding getDirectBinding(Expression someExpression) {
111 if (someExpression instanceof SingleNameReference) {
112 return ((SingleNameReference)someExpression).binding;
113 } else if (someExpression instanceof FieldReference) {
114 FieldReference fieldRef = (FieldReference)someExpression;
115 if (fieldRef.receiver.isThis() && !(fieldRef.receiver instanceof QualifiedThisReference)) {
116 return fieldRef.binding;
121 FieldBinding getLastField(Expression someExpression) {
122 if (someExpression instanceof SingleNameReference) {
123 if ((someExpression.bits & RestrictiveFlagMASK) == Binding.FIELD) {
124 return (FieldBinding) ((SingleNameReference)someExpression).binding;
126 } else if (someExpression instanceof FieldReference) {
127 return ((FieldReference)someExpression).binding;
128 } else if (someExpression instanceof QualifiedNameReference) {
129 QualifiedNameReference qName = (QualifiedNameReference) someExpression;
130 if (qName.otherBindings == null && ((someExpression.bits & RestrictiveFlagMASK) == Binding.FIELD)) {
131 return (FieldBinding)qName.binding;
133 return qName.otherBindings[qName.otherBindings.length - 1];
139 public int nullStatus(FlowInfo flowInfo) {
140 return this.expression.nullStatus(flowInfo);
143 public StringBuffer print(int indent, StringBuffer output) {
145 //no () when used as a statement
146 printIndent(indent, output);
147 return printExpressionNoParenthesis(indent, output);
149 public StringBuffer printExpression(int indent, StringBuffer output) {
151 //subclass redefine printExpressionNoParenthesis()
153 return printExpressionNoParenthesis(0, output).append(')');
156 public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
158 lhs.printExpression(indent, output).append(" = "); //$NON-NLS-1$
159 return expression.printExpression(0, output);
162 public StringBuffer printStatement(int indent, StringBuffer output) {
164 //no () when used as a statement
165 return print(indent, output).append(';');
168 public TypeBinding resolveType(BlockScope scope) {
170 // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference
171 constant = NotAConstant;
172 if (!(this.lhs instanceof Reference) || this.lhs.isThis()) {
173 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
176 TypeBinding lhsType = this.resolvedType = lhs.resolveType(scope);
177 expression.setExpectedType(lhsType); // needed in case of generic method invocation
178 TypeBinding rhsType = expression.resolveType(scope);
179 if (lhsType == null || rhsType == null) {
182 checkAssignmentEffect(scope);
184 // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character
185 // may require to widen the rhs expression at runtime
186 if (lhsType != rhsType) // must call before computeConversion() and typeMismatchError()
187 scope.compilationUnitScope().recordTypeConversion(lhsType, rhsType);
188 if ((expression.isConstantValueOfTypeAssignableToType(rhsType, lhsType)
189 || (lhsType.isBaseType() && BaseTypeBinding.isWidening(lhsType.id, rhsType.id)))
190 || rhsType.isCompatibleWith(lhsType)) {
191 expression.computeConversion(scope, lhsType, rhsType);
192 checkAssignment(scope, lhsType, rhsType);
193 return this.resolvedType;
194 } else if (scope.isBoxingCompatibleWith(rhsType, lhsType)) {
195 expression.computeConversion(scope, lhsType, rhsType);
196 return this.resolvedType;
198 scope.problemReporter().typeMismatchError(rhsType, lhsType, expression);
202 * @see org.eclipse.jdt.internal.compiler.ast.Expression#resolveTypeExpecting(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.lookup.TypeBinding)
204 public TypeBinding resolveTypeExpecting(
206 TypeBinding expectedType) {
208 TypeBinding type = super.resolveTypeExpecting(scope, expectedType);
209 if (type == null) return null;
210 TypeBinding lhsType = this.resolvedType;
211 TypeBinding rhsType = this.expression.resolvedType;
212 // signal possible accidental boolean assignment (instead of using '==' operator)
213 if (expectedType == BooleanBinding
214 && lhsType == BooleanBinding
215 && (this.lhs.bits & IsStrictlyAssignedMASK) != 0) {
216 scope.problemReporter().possibleAccidentalBooleanAssignment(this);
218 checkAssignment(scope, lhsType, rhsType);
222 public void traverse(ASTVisitor visitor, BlockScope scope) {
224 if (visitor.visit(this, scope)) {
225 lhs.traverse(visitor, scope);
226 expression.traverse(visitor, scope);
228 visitor.endVisit(this, scope);