1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.compiler.ast;
14 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
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.lookup.BaseTypeBinding;
18 import net.sourceforge.phpdt.internal.compiler.lookup.Binding;
19 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
22 public class Assignment extends Expression {
24 public Expression lhs;
26 public Expression expression;
28 public Assignment(Expression lhs, Expression expression, int sourceEnd) {
29 // lhs is always a reference by construction ,
30 // but is build as an expression ==> the checkcast cannot fail
33 lhs.bits |= IsStrictlyAssignedMASK; // tag lhs as assigned
35 this.expression = expression;
37 this.sourceStart = lhs.sourceStart;
38 this.sourceEnd = sourceEnd;
41 public FlowInfo analyseCode(BlockScope currentScope,
42 FlowContext flowContext, FlowInfo flowInfo) {
43 // record setting a variable: various scenarii are possible, setting an
45 // a field reference, a blank final field reference, a field of an
46 // enclosing instance or
47 // just a local variable.
49 return ((Reference) lhs).analyseAssignment(currentScope, flowContext,
50 flowInfo, this, false).unconditionalInits();
53 void checkAssignmentEffect(BlockScope scope) {
55 Binding left = getDirectBinding(this.lhs);
56 if (left != null && left == getDirectBinding(this.expression)) {
57 scope.problemReporter().assignmentHasNoEffect(this,
58 left.shortReadableName());
59 this.bits |= IsAssignmentWithNoEffectMASK; // record assignment has
64 // public void generateCode(
65 // BlockScope currentScope,
66 // CodeStream codeStream,
67 // boolean valueRequired) {
69 // // various scenarii are possible, setting an array reference,
70 // // a field reference, a blank final field reference, a field of an
71 // enclosing instance or
72 // // just a local variable.
74 // int pc = codeStream.position;
75 // if ((this.bits & IsAssignmentWithNoEffectMASK) != 0) {
76 // if (valueRequired) {
77 // this.expression.generateCode(currentScope, codeStream, true);
80 // ((Reference) lhs).generateAssignment(currentScope, codeStream, this,
82 // // variable may have been optimized out
83 // // the lhs is responsible to perform the implicitConversion generation
84 // for the assignment since optimized for unused local assignment.
86 // codeStream.recordPositionsFrom(pc, this.sourceStart);
89 Binding getDirectBinding(Expression someExpression) {
90 if (someExpression instanceof SingleNameReference) {
91 return ((SingleNameReference) someExpression).binding;
92 } else if (someExpression instanceof FieldReference) {
93 FieldReference fieldRef = (FieldReference) someExpression;
94 if (fieldRef.receiver.isThis()
95 && !(fieldRef.receiver instanceof QualifiedThisReference)) {
96 return fieldRef.binding;
102 public StringBuffer print(int indent, StringBuffer output) {
104 // no () when used as a statement
105 printIndent(indent, output);
106 return printExpressionNoParenthesis(indent, output);
109 public StringBuffer printExpression(int indent, StringBuffer output) {
111 // subclass redefine printExpressionNoParenthesis()
113 return printExpressionNoParenthesis(0, output).append(')');
116 public StringBuffer printExpressionNoParenthesis(int indent,
117 StringBuffer output) {
119 lhs.printExpression(indent, output).append(" = "); //$NON-NLS-1$
120 return expression.printExpression(0, output);
123 public StringBuffer printStatement(int indent, StringBuffer output) {
125 // no () when used as a statement
126 return print(indent, output).append(';');
129 public TypeBinding resolveType(BlockScope scope) {
131 // due to syntax lhs may be only a NameReference, a FieldReference or an
133 constant = NotAConstant;
134 if (!(this.lhs instanceof Reference)) {
135 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
137 this.resolvedType = lhs.resolveType(scope); // expressionType contains
138 // the assignment type (lhs
140 TypeBinding rhsType = expression.resolveType(scope);
141 if (this.resolvedType == null || rhsType == null) {
144 checkAssignmentEffect(scope);
146 // Compile-time conversion of base-types : implicit narrowing integer
147 // into byte/short/character
148 // may require to widen the rhs expression at runtime
149 if ((expression.isConstantValueOfTypeAssignableToType(rhsType,
150 this.resolvedType) || (this.resolvedType.isBaseType() && BaseTypeBinding
151 .isWidening(this.resolvedType.id, rhsType.id)))
152 || rhsType.isCompatibleWith(this.resolvedType)) {
153 expression.implicitWidening(this.resolvedType, rhsType);
154 return this.resolvedType;
156 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
157 expression, rhsType, this.resolvedType);
158 return this.resolvedType;
161 public String toString(int tab) {
163 // no () when used as a statement
164 return tabString(tab) + toStringExpressionNoParenthesis();
167 public String toStringExpression() {
169 // subclass redefine toStringExpressionNoParenthesis()
170 return "(" + toStringExpressionNoParenthesis() + ")"; //$NON-NLS-2$ //$NON-NLS-1$
173 public String toStringExpressionNoParenthesis() {
175 return lhs.toStringExpression()
178 + ((expression.constant != null)
179 && (expression.constant != NotAConstant) ? " /*cst:" + expression.constant.toString() + "*/ " //$NON-NLS-1$ //$NON-NLS-2$
181 + expression.toStringExpression();
184 public void traverse(ASTVisitor visitor, BlockScope scope) {
186 if (visitor.visit(this, scope)) {
187 lhs.traverse(visitor, scope);
188 expression.traverse(visitor, scope);
190 visitor.endVisit(this, scope);