delete finally
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / 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.phpeclipse.internal.compiler.ast;
13
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;
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
96         public TypeBinding resolveType(BlockScope scope) {
97
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);
102                 }
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) {
106                         return null;
107                 }
108                 checkAssignmentEffect(scope);
109                                 
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;
117                 }
118                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
119                         expression,
120                         rhsType,
121                         this.resolvedType);
122                 return this.resolvedType;
123         }
124
125         public String toString(int tab) {
126
127                 //no () when used as a statement 
128                 return tabString(tab) + toStringExpressionNoParenthesis();
129         }
130
131         public String toStringExpression() {
132
133                 //subclass redefine toStringExpressionNoParenthesis()
134                 return "(" + toStringExpressionNoParenthesis() + ")"; //$NON-NLS-2$ //$NON-NLS-1$
135         } 
136         
137         public String toStringExpressionNoParenthesis() {
138
139                 return lhs.toStringExpression() + " " //$NON-NLS-1$
140                         + "=" //$NON-NLS-1$
141                         + ((expression.constant != null) && (expression.constant != NotAConstant)
142                                 ? " /*cst:" + expression.constant.toString() + "*/ " //$NON-NLS-1$ //$NON-NLS-2$
143                                 : " ")  //$NON-NLS-1$
144                         + expression.toStringExpression();
145         }
146         public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
147                 
148                 if (visitor.visit(this, scope)) {
149                         lhs.traverse(visitor, scope);
150                         expression.traverse(visitor, scope);
151                 }
152                 visitor.endVisit(this, scope);
153         }
154 }