/*******************************************************************************
- * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v0.5
+ * are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v05.html
+ * http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
- ******************************************************************************/
+ *******************************************************************************/
package net.sourceforge.phpdt.internal.compiler.ast;
-import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
-import net.sourceforge.phpdt.internal.compiler.codegen.*;
-import net.sourceforge.phpdt.internal.compiler.flow.*;
-import net.sourceforge.phpdt.internal.compiler.lookup.*;
+import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
+import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
+import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
+import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
+import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
+import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
+import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
+import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
+import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
+
public class InstanceOfExpression extends OperatorExpression {
public final boolean areTypesCastCompatible(
BlockScope scope,
- TypeBinding castTb,
- TypeBinding expressionTb) {
+ TypeBinding castType,
+ TypeBinding expressionType) {
// see specifications p.68
//A more cpmplete version of this method is provided on
//CastExpression (it deals with constant and need runtime checkcast)
+ if (castType == expressionType) return true;
+
//by grammatical construction, the first test is ALWAYS false
//if (castTb.isBaseType())
//{ if (expressionTb.isBaseType())
//else
{ //-------------checkcast to something which is NOT a basetype----------------------------------
- if (NullBinding == expressionTb)
- //null is compatible with every thing ....
- {
+ //null is compatible with every thing ....
+ if (NullBinding == expressionType) {
return true;
}
- if (expressionTb.isArrayType()) {
- if (castTb.isArrayType()) {
+ if (expressionType.isArrayType()) {
+ if (castType.isArrayType()) {
//------- (castTb.isArray) expressionTb.isArray -----------
- TypeBinding expressionEltTb = ((ArrayBinding) expressionTb).elementsType(scope);
+ TypeBinding expressionEltTb = ((ArrayBinding) expressionType).elementsType(scope);
if (expressionEltTb.isBaseType())
// <---stop the recursion-------
- return ((ArrayBinding) castTb).elementsType(scope) == expressionEltTb;
+ return ((ArrayBinding) castType).elementsType(scope) == expressionEltTb;
//recursivly on the elts...
return areTypesCastCompatible(
scope,
- ((ArrayBinding) castTb).elementsType(scope),
+ ((ArrayBinding) castType).elementsType(scope),
expressionEltTb);
}
- if (castTb.isClass()) {
+ if (castType.isClass()) {
//------(castTb.isClass) expressionTb.isArray ---------------
- if (scope.isJavaLangObject(castTb))
+ if (scope.isJavaLangObject(castType))
return true;
return false;
}
- if (castTb.isInterface()) {
+ if (castType.isInterface()) {
//------- (castTb.isInterface) expressionTb.isArray -----------
- if (scope.isJavaLangCloneable(castTb) || scope.isJavaIoSerializable(castTb)) {
+ if (scope.isJavaLangCloneable(castType) || scope.isJavaIoSerializable(castType)) {
return true;
}
return false;
return false;
}
- if (expressionTb.isBaseType()) {
+ if (expressionType.isBaseType()) {
return false;
}
- if (expressionTb.isClass()) {
- if (castTb.isArrayType()) {
+ if (expressionType.isClass()) {
+ if (castType.isArrayType()) {
// ---- (castTb.isArray) expressionTb.isClass -------
- if (scope.isJavaLangObject(expressionTb)) {
+ if (scope.isJavaLangObject(expressionType)) {
return true;
} else {
return false;
}
}
- if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------
- if (scope.areTypesCompatible(expressionTb, castTb))
+ if (castType.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------
+ if (expressionType.isCompatibleWith(castType))
return true;
else {
- if (scope.areTypesCompatible(castTb, expressionTb)) {
+ if (castType.isCompatibleWith(expressionType)) {
return true;
}
return false;
}
}
- if (castTb.isInterface()) {
+ if (castType.isInterface()) {
// ----- (castTb.isInterface) expressionTb.isClass -------
- if (((ReferenceBinding) expressionTb).isFinal()) {
+ if (((ReferenceBinding) expressionType).isFinal()) {
//no subclass for expressionTb, thus compile-time check is valid
- if (scope.areTypesCompatible(expressionTb, castTb))
+ if (expressionType.isCompatibleWith(castType))
return true;
return false;
} else {
return false;
}
- if (expressionTb.isInterface()) {
- if (castTb.isArrayType()) {
+ if (expressionType.isInterface()) {
+ if (castType.isArrayType()) {
// ----- (castTb.isArray) expressionTb.isInterface ------
- if (scope.isJavaLangCloneable(expressionTb)
- || scope.isJavaIoSerializable(expressionTb))
+ if (scope.isJavaLangCloneable(expressionType)
+ || scope.isJavaIoSerializable(expressionType))
//potential runtime error
{
return true;
}
return false;
}
- if (castTb.isClass()) {
+ if (castType.isClass()) {
// ----- (castTb.isClass) expressionTb.isInterface --------
- if (scope.isJavaLangObject(castTb))
+ if (scope.isJavaLangObject(castType))
return true;
- if (((ReferenceBinding) castTb).isFinal()) {
+ if (((ReferenceBinding) castType).isFinal()) {
//no subclass for castTb, thus compile-time check is valid
- if (scope.areTypesCompatible(castTb, expressionTb)) {
+ if (castType.isCompatibleWith(expressionType)) {
return true;
}
return false;
}
return true;
}
- if (castTb.isInterface()) {
+ if (castType.isInterface()) {
// ----- (castTb.isInterface) expressionTb.isInterface -------
- if (castTb != expressionTb
- && (Scope.compareTypes(castTb, expressionTb) == NotRelated)) {
- MethodBinding[] castTbMethods = ((ReferenceBinding) castTb).methods();
+ if ((Scope.compareTypes(castType, expressionType) == NotRelated)) {
+ MethodBinding[] castTbMethods = ((ReferenceBinding) castType).methods();
int castTbMethodsLength = castTbMethods.length;
MethodBinding[] expressionTbMethods =
- ((ReferenceBinding) expressionTb).methods();
+ ((ReferenceBinding) expressionType).methods();
int expressionTbMethodsLength = expressionTbMethods.length;
for (int i = 0; i < castTbMethodsLength; i++) {
for (int j = 0; j < expressionTbMethodsLength; j++) {
/**
* Code generation for instanceOfExpression
*
- * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
- * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
+ * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
+ * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
* @param valueRequired boolean
*/
- public void generateCode(
- BlockScope currentScope,
- CodeStream codeStream,
- boolean valueRequired) {
-
- int pc = codeStream.position;
- expression.generateCode(currentScope, codeStream, true);
- codeStream.instance_of(type.binding);
- if (!valueRequired)
- codeStream.pop();
- codeStream.recordPositionsFrom(pc, this.sourceStart);
- }
+// public void generateCode(
+// BlockScope currentScope,
+// CodeStream codeStream,
+// boolean valueRequired) {
+//
+// int pc = codeStream.position;
+// expression.generateCode(currentScope, codeStream, true);
+// codeStream.instance_of(type.resolvedType);
+// if (!valueRequired)
+// codeStream.pop();
+// codeStream.recordPositionsFrom(pc, this.sourceStart);
+// }
public TypeBinding resolveType(BlockScope scope) {
constant = NotAConstant;
- TypeBinding expressionTb = expression.resolveType(scope);
- TypeBinding checkTb = type.resolveType(scope);
- if (expressionTb == null || checkTb == null)
+ TypeBinding expressionType = expression.resolveType(scope);
+ TypeBinding checkType = type.resolveType(scope);
+ if (expressionType == null || checkType == null)
return null;
- if (!areTypesCastCompatible(scope, checkTb, expressionTb)) {
- scope.problemReporter().notCompatibleTypesError(this, expressionTb, checkTb);
+ if (!areTypesCastCompatible(scope, checkType, expressionType)) {
+ scope.problemReporter().notCompatibleTypesError(this, expressionType, checkType);
return null;
}
- this.typeBinding = BooleanBinding;
+ this.resolvedType = BooleanBinding;
return BooleanBinding;
}
+ public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
+ expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
+ return type.print(0, output);
+ }
public String toStringExpressionNoParenthesis() {
return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
type.toString(0);
}
- public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
+ public void traverse(ASTVisitor visitor, BlockScope scope) {
if (visitor.visit(this, scope)) {
expression.traverse(visitor, scope);
}
visitor.endVisit(this, scope);
}
-}
\ No newline at end of file
+}