improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / MethodBinding.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.phpdt.internal.compiler.lookup;
12
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
15 import net.sourceforge.phpeclipse.internal.compiler.ast.ConstructorDeclaration;
16
17 public class MethodBinding extends Binding implements BaseTypes, TypeConstants {
18         public int modifiers;
19         public char[] selector;
20         public TypeBinding returnType;
21         public TypeBinding[] parameters;
22         public ReferenceBinding[] thrownExceptions;
23         public ReferenceBinding declaringClass;
24
25         char[] signature;
26
27 protected MethodBinding() {
28 }
29 public MethodBinding(int modifiers, char[] selector, TypeBinding returnType, TypeBinding[] args, ReferenceBinding[] exceptions, ReferenceBinding declaringClass) {
30         this.modifiers = modifiers;
31         this.selector = selector;
32         this.returnType = returnType;
33         this.parameters = (args == null || args.length == 0) ? NoParameters : args;
34         this.thrownExceptions = (exceptions == null || exceptions.length == 0) ? NoExceptions : exceptions;
35         this.declaringClass = declaringClass;
36
37         // propagate the strictfp & deprecated modifiers
38         if (this.declaringClass != null) {
39 //              if (this.declaringClass.isStrictfp())
40 //                      if (!(isNative() || isAbstract()))
41 //                              this.modifiers |= AccStrictfp;
42                 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
43                         this.modifiers |= AccDeprecatedImplicitly;
44         }
45 }
46 public MethodBinding(int modifiers, TypeBinding[] args, ReferenceBinding[] exceptions, ReferenceBinding declaringClass) {
47         this(modifiers, ConstructorDeclaration.ConstantPoolName, VoidBinding, args, exceptions, declaringClass);
48 }
49 // special API used to change method declaring class for runtime visibility check
50 public MethodBinding(MethodBinding initialMethodBinding, ReferenceBinding declaringClass) {
51         this.modifiers = initialMethodBinding.modifiers;
52         this.selector = initialMethodBinding.selector;
53         this.returnType = initialMethodBinding.returnType;
54         this.parameters = initialMethodBinding.parameters;
55         this.thrownExceptions = initialMethodBinding.thrownExceptions;
56         this.declaringClass = declaringClass;
57 }
58 /* Answer true if the argument types & the receiver's parameters are equal
59 */
60
61 public final boolean areParametersEqual(MethodBinding method) {
62         TypeBinding[] args = method.parameters;
63         if (parameters == args)
64                 return true;
65
66         int length = parameters.length;
67         if (length != args.length)
68                 return false;
69         
70         for (int i = 0; i < length; i++)
71                 if (parameters[i] != args[i])
72                         return false;
73         return true;
74 }
75 /* API
76 * Answer the receiver's binding type from Binding.BindingID.
77 */
78
79 public final int bindingType() {
80         return METHOD;
81 }
82 /* Answer true if the receiver is visible to the type provided by the scope.
83 * InvocationSite implements isSuperAccess() to provide additional information
84 * if the receiver is protected.
85 *
86 * NOTE: This method should ONLY be sent if the receiver is a constructor.
87 *
88 * NOTE: Cannot invoke this method with a compilation unit scope.
89 */
90
91 public final boolean canBeSeenBy(InvocationSite invocationSite, Scope scope) {
92         if (isPublic()) return true;
93
94         SourceTypeBinding invocationType = scope.enclosingSourceType();
95         if (invocationType == declaringClass) return true;
96
97         if (isProtected()) {
98                 // answer true if the receiver is in the same package as the invocationType
99                 if (invocationType.fPackage == declaringClass.fPackage) return true;
100                 return invocationSite.isSuperAccess();
101         }
102
103         if (isPrivate()) {
104                 // answer true if the invocationType and the declaringClass have a common enclosingType
105                 // already know they are not the identical type
106                 ReferenceBinding outerInvocationType = invocationType;
107                 ReferenceBinding temp = outerInvocationType.enclosingType();
108                 while (temp != null) {
109                         outerInvocationType = temp;
110                         temp = temp.enclosingType();
111                 }
112
113                 ReferenceBinding outerDeclaringClass = declaringClass;
114                 temp = outerDeclaringClass.enclosingType();
115                 while (temp != null) {
116                         outerDeclaringClass = temp;
117                         temp = temp.enclosingType();
118                 }
119                 return outerInvocationType == outerDeclaringClass;
120         }
121
122         // isDefault()
123         return invocationType.fPackage == declaringClass.fPackage;
124 }
125 /* Answer true if the receiver is visible to the type provided by the scope.
126 * InvocationSite implements isSuperAccess() to provide additional information
127 * if the receiver is protected.
128 *
129 * NOTE: Cannot invoke this method with a compilation unit scope.
130 */
131 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
132         if (isPublic()) return true;
133
134         SourceTypeBinding invocationType = scope.enclosingSourceType();
135         if (invocationType == declaringClass && invocationType == receiverType) return true;
136
137         if (isProtected()) {
138                 // answer true if the invocationType is the declaringClass or they are in the same package
139                 // OR the invocationType is a subclass of the declaringClass
140                 //    AND the receiverType is the invocationType or its subclass
141                 //    OR the method is a static method accessed directly through a type
142                 //    OR previous assertions are true for one of the enclosing type
143                 if (invocationType == declaringClass) return true;
144                 if (invocationType.fPackage == declaringClass.fPackage) return true;
145                 
146                 ReferenceBinding currentType = invocationType;
147                 int depth = 0;
148                 do {
149                         if (declaringClass.isSuperclassOf(currentType)) {
150                                 if (invocationSite.isSuperAccess()){
151                                         return true;
152                                 }
153                                 // receiverType can be an array binding in one case... see if you can change it
154                                 if (receiverType instanceof ArrayBinding){
155                                         return false;
156                                 }
157                                 if (isStatic()){
158                                         return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
159                                 }
160                                 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
161                                         if (depth > 0) invocationSite.setDepth(depth);
162                                         return true;
163                                 }
164                         }
165                         depth++;
166                         currentType = currentType.enclosingType();
167                 } while (currentType != null);
168                 return false;
169         }
170
171         if (isPrivate()) {
172                 // answer true if the receiverType is the declaringClass
173                 // AND the invocationType and the declaringClass have a common enclosingType
174                 if (receiverType != declaringClass) return false;
175
176                 if (invocationType != declaringClass) {
177                         ReferenceBinding outerInvocationType = invocationType;
178                         ReferenceBinding temp = outerInvocationType.enclosingType();
179                         while (temp != null) {
180                                 outerInvocationType = temp;
181                                 temp = temp.enclosingType();
182                         }
183
184                         ReferenceBinding outerDeclaringClass = declaringClass;
185                         temp = outerDeclaringClass.enclosingType();
186                         while (temp != null) {
187                                 outerDeclaringClass = temp;
188                                 temp = temp.enclosingType();
189                         }
190                         if (outerInvocationType != outerDeclaringClass) return false;
191                 }
192                 return true;
193         }
194
195         // isDefault()
196         if (invocationType.fPackage != declaringClass.fPackage) return false;
197
198         // receiverType can be an array binding in one case... see if you can change it
199         if (receiverType instanceof ArrayBinding)
200                 return false;
201         ReferenceBinding type = (ReferenceBinding) receiverType;
202         PackageBinding declaringPackage = declaringClass.fPackage;
203         do {
204                 if (declaringClass == type) return true;
205                 if (declaringPackage != type.fPackage) return false;
206         } while ((type = type.superclass()) != null);
207         return false;
208 }
209
210 /* Answer the receiver's constant pool name.
211 *
212 * <init> for constructors
213 * <clinit> for clinit methods
214 * or the source name of the method
215 */
216 public final char[] constantPoolName() {
217         return selector;
218 }
219 public final int getAccessFlags() {
220         return modifiers & AccJustFlag;
221 }
222
223 /* Answer true if the receiver is an abstract method
224 */
225 public final boolean isAbstract() {
226         return (modifiers & AccAbstract) != 0;
227 }
228
229 /* Answer true if the receiver is a constructor
230 */
231 public final boolean isConstructor() {
232         return selector == ConstructorDeclaration.ConstantPoolName;
233 }
234 protected boolean isConstructorRelated() {
235         return isConstructor();
236 }
237
238 /* Answer true if the receiver has default visibility
239 */
240 public final boolean isDefault() {
241         return !isPublic() && !isProtected() && !isPrivate();
242 }
243
244 /* Answer true if the receiver is a system generated default abstract method
245 */
246 public final boolean isDefaultAbstract() {
247         return (modifiers & AccDefaultAbstract) != 0;
248 }
249
250 /* Answer true if the receiver is a deprecated method
251 */
252 public final boolean isDeprecated() {
253         return (modifiers & AccDeprecated) != 0;
254 }
255
256 /* Answer true if the receiver is final and cannot be overridden
257 */
258 public final boolean isFinal() {
259         return (modifiers & AccFinal) != 0;
260 }
261
262 /* Answer true if the receiver is implementing another method
263  * in other words, it is overriding and concrete, and overriden method is abstract
264  * Only set for source methods
265 */
266 public final boolean isImplementing() {
267         return (modifiers & AccImplementing) != 0;
268 }
269
270 /* Answer true if the receiver is a native method
271 */
272 //public final boolean isNative() {
273 //      return (modifiers & AccNative) != 0;
274 //}
275
276 /* Answer true if the receiver is overriding another method
277  * Only set for source methods
278 */
279 public final boolean isOverriding() {
280         return (modifiers & AccOverriding) != 0;
281 }
282 /*
283  * Answer true if the receiver is a "public static void main(String[])" method
284  */
285 public final boolean isMain() {
286         if (this.selector.length == 4 && CharOperation.equals(this.selector, MAIN)
287                         && ((this.modifiers & (AccPublic | AccStatic)) != 0)
288                         && VoidBinding == this.returnType  
289                         && this.parameters.length == 1) {
290                 TypeBinding paramType = this.parameters[0];
291                 if (paramType.dimensions() == 1 && paramType.leafComponentType().id == TypeIds.T_JavaLangString) {
292                         return true;
293                 }
294         }
295         return false;
296 }
297 /* Answer true if the receiver has private visibility
298 */
299 public final boolean isPrivate() {
300         return (modifiers & AccPrivate) != 0;
301 }
302
303 /* Answer true if the receiver has private visibility and is used locally
304 */
305 public final boolean isPrivateUsed() {
306         return (modifiers & AccPrivateUsed) != 0;
307 }
308
309 /* Answer true if the receiver has protected visibility
310 */
311 public final boolean isProtected() {
312         return (modifiers & AccProtected) != 0;
313 }
314
315 /* Answer true if the receiver has public visibility
316 */
317 public final boolean isPublic() {
318         return (modifiers & AccPublic) != 0;
319 }
320
321 /* Answer true if the receiver got requested to clear the private modifier
322  * during private access emulation.
323  */
324 public final boolean isRequiredToClearPrivateModifier() {
325         return (modifiers & AccClearPrivateModifier) != 0;
326 }
327
328 /* Answer true if the receiver is a static method
329 */
330 public final boolean isStatic() {
331         return (modifiers & AccStatic) != 0;
332 }
333
334 /* Answer true if all float operations must adher to IEEE 754 float/double rules
335 */
336 //public final boolean isStrictfp() {
337 //      return (modifiers & AccStrictfp) != 0;
338 //}
339
340 /* Answer true if the receiver is a synchronized method
341 */
342 //public final boolean isSynchronized() {
343 //      return (modifiers & AccSynchronized) != 0;
344 //}
345
346 /* Answer true if the receiver has public visibility
347 */
348 //public final boolean isSynthetic() {
349 //      return (modifiers & AccSynthetic) != 0;
350 //}
351
352 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
353 */
354 public final boolean isViewedAsDeprecated() {
355         return (modifiers & AccDeprecated) != 0 ||
356                 (modifiers & AccDeprecatedImplicitly) != 0;
357 }
358
359 public char[] readableName() /* foo(int, Thread) */ {
360         StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
361         if (isConstructor())
362                 buffer.append(declaringClass.sourceName());
363         else
364                 buffer.append(selector);
365         buffer.append('(');
366         if (parameters != NoParameters) {
367                 for (int i = 0, length = parameters.length; i < length; i++) {
368                         if (i > 0)
369                                 buffer.append(", "); //$NON-NLS-1$
370                         buffer.append(parameters[i].sourceName());
371                 }
372         }
373         buffer.append(')');
374         return buffer.toString().toCharArray();
375 }
376
377 /**
378  * @see net.sourceforge.phpdt.internal.compiler.lookup.Binding#shortReadableName()
379  */
380 public char[] shortReadableName() {
381         StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
382         if (isConstructor())
383                 buffer.append(declaringClass.shortReadableName());
384         else
385                 buffer.append(selector);
386         buffer.append('(');
387         if (parameters != NoParameters) {
388                 for (int i = 0, length = parameters.length; i < length; i++) {
389                         if (i > 0)
390                                 buffer.append(", "); //$NON-NLS-1$
391                         buffer.append(parameters[i].shortReadableName());
392                 }
393         }
394         buffer.append(')');
395         return buffer.toString().toCharArray();
396 }
397
398 protected final void selector(char[] selector) {
399         this.selector = selector;
400         this.signature = null;
401 }
402
403 /* Answer the receiver's signature.
404 *
405 * NOTE: This method should only be used during/after code gen.
406 * The signature is cached so if the signature of the return type or any parameter
407 * type changes, the cached state is invalid.
408 */
409 public final char[] signature() /* (ILjava/lang/Thread;)Ljava/lang/Object; */ {
410         if (signature != null)
411                 return signature;
412
413         StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
414         buffer.append('(');
415         
416         TypeBinding[] targetParameters = this.parameters;
417         boolean considerSynthetics = isConstructorRelated() && declaringClass.isNestedType();
418         if (considerSynthetics) {
419                 
420                 // take into account the synthetic argument type signatures as well
421                 ReferenceBinding[] syntheticArgumentTypes = declaringClass.syntheticEnclosingInstanceTypes();
422                 int count = syntheticArgumentTypes == null ? 0 : syntheticArgumentTypes.length;
423                 for (int i = 0; i < count; i++) {
424                         buffer.append(syntheticArgumentTypes[i].signature());
425                 }
426                 
427                 if (this instanceof SyntheticAccessMethodBinding) {
428                         targetParameters = ((SyntheticAccessMethodBinding)this).targetMethod.parameters;
429                 }
430         }
431
432         if (targetParameters != NoParameters) {
433                 for (int i = 0; i < targetParameters.length; i++) {
434                         buffer.append(targetParameters[i].signature());
435                 }
436         }
437         if (considerSynthetics) {
438                 SyntheticArgumentBinding[] syntheticOuterArguments = declaringClass.syntheticOuterLocalVariables();
439                 int count = syntheticOuterArguments == null ? 0 : syntheticOuterArguments.length;
440                 for (int i = 0; i < count; i++) {
441                         buffer.append(syntheticOuterArguments[i].type.signature());
442                 }
443                 // move the extra padding arguments of the synthetic constructor invocation to the end          
444                 for (int i = targetParameters.length, extraLength = parameters.length; i < extraLength; i++) {
445                         buffer.append(parameters[i].signature());
446                 }
447         }
448         buffer.append(')');
449         buffer.append(returnType.signature());
450         return signature = buffer.toString().toCharArray();
451 }
452 public final int sourceEnd() {
453         AbstractMethodDeclaration method = sourceMethod();
454         if (method == null)
455                 return 0;
456         else
457                 return method.sourceEnd;
458 }
459 AbstractMethodDeclaration sourceMethod() {
460         SourceTypeBinding sourceType;
461         try {
462                 sourceType = (SourceTypeBinding) declaringClass;
463         } catch (ClassCastException e) {
464                 return null;            
465         }
466
467         AbstractMethodDeclaration[] methods = sourceType.scope.referenceContext.methods;
468         for (int i = methods.length; --i >= 0;)
469                 if (this == methods[i].binding)
470                         return methods[i];
471         return null;            
472 }
473 public final int sourceStart() {
474         AbstractMethodDeclaration method = sourceMethod();
475         if (method == null)
476                 return 0;
477         else
478                 return method.sourceStart;
479 }
480 /* During private access emulation, the binding can be requested to loose its
481  * private visibility when the class file is dumped.
482  */
483
484 public final void tagForClearingPrivateModifier() {
485         modifiers |= AccClearPrivateModifier;
486 }
487 public String toString() {
488         String s = (returnType != null) ? returnType.debugName() : "NULL TYPE"; //$NON-NLS-1$
489         s += " "; //$NON-NLS-1$
490         s += (selector != null) ? new String(selector) : "UNNAMED METHOD"; //$NON-NLS-1$
491
492         s += "("; //$NON-NLS-1$
493         if (parameters != null) {
494                 if (parameters != NoParameters) {
495                         for (int i = 0, length = parameters.length; i < length; i++) {
496                                 if (i  > 0)
497                                         s += ", "; //$NON-NLS-1$
498                                 s += (parameters[i] != null) ? parameters[i].debugName() : "NULL TYPE"; //$NON-NLS-1$
499                         }
500                 }
501         } else {
502                 s += "NULL PARAMETERS"; //$NON-NLS-1$
503         }
504         s += ") "; //$NON-NLS-1$
505
506         if (thrownExceptions != null) {
507                 if (thrownExceptions != NoExceptions) {
508                         s += "throws "; //$NON-NLS-1$
509                         for (int i = 0, length = thrownExceptions.length; i < length; i++) {
510                                 if (i  > 0)
511                                         s += ", "; //$NON-NLS-1$
512                                 s += (thrownExceptions[i] != null) ? thrownExceptions[i].debugName() : "NULL TYPE"; //$NON-NLS-1$
513                         }
514                 }
515         } else {
516                 s += "NULL THROWN EXCEPTIONS"; //$NON-NLS-1$
517         }
518         return s;
519 }
520 }