fixed accelerator problem; slightly improved PHP Perspective
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / InstanceOfExpression.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
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;
23
24 public class InstanceOfExpression extends OperatorExpression {
25
26         public Expression expression;
27         public TypeReference type;
28
29         public InstanceOfExpression(
30                 Expression expression,
31                 TypeReference type,
32                 int operator) {
33
34                 this.expression = expression;
35                 this.type = type;
36                 this.bits |= operator << OperatorSHIFT;
37                 this.sourceStart = expression.sourceStart;
38                 this.sourceEnd = type.sourceEnd;
39         }
40
41         public FlowInfo analyseCode(
42                 BlockScope currentScope,
43                 FlowContext flowContext,
44                 FlowInfo flowInfo) {
45
46                 return expression
47                         .analyseCode(currentScope, flowContext, flowInfo)
48                         .unconditionalInits();
49         }
50
51         public final boolean areTypesCastCompatible(
52                 BlockScope scope,
53                 TypeBinding castTb,
54                 TypeBinding expressionTb) {
55
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)
59
60                 //by grammatical construction, the first test is ALWAYS false
61                 //if (castTb.isBaseType())
62                 //{     if (expressionTb.isBaseType())
63                 //      {       if (expression.isConstantValueOfTypeAssignableToType(expressionTb,castTb))
64                 //              {       return true;}
65                 //              else
66                 //              {       if (expressionTb==castTb)
67                 //                      {       return true;}
68                 //                      else 
69                 //                      {       if (scope.areTypesCompatible(expressionTb,castTb))
70                 //                              {       return true; }
71                 //                              
72                 //                              if (BaseTypeBinding.isNarrowing(castTb.id,expressionTb.id))
73                 //                              {       return true;}
74                 //                              return false;}}}
75                 //      else
76                 //      {       return false; }}
77                 //else
78                 { //-------------checkcast to something which is NOT a basetype----------------------------------       
79
80                         if (NullBinding == expressionTb)
81                                 //null is compatible with every thing .... 
82                                 {
83                                 return true;
84                         }
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(
94                                                 scope,
95                                                 ((ArrayBinding) castTb).elementsType(scope),
96                                                 expressionEltTb);
97                                 }
98                                 if (castTb.isClass()) {
99                                         //------(castTb.isClass) expressionTb.isArray ---------------   
100                                         if (scope.isJavaLangObject(castTb))
101                                                 return true;
102                                         return false;
103                                 }
104                                 if (castTb.isInterface()) {
105                                         //------- (castTb.isInterface) expressionTb.isArray -----------
106                                         if (scope.isJavaLangCloneable(castTb) || scope.isJavaIoSerializable(castTb)) {
107                                                 return true;
108                                         }
109                                         return false;
110                                 }
111
112                                 return false;
113                         }
114                         if (expressionTb.isBaseType()) {
115                                 return false;
116                         }
117                         if (expressionTb.isClass()) {
118                                 if (castTb.isArrayType()) {
119                                         // ---- (castTb.isArray) expressionTb.isClass -------
120                                         if (scope.isJavaLangObject(expressionTb)) {
121                                                 return true;
122                                         } else {
123                                                 return false;
124                                         }
125                                 }
126                                 if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------ 
127                                         if (BlockScope.areTypesCompatible(expressionTb, castTb))
128                                                 return true;
129                                         else {
130                                                 if (BlockScope.areTypesCompatible(castTb, expressionTb)) {
131                                                         return true;
132                                                 }
133                                                 return false;
134                                         }
135                                 }
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))
141                                                         return true;
142                                                 return false;
143                                         } else {
144                                                 return true;
145                                         }
146                                 }
147
148                                 return false;
149                         }
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
156                                                 {
157                                                 return true;
158                                         }
159                                         return false;
160                                 }
161                                 if (castTb.isClass()) {
162                                         // ----- (castTb.isClass) expressionTb.isInterface --------
163                                         if (scope.isJavaLangObject(castTb))
164                                                 return true;
165                                         if (((ReferenceBinding) castTb).isFinal()) {
166                                                 //no subclass for castTb, thus compile-time check is valid
167                                                 if (BlockScope.areTypesCompatible(castTb, expressionTb)) {
168                                                         return true;
169                                                 }
170                                                 return false;
171                                         }
172                                         return true;
173                                 }
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])) {
188                                                                                         return false;
189                                                                                 }
190                                                                         }
191                                                                 }
192                                                         }
193                                                 }
194                                         }
195                                         return true;
196                                 }
197
198                                 return false;
199                         } 
200
201                         return false;
202                 }
203         }
204         /**
205          * Code generation for instanceOfExpression
206          *
207          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
208          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
209          * @param valueRequired boolean
210         */
211         public void generateCode(
212                 BlockScope currentScope,
213                 CodeStream codeStream,
214                 boolean valueRequired) {
215
216                 int pc = codeStream.position;
217                 expression.generateCode(currentScope, codeStream, true);
218                 codeStream.instance_of(type.binding);
219                 if (!valueRequired)
220                         codeStream.pop();
221                 codeStream.recordPositionsFrom(pc, this.sourceStart);
222         }
223
224         public TypeBinding resolveType(BlockScope scope) {
225
226                 constant = NotAConstant;
227                 TypeBinding expressionTb = expression.resolveType(scope);
228                 TypeBinding checkTb = type.resolveType(scope);
229                 if (expressionTb == null || checkTb == null)
230                         return null;
231
232                 if (!areTypesCastCompatible(scope, checkTb, expressionTb)) {
233                         scope.problemReporter().notCompatibleTypesError(this, expressionTb, checkTb);
234                         return null;
235                 }
236                 this.typeBinding = BooleanBinding;
237                 return BooleanBinding;
238         }
239
240         public String toStringExpressionNoParenthesis() {
241
242                 return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
243                 type.toString(0);
244         }
245
246         public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
247
248                 if (visitor.visit(this, scope)) {
249                         expression.traverse(visitor, scope);
250                         type.traverse(visitor, scope);
251                 }
252                 visitor.endVisit(this, scope);
253         }
254 }