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.IAbstractSyntaxTreeVisitor;
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;
96 public TypeBinding resolveType(BlockScope scope) {
98 // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference
99 constant = NotAConstant;
100 if (!(this.lhs instanceof Reference)) {
101 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
103 this.resolvedType = lhs.resolveType(scope); // expressionType contains the assignment type (lhs Type)
104 TypeBinding rhsType = expression.resolveType(scope);
105 if (this.resolvedType == null || rhsType == null) {
108 checkAssignmentEffect(scope);
110 // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character
111 // may require to widen the rhs expression at runtime
112 if ((expression.isConstantValueOfTypeAssignableToType(rhsType, this.resolvedType)
113 || (this.resolvedType.isBaseType() && BaseTypeBinding.isWidening(this.resolvedType.id, rhsType.id)))
114 || rhsType.isCompatibleWith(this.resolvedType)) {
115 expression.implicitWidening(this.resolvedType, rhsType);
116 return this.resolvedType;
118 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
122 return this.resolvedType;
125 public String toString(int tab) {
127 //no () when used as a statement
128 return tabString(tab) + toStringExpressionNoParenthesis();
131 public String toStringExpression() {
133 //subclass redefine toStringExpressionNoParenthesis()
134 return "(" + toStringExpressionNoParenthesis() + ")"; //$NON-NLS-2$ //$NON-NLS-1$
137 public String toStringExpressionNoParenthesis() {
139 return lhs.toStringExpression() + " " //$NON-NLS-1$
141 + ((expression.constant != null) && (expression.constant != NotAConstant)
142 ? " /*cst:" + expression.constant.toString() + "*/ " //$NON-NLS-1$ //$NON-NLS-2$
144 + expression.toStringExpression();
146 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
148 if (visitor.visit(this, scope)) {
149 lhs.traverse(visitor, scope);
150 expression.traverse(visitor, scope);
152 visitor.endVisit(this, scope);