*** empty log message ***
[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.*;
15 import net.sourceforge.phpdt.internal.compiler.flow.*;
16 import net.sourceforge.phpdt.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                 return expression
41                         .analyseCode(currentScope, flowContext, flowInfo)
42                         .unconditionalInits();
43         }
44
45         public final boolean areTypesCastCompatible(
46                 BlockScope scope,
47                 TypeBinding castTb,
48                 TypeBinding expressionTb) {
49
50                 //      see specifications p.68
51                 //A more cpmplete version of this method is provided on
52                 //CastExpression (it deals with constant and need runtime checkcast)
53
54                 //by grammatical construction, the first test is ALWAYS false
55                 //if (castTb.isBaseType())
56                 //{     if (expressionTb.isBaseType())
57                 //      {       if (expression.isConstantValueOfTypeAssignableToType(expressionTb,castTb))
58                 //              {       return true;}
59                 //              else
60                 //              {       if (expressionTb==castTb)
61                 //                      {       return true;}
62                 //                      else 
63                 //                      {       if (scope.areTypesCompatible(expressionTb,castTb))
64                 //                              {       return true; }
65                 //                              
66                 //                              if (BaseTypeBinding.isNarrowing(castTb.id,expressionTb.id))
67                 //                              {       return true;}
68                 //                              return false;}}}
69                 //      else
70                 //      {       return false; }}
71                 //else
72                 { //-------------checkcast to something which is NOT a basetype----------------------------------       
73
74                         if (NullBinding == expressionTb)
75                                 //null is compatible with every thing .... 
76                                 {
77                                 return true;
78                         }
79                         if (expressionTb.isArrayType()) {
80                                 if (castTb.isArrayType()) {
81                                         //------- (castTb.isArray) expressionTb.isArray -----------
82                                         TypeBinding expressionEltTb = ((ArrayBinding) expressionTb).elementsType(scope);
83                                         if (expressionEltTb.isBaseType())
84                                                 // <---stop the recursion------- 
85                                                 return ((ArrayBinding) castTb).elementsType(scope) == expressionEltTb;
86                                         //recursivly on the elts...
87                                         return areTypesCastCompatible(
88                                                 scope,
89                                                 ((ArrayBinding) castTb).elementsType(scope),
90                                                 expressionEltTb);
91                                 }
92                                 if (castTb.isClass()) {
93                                         //------(castTb.isClass) expressionTb.isArray ---------------   
94                                         if (scope.isJavaLangObject(castTb))
95                                                 return true;
96                                         return false;
97                                 }
98                                 if (castTb.isInterface()) {
99                                         //------- (castTb.isInterface) expressionTb.isArray -----------
100                                         if (scope.isJavaLangCloneable(castTb) || scope.isJavaIoSerializable(castTb)) {
101                                                 return true;
102                                         }
103                                         return false;
104                                 }
105
106                                 return false;
107                         }
108                         if (expressionTb.isBaseType()) {
109                                 return false;
110                         }
111                         if (expressionTb.isClass()) {
112                                 if (castTb.isArrayType()) {
113                                         // ---- (castTb.isArray) expressionTb.isClass -------
114                                         if (scope.isJavaLangObject(expressionTb)) {
115                                                 return true;
116                                         } else {
117                                                 return false;
118                                         }
119                                 }
120                                 if (castTb.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------ 
121                                         if (scope.areTypesCompatible(expressionTb, castTb))
122                                                 return true;
123                                         else {
124                                                 if (scope.areTypesCompatible(castTb, expressionTb)) {
125                                                         return true;
126                                                 }
127                                                 return false;
128                                         }
129                                 }
130                                 if (castTb.isInterface()) {
131                                         // ----- (castTb.isInterface) expressionTb.isClass -------  
132                                         if (((ReferenceBinding) expressionTb).isFinal()) {
133                                                 //no subclass for expressionTb, thus compile-time check is valid
134                                                 if (scope.areTypesCompatible(expressionTb, castTb))
135                                                         return true;
136                                                 return false;
137                                         } else {
138                                                 return true;
139                                         }
140                                 }
141
142                                 return false;
143                         }
144                         if (expressionTb.isInterface()) {
145                                 if (castTb.isArrayType()) {
146                                         // ----- (castTb.isArray) expressionTb.isInterface ------
147                                         if (scope.isJavaLangCloneable(expressionTb)
148                                                 || scope.isJavaIoSerializable(expressionTb))
149                                                 //potential runtime error
150                                                 {
151                                                 return true;
152                                         }
153                                         return false;
154                                 }
155                                 if (castTb.isClass()) {
156                                         // ----- (castTb.isClass) expressionTb.isInterface --------
157                                         if (scope.isJavaLangObject(castTb))
158                                                 return true;
159                                         if (((ReferenceBinding) castTb).isFinal()) {
160                                                 //no subclass for castTb, thus compile-time check is valid
161                                                 if (scope.areTypesCompatible(castTb, expressionTb)) {
162                                                         return true;
163                                                 }
164                                                 return false;
165                                         }
166                                         return true;
167                                 }
168                                 if (castTb.isInterface()) {
169                                         // ----- (castTb.isInterface) expressionTb.isInterface -------
170                                         if (castTb != expressionTb
171                                                 && (Scope.compareTypes(castTb, expressionTb) == NotRelated)) {
172                                                 MethodBinding[] castTbMethods = ((ReferenceBinding) castTb).methods();
173                                                 int castTbMethodsLength = castTbMethods.length;
174                                                 MethodBinding[] expressionTbMethods =
175                                                         ((ReferenceBinding) expressionTb).methods();
176                                                 int expressionTbMethodsLength = expressionTbMethods.length;
177                                                 for (int i = 0; i < castTbMethodsLength; i++) {
178                                                         for (int j = 0; j < expressionTbMethodsLength; j++) {
179                                                                 if (castTbMethods[i].selector == expressionTbMethods[j].selector) {
180                                                                         if (castTbMethods[i].returnType != expressionTbMethods[j].returnType) {
181                                                                                 if (castTbMethods[i].areParametersEqual(expressionTbMethods[j])) {
182                                                                                         return false;
183                                                                                 }
184                                                                         }
185                                                                 }
186                                                         }
187                                                 }
188                                         }
189                                         return true;
190                                 }
191
192                                 return false;
193                         } 
194
195                         return false;
196                 }
197         }
198         /**
199          * Code generation for instanceOfExpression
200          *
201          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
202          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
203          * @param valueRequired boolean
204         */
205         public void generateCode(
206                 BlockScope currentScope,
207                 CodeStream codeStream,
208                 boolean valueRequired) {
209
210                 int pc = codeStream.position;
211                 expression.generateCode(currentScope, codeStream, true);
212                 codeStream.instance_of(type.binding);
213                 if (!valueRequired)
214                         codeStream.pop();
215                 codeStream.recordPositionsFrom(pc, this.sourceStart);
216         }
217
218         public TypeBinding resolveType(BlockScope scope) {
219
220                 constant = NotAConstant;
221                 TypeBinding expressionTb = expression.resolveType(scope);
222                 TypeBinding checkTb = type.resolveType(scope);
223                 if (expressionTb == null || checkTb == null)
224                         return null;
225
226                 if (!areTypesCastCompatible(scope, checkTb, expressionTb)) {
227                         scope.problemReporter().notCompatibleTypesError(this, expressionTb, checkTb);
228                         return null;
229                 }
230                 this.typeBinding = BooleanBinding;
231                 return BooleanBinding;
232         }
233
234         public String toStringExpressionNoParenthesis() {
235
236                 return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
237                 type.toString(0);
238         }
239
240         public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
241
242                 if (visitor.visit(this, scope)) {
243                         expression.traverse(visitor, scope);
244                         type.traverse(visitor, scope);
245                 }
246                 visitor.endVisit(this, scope);
247         }
248 }