1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
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.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
21 public class Assignment extends Expression {
24 public Expression expression;
25 public TypeBinding lhsType;
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
31 this.lhs = (Reference) lhs;
32 this.expression = expression;
34 this.sourceStart = lhs.sourceStart;
35 this.sourceEnd = sourceEnd;
38 public FlowInfo analyseCode(
39 BlockScope currentScope,
40 FlowContext flowContext,
42 // record setting a variable: various scenarii are possible, setting an array reference,
43 // a field reference, a blank final field reference, a field of an enclosing instance or
44 // just a local variable.
47 .analyseAssignment(currentScope, flowContext, flowInfo, this, false)
48 .unconditionalInits();
51 public void generateCode(
52 BlockScope currentScope,
53 CodeStream codeStream,
54 boolean valueRequired) {
56 // various scenarii are possible, setting an array reference,
57 // a field reference, a blank final field reference, a field of an enclosing instance or
58 // just a local variable.
60 int pc = codeStream.position;
61 lhs.generateAssignment(currentScope, codeStream, this, valueRequired);
62 // variable may have been optimized out
63 // the lhs is responsible to perform the implicitConversion generation for the assignment since optimized for unused local assignment.
64 codeStream.recordPositionsFrom(pc, this.sourceStart);
67 public TypeBinding resolveType(BlockScope scope) {
69 // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference
70 constant = NotAConstant;
71 this.lhsType = lhs.resolveType(scope);
72 TypeBinding expressionTb = expression.resolveType(scope);
73 if (this.lhsType == null || expressionTb == null)
76 // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character
77 // may require to widen the rhs expression at runtime
78 if ((expression.isConstantValueOfTypeAssignableToType(expressionTb, this.lhsType)
79 || (this.lhsType.isBaseType() && BaseTypeBinding.isWidening(this.lhsType.id, expressionTb.id)))
80 || (BlockScope.areTypesCompatible(expressionTb, this.lhsType))) {
81 expression.implicitWidening(this.lhsType, expressionTb);
84 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
91 public String toString(int tab) {
93 //no () when used as a statement
94 return tabString(tab) + toStringExpressionNoParenthesis();
97 public String toStringExpression() {
99 //subclass redefine toStringExpressionNoParenthesis()
100 return "(" + toStringExpressionNoParenthesis() + ")"; //$NON-NLS-2$ //$NON-NLS-1$
103 public String toStringExpressionNoParenthesis() {
105 return lhs.toStringExpression() + " " //$NON-NLS-1$
107 + ((expression.constant != null) && (expression.constant != NotAConstant)
108 ? " /*cst:" + expression.constant.toString() + "*/ " //$NON-NLS-1$ //$NON-NLS-2$
110 + expression.toStringExpression();
112 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
114 if (visitor.visit(this, scope)) {
115 lhs.traverse(visitor, scope);
116 expression.traverse(visitor, scope);
118 visitor.endVisit(this, scope);