changes for new Action
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / internal / compiler / ast / CompoundAssignment.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  *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
12
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;
18
19 public class CompoundAssignment extends Assignment implements OperatorIds {
20         public int operator;
21         public int assignmentImplicitConversion;
22
23         //  var op exp is equivalent to var = (varType) var op exp
24         // assignmentImplicitConversion stores the cast needed for the assignment
25
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
29
30         super(lhs, expression, sourceEnd);
31         lhs.bits &= ~IsStrictlyAssignedMASK; // tag lhs as NON assigned - it is also a read access
32         this.operator = operator ;
33 }
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.
38
39         return  ((Reference) lhs).analyseAssignment(currentScope, flowContext, flowInfo, this, true).unconditionalInits();
40 }
41 //public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
42 //
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.
46 //
47 //      int pc = codeStream.position;
48 //       ((Reference) lhs).generateCompoundAssignment(currentScope, codeStream, expression, operator, assignmentImplicitConversion, valueRequired);
49 //      if (valueRequired) {
50 //              codeStream.generateImplicitConversion(implicitConversion);
51 //      }
52 //      codeStream.recordPositionsFrom(pc, this.sourceStart);
53 //}
54 public String operatorToString() {
55         switch (operator) {
56                 case PLUS :
57                         return "+="; //$NON-NLS-1$
58                 case MINUS :
59                         return "-="; //$NON-NLS-1$
60                 case MULTIPLY :
61                         return "*="; //$NON-NLS-1$
62                 case DIVIDE :
63                         return "/="; //$NON-NLS-1$
64                 case AND :
65                         return "&="; //$NON-NLS-1$
66                 case OR :
67                         return "|="; //$NON-NLS-1$
68                 case XOR :
69                         return "^="; //$NON-NLS-1$
70                 case REMAINDER :
71                         return "%="; //$NON-NLS-1$
72                 case LEFT_SHIFT :
73                         return "<<="; //$NON-NLS-1$
74                 case RIGHT_SHIFT :
75                         return ">>="; //$NON-NLS-1$
76                 case UNSIGNED_RIGHT_SHIFT :
77                         return ">>>="; //$NON-NLS-1$
78         };
79         return "unknown operator"; //$NON-NLS-1$
80 }
81 public TypeBinding resolveType(BlockScope scope) {
82         constant = NotAConstant;
83         if (!(this.lhs instanceof Reference)) {
84                 scope.problemReporter().expressionShouldBeAVariable(this.lhs);
85         }
86         TypeBinding lhsType = lhs.resolveType(scope);
87         TypeBinding expressionType = expression.resolveType(scope);
88         if (lhsType == null || expressionType == null)
89                 return null;
90
91         int lhsId = lhsType.id;
92         int expressionId = expressionType.id;
93         if (restrainUsageToNumericTypes() && !lhsType.isNumericType()) {
94                 scope.problemReporter().operatorOnlyValidOnNumericType(this, lhsType, expressionType);
95                 return null;
96         }
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);
100                         return null;
101                 }
102                 expressionId = T_Object; // use the Object has tag table
103         }
104
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
109
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);
114                 return null;
115         }
116         if (operator == PLUS){
117                 if(scope.isJavaLangObject(lhsType)) {
118                         // <Object> += <String> is illegal
119                         scope.problemReporter().invalidOperator(this, lhsType, expressionType);
120                         return null;
121                 } else if ((lhsType.isNumericType() || lhsId == T_boolean) && !expressionType.isNumericType()){
122                         // <int | boolean> += <String> is illegal
123                         scope.problemReporter().invalidOperator(this, lhsType, expressionType);
124                         return null;
125                 }
126         }
127         lhs.implicitConversion = result >>> 12;
128         expression.implicitConversion = (result >>> 4) & 0x000FF;
129         assignmentImplicitConversion = (lhsId << 4) + (result & 0x0000F);
130         return this.resolvedType = lhsType;
131 }
132 public boolean restrainUsageToNumericTypes(){
133         return false ;}
134 public String toStringExpressionNoParenthesis() {
135
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);
143         }
144         visitor.endVisit(this, scope);
145 }
146 }