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 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
17 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
19 public class CompoundAssignment extends Assignment implements OperatorIds {
22 public int assignmentImplicitConversion;
24 // var op exp is equivalent to var = (varType) var op exp
25 // assignmentImplicitConversion stores the cast needed for the assignment
27 public CompoundAssignment(Expression lhs, Expression expression,
28 int operator, int sourceEnd) {
29 // lhs is always a reference by construction ,
30 // but is build as an expression ==> the checkcast cannot fail
32 super(lhs, expression, sourceEnd);
33 lhs.bits &= ~IsStrictlyAssignedMASK; // tag lhs as NON assigned - it
34 // is also a read access
35 this.operator = operator;
38 public FlowInfo analyseCode(BlockScope currentScope,
39 FlowContext flowContext, FlowInfo flowInfo) {
40 // record setting a variable: various scenarii are possible, setting an
42 // a field reference, a blank final field reference, a field of an
43 // enclosing instance or
44 // just a local variable.
46 return ((Reference) lhs).analyseAssignment(currentScope, flowContext,
47 flowInfo, this, true).unconditionalInits();
50 // public void generateCode(BlockScope currentScope, CodeStream codeStream,
51 // boolean valueRequired) {
53 // // various scenarii are possible, setting an array reference,
54 // // a field reference, a blank final field reference, a field of an
55 // enclosing instance or
56 // // just a local variable.
58 // int pc = codeStream.position;
59 // ((Reference) lhs).generateCompoundAssignment(currentScope, codeStream,
60 // expression, operator, assignmentImplicitConversion, valueRequired);
61 // if (valueRequired) {
62 // codeStream.generateImplicitConversion(implicitConversion);
64 // codeStream.recordPositionsFrom(pc, this.sourceStart);
66 public String operatorToString() {
69 return "+="; //$NON-NLS-1$
71 return "-="; //$NON-NLS-1$
73 return "*="; //$NON-NLS-1$
75 return "/="; //$NON-NLS-1$
77 return "&="; //$NON-NLS-1$
79 return "|="; //$NON-NLS-1$
81 return "^="; //$NON-NLS-1$
83 return "%="; //$NON-NLS-1$
85 return "<<="; //$NON-NLS-1$
87 return ">>="; //$NON-NLS-1$
88 case UNSIGNED_RIGHT_SHIFT:
89 return ">>>="; //$NON-NLS-1$
92 return "unknown operator"; //$NON-NLS-1$
95 public TypeBinding resolveType(BlockScope scope) {
96 constant = NotAConstant;
97 if (!(this.lhs instanceof Reference)) {
98 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
100 TypeBinding lhsType = lhs.resolveType(scope);
101 TypeBinding expressionType = expression.resolveType(scope);
102 if (lhsType == null || expressionType == null)
105 int lhsId = lhsType.id;
106 int expressionId = expressionType.id;
107 if (restrainUsageToNumericTypes() && !lhsType.isNumericType()) {
108 scope.problemReporter().operatorOnlyValidOnNumericType(this,
109 lhsType, expressionType);
112 if (lhsId > 15 || expressionId > 15) {
113 if (lhsId != T_String) { // String += Object is valid wheraas
114 // Object -= String is not
115 scope.problemReporter().invalidOperator(this, lhsType,
119 expressionId = T_Object; // use the Object has tag table
122 // the code is an int
123 // (cast) left Op (cast) rigth --> result
124 // 0000 0000 0000 0000 0000
125 // <<16 <<12 <<8 <<4 <<0
127 // the conversion is stored INTO the reference (info needed for the code
129 int result = OperatorExpression.ResolveTypeTables[operator][(lhsId << 4)
131 if (result == T_undefined) {
132 scope.problemReporter().invalidOperator(this, lhsType,
136 if (operator == PLUS) {
137 if (scope.isJavaLangObject(lhsType)) {
138 // <Object> += <String> is illegal
139 scope.problemReporter().invalidOperator(this, lhsType,
142 } else if ((lhsType.isNumericType() || lhsId == T_boolean)
143 && !expressionType.isNumericType()) {
144 // <int | boolean> += <String> is illegal
145 scope.problemReporter().invalidOperator(this, lhsType,
150 lhs.implicitConversion = result >>> 12;
151 expression.implicitConversion = (result >>> 4) & 0x000FF;
152 assignmentImplicitConversion = (lhsId << 4) + (result & 0x0000F);
153 return this.resolvedType = lhsType;
156 public boolean restrainUsageToNumericTypes() {
160 public String toStringExpressionNoParenthesis() {
162 return lhs.toStringExpression() + " " + //$NON-NLS-1$
163 operatorToString() + " " + //$NON-NLS-1$
164 expression.toStringExpression();
167 public void traverse(ASTVisitor visitor, BlockScope scope) {
168 if (visitor.visit(this, scope)) {
169 lhs.traverse(visitor, scope);
170 expression.traverse(visitor, scope);
172 visitor.endVisit(this, scope);