Removed some internal constants
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / internal / compiler / ast / InstanceOfExpression.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpeclipse.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
16 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
19 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
22
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 castType,
54                 TypeBinding expressionType) {
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                 if (castType == expressionType) return true;
61                 
62                 //by grammatical construction, the first test is ALWAYS false
63                 //if (castTb.isBaseType())
64                 //{     if (expressionTb.isBaseType())
65                 //      {       if (expression.isConstantValueOfTypeAssignableToType(expressionTb,castTb))
66                 //              {       return true;}
67                 //              else
68                 //              {       if (expressionTb==castTb)
69                 //                      {       return true;}
70                 //                      else 
71                 //                      {       if (scope.areTypesCompatible(expressionTb,castTb))
72                 //                              {       return true; }
73                 //                              
74                 //                              if (BaseTypeBinding.isNarrowing(castTb.id,expressionTb.id))
75                 //                              {       return true;}
76                 //                              return false;}}}
77                 //      else
78                 //      {       return false; }}
79                 //else
80                 { //-------------checkcast to something which is NOT a basetype----------------------------------       
81
82                         //null is compatible with every thing .... 
83                         if (NullBinding == expressionType) {
84                                 return true;
85                         }
86                         if (expressionType.isArrayType()) {
87                                 if (castType.isArrayType()) {
88                                         //------- (castTb.isArray) expressionTb.isArray -----------
89                                         TypeBinding expressionEltTb = ((ArrayBinding) expressionType).elementsType(scope);
90                                         if (expressionEltTb.isBaseType())
91                                                 // <---stop the recursion------- 
92                                                 return ((ArrayBinding) castType).elementsType(scope) == expressionEltTb;
93                                         //recursivly on the elts...
94                                         return areTypesCastCompatible(
95                                                 scope,
96                                                 ((ArrayBinding) castType).elementsType(scope),
97                                                 expressionEltTb);
98                                 }
99                                 if (castType.isClass()) {
100                                         //------(castTb.isClass) expressionTb.isArray ---------------   
101                                         if (scope.isJavaLangObject(castType))
102                                                 return true;
103                                         return false;
104                                 }
105                                 if (castType.isInterface()) {
106                                         //------- (castTb.isInterface) expressionTb.isArray -----------
107                                         if (scope.isJavaLangCloneable(castType) || scope.isJavaIoSerializable(castType)) {
108                                                 return true;
109                                         }
110                                         return false;
111                                 }
112
113                                 return false;
114                         }
115                         if (expressionType.isBaseType()) {
116                                 return false;
117                         }
118                         if (expressionType.isClass()) {
119                                 if (castType.isArrayType()) {
120                                         // ---- (castTb.isArray) expressionTb.isClass -------
121                                         if (scope.isJavaLangObject(expressionType)) {
122                                                 return true;
123                                         } else {
124                                                 return false;
125                                         }
126                                 }
127                                 if (castType.isClass()) { // ----- (castTb.isClass) expressionTb.isClass ------ 
128                                         if (expressionType.isCompatibleWith(castType))
129                                                 return true;
130                                         else {
131                                                 if (castType.isCompatibleWith(expressionType)) {
132                                                         return true;
133                                                 }
134                                                 return false;
135                                         }
136                                 }
137                                 if (castType.isInterface()) {
138                                         // ----- (castTb.isInterface) expressionTb.isClass -------  
139                                         if (((ReferenceBinding) expressionType).isFinal()) {
140                                                 //no subclass for expressionTb, thus compile-time check is valid
141                                                 if (expressionType.isCompatibleWith(castType))
142                                                         return true;
143                                                 return false;
144                                         } else {
145                                                 return true;
146                                         }
147                                 }
148
149                                 return false;
150                         }
151                         if (expressionType.isInterface()) {
152                                 if (castType.isArrayType()) {
153                                         // ----- (castTb.isArray) expressionTb.isInterface ------
154                                         if (scope.isJavaLangCloneable(expressionType)
155                                                 || scope.isJavaIoSerializable(expressionType))
156                                                 //potential runtime error
157                                                 {
158                                                 return true;
159                                         }
160                                         return false;
161                                 }
162                                 if (castType.isClass()) {
163                                         // ----- (castTb.isClass) expressionTb.isInterface --------
164                                         if (scope.isJavaLangObject(castType))
165                                                 return true;
166                                         if (((ReferenceBinding) castType).isFinal()) {
167                                                 //no subclass for castTb, thus compile-time check is valid
168                                                 if (castType.isCompatibleWith(expressionType)) {
169                                                         return true;
170                                                 }
171                                                 return false;
172                                         }
173                                         return true;
174                                 }
175                                 if (castType.isInterface()) {
176                                         // ----- (castTb.isInterface) expressionTb.isInterface -------
177                                         if ((Scope.compareTypes(castType, expressionType) == NotRelated)) {
178                                                 MethodBinding[] castTbMethods = ((ReferenceBinding) castType).methods();
179                                                 int castTbMethodsLength = castTbMethods.length;
180                                                 MethodBinding[] expressionTbMethods =
181                                                         ((ReferenceBinding) expressionType).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 net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
208          * @param codeStream net.sourceforge.phpdt.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.resolvedType);
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 expressionType = expression.resolveType(scope);
228                 TypeBinding checkType = type.resolveType(scope);
229                 if (expressionType == null || checkType == null)
230                         return null;
231
232                 if (!areTypesCastCompatible(scope, checkType, expressionType)) {
233                         scope.problemReporter().notCompatibleTypesError(this, expressionType, checkType);
234                         return null;
235                 }
236                 this.resolvedType = BooleanBinding;
237                 return BooleanBinding;
238         }
239         public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
240
241                 expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
242                 return type.print(0, output);
243         }
244         public String toStringExpressionNoParenthesis() {
245
246                 return expression.toStringExpression() + " instanceof " + //$NON-NLS-1$
247                 type.toString(0);
248         }
249
250         public void traverse(ASTVisitor visitor, BlockScope scope) {
251
252                 if (visitor.visit(this, scope)) {
253                         expression.traverse(visitor, scope);
254                         type.traverse(visitor, scope);
255                 }
256                 visitor.endVisit(this, scope);
257         }
258 }