1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.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();
43 expression.checkNullStatus(currentScope, flowContext, flowInfo, FlowInfo.NON_NULL);
48 * Code generation for instanceOfExpression
50 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
51 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
52 * @param valueRequired boolean
54 public void generateCode(
55 BlockScope currentScope,
56 CodeStream codeStream,
57 boolean valueRequired) {
59 int pc = codeStream.position;
60 expression.generateCode(currentScope, codeStream, true);
61 codeStream.instance_of(type.resolvedType);
63 codeStream.generateImplicitConversion(implicitConversion);
67 codeStream.recordPositionsFrom(pc, this.sourceStart);
70 public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
72 expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
73 return type.print(0, output);
76 * @see org.eclipse.jdt.internal.compiler.ast.Expression#reportIllegalCast(org.eclipse.jdt.internal.compiler.lookup.Scope, org.eclipse.jdt.internal.compiler.lookup.TypeBinding, org.eclipse.jdt.internal.compiler.lookup.TypeBinding)
78 public void reportIllegalCast(Scope scope, TypeBinding castType, TypeBinding expressionType) {
79 scope.problemReporter().notCompatibleTypesError(this, expressionType, castType);
81 public TypeBinding resolveType(BlockScope scope) {
83 constant = NotAConstant;
84 TypeBinding expressionType = expression.resolveType(scope);
85 TypeBinding checkedType = type.resolveType(scope, true /* check bounds*/);
86 if (expressionType == null || checkedType == null)
89 if (checkedType.isTypeVariable() || checkedType.isBoundParameterizedType() || checkedType.isGenericType()) {
90 scope.problemReporter().illegalInstanceOfGenericType(checkedType, this);
92 checkCastTypesCompatibility(scope, checkedType, expressionType, null);
94 return this.resolvedType = BooleanBinding;
97 * @see org.eclipse.jdt.internal.compiler.ast.Expression#tagAsUnnecessaryCast(Scope,TypeBinding)
99 public void tagAsUnnecessaryCast(Scope scope, TypeBinding castType) {
100 scope.problemReporter().unnecessaryInstanceof(this, castType);
102 public void traverse(ASTVisitor visitor, BlockScope scope) {
104 if (visitor.visit(this, scope)) {
105 expression.traverse(visitor, scope);
106 type.traverse(visitor, scope);
108 visitor.endVisit(this, scope);