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.*;
15 import net.sourceforge.phpdt.internal.compiler.flow.*;
16 import net.sourceforge.phpdt.internal.compiler.lookup.*;
18 public class InstanceOfExpression extends OperatorExpression {
20 public Expression expression;
21 public TypeReference type;
23 public InstanceOfExpression(
24 Expression expression,
28 this.expression = expression;
30 this.bits |= operator << OperatorSHIFT;
31 this.sourceStart = expression.sourceStart;
32 this.sourceEnd = type.sourceEnd;
35 public FlowInfo analyseCode(
36 BlockScope currentScope,
37 FlowContext flowContext,
41 .analyseCode(currentScope, flowContext, flowInfo)
42 .unconditionalInits();
45 public final boolean areTypesCastCompatible(
48 TypeBinding expressionTb) {
50 // see specifications p.68
51 //A more cpmplete version of this method is provided on
52 //CastExpression (it deals with constant and need runtime checkcast)
54 //by grammatical construction, the first test is ALWAYS false
55 //if (castTb.isBaseType())
56 //{ if (expressionTb.isBaseType())
57 // { if (expression.isConstantValueOfTypeAssignableToType(expressionTb,castTb))
60 // { if (expressionTb==castTb)
63 // { if (scope.areTypesCompatible(expressionTb,castTb))
66 // if (BaseTypeBinding.isNarrowing(castTb.id,expressionTb.id))
72 { //-------------checkcast to something which is NOT a basetype----------------------------------
74 if (NullBinding == expressionTb)
75 //null is compatible with every thing ....
79 if (expressionTb.isArrayType()) {
80 if (castTb.isArrayType()) {
81 //------- (castTb.isArray) expressionTb.isArray -----------
82 TypeBinding expressionEltTb = ((ArrayBinding) expressionTb).elementsType(scope);
83 if (expressionEltTb.isBaseType())
84 // <---stop the recursion-------
85 return ((ArrayBinding) castTb).elementsType(scope) == expressionEltTb;
86 //recursivly on the elts...
87 return areTypesCastCompatible(
89 ((ArrayBinding) castTb).elementsType(scope),
92 if (castTb.isClass()) {
93 //------(castTb.isClass) expressionTb.isArray ---------------
94 if (scope.isJavaLangObject(castTb))
98 if (castTb.isInterface()) {
99 //------- (castTb.isInterface) expressionTb.isArray -----------
100 if (scope.isJavaLangCloneable(castTb) || scope.isJavaIoSerializable(castTb)) {
108 if (expressionTb.isBaseType()) {
111 if (expressionTb.isClass()) {
112 if (castTb.isArrayType()) {
113 // ---- (castTb.isArray) expressionTb.isClass -------
114 if (scope.isJavaLangObject(expressionTb)) {
120 if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------
121 if (scope.areTypesCompatible(expressionTb, castTb))
124 if (scope.areTypesCompatible(castTb, expressionTb)) {
130 if (castTb.isInterface()) {
131 // ----- (castTb.isInterface) expressionTb.isClass -------
132 if (((ReferenceBinding) expressionTb).isFinal()) {
133 //no subclass for expressionTb, thus compile-time check is valid
134 if (scope.areTypesCompatible(expressionTb, castTb))
144 if (expressionTb.isInterface()) {
145 if (castTb.isArrayType()) {
146 // ----- (castTb.isArray) expressionTb.isInterface ------
147 if (scope.isJavaLangCloneable(expressionTb)
148 || scope.isJavaIoSerializable(expressionTb))
149 //potential runtime error
155 if (castTb.isClass()) {
156 // ----- (castTb.isClass) expressionTb.isInterface --------
157 if (scope.isJavaLangObject(castTb))
159 if (((ReferenceBinding) castTb).isFinal()) {
160 //no subclass for castTb, thus compile-time check is valid
161 if (scope.areTypesCompatible(castTb, expressionTb)) {
168 if (castTb.isInterface()) {
169 // ----- (castTb.isInterface) expressionTb.isInterface -------
170 if (castTb != expressionTb
171 && (Scope.compareTypes(castTb, expressionTb) == NotRelated)) {
172 MethodBinding[] castTbMethods = ((ReferenceBinding) castTb).methods();
173 int castTbMethodsLength = castTbMethods.length;
174 MethodBinding[] expressionTbMethods =
175 ((ReferenceBinding) expressionTb).methods();
176 int expressionTbMethodsLength = expressionTbMethods.length;
177 for (int i = 0; i < castTbMethodsLength; i++) {
178 for (int j = 0; j < expressionTbMethodsLength; j++) {
179 if (castTbMethods[i].selector == expressionTbMethods[j].selector) {
180 if (castTbMethods[i].returnType != expressionTbMethods[j].returnType) {
181 if (castTbMethods[i].areParametersEqual(expressionTbMethods[j])) {
199 * Code generation for instanceOfExpression
201 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
202 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
203 * @param valueRequired boolean
205 public void generateCode(
206 BlockScope currentScope,
207 CodeStream codeStream,
208 boolean valueRequired) {
210 int pc = codeStream.position;
211 expression.generateCode(currentScope, codeStream, true);
212 codeStream.instance_of(type.binding);
215 codeStream.recordPositionsFrom(pc, this.sourceStart);
218 public TypeBinding resolveType(BlockScope scope) {
220 constant = NotAConstant;
221 TypeBinding expressionTb = expression.resolveType(scope);
222 TypeBinding checkTb = type.resolveType(scope);
223 if (expressionTb == null || checkTb == null)
226 if (!areTypesCastCompatible(scope, checkTb, expressionTb)) {
227 scope.problemReporter().notCompatibleTypesError(this, expressionTb, checkTb);
230 this.typeBinding = BooleanBinding;
231 return BooleanBinding;
234 public String toStringExpressionNoParenthesis() {
236 return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
240 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
242 if (visitor.visit(this, scope)) {
243 expression.traverse(visitor, scope);
244 type.traverse(visitor, scope);
246 visitor.endVisit(this, scope);