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.phpeclipse.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;
25 public Expression expression;
27 public Assignment(Expression lhs, Expression expression, int sourceEnd) {
28 //lhs is always a reference by construction ,
29 //but is build as an expression ==> the checkcast cannot fail
32 lhs.bits |= IsStrictlyAssignedMASK; // tag lhs as assigned
34 this.expression = expression;
36 this.sourceStart = lhs.sourceStart;
37 this.sourceEnd = sourceEnd;
40 public FlowInfo analyseCode(
41 BlockScope currentScope,
42 FlowContext flowContext,
44 // record setting a variable: various scenarii are possible, setting an array reference,
45 // a field reference, a blank final field reference, a field of an enclosing instance or
46 // just a local variable.
48 return ((Reference) lhs)
49 .analyseAssignment(currentScope, flowContext, flowInfo, this, false)
50 .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, left.shortReadableName());
58 this.bits |= IsAssignmentWithNoEffectMASK; // record assignment has no effect
62 // public void generateCode(
63 // BlockScope currentScope,
64 // CodeStream codeStream,
65 // boolean valueRequired) {
67 // // various scenarii are possible, setting an array reference,
68 // // a field reference, a blank final field reference, a field of an enclosing instance or
69 // // just a local variable.
71 // int pc = codeStream.position;
72 // if ((this.bits & IsAssignmentWithNoEffectMASK) != 0) {
73 // if (valueRequired) {
74 // this.expression.generateCode(currentScope, codeStream, true);
77 // ((Reference) lhs).generateAssignment(currentScope, codeStream, this, valueRequired);
78 // // variable may have been optimized out
79 // // the lhs is responsible to perform the implicitConversion generation for the assignment since optimized for unused local assignment.
81 // codeStream.recordPositionsFrom(pc, this.sourceStart);
84 Binding getDirectBinding(Expression someExpression) {
85 if (someExpression instanceof SingleNameReference) {
86 return ((SingleNameReference)someExpression).binding;
87 } else if (someExpression instanceof FieldReference) {
88 FieldReference fieldRef = (FieldReference)someExpression;
89 if (fieldRef.receiver.isThis() && !(fieldRef.receiver instanceof QualifiedThisReference)) {
90 return fieldRef.binding;
95 public StringBuffer print(int indent, StringBuffer output) {
97 //no () when used as a statement
98 printIndent(indent, output);
99 return printExpressionNoParenthesis(indent, output);
101 public StringBuffer printExpression(int indent, StringBuffer output) {
103 //subclass redefine printExpressionNoParenthesis()
105 return printExpressionNoParenthesis(0, output).append(')');
108 public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
110 lhs.printExpression(indent, output).append(" = "); //$NON-NLS-1$
111 return expression.printExpression(0, output);
114 public StringBuffer printStatement(int indent, StringBuffer output) {
116 //no () when used as a statement
117 return print(indent, output).append(';');
119 public TypeBinding resolveType(BlockScope scope) {
121 // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference
122 constant = NotAConstant;
123 if (!(this.lhs instanceof Reference)) {
124 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
126 this.resolvedType = lhs.resolveType(scope); // expressionType contains the assignment type (lhs Type)
127 TypeBinding rhsType = expression.resolveType(scope);
128 if (this.resolvedType == null || rhsType == null) {
131 checkAssignmentEffect(scope);
133 // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character
134 // may require to widen the rhs expression at runtime
135 if ((expression.isConstantValueOfTypeAssignableToType(rhsType, this.resolvedType)
136 || (this.resolvedType.isBaseType() && BaseTypeBinding.isWidening(this.resolvedType.id, rhsType.id)))
137 || rhsType.isCompatibleWith(this.resolvedType)) {
138 expression.implicitWidening(this.resolvedType, rhsType);
139 return this.resolvedType;
141 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
145 return this.resolvedType;
148 public String toString(int tab) {
150 //no () when used as a statement
151 return tabString(tab) + toStringExpressionNoParenthesis();
154 public String toStringExpression() {
156 //subclass redefine toStringExpressionNoParenthesis()
157 return "(" + toStringExpressionNoParenthesis() + ")"; //$NON-NLS-2$ //$NON-NLS-1$
160 public String toStringExpressionNoParenthesis() {
162 return lhs.toStringExpression() + " " //$NON-NLS-1$
164 + ((expression.constant != null) && (expression.constant != NotAConstant)
165 ? " /*cst:" + expression.constant.toString() + "*/ " //$NON-NLS-1$ //$NON-NLS-2$
167 + expression.toStringExpression();
169 public void traverse(ASTVisitor visitor, BlockScope scope) {
171 if (visitor.visit(this, scope)) {
172 lhs.traverse(visitor, scope);
173 expression.traverse(visitor, scope);
175 visitor.endVisit(this, scope);