added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / InstanceOfExpression.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
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.*;
17
18 public class InstanceOfExpression extends OperatorExpression {
19
20         public Expression expression;
21         public TypeReference type;
22
23         public InstanceOfExpression(
24                 Expression expression,
25                 TypeReference type,
26                 int operator) {
27
28                 this.expression = expression;
29                 this.type = type;
30                 this.bits |= operator << OperatorSHIFT;
31                 this.sourceStart = expression.sourceStart;
32                 this.sourceEnd = type.sourceEnd;
33         }
34
35         public FlowInfo analyseCode(
36                 BlockScope currentScope,
37                 FlowContext flowContext,
38                 FlowInfo flowInfo) {
39
40                 flowInfo = expression
41                         .analyseCode(currentScope, flowContext, flowInfo)
42                         .unconditionalInits();
43                 expression.checkNullStatus(currentScope, flowContext, flowInfo, FlowInfo.NON_NULL);
44                 return flowInfo;
45         }
46
47         /**
48          * Code generation for instanceOfExpression
49          *
50          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
51          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
52          * @param valueRequired boolean
53         */
54         public void generateCode(
55                 BlockScope currentScope,
56                 CodeStream codeStream,
57                 boolean valueRequired) {
58
59                 int pc = codeStream.position;
60                 expression.generateCode(currentScope, codeStream, true);
61                 codeStream.instance_of(type.resolvedType);
62                 if (valueRequired) {
63                         codeStream.generateImplicitConversion(implicitConversion);
64                 } else {
65                         codeStream.pop();
66                 }
67                 codeStream.recordPositionsFrom(pc, this.sourceStart);
68         }
69
70         public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
71
72                 expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
73                 return type.print(0, output);
74         }
75         /**
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)
77          */
78         public void reportIllegalCast(Scope scope, TypeBinding castType, TypeBinding expressionType) {
79                 scope.problemReporter().notCompatibleTypesError(this, expressionType, castType);
80         }
81         public TypeBinding resolveType(BlockScope scope) {
82
83                 constant = NotAConstant;
84                 TypeBinding expressionType = expression.resolveType(scope);
85                 TypeBinding checkedType = type.resolveType(scope, true /* check bounds*/);
86                 if (expressionType == null || checkedType == null)
87                         return null;
88
89                 if (checkedType.isTypeVariable() || checkedType.isBoundParameterizedType() || checkedType.isGenericType()) {
90                         scope.problemReporter().illegalInstanceOfGenericType(checkedType, this);
91                 } else {
92                         checkCastTypesCompatibility(scope, checkedType, expressionType, null);
93                 }
94                 return this.resolvedType = BooleanBinding;
95         }
96         /**
97          * @see org.eclipse.jdt.internal.compiler.ast.Expression#tagAsUnnecessaryCast(Scope,TypeBinding)
98          */
99         public void tagAsUnnecessaryCast(Scope scope, TypeBinding castType) {
100                 scope.problemReporter().unnecessaryInstanceof(this, castType);
101         }
102         public void traverse(ASTVisitor visitor, BlockScope scope) {
103
104                 if (visitor.visit(this, scope)) {
105                         expression.traverse(visitor, scope);
106                         type.traverse(visitor, scope);
107                 }
108                 visitor.endVisit(this, scope);
109         }
110 }