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.ArrayBinding;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
22 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 expressionTb) {
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 //by grammatical construction, the first test is ALWAYS false
61 //if (castTb.isBaseType())
62 //{ if (expressionTb.isBaseType())
63 // { if (expression.isConstantValueOfTypeAssignableToType(expressionTb,castTb))
66 // { if (expressionTb==castTb)
69 // { if (scope.areTypesCompatible(expressionTb,castTb))
72 // if (BaseTypeBinding.isNarrowing(castTb.id,expressionTb.id))
78 { //-------------checkcast to something which is NOT a basetype----------------------------------
80 if (NullBinding == expressionTb)
81 //null is compatible with every thing ....
85 if (expressionTb.isArrayType()) {
86 if (castTb.isArrayType()) {
87 //------- (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(
95 ((ArrayBinding) castTb).elementsType(scope),
98 if (castTb.isClass()) {
99 //------(castTb.isClass) expressionTb.isArray ---------------
100 if (scope.isJavaLangObject(castTb))
104 if (castTb.isInterface()) {
105 //------- (castTb.isInterface) expressionTb.isArray -----------
106 if (scope.isJavaLangCloneable(castTb) || scope.isJavaIoSerializable(castTb)) {
114 if (expressionTb.isBaseType()) {
117 if (expressionTb.isClass()) {
118 if (castTb.isArrayType()) {
119 // ---- (castTb.isArray) expressionTb.isClass -------
120 if (scope.isJavaLangObject(expressionTb)) {
126 if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------
127 if (BlockScope.areTypesCompatible(expressionTb, castTb))
130 if (BlockScope.areTypesCompatible(castTb, expressionTb)) {
136 if (castTb.isInterface()) {
137 // ----- (castTb.isInterface) expressionTb.isClass -------
138 if (((ReferenceBinding) expressionTb).isFinal()) {
139 //no subclass for expressionTb, thus compile-time check is valid
140 if (BlockScope.areTypesCompatible(expressionTb, castTb))
150 if (expressionTb.isInterface()) {
151 if (castTb.isArrayType()) {
152 // ----- (castTb.isArray) expressionTb.isInterface ------
153 if (scope.isJavaLangCloneable(expressionTb)
154 || scope.isJavaIoSerializable(expressionTb))
155 //potential runtime error
161 if (castTb.isClass()) {
162 // ----- (castTb.isClass) expressionTb.isInterface --------
163 if (scope.isJavaLangObject(castTb))
165 if (((ReferenceBinding) castTb).isFinal()) {
166 //no subclass for castTb, thus compile-time check is valid
167 if (BlockScope.areTypesCompatible(castTb, expressionTb)) {
174 if (castTb.isInterface()) {
175 // ----- (castTb.isInterface) expressionTb.isInterface -------
176 if (castTb != expressionTb
177 && (Scope.compareTypes(castTb, expressionTb) == NotRelated)) {
178 MethodBinding[] castTbMethods = ((ReferenceBinding) castTb).methods();
179 int castTbMethodsLength = castTbMethods.length;
180 MethodBinding[] expressionTbMethods =
181 ((ReferenceBinding) expressionTb).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 org.eclipse.jdt.internal.compiler.lookup.BlockScope
208 * @param codeStream org.eclipse.jdt.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.binding);
221 codeStream.recordPositionsFrom(pc, this.sourceStart);
224 public TypeBinding resolveType(BlockScope scope) {
226 constant = NotAConstant;
227 TypeBinding expressionTb = expression.resolveType(scope);
228 TypeBinding checkTb = type.resolveType(scope);
229 if (expressionTb == null || checkTb == null)
232 if (!areTypesCastCompatible(scope, checkTb, expressionTb)) {
233 scope.problemReporter().notCompatibleTypesError(this, expressionTb, checkTb);
236 this.typeBinding = BooleanBinding;
237 return BooleanBinding;
240 public String toStringExpressionNoParenthesis() {
242 return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
246 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
248 if (visitor.visit(this, scope)) {
249 expression.traverse(visitor, scope);
250 type.traverse(visitor, scope);
252 visitor.endVisit(this, scope);