bugfix 1427544, http://sourceforge.net/tracker/?func=detail&atid=484801&aid=1427544...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Assignment.java
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
7  * 
8  * Contributors:
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;
13
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;
21
22 public class Assignment extends Expression {
23
24         public Expression lhs;
25         public Expression expression;
26                 
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
30
31                 this.lhs = lhs;
32                 lhs.bits |= IsStrictlyAssignedMASK; // tag lhs as assigned
33                 
34                 this.expression = expression;
35
36                 this.sourceStart = lhs.sourceStart;
37                 this.sourceEnd = sourceEnd;
38         }
39
40         public FlowInfo analyseCode(
41                 BlockScope currentScope,
42                 FlowContext flowContext,
43                 FlowInfo flowInfo) {
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.
47
48                 return ((Reference) lhs)
49                         .analyseAssignment(currentScope, flowContext, flowInfo, this, false)
50                         .unconditionalInits();
51         }
52
53         void checkAssignmentEffect(BlockScope scope) {
54                 
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
59                 }
60         }
61
62 //      public void generateCode(
63 //              BlockScope currentScope,
64 //              CodeStream codeStream,
65 //              boolean valueRequired) {
66 //
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.
70 //
71 //              int pc = codeStream.position;
72 //              if ((this.bits & IsAssignmentWithNoEffectMASK) != 0) {
73 //                      if (valueRequired) {
74 //                              this.expression.generateCode(currentScope, codeStream, true);
75 //                      }
76 //              } else {
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.
80 //              }
81 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
82 //      }
83
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;
91                         }                       
92                 }
93                 return null;
94         }
95         public StringBuffer print(int indent, StringBuffer output) {
96
97                 //no () when used as a statement 
98                 printIndent(indent, output);
99                 return printExpressionNoParenthesis(indent, output);
100         }
101         public StringBuffer printExpression(int indent, StringBuffer output) {
102
103                 //subclass redefine printExpressionNoParenthesis()
104                 output.append('(');
105                 return printExpressionNoParenthesis(0, output).append(')');
106         } 
107
108         public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
109
110                 lhs.printExpression(indent, output).append(" = "); //$NON-NLS-1$
111                 return expression.printExpression(0, output);
112         }
113         
114         public StringBuffer printStatement(int indent, StringBuffer output) {
115
116                 //no () when used as a statement 
117                 return print(indent, output).append(';');
118         }
119         public TypeBinding resolveType(BlockScope scope) {
120
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);
125                 }
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) {
129                         return null;
130                 }
131                 checkAssignmentEffect(scope);
132                                 
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;
140                 }
141                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
142                         expression,
143                         rhsType,
144                         this.resolvedType);
145                 return this.resolvedType;
146         }
147
148         public String toString(int tab) {
149
150                 //no () when used as a statement 
151                 return tabString(tab) + toStringExpressionNoParenthesis();
152         }
153
154         public String toStringExpression() {
155
156                 //subclass redefine toStringExpressionNoParenthesis()
157                 return "(" + toStringExpressionNoParenthesis() + ")"; //$NON-NLS-2$ //$NON-NLS-1$
158         } 
159         
160         public String toStringExpressionNoParenthesis() {
161
162                 return lhs.toStringExpression() + " " //$NON-NLS-1$
163                         + "=" //$NON-NLS-1$
164                         + ((expression.constant != null) && (expression.constant != NotAConstant)
165                                 ? " /*cst:" + expression.constant.toString() + "*/ " //$NON-NLS-1$ //$NON-NLS-2$
166                                 : " ")  //$NON-NLS-1$
167                         + expression.toStringExpression();
168         }
169         public void traverse(ASTVisitor visitor, BlockScope scope) {
170                 
171                 if (visitor.visit(this, scope)) {
172                         lhs.traverse(visitor, scope);
173                         expression.traverse(visitor, scope);
174                 }
175                 visitor.endVisit(this, scope);
176         }
177 }