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.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.ArrayBinding;
 
  17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
 
  18 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
 
  19 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
 
  20 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
 
  21 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
 
  24 public class InstanceOfExpression extends OperatorExpression {
 
  26         public Expression expression;
 
  27         public TypeReference type;
 
  29         public InstanceOfExpression(
 
  30                 Expression expression,
 
  34                 this.expression = expression;
 
  36                 this.bits |= operator << OperatorSHIFT;
 
  37                 this.sourceStart = expression.sourceStart;
 
  38                 this.sourceEnd = type.sourceEnd;
 
  41         public FlowInfo analyseCode(
 
  42                 BlockScope currentScope,
 
  43                 FlowContext flowContext,
 
  47                         .analyseCode(currentScope, flowContext, flowInfo)
 
  48                         .unconditionalInits();
 
  51         public final boolean areTypesCastCompatible(
 
  54                 TypeBinding expressionType) {
 
  56                 //      see specifications p.68
 
  57                 //A more cpmplete version of this method is provided on
 
  58                 //CastExpression (it deals with constant and need runtime checkcast)
 
  60                 if (castType == expressionType) return true;
 
  62                 //by grammatical construction, the first test is ALWAYS false
 
  63                 //if (castTb.isBaseType())
 
  64                 //{     if (expressionTb.isBaseType())
 
  65                 //      {       if (expression.isConstantValueOfTypeAssignableToType(expressionTb,castTb))
 
  68                 //              {       if (expressionTb==castTb)
 
  71                 //                      {       if (scope.areTypesCompatible(expressionTb,castTb))
 
  74                 //                              if (BaseTypeBinding.isNarrowing(castTb.id,expressionTb.id))
 
  80                 { //-------------checkcast to something which is NOT a basetype----------------------------------       
 
  82                         //null is compatible with every thing .... 
 
  83                         if (NullBinding == expressionType) {
 
  86                         if (expressionType.isArrayType()) {
 
  87                                 if (castType.isArrayType()) {
 
  88                                         //------- (castTb.isArray) expressionTb.isArray -----------
 
  89                                         TypeBinding expressionEltTb = ((ArrayBinding) expressionType).elementsType(scope);
 
  90                                         if (expressionEltTb.isBaseType())
 
  91                                                 // <---stop the recursion------- 
 
  92                                                 return ((ArrayBinding) castType).elementsType(scope) == expressionEltTb;
 
  93                                         //recursivly on the elts...
 
  94                                         return areTypesCastCompatible(
 
  96                                                 ((ArrayBinding) castType).elementsType(scope),
 
  99                                 if (castType.isClass()) {
 
 100                                         //------(castTb.isClass) expressionTb.isArray ---------------   
 
 101                                         if (scope.isJavaLangObject(castType))
 
 105                                 if (castType.isInterface()) {
 
 106                                         //------- (castTb.isInterface) expressionTb.isArray -----------
 
 107                                         if (scope.isJavaLangCloneable(castType) || scope.isJavaIoSerializable(castType)) {
 
 115                         if (expressionType.isBaseType()) {
 
 118                         if (expressionType.isClass()) {
 
 119                                 if (castType.isArrayType()) {
 
 120                                         // ---- (castTb.isArray) expressionTb.isClass -------
 
 121                                         if (scope.isJavaLangObject(expressionType)) {
 
 127                                 if (castType.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------ 
 
 128                                         if (expressionType.isCompatibleWith(castType))
 
 131                                                 if (castType.isCompatibleWith(expressionType)) {
 
 137                                 if (castType.isInterface()) {
 
 138                                         // ----- (castTb.isInterface) expressionTb.isClass -------  
 
 139                                         if (((ReferenceBinding) expressionType).isFinal()) {
 
 140                                                 //no subclass for expressionTb, thus compile-time check is valid
 
 141                                                 if (expressionType.isCompatibleWith(castType))
 
 151                         if (expressionType.isInterface()) {
 
 152                                 if (castType.isArrayType()) {
 
 153                                         // ----- (castTb.isArray) expressionTb.isInterface ------
 
 154                                         if (scope.isJavaLangCloneable(expressionType)
 
 155                                                 || scope.isJavaIoSerializable(expressionType))
 
 156                                                 //potential runtime error
 
 162                                 if (castType.isClass()) {
 
 163                                         // ----- (castTb.isClass) expressionTb.isInterface --------
 
 164                                         if (scope.isJavaLangObject(castType))
 
 166                                         if (((ReferenceBinding) castType).isFinal()) {
 
 167                                                 //no subclass for castTb, thus compile-time check is valid
 
 168                                                 if (castType.isCompatibleWith(expressionType)) {
 
 175                                 if (castType.isInterface()) {
 
 176                                         // ----- (castTb.isInterface) expressionTb.isInterface -------
 
 177                                         if ((Scope.compareTypes(castType, expressionType) == NotRelated)) {
 
 178                                                 MethodBinding[] castTbMethods = ((ReferenceBinding) castType).methods();
 
 179                                                 int castTbMethodsLength = castTbMethods.length;
 
 180                                                 MethodBinding[] expressionTbMethods =
 
 181                                                         ((ReferenceBinding) expressionType).methods();
 
 182                                                 int expressionTbMethodsLength = expressionTbMethods.length;
 
 183                                                 for (int i = 0; i < castTbMethodsLength; i++) {
 
 184                                                         for (int j = 0; j < expressionTbMethodsLength; j++) {
 
 185                                                                 if (castTbMethods[i].selector == expressionTbMethods[j].selector) {
 
 186                                                                         if (castTbMethods[i].returnType != expressionTbMethods[j].returnType) {
 
 187                                                                                 if (castTbMethods[i].areParametersEqual(expressionTbMethods[j])) {
 
 205          * Code generation for instanceOfExpression
 
 207          * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
 
 208          * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
 
 209          * @param valueRequired boolean
 
 211 //      public void generateCode(
 
 212 //              BlockScope currentScope,
 
 213 //              CodeStream codeStream,
 
 214 //              boolean valueRequired) {
 
 216 //              int pc = codeStream.position;
 
 217 //              expression.generateCode(currentScope, codeStream, true);
 
 218 //              codeStream.instance_of(type.resolvedType);
 
 219 //              if (!valueRequired)
 
 221 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
 
 224         public TypeBinding resolveType(BlockScope scope) {
 
 226                 constant = NotAConstant;
 
 227                 TypeBinding expressionType = expression.resolveType(scope);
 
 228                 TypeBinding checkType = type.resolveType(scope);
 
 229                 if (expressionType == null || checkType == null)
 
 232                 if (!areTypesCastCompatible(scope, checkType, expressionType)) {
 
 233                         scope.problemReporter().notCompatibleTypesError(this, expressionType, checkType);
 
 236                 this.resolvedType = BooleanBinding;
 
 237                 return BooleanBinding;
 
 239         public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
 
 241                 expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
 
 242                 return type.print(0, output);
 
 244         public String toStringExpressionNoParenthesis() {
 
 246                 return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
 
 250         public void traverse(ASTVisitor visitor, BlockScope scope) {
 
 252                 if (visitor.visit(this, scope)) {
 
 253                         expression.traverse(visitor, scope);
 
 254                         type.traverse(visitor, scope);
 
 256                 visitor.endVisit(this, scope);