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.phpeclipse.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
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 {
21 public int assignmentImplicitConversion;
23 // var op exp is equivalent to var = (varType) var op exp
24 // assignmentImplicitConversion stores the cast needed for the assignment
26 public CompoundAssignment(Expression lhs, Expression expression,int operator, int sourceEnd) {
27 //lhs is always a reference by construction ,
28 //but is build as an expression ==> the checkcast cannot fail
30 super(lhs, expression, sourceEnd);
31 lhs.bits &= ~IsStrictlyAssignedMASK; // tag lhs as NON assigned - it is also a read access
32 this.operator = operator ;
34 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
35 // record setting a variable: various scenarii are possible, setting an array reference,
36 // a field reference, a blank final field reference, a field of an enclosing instance or
37 // just a local variable.
39 return ((Reference) lhs).analyseAssignment(currentScope, flowContext, flowInfo, this, true).unconditionalInits();
41 //public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
43 // // various scenarii are possible, setting an array reference,
44 // // a field reference, a blank final field reference, a field of an enclosing instance or
45 // // just a local variable.
47 // int pc = codeStream.position;
48 // ((Reference) lhs).generateCompoundAssignment(currentScope, codeStream, expression, operator, assignmentImplicitConversion, valueRequired);
49 // if (valueRequired) {
50 // codeStream.generateImplicitConversion(implicitConversion);
52 // codeStream.recordPositionsFrom(pc, this.sourceStart);
54 public String operatorToString() {
57 return "+="; //$NON-NLS-1$
59 return "-="; //$NON-NLS-1$
61 return "*="; //$NON-NLS-1$
63 return "/="; //$NON-NLS-1$
65 return "&="; //$NON-NLS-1$
67 return "|="; //$NON-NLS-1$
69 return "^="; //$NON-NLS-1$
71 return "%="; //$NON-NLS-1$
73 return "<<="; //$NON-NLS-1$
75 return ">>="; //$NON-NLS-1$
76 case UNSIGNED_RIGHT_SHIFT :
77 return ">>>="; //$NON-NLS-1$
79 return "unknown operator"; //$NON-NLS-1$
81 public TypeBinding resolveType(BlockScope scope) {
82 constant = NotAConstant;
83 if (!(this.lhs instanceof Reference)) {
84 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
86 TypeBinding lhsType = lhs.resolveType(scope);
87 TypeBinding expressionType = expression.resolveType(scope);
88 if (lhsType == null || expressionType == null)
91 int lhsId = lhsType.id;
92 int expressionId = expressionType.id;
93 if (restrainUsageToNumericTypes() && !lhsType.isNumericType()) {
94 scope.problemReporter().operatorOnlyValidOnNumericType(this, lhsType, expressionType);
97 if (lhsId > 15 || expressionId > 15) {
98 if (lhsId != T_String) { // String += Object is valid wheraas Object -= String is not
99 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
102 expressionId = T_Object; // use the Object has tag table
105 // the code is an int
106 // (cast) left Op (cast) rigth --> result
107 // 0000 0000 0000 0000 0000
108 // <<16 <<12 <<8 <<4 <<0
110 // the conversion is stored INTO the reference (info needed for the code gen)
111 int result = OperatorExpression.ResolveTypeTables[operator][ (lhsId << 4) + expressionId];
112 if (result == T_undefined) {
113 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
116 if (operator == PLUS){
117 if(scope.isJavaLangObject(lhsType)) {
118 // <Object> += <String> is illegal
119 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
121 } else if ((lhsType.isNumericType() || lhsId == T_boolean) && !expressionType.isNumericType()){
122 // <int | boolean> += <String> is illegal
123 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
127 lhs.implicitConversion = result >>> 12;
128 expression.implicitConversion = (result >>> 4) & 0x000FF;
129 assignmentImplicitConversion = (lhsId << 4) + (result & 0x0000F);
130 return this.resolvedType = lhsType;
132 public boolean restrainUsageToNumericTypes(){
134 public String toStringExpressionNoParenthesis() {
136 return lhs.toStringExpression() + " " + //$NON-NLS-1$
137 operatorToString() + " " + //$NON-NLS-1$
138 expression.toStringExpression() ; }
139 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
140 if (visitor.visit(this, scope)) {
141 lhs.traverse(visitor, scope);
142 expression.traverse(visitor, scope);
144 visitor.endVisit(this, scope);