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.codegen.Label;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
17 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
18 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
19 import net.sourceforge.phpdt.internal.compiler.impl.NullConstant;
20 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
22 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
23 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
24 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
25 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
27 public class EqualExpression extends BinaryExpression {
29 public EqualExpression(Expression left, Expression right,int operator) {
30 super(left,right,operator);
32 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
33 if (((bits & OperatorMASK) >> OperatorSHIFT) == EQUAL_EQUAL) {
34 if ((left.constant != NotAConstant) && (left.constant.typeID() == T_boolean)) {
35 if (left.constant.booleanValue()) { // true == anything
36 // this is equivalent to the right argument inits
37 return right.analyseCode(currentScope, flowContext, flowInfo);
38 } else { // false == anything
39 // this is equivalent to the right argument inits negated
40 return right.analyseCode(currentScope, flowContext, flowInfo).asNegatedCondition();
43 if ((right.constant != NotAConstant) && (right.constant.typeID() == T_boolean)) {
44 if (right.constant.booleanValue()) { // anything == true
45 // this is equivalent to the right argument inits
46 return left.analyseCode(currentScope, flowContext, flowInfo);
47 } else { // anything == false
48 // this is equivalent to the right argument inits negated
49 return left.analyseCode(currentScope, flowContext, flowInfo).asNegatedCondition();
52 return right.analyseCode(
53 currentScope, flowContext,
54 left.analyseCode(currentScope, flowContext, flowInfo).unconditionalInits()).unconditionalInits();
55 } else { //NOT_EQUAL :
56 if ((left.constant != NotAConstant) && (left.constant.typeID() == T_boolean)) {
57 if (!left.constant.booleanValue()) { // false != anything
58 // this is equivalent to the right argument inits
59 return right.analyseCode(currentScope, flowContext, flowInfo);
60 } else { // true != anything
61 // this is equivalent to the right argument inits negated
62 return right.analyseCode(currentScope, flowContext, flowInfo).asNegatedCondition();
65 if ((right.constant != NotAConstant) && (right.constant.typeID() == T_boolean)) {
66 if (!right.constant.booleanValue()) { // anything != false
67 // this is equivalent to the right argument inits
68 return left.analyseCode(currentScope, flowContext, flowInfo);
69 } else { // anything != true
70 // this is equivalent to the right argument inits negated
71 return left.analyseCode(currentScope, flowContext, flowInfo).asNegatedCondition();
74 return right.analyseCode(
75 currentScope, flowContext,
76 left.analyseCode(currentScope, flowContext, flowInfo).unconditionalInits()).asNegatedCondition().unconditionalInits();
79 public final boolean areTypesCastCompatible(BlockScope scope, TypeBinding castTb, TypeBinding expressionTb) {
80 //see specifications p.68
81 //A more complete version of this method is provided on
82 //CastExpression (it deals with constant and need runtime checkcast)
85 //========ARRAY===============
86 if (expressionTb.isArrayType()) {
87 if (castTb.isArrayType()) { //------- (castTb.isArray) expressionTb.isArray -----------
88 TypeBinding expressionEltTb = ((ArrayBinding) expressionTb).elementsType(scope);
89 if (expressionEltTb.isBaseType())
90 // <---stop the recursion-------
91 return ((ArrayBinding) castTb).elementsType(scope) == expressionEltTb;
92 //recursivly on the elts...
93 return areTypesCastCompatible(scope, ((ArrayBinding) castTb).elementsType(scope), expressionEltTb);
95 if (castTb.isBaseType()) {
98 if (castTb.isClass()) { //------(castTb.isClass) expressionTb.isArray ---------------
99 if (scope.isJavaLangObject(castTb))
103 if (castTb.isInterface()) { //------- (castTb.isInterface) expressionTb.isArray -----------
104 if (scope.isJavaLangCloneable(castTb) || scope.isJavaIoSerializable(castTb)) {
113 //------------(castType) null--------------
114 if (expressionTb == NullBinding) {
115 return !castTb.isBaseType();
118 //========BASETYPE==============
119 if (expressionTb.isBaseType()) {
124 //========REFERENCE TYPE===================
126 if (expressionTb.isClass()) {
127 if (castTb.isArrayType()) { // ---- (castTb.isArray) expressionTb.isClass -------
128 if (scope.isJavaLangObject(expressionTb))
131 if (castTb.isBaseType()) {
134 if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------
135 if (BlockScope.areTypesCompatible(expressionTb, castTb))
138 if (BlockScope.areTypesCompatible(castTb, expressionTb)) {
144 if (castTb.isInterface()) { // ----- (castTb.isInterface) expressionTb.isClass -------
145 if (((ReferenceBinding) expressionTb).isFinal()) { //no subclass for expressionTb, thus compile-time check is valid
146 if (BlockScope.areTypesCompatible(expressionTb, castTb))
156 if (expressionTb.isInterface()) {
157 if (castTb.isArrayType()) { // ----- (castTb.isArray) expressionTb.isInterface ------
158 if (scope.isJavaLangCloneable(expressionTb) || scope.isJavaIoSerializable(expressionTb))
159 //potential runtime error
165 if (castTb.isBaseType()) {
168 if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isInterface --------
169 if (scope.isJavaLangObject(castTb))
171 if (((ReferenceBinding) castTb).isFinal()) { //no subclass for castTb, thus compile-time check is valid
172 if (BlockScope.areTypesCompatible(castTb, expressionTb)) {
179 if (castTb.isInterface()) { // ----- (castTb.isInterface) expressionTb.isInterface -------
180 if (castTb != expressionTb && (Scope.compareTypes(castTb, expressionTb) == NotRelated)) {
181 MethodBinding[] castTbMethods = ((ReferenceBinding) castTb).methods();
182 int castTbMethodsLength = castTbMethods.length;
183 MethodBinding[] expressionTbMethods = ((ReferenceBinding) expressionTb).methods();
184 int expressionTbMethodsLength = expressionTbMethods.length;
185 for (int i = 0; i < castTbMethodsLength; i++) {
186 for (int j = 0; j < expressionTbMethodsLength; j++) {
187 if (castTbMethods[i].selector == expressionTbMethods[j].selector) {
188 if (castTbMethods[i].returnType != expressionTbMethods[j].returnType) {
189 if (castTbMethods[i].areParametersEqual(expressionTbMethods[j])) {
205 public final void computeConstant(TypeBinding leftTb, TypeBinding rightTb) {
206 if ((left.constant != NotAConstant) && (right.constant != NotAConstant)) {
208 Constant.computeConstantOperationEQUAL_EQUAL(
214 if (((bits & OperatorMASK) >> OperatorSHIFT) == NOT_EQUAL)
215 constant = Constant.fromValue(!constant.booleanValue());
217 constant = NotAConstant;
221 * Normal == or != code generation.
223 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
224 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
225 * @param valueRequired boolean
227 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
229 if (constant != NotAConstant) {
230 int pc = codeStream.position;
232 codeStream.generateConstant(constant, implicitConversion);
233 codeStream.recordPositionsFrom(pc, this.sourceStart);
237 generateOptimizedBoolean(
241 falseLabel = new Label(codeStream),
243 if (falseLabel.hasForwardReferences()) {
245 // comparison is TRUE
246 codeStream.iconst_1();
247 if ((bits & ValueForReturnMASK) != 0){
248 codeStream.ireturn();
249 // comparison is FALSE
251 codeStream.iconst_0();
253 Label endLabel = new Label(codeStream);
254 codeStream.goto_(endLabel);
255 codeStream.decrStackSize(1);
256 // comparison is FALSE
258 codeStream.iconst_0();
267 * Boolean operator code generation
268 * Optimized operations are: == and !=
270 public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStream, Label trueLabel, Label falseLabel, boolean valueRequired) {
271 if ((constant != Constant.NotAConstant) && (constant.typeID() == T_boolean)) {
272 super.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel, valueRequired);
275 int pc = codeStream.position;
276 if (((bits & OperatorMASK) >> OperatorSHIFT) == EQUAL_EQUAL) {
277 if ((left.implicitConversion & 0xF) /*compile-time*/ == T_boolean) {
278 generateOptimizedBooleanEqual(currentScope, codeStream, trueLabel, falseLabel, valueRequired);
280 generateOptimizedNonBooleanEqual(currentScope, codeStream, trueLabel, falseLabel, valueRequired);
283 if ((left.implicitConversion & 0xF) /*compile-time*/ == T_boolean) {
284 generateOptimizedBooleanEqual(currentScope, codeStream, falseLabel, trueLabel, valueRequired);
286 generateOptimizedNonBooleanEqual(currentScope, codeStream, falseLabel, trueLabel, valueRequired);
289 codeStream.recordPositionsFrom(pc, this.sourceStart);
292 * Boolean generation for == with boolean operands
294 * Note this code does not optimize conditional constants !!!!
296 public void generateOptimizedBooleanEqual(BlockScope currentScope, CodeStream codeStream, Label trueLabel, Label falseLabel, boolean valueRequired) {
297 int pc = codeStream.position;
298 // optimized cases: true == x, false == x
299 if (left.constant != NotAConstant) {
300 boolean inline = left.constant.booleanValue();
301 right.generateOptimizedBoolean(currentScope, codeStream, (inline ? trueLabel : falseLabel), (inline ? falseLabel : trueLabel), valueRequired);
302 codeStream.recordPositionsFrom(pc, this.sourceStart);
304 } // optimized cases: x == true, x == false
305 if (right.constant != NotAConstant) {
306 boolean inline = right.constant.booleanValue();
307 left.generateOptimizedBoolean(currentScope, codeStream, (inline ? trueLabel : falseLabel), (inline ? falseLabel : trueLabel), valueRequired);
308 codeStream.recordPositionsFrom(pc, this.sourceStart);
312 left.generateCode(currentScope, codeStream, valueRequired);
313 right.generateCode(currentScope, codeStream, valueRequired);
315 if (falseLabel == null) {
316 if (trueLabel != null) {
317 // implicit falling through the FALSE case
318 codeStream.if_icmpeq(trueLabel);
321 // implicit falling through the TRUE case
322 if (trueLabel == null) {
323 codeStream.if_icmpne(falseLabel);
325 // no implicit fall through TRUE/FALSE --> should never occur
329 codeStream.recordPositionsFrom(pc, this.sourceStart);
332 * Boolean generation for == with non-boolean operands
335 public void generateOptimizedNonBooleanEqual(BlockScope currentScope, CodeStream codeStream, Label trueLabel, Label falseLabel, boolean valueRequired) {
336 int pc = codeStream.position;
338 if ((inline = right.constant) != NotAConstant) {
339 // optimized case: x == null
340 if (right.constant == NullConstant.Default) {
341 left.generateCode(currentScope, codeStream, valueRequired);
343 if (falseLabel == null) {
344 if (trueLabel != null) {
345 // implicit falling through the FALSE case
346 codeStream.ifnull(trueLabel);
349 // implicit falling through the TRUE case
350 if (trueLabel == null) {
351 codeStream.ifnonnull(falseLabel);
353 // no implicit fall through TRUE/FALSE --> should never occur
357 codeStream.recordPositionsFrom(pc, this.sourceStart);
360 // optimized case: x == 0
361 if (((left.implicitConversion >> 4) == T_int) && (inline.intValue() == 0)) {
362 left.generateCode(currentScope, codeStream, valueRequired);
364 if (falseLabel == null) {
365 if (trueLabel != null) {
366 // implicit falling through the FALSE case
367 codeStream.ifeq(trueLabel);
370 // implicit falling through the TRUE case
371 if (trueLabel == null) {
372 codeStream.ifne(falseLabel);
374 // no implicit fall through TRUE/FALSE --> should never occur
378 codeStream.recordPositionsFrom(pc, this.sourceStart);
382 if ((inline = left.constant) != NotAConstant) {
383 // optimized case: null == x
384 if (left.constant == NullConstant.Default) {
385 right.generateCode(currentScope, codeStream, valueRequired);
387 if (falseLabel == null) {
388 if (trueLabel != null) {
389 // implicit falling through the FALSE case
390 codeStream.ifnull(trueLabel);
393 // implicit falling through the TRUE case
394 if (trueLabel == null) {
395 codeStream.ifnonnull(falseLabel);
397 // no implicit fall through TRUE/FALSE --> should never occur
401 codeStream.recordPositionsFrom(pc, this.sourceStart);
404 // optimized case: 0 == x
405 if (((left.implicitConversion >> 4) == T_int)
406 && (inline.intValue() == 0)) {
407 right.generateCode(currentScope, codeStream, valueRequired);
409 if (falseLabel == null) {
410 if (trueLabel != null) {
411 // implicit falling through the FALSE case
412 codeStream.ifeq(trueLabel);
415 // implicit falling through the TRUE case
416 if (trueLabel == null) {
417 codeStream.ifne(falseLabel);
419 // no implicit fall through TRUE/FALSE --> should never occur
423 codeStream.recordPositionsFrom(pc, this.sourceStart);
428 left.generateCode(currentScope, codeStream, valueRequired);
429 right.generateCode(currentScope, codeStream, valueRequired);
431 if (falseLabel == null) {
432 if (trueLabel != null) {
433 // implicit falling through the FALSE case
434 switch (left.implicitConversion >> 4) { // operand runtime type
436 codeStream.if_icmpeq(trueLabel);
440 codeStream.ifeq(trueLabel);
444 codeStream.ifeq(trueLabel);
448 codeStream.ifeq(trueLabel);
451 codeStream.if_acmpeq(trueLabel);
455 // implicit falling through the TRUE case
456 if (trueLabel == null) {
457 switch (left.implicitConversion >> 4) { // operand runtime type
459 codeStream.if_icmpne(falseLabel);
463 codeStream.ifne(falseLabel);
467 codeStream.ifne(falseLabel);
471 codeStream.ifne(falseLabel);
474 codeStream.if_acmpne(falseLabel);
477 // no implicit fall through TRUE/FALSE --> should never occur
481 codeStream.recordPositionsFrom(pc, this.sourceStart);
483 public boolean isCompactableOperation() {
486 public TypeBinding resolveType(BlockScope scope) {
487 // always return BooleanBinding
488 TypeBinding leftTb = left.resolveType(scope);
489 TypeBinding rightTb = right.resolveType(scope);
490 if (leftTb == null || rightTb == null){
491 constant = NotAConstant;
496 if (leftTb.isBaseType() && rightTb.isBaseType()) {
497 // the code is an int
498 // (cast) left == (cast) rigth --> result
499 // 0000 0000 0000 0000 0000
500 // <<16 <<12 <<8 <<4 <<0
501 int result = ResolveTypeTables[EQUAL_EQUAL][ (leftTb.id << 4) + rightTb.id];
502 left.implicitConversion = result >>> 12;
503 right.implicitConversion = (result >>> 4) & 0x000FF;
504 bits |= result & 0xF;
505 if ((result & 0x0000F) == T_undefined) {
506 constant = Constant.NotAConstant;
507 scope.problemReporter().invalidOperator(this, leftTb, rightTb);
510 computeConstant(leftTb, rightTb);
511 this.typeBinding = BooleanBinding;
512 return BooleanBinding;
517 if (areTypesCastCompatible(scope, rightTb, leftTb) || areTypesCastCompatible(scope, leftTb, rightTb)) {
518 // (special case for String)
519 if ((rightTb.id == T_String) && (leftTb.id == T_String))
520 computeConstant(leftTb, rightTb);
522 constant = NotAConstant;
523 if (rightTb.id == T_String)
524 right.implicitConversion = String2String;
525 if (leftTb.id == T_String)
526 left.implicitConversion = String2String;
527 this.typeBinding = BooleanBinding;
528 return BooleanBinding;
530 constant = NotAConstant;
531 scope.problemReporter().notCompatibleTypesError(this, leftTb, rightTb);
534 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
535 if (visitor.visit(this, scope)) {
536 left.traverse(visitor, scope);
537 right.traverse(visitor, scope);
539 visitor.endVisit(this, scope);