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.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
20 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,int operator, int sourceEnd) {
28 //lhs is always a reference by construction ,
29 //but is build as an expression ==> the checkcast cannot fail
31 super(lhs, expression, sourceEnd);
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 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 lhs.generateCompoundAssignment(currentScope, codeStream, expression, operator, assignmentImplicitConversion, 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 TypeBinding lhsType = lhs.resolveType(scope);
84 TypeBinding expressionType = expression.resolveType(scope);
85 if (lhsType == null || expressionType == null)
88 int lhsId = lhsType.id;
89 int expressionId = expressionType.id;
90 if (restrainUsageToNumericTypes() && !lhsType.isNumericType()) {
91 scope.problemReporter().operatorOnlyValidOnNumericType(this, lhsType, expressionType);
94 if (lhsId > 15 || expressionId > 15) {
95 if (lhsId != T_String) { // String += Object is valid wheraas Object -= String is not
96 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
99 expressionId = T_Object; // use the Object has tag table
102 // the code is an int
103 // (cast) left Op (cast) rigth --> result
104 // 0000 0000 0000 0000 0000
105 // <<16 <<12 <<8 <<4 <<0
107 // the conversion is stored INTO the reference (info needed for the code gen)
108 int result = OperatorExpression.ResolveTypeTables[operator][ (lhsId << 4) + expressionId];
109 if (result == T_undefined) {
110 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
113 if (operator == PLUS){
114 if(scope.isJavaLangObject(lhsType)) {
115 // <Object> += <String> is illegal
116 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
118 } else if ((lhsType.isNumericType() || lhsId == T_boolean) && !expressionType.isNumericType()){
119 // <int | boolean> += <String> is illegal
120 scope.problemReporter().invalidOperator(this, lhsType, expressionType);
124 lhs.implicitConversion = result >>> 12;
125 expression.implicitConversion = (result >>> 4) & 0x000FF;
126 assignmentImplicitConversion = (lhsId << 4) + (result & 0x0000F);
129 public boolean restrainUsageToNumericTypes(){
131 public String toStringExpressionNoParenthesis() {
133 return lhs.toStringExpression() + " " + //$NON-NLS-1$
134 operatorToString() + " " + //$NON-NLS-1$
135 expression.toStringExpression() ; }
136 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
137 if (visitor.visit(this, scope)) {
138 lhs.traverse(visitor, scope);
139 expression.traverse(visitor, scope);
141 visitor.endVisit(this, scope);