added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / Assignment.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
13
14 import org.eclipse.jdt.internal.compiler.ASTVisitor;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
18
19 public class Assignment extends Expression {
20
21         public Expression lhs;
22         public Expression expression;
23                 
24         public Assignment(Expression lhs, Expression expression, int sourceEnd) {
25                 //lhs is always a reference by construction ,
26                 //but is build as an expression ==> the checkcast cannot fail
27
28                 this.lhs = lhs;
29                 lhs.bits |= IsStrictlyAssignedMASK; // tag lhs as assigned
30                 
31                 this.expression = expression;
32
33                 this.sourceStart = lhs.sourceStart;
34                 this.sourceEnd = sourceEnd;
35         }
36
37         public FlowInfo analyseCode(
38                 BlockScope currentScope,
39                 FlowContext flowContext,
40                 FlowInfo flowInfo) {
41                 // record setting a variable: various scenarii are possible, setting an array reference, 
42                 // a field reference, a blank final field reference, a field of an enclosing instance or 
43                 // just a local variable.
44
45                 LocalVariableBinding local = this.lhs.localVariableBinding();
46                 int nullStatus = this.expression.nullStatus(flowInfo);
47                 if (local != null && nullStatus == FlowInfo.NULL) {
48                                 flowContext.recordUsingNullReference(currentScope, local, this.lhs, FlowInfo.NON_NULL, flowInfo);
49                 }
50                 flowInfo = ((Reference) lhs)
51                         .analyseAssignment(currentScope, flowContext, flowInfo, this, false)
52                         .unconditionalInits();
53                 if (local != null) {
54                         switch(nullStatus) {
55                                 case FlowInfo.NULL :
56                                         flowInfo.markAsDefinitelyNull(local);
57                                         break;
58                                 case FlowInfo.NON_NULL :
59                                         flowInfo.markAsDefinitelyNonNull(local);
60                                         break;
61                         }
62                 }               
63                 return flowInfo;
64         }
65
66         void checkAssignmentEffect(BlockScope scope) {
67                 
68                 Binding left = getDirectBinding(this.lhs);
69                 if (left != null && left == getDirectBinding(this.expression)) {
70                         scope.problemReporter().assignmentHasNoEffect(this, left.shortReadableName());
71                         this.bits |= IsAssignmentWithNoEffectMASK; // record assignment has no effect
72                 }
73         }
74
75         void checkAssignment(BlockScope scope, TypeBinding lhsType, TypeBinding rhsType) {
76                 
77                 FieldBinding leftField = getLastField(this.lhs);
78                 if (leftField != null &&  rhsType != NullBinding && lhsType.isWildcard() && ((WildcardBinding)lhsType).kind != Wildcard.SUPER) {
79                     scope.problemReporter().wildcardAssignment(lhsType, rhsType, this.expression);
80                 } else if (leftField != null && leftField.declaringClass != null /*length pseudo field*/&& leftField.declaringClass.isRawType() 
81                         && (rhsType.isParameterizedType() || rhsType.isGenericType())) {
82                     scope.problemReporter().unsafeRawFieldAssignment(leftField, rhsType, this.lhs);
83                 } else if (rhsType.needsUncheckedConversion(lhsType)) {
84                     scope.problemReporter().unsafeRawConversion(this.expression, rhsType, lhsType);
85                 }               
86         }
87         
88         public void generateCode(
89                 BlockScope currentScope,
90                 CodeStream codeStream,
91                 boolean valueRequired) {
92
93                 // various scenarii are possible, setting an array reference, 
94                 // a field reference, a blank final field reference, a field of an enclosing instance or 
95                 // just a local variable.
96
97                 int pc = codeStream.position;
98                 if ((this.bits & IsAssignmentWithNoEffectMASK) != 0) {
99                         if (valueRequired) {
100                                 this.expression.generateCode(currentScope, codeStream, true);
101                         }
102                 } else {
103                          ((Reference) lhs).generateAssignment(currentScope, codeStream, this, valueRequired);
104                         // variable may have been optimized out
105                         // the lhs is responsible to perform the implicitConversion generation for the assignment since optimized for unused local assignment.
106                 }
107                 codeStream.recordPositionsFrom(pc, this.sourceStart);
108         }
109
110         Binding getDirectBinding(Expression someExpression) {
111                 if (someExpression instanceof SingleNameReference) {
112                         return ((SingleNameReference)someExpression).binding;
113                 } else if (someExpression instanceof FieldReference) {
114                         FieldReference fieldRef = (FieldReference)someExpression;
115                         if (fieldRef.receiver.isThis() && !(fieldRef.receiver instanceof QualifiedThisReference)) {
116                                 return fieldRef.binding;
117                         }                       
118                 }
119                 return null;
120         }
121         FieldBinding getLastField(Expression someExpression) {
122             if (someExpression instanceof SingleNameReference) {
123                 if ((someExpression.bits & RestrictiveFlagMASK) == Binding.FIELD) {
124                     return (FieldBinding) ((SingleNameReference)someExpression).binding;
125                 }
126             } else if (someExpression instanceof FieldReference) {
127                 return ((FieldReference)someExpression).binding;
128             } else if (someExpression instanceof QualifiedNameReference) {
129                 QualifiedNameReference qName = (QualifiedNameReference) someExpression;
130                 if (qName.otherBindings == null && ((someExpression.bits & RestrictiveFlagMASK) == Binding.FIELD)) {
131                     return (FieldBinding)qName.binding;
132                 } else {
133                     return qName.otherBindings[qName.otherBindings.length - 1];
134                 }
135             }
136             return null;
137         }       
138
139         public int nullStatus(FlowInfo flowInfo) {
140                 return this.expression.nullStatus(flowInfo);
141         }
142         
143         public StringBuffer print(int indent, StringBuffer output) {
144
145                 //no () when used as a statement 
146                 printIndent(indent, output);
147                 return printExpressionNoParenthesis(indent, output);
148         }
149         public StringBuffer printExpression(int indent, StringBuffer output) {
150
151                 //subclass redefine printExpressionNoParenthesis()
152                 output.append('(');
153                 return printExpressionNoParenthesis(0, output).append(')');
154         } 
155
156         public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
157
158                 lhs.printExpression(indent, output).append(" = "); //$NON-NLS-1$
159                 return expression.printExpression(0, output);
160         }
161         
162         public StringBuffer printStatement(int indent, StringBuffer output) {
163
164                 //no () when used as a statement 
165                 return print(indent, output).append(';');
166         }
167
168         public TypeBinding resolveType(BlockScope scope) {
169
170                 // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference
171                 constant = NotAConstant;
172                 if (!(this.lhs instanceof Reference) || this.lhs.isThis()) {
173                         scope.problemReporter().expressionShouldBeAVariable(this.lhs);
174                         return null;
175                 }
176                 TypeBinding lhsType = this.resolvedType = lhs.resolveType(scope);
177                 expression.setExpectedType(lhsType); // needed in case of generic method invocation
178                 TypeBinding rhsType = expression.resolveType(scope);
179                 if (lhsType == null || rhsType == null) {
180                         return null;
181                 }
182                 checkAssignmentEffect(scope);
183
184                 // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character
185                 // may require to widen the rhs expression at runtime
186                 if (lhsType != rhsType) // must call before computeConversion() and typeMismatchError()
187                         scope.compilationUnitScope().recordTypeConversion(lhsType, rhsType);
188                 if ((expression.isConstantValueOfTypeAssignableToType(rhsType, lhsType)
189                                 || (lhsType.isBaseType() && BaseTypeBinding.isWidening(lhsType.id, rhsType.id)))
190                                 || rhsType.isCompatibleWith(lhsType)) {
191                         expression.computeConversion(scope, lhsType, rhsType);
192                         checkAssignment(scope, lhsType, rhsType);
193                         return this.resolvedType;
194                 } else if (scope.isBoxingCompatibleWith(rhsType, lhsType)) {
195                         expression.computeConversion(scope, lhsType, rhsType);
196                         return this.resolvedType;
197                 } 
198                 scope.problemReporter().typeMismatchError(rhsType, lhsType, expression);
199                 return lhsType;
200         }
201         /* (non-Javadoc)
202          * @see org.eclipse.jdt.internal.compiler.ast.Expression#resolveTypeExpecting(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.lookup.TypeBinding)
203          */
204         public TypeBinding resolveTypeExpecting(
205                         BlockScope scope,
206                         TypeBinding expectedType) {
207
208                 TypeBinding type = super.resolveTypeExpecting(scope, expectedType);
209                 if (type == null) return null;
210                 TypeBinding lhsType = this.resolvedType; 
211                 TypeBinding rhsType = this.expression.resolvedType;
212                 // signal possible accidental boolean assignment (instead of using '==' operator)
213                 if (expectedType == BooleanBinding 
214                                 && lhsType == BooleanBinding 
215                                 && (this.lhs.bits & IsStrictlyAssignedMASK) != 0) {
216                         scope.problemReporter().possibleAccidentalBooleanAssignment(this);
217                 }
218                 checkAssignment(scope, lhsType, rhsType);
219                 return type;
220         }
221
222         public void traverse(ASTVisitor visitor, BlockScope scope) {
223                 
224                 if (visitor.visit(this, scope)) {
225                         lhs.traverse(visitor, scope);
226                         expression.traverse(visitor, scope);
227                 }
228                 visitor.endVisit(this, scope);
229         }
230 }