Change Eclipse-LazyStart: true to Bundle-ActivationPolicy: lazy (upgrade to The Equin...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / 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.phpdt.internal.compiler.ast;
12
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;
18
19 public class CompoundAssignment extends Assignment implements OperatorIds {
20         public int operator;
21
22         public int assignmentImplicitConversion;
23
24         // var op exp is equivalent to var = (varType) var op exp
25         // assignmentImplicitConversion stores the cast needed for the assignment
26
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
31
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;
36         }
37
38         public FlowInfo analyseCode(BlockScope currentScope,
39                         FlowContext flowContext, FlowInfo flowInfo) {
40                 // record setting a variable: various scenarii are possible, setting an
41                 // array reference,
42                 // a field reference, a blank final field reference, a field of an
43                 // enclosing instance or
44                 // just a local variable.
45
46                 return ((Reference) lhs).analyseAssignment(currentScope, flowContext,
47                                 flowInfo, this, true).unconditionalInits();
48         }
49
50         // public void generateCode(BlockScope currentScope, CodeStream codeStream,
51         // boolean valueRequired) {
52         //
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.
57         //
58         // int pc = codeStream.position;
59         // ((Reference) lhs).generateCompoundAssignment(currentScope, codeStream,
60         // expression, operator, assignmentImplicitConversion, valueRequired);
61         // if (valueRequired) {
62         // codeStream.generateImplicitConversion(implicitConversion);
63         // }
64         // codeStream.recordPositionsFrom(pc, this.sourceStart);
65         // }
66         public String operatorToString() {
67                 switch (operator) {
68                 case PLUS:
69                         return "+="; //$NON-NLS-1$
70                 case MINUS:
71                         return "-="; //$NON-NLS-1$
72                 case MULTIPLY:
73                         return "*="; //$NON-NLS-1$
74                 case DIVIDE:
75                         return "/="; //$NON-NLS-1$
76                 case AND:
77                         return "&="; //$NON-NLS-1$
78                 case OR:
79                         return "|="; //$NON-NLS-1$
80                 case XOR:
81                         return "^="; //$NON-NLS-1$
82                 case REMAINDER:
83                         return "%="; //$NON-NLS-1$
84                 case LEFT_SHIFT:
85                         return "<<="; //$NON-NLS-1$
86                 case RIGHT_SHIFT:
87                         return ">>="; //$NON-NLS-1$
88                 case UNSIGNED_RIGHT_SHIFT:
89                         return ">>>="; //$NON-NLS-1$
90                 }
91                 ;
92                 return "unknown operator"; //$NON-NLS-1$
93         }
94
95         public TypeBinding resolveType(BlockScope scope) {
96                 constant = NotAConstant;
97                 if (!(this.lhs instanceof Reference)) {
98                         scope.problemReporter().expressionShouldBeAVariable(this.lhs);
99                 }
100                 TypeBinding lhsType = lhs.resolveType(scope);
101                 TypeBinding expressionType = expression.resolveType(scope);
102                 if (lhsType == null || expressionType == null)
103                         return null;
104
105                 int lhsId = lhsType.id;
106                 int expressionId = expressionType.id;
107                 if (restrainUsageToNumericTypes() && !lhsType.isNumericType()) {
108                         scope.problemReporter().operatorOnlyValidOnNumericType(this,
109                                         lhsType, expressionType);
110                         return null;
111                 }
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,
116                                                 expressionType);
117                                 return null;
118                         }
119                         expressionId = T_Object; // use the Object has tag table
120                 }
121
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
126
127                 // the conversion is stored INTO the reference (info needed for the code
128                 // gen)
129                 int result = OperatorExpression.ResolveTypeTables[operator][(lhsId << 4)
130                                 + expressionId];
131                 if (result == T_undefined) {
132                         scope.problemReporter().invalidOperator(this, lhsType,
133                                         expressionType);
134                         return null;
135                 }
136                 if (operator == PLUS) {
137                         if (scope.isJavaLangObject(lhsType)) {
138                                 // <Object> += <String> is illegal
139                                 scope.problemReporter().invalidOperator(this, lhsType,
140                                                 expressionType);
141                                 return null;
142                         } else if ((lhsType.isNumericType() || lhsId == T_boolean)
143                                         && !expressionType.isNumericType()) {
144                                 // <int | boolean> += <String> is illegal
145                                 scope.problemReporter().invalidOperator(this, lhsType,
146                                                 expressionType);
147                                 return null;
148                         }
149                 }
150                 lhs.implicitConversion = result >>> 12;
151                 expression.implicitConversion = (result >>> 4) & 0x000FF;
152                 assignmentImplicitConversion = (lhsId << 4) + (result & 0x0000F);
153                 return this.resolvedType = lhsType;
154         }
155
156         public boolean restrainUsageToNumericTypes() {
157                 return false;
158         }
159
160         public String toStringExpressionNoParenthesis() {
161
162                 return lhs.toStringExpression() + " " + //$NON-NLS-1$
163                                 operatorToString() + " " + //$NON-NLS-1$
164                                 expression.toStringExpression();
165         }
166
167         public void traverse(ASTVisitor visitor, BlockScope scope) {
168                 if (visitor.visit(this, scope)) {
169                         lhs.traverse(visitor, scope);
170                         expression.traverse(visitor, scope);
171                 }
172                 visitor.endVisit(this, scope);
173         }
174 }