*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / Scope.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.phpdt.internal.compiler.impl.ReferenceContext;
15 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
16 import net.sourceforge.phpdt.internal.compiler.util.ObjectVector;
17 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
18 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
19 import net.sourceforge.phpeclipse.internal.compiler.ast.ImportReference;
20 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
21
22 public abstract class Scope
23         implements
24                 BaseTypes,
25                 BindingIds,
26                 CompilerModifiers,
27                 ProblemReasons,
28                 TagBits,
29                 TypeConstants,
30                 TypeIds {
31
32         public Scope parent;
33         public int kind;
34
35         public final static int BLOCK_SCOPE = 1;
36         public final static int METHOD_SCOPE = 2;
37         public final static int CLASS_SCOPE = 3;
38         public final static int COMPILATION_UNIT_SCOPE = 4;
39         protected Scope(int kind, Scope parent) {
40                 this.kind = kind;
41                 this.parent = parent;
42         }
43
44         public abstract ProblemReporter problemReporter();
45
46         // Internal use only
47         protected final boolean areParametersAssignable(TypeBinding[] parameters, TypeBinding[] arguments) {
48                 if (parameters == arguments)
49                         return true;
50
51                 int length = parameters.length;
52                 if (length != arguments.length)
53                         return false;
54
55                 for (int i = 0; i < length; i++)
56                         if (parameters[i] != arguments[i])
57                                 if (!arguments[i].isCompatibleWith(parameters[i]))
58                                         return false;
59                 return true;
60         }
61
62         /* Answer an int describing the relationship between the given types.
63         *
64         *               NotRelated 
65         *               EqualOrMoreSpecific : left is compatible with right
66         *               MoreGeneric : right is compatible with left
67         */
68         public static int compareTypes(TypeBinding left, TypeBinding right) {
69                 if (left.isCompatibleWith(right))
70                         return EqualOrMoreSpecific;
71                 if (right.isCompatibleWith(left))
72                         return MoreGeneric;
73                 return NotRelated;
74         }
75
76         /* Answer an int describing the relationship between the given type and unchecked exceptions.
77         *
78         *       NotRelated 
79         *       EqualOrMoreSpecific : type is known for sure to be an unchecked exception type
80         *       MoreGeneric : type is a supertype of an actual unchecked exception type
81         */
82         public int compareUncheckedException(ReferenceBinding type) {
83                 int comparison = compareTypes(type, getJavaLangRuntimeException());
84                 if (comparison != 0) return comparison;
85                 return compareTypes(type, getJavaLangError());
86         }
87
88         public final CompilationUnitScope compilationUnitScope() {
89                 Scope lastScope = null;
90                 Scope scope = this;
91                 do {
92                         lastScope = scope;
93                         scope = scope.parent;
94                 } while (scope != null);
95                 return (CompilationUnitScope) lastScope;
96         }
97
98         public ArrayBinding createArray(TypeBinding type, int dimension) {
99                 if (type.isValidBinding())
100                         return environment().createArrayType(type, dimension);
101                 else
102                         return new ArrayBinding(type, dimension);
103         }
104
105         public final ClassScope enclosingClassScope() {
106                 Scope scope = this;
107                 while ((scope = scope.parent) != null) {
108                         if (scope instanceof ClassScope) return (ClassScope)scope;
109                 }
110                 return null; // may answer null if no type around
111         }
112
113         public final MethodScope enclosingMethodScope() {
114                 Scope scope = this;
115                 while ((scope = scope.parent) != null) {
116                         if (scope instanceof MethodScope) return (MethodScope)scope;
117                 }
118                 return null; // may answer null if no method around
119         }
120
121         /* Answer the receiver's enclosing source type.
122         */
123         public final SourceTypeBinding enclosingSourceType() {
124                 Scope scope = this;
125                 do {
126                         if (scope instanceof ClassScope)
127                                 return ((ClassScope) scope).referenceContext.binding;
128                         scope = scope.parent;
129                 } while (scope != null);
130                 return null;
131         }
132         public final LookupEnvironment environment() {
133                 Scope scope, unitScope = this;
134                 while ((scope = unitScope.parent) != null)
135                         unitScope = scope;
136                 return ((CompilationUnitScope) unitScope).environment;
137         }
138
139         // Internal use only
140         public ReferenceBinding findDirectMemberType(char[] typeName, ReferenceBinding enclosingType) {
141                 if ((enclosingType.tagBits & HasNoMemberTypes) != 0)
142                         return null; // know it has no member types (nor inherited member types)
143
144                 SourceTypeBinding enclosingSourceType = enclosingSourceType();
145                 compilationUnitScope().recordReference(enclosingType.compoundName, typeName);
146                 ReferenceBinding memberType = enclosingType.getMemberType(typeName);
147                 if (memberType != null) {
148                         compilationUnitScope().recordTypeReference(memberType); // to record supertypes
149                         if (enclosingSourceType == null
150                                 ? memberType.canBeSeenBy(getCurrentPackage())
151                                 : memberType.canBeSeenBy(enclosingType, enclosingSourceType))
152                                 return memberType;
153                         else
154                                 return new ProblemReferenceBinding(typeName, memberType, NotVisible);
155                 }
156                 return null;
157         }
158
159         // Internal use only
160         public MethodBinding findExactMethod(
161                 ReferenceBinding receiverType,
162                 char[] selector,
163                 TypeBinding[] argumentTypes,
164                 InvocationSite invocationSite) {
165
166                 compilationUnitScope().recordTypeReference(receiverType);
167                 compilationUnitScope().recordTypeReferences(argumentTypes);
168                 MethodBinding exactMethod = receiverType.getExactMethod(selector, argumentTypes);
169                 if (exactMethod != null) {
170                         compilationUnitScope().recordTypeReferences(exactMethod.thrownExceptions);
171                         if (receiverType.isInterface() || exactMethod.canBeSeenBy(receiverType, invocationSite, this))
172                                 return exactMethod;
173                 }
174                 return null;
175         }
176
177         // Internal use only
178         /*      Answer the field binding that corresponds to fieldName.
179                 Start the lookup at the receiverType.
180                 InvocationSite implements
181                         isSuperAccess(); this is used to determine if the discovered field is visible.
182                 Only fields defined by the receiverType or its supertypes are answered;
183                 a field of an enclosing type will not be found using this API.
184         
185                 If no visible field is discovered, null is answered.
186         */
187         public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite) {
188                 if (receiverType.isBaseType()) return null;
189                 if (receiverType.isArrayType()) {
190                         TypeBinding leafType = receiverType.leafComponentType();
191                         if (leafType instanceof ReferenceBinding) {
192                                 if (!((ReferenceBinding) leafType).canBeSeenBy(this))
193                                         return new ProblemFieldBinding((ReferenceBinding)leafType, fieldName, ReceiverTypeNotVisible);
194                         }
195                         if (CharOperation.equals(fieldName, LENGTH))
196                                 return ArrayBinding.LengthField;
197                         return null;
198                 }
199
200                 compilationUnitScope().recordTypeReference(receiverType);
201
202                 ReferenceBinding currentType = (ReferenceBinding) receiverType;
203                 if (!currentType.canBeSeenBy(this))
204                         return new ProblemFieldBinding(currentType, fieldName, ReceiverTypeNotVisible);
205
206                 FieldBinding field = currentType.getField(fieldName);
207                 if (field != null) {
208                         if (field.canBeSeenBy(currentType, invocationSite, this))
209                                 return field;
210                         else
211                                 return new ProblemFieldBinding(field.declaringClass, fieldName, NotVisible);
212                 }
213                 // collect all superinterfaces of receiverType until the field is found in a supertype
214                 ReferenceBinding[][] interfacesToVisit = null;
215                 int lastPosition = -1;
216                 FieldBinding visibleField = null;
217                 boolean keepLooking = true;
218                 boolean notVisible = false;
219                 // we could hold onto the not visible field for extra error reporting
220                 while (keepLooking) {
221                         ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
222                         if (itsInterfaces != NoSuperInterfaces) {
223                                 if (interfacesToVisit == null)
224                                         interfacesToVisit = new ReferenceBinding[5][];
225                                 if (++lastPosition == interfacesToVisit.length)
226                                         System.arraycopy(
227                                                 interfacesToVisit,
228                                                 0,
229                                                 interfacesToVisit = new ReferenceBinding[lastPosition * 2][],
230                                                 0,
231                                                 lastPosition);
232                                 interfacesToVisit[lastPosition] = itsInterfaces;
233                         }
234                         if ((currentType = currentType.superclass()) == null)
235                                 break;
236
237                         if ((field = currentType.getField(fieldName)) != null) {
238                                 keepLooking = false;
239                                 if (field.canBeSeenBy(receiverType, invocationSite, this)) {
240                                         if (visibleField == null)
241                                                 visibleField = field;
242                                         else
243                                                 return new ProblemFieldBinding(visibleField.declaringClass, fieldName, Ambiguous);
244                                 } else {
245                                         notVisible = true;
246                                 }
247                         }
248                 }
249
250                 // walk all visible interfaces to find ambiguous references
251                 if (interfacesToVisit != null) {
252                         ProblemFieldBinding ambiguous = null;
253                         done : for (int i = 0; i <= lastPosition; i++) {
254                                 ReferenceBinding[] interfaces = interfacesToVisit[i];
255                                 for (int j = 0, length = interfaces.length; j < length; j++) {
256                                         ReferenceBinding anInterface = interfaces[j];
257                                         if ((anInterface.tagBits & InterfaceVisited) == 0) {
258                                                 // if interface as not already been visited
259                                                 anInterface.tagBits |= InterfaceVisited;
260                                                 if ((field = anInterface.getField(fieldName)) != null) {
261                                                         if (visibleField == null) {
262                                                                 visibleField = field;
263                                                         } else {
264                                                                 ambiguous = new ProblemFieldBinding(visibleField.declaringClass, fieldName, Ambiguous);
265                                                                 break done;
266                                                         }
267                                                 } else {
268                                                         ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
269                                                         if (itsInterfaces != NoSuperInterfaces) {
270                                                                 if (++lastPosition == interfacesToVisit.length)
271                                                                         System.arraycopy(
272                                                                                 interfacesToVisit,
273                                                                                 0,
274                                                                                 interfacesToVisit = new ReferenceBinding[lastPosition * 2][],
275                                                                                 0,
276                                                                                 lastPosition);
277                                                                 interfacesToVisit[lastPosition] = itsInterfaces;
278                                                         }
279                                                 }
280                                         }
281                                 }
282                         }
283
284                         // bit reinitialization
285                         for (int i = 0; i <= lastPosition; i++) {
286                                 ReferenceBinding[] interfaces = interfacesToVisit[i];
287                                 for (int j = 0, length = interfaces.length; j < length; j++)
288                                         interfaces[j].tagBits &= ~InterfaceVisited;
289                         }
290                         if (ambiguous != null)
291                                 return ambiguous;
292                 }
293
294                 if (visibleField != null)
295                         return visibleField;
296                 if (notVisible)
297                         return new ProblemFieldBinding(currentType, fieldName, NotVisible);
298                 return null;
299         }
300
301         // Internal use only
302         public ReferenceBinding findMemberType(char[] typeName, ReferenceBinding enclosingType) {
303                 if ((enclosingType.tagBits & HasNoMemberTypes) != 0)
304                         return null; // know it has no member types (nor inherited member types)
305
306                 SourceTypeBinding enclosingSourceType = enclosingSourceType();
307                 PackageBinding currentPackage = getCurrentPackage();
308                 compilationUnitScope().recordReference(enclosingType.compoundName, typeName);
309                 ReferenceBinding memberType = enclosingType.getMemberType(typeName);
310                 if (memberType != null) {
311                         compilationUnitScope().recordTypeReference(memberType); // to record supertypes
312                         if (enclosingSourceType == null
313                                 ? memberType.canBeSeenBy(currentPackage)
314                                 : memberType.canBeSeenBy(enclosingType, enclosingSourceType))
315                                 return memberType;
316                         else
317                                 return new ProblemReferenceBinding(typeName, memberType, NotVisible);
318                 }
319
320                 // collect all superinterfaces of receiverType until the memberType is found in a supertype
321                 ReferenceBinding currentType = enclosingType;
322                 ReferenceBinding[][] interfacesToVisit = null;
323                 int lastPosition = -1;
324                 ReferenceBinding visibleMemberType = null;
325                 boolean keepLooking = true;
326                 ReferenceBinding notVisible = null;
327                 // we could hold onto the not visible field for extra error reporting
328                 while (keepLooking) {
329                         ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
330                         if (itsInterfaces != NoSuperInterfaces) {
331                                 if (interfacesToVisit == null)
332                                         interfacesToVisit = new ReferenceBinding[5][];
333                                 if (++lastPosition == interfacesToVisit.length)
334                                         System.arraycopy(
335                                                 interfacesToVisit,
336                                                 0,
337                                                 interfacesToVisit = new ReferenceBinding[lastPosition * 2][],
338                                                 0,
339                                                 lastPosition);
340                                 interfacesToVisit[lastPosition] = itsInterfaces;
341                         }
342                         if ((currentType = currentType.superclass()) == null)
343                                 break;
344
345                         compilationUnitScope().recordReference(currentType.compoundName, typeName);
346                         if ((memberType = currentType.getMemberType(typeName)) != null) {
347                                 compilationUnitScope().recordTypeReference(memberType); // to record supertypes
348                                 keepLooking = false;
349                                 if (enclosingSourceType == null
350                                         ? memberType.canBeSeenBy(currentPackage)
351                                         : memberType.canBeSeenBy(enclosingType, enclosingSourceType)) {
352                                                 if (visibleMemberType == null)
353                                                         visibleMemberType = memberType;
354                                                 else
355                                                         return new ProblemReferenceBinding(typeName, Ambiguous);
356                                 } else {
357                                         notVisible = memberType;
358                                 }
359                         }
360                 }
361                 // walk all visible interfaces to find ambiguous references
362                 if (interfacesToVisit != null) {
363                         ProblemReferenceBinding ambiguous = null;
364                         done : for (int i = 0; i <= lastPosition; i++) {
365                                 ReferenceBinding[] interfaces = interfacesToVisit[i];
366                                 for (int j = 0, length = interfaces.length; j < length; j++) {
367                                         ReferenceBinding anInterface = interfaces[j];
368                                         if ((anInterface.tagBits & InterfaceVisited) == 0) {
369                                                 // if interface as not already been visited
370                                                 anInterface.tagBits |= InterfaceVisited;
371                                                 compilationUnitScope().recordReference(anInterface.compoundName, typeName);
372                                                 if ((memberType = anInterface.getMemberType(typeName)) != null) {
373                                                         compilationUnitScope().recordTypeReference(memberType); // to record supertypes
374                                                         if (visibleMemberType == null) {
375                                                                 visibleMemberType = memberType;
376                                                         } else {
377                                                                 ambiguous = new ProblemReferenceBinding(typeName, Ambiguous);
378                                                                 break done;
379                                                         }
380                                                 } else {
381                                                         ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
382                                                         if (itsInterfaces != NoSuperInterfaces) {
383                                                                 if (++lastPosition == interfacesToVisit.length)
384                                                                         System.arraycopy(
385                                                                                 interfacesToVisit,
386                                                                                 0,
387                                                                                 interfacesToVisit = new ReferenceBinding[lastPosition * 2][],
388                                                                                 0,
389                                                                                 lastPosition);
390                                                                 interfacesToVisit[lastPosition] = itsInterfaces;
391                                                         }
392                                                 }
393                                         }
394                                 }
395                         }
396
397                         // bit reinitialization
398                         for (int i = 0; i <= lastPosition; i++) {
399                                 ReferenceBinding[] interfaces = interfacesToVisit[i];
400                                 for (int j = 0, length = interfaces.length; j < length; j++)
401                                         interfaces[j].tagBits &= ~InterfaceVisited;
402                         }
403                         if (ambiguous != null)
404                                 return ambiguous;
405                 }
406                 if (visibleMemberType != null)
407                         return visibleMemberType;
408                 if (notVisible != null)
409                         return new ProblemReferenceBinding(typeName, notVisible, NotVisible);
410                 return null;
411         }
412
413         // Internal use only
414         public MethodBinding findMethod(
415                 ReferenceBinding receiverType,
416                 char[] selector,
417                 TypeBinding[] argumentTypes,
418                 InvocationSite invocationSite) {
419
420                 ReferenceBinding currentType = receiverType;
421                 MethodBinding matchingMethod = null;
422                 ObjectVector found = new ObjectVector();
423
424                 compilationUnitScope().recordTypeReference(receiverType);
425                 compilationUnitScope().recordTypeReferences(argumentTypes);
426
427                 if (currentType.isInterface()) {
428                         MethodBinding[] currentMethods = currentType.getMethods(selector);
429                         int currentLength = currentMethods.length;
430                         if (currentLength == 1) {
431                                 matchingMethod = currentMethods[0];
432                         } else if (currentLength > 1) {
433                                 found.addAll(currentMethods);
434                         }
435                         matchingMethod = findMethodInSuperInterfaces(currentType, selector, found, matchingMethod);
436                         currentType = getJavaLangObject();
437                 }
438
439 //              boolean isCompliant14 = compilationUnitScope().environment.options.complianceLevel >= CompilerOptions.JDK1_4;
440                 // superclass lookup
441                 ReferenceBinding classHierarchyStart = currentType;
442                 while (currentType != null) {
443                         MethodBinding[] currentMethods = currentType.getMethods(selector);
444                         int currentLength = currentMethods.length;
445                         
446                         /*
447                          * if 1.4 compliant, must filter out redundant protected methods from superclasses
448                          */
449 //                      if (isCompliant14){                      
450 //                              nextMethod: for (int i = 0; i < currentLength; i++){
451 //                                      MethodBinding currentMethod = currentMethods[i];
452 //                                      // protected method need to be checked only - default access is already dealt with in #canBeSeen implementation
453 //                                      // when checking that p.C -> q.B -> p.A cannot see default access members from A through B.
454 //                                      if ((currentMethod.modifiers & AccProtected) == 0) continue nextMethod;
455 //                                      if (matchingMethod != null){
456 //                                              if (currentMethod.areParametersEqual(matchingMethod)){
457 //                                                      currentLength--;
458 //                                                      currentMethods[i] = null; // discard this match
459 //                                                      continue nextMethod;
460 //                                              }
461 //                                      } else {
462 //                                              for (int j = 0, max = found.size; j < max; j++) {
463 //                                                      if (((MethodBinding)found.elementAt(j)).areParametersEqual(currentMethod)){
464 //                                                              currentLength--;
465 //                                                              currentMethods[i] = null;
466 //                                                              continue nextMethod;
467 //                                                      }
468 //                                              }
469 //                                      }
470 //                              }
471 //                      }
472                         
473                         if (currentLength == 1 && matchingMethod == null && found.size == 0) {
474                                 matchingMethod = currentMethods[0];
475                         } else if (currentLength > 0) {
476                                 if (matchingMethod != null) {
477                                         found.add(matchingMethod);
478                                         matchingMethod = null;
479                                 }
480                                 // append currentMethods, filtering out null entries
481                                 int maxMethod = currentMethods.length;
482                                 if (maxMethod == currentLength) { // no method was eliminated for 1.4 compliance (see above)
483                                         found.addAll(currentMethods);
484                                 } else {
485                                         for (int i = 0, max = currentMethods.length; i < max; i++) {
486                                                 MethodBinding currentMethod = currentMethods[i];
487                                                 if (currentMethod != null) found.add(currentMethod);
488                                         }
489                                 }
490                         }
491                         currentType = currentType.superclass();
492                 }
493
494                 int foundSize = found.size;
495                 if (foundSize == 0) {
496                         if (matchingMethod != null && areParametersAssignable(matchingMethod.parameters, argumentTypes)) {
497                                 // (if no default abstract) must explicitly look for one instead, which could be a better match
498                                 if (!matchingMethod.canBeSeenBy(receiverType, invocationSite, this)) {
499                                         // ignore matching method (to be consistent with multiple matches, none visible (matching method is then null)
500                                         MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, null, found);                                             
501                                         if (interfaceMethod != null) return interfaceMethod;
502                                 }
503                                 return matchingMethod;
504                         } 
505                         return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, matchingMethod, found);
506                 }
507
508                 MethodBinding[] candidates = new MethodBinding[foundSize];
509                 int candidatesCount = 0;
510                 // argument type compatibility check
511                 for (int i = 0; i < foundSize; i++) {
512                         MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
513                         if (areParametersAssignable(methodBinding.parameters, argumentTypes))
514                                 candidates[candidatesCount++] = methodBinding;
515                 }
516                 if (candidatesCount == 1) {
517                         compilationUnitScope().recordTypeReferences(candidates[0].thrownExceptions);
518                         return candidates[0]; // have not checked visibility
519                 }
520                 if (candidatesCount == 0) { // try to find a close match when the parameter order is wrong or missing some parameters
521                         MethodBinding interfaceMethod =
522                                 findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, matchingMethod, found);
523                         if (interfaceMethod != null) return interfaceMethod;
524
525                         int argLength = argumentTypes.length;
526                         foundSize = found.size;
527                         nextMethod : for (int i = 0; i < foundSize; i++) {
528                                 MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
529                                 TypeBinding[] params = methodBinding.parameters;
530                                 int paramLength = params.length;
531                                 nextArg: for (int a = 0; a < argLength; a++) {
532                                         TypeBinding arg = argumentTypes[a];
533                                         for (int p = 0; p < paramLength; p++)
534                                                 if (params[p] == arg)
535                                                         continue nextArg;
536                                         continue nextMethod;
537                                 }
538                                 return methodBinding;
539                         }
540                         return (MethodBinding) found.elementAt(0); // no good match so just use the first one found
541                 }
542
543                 // visibility check
544                 int visiblesCount = 0;
545                 for (int i = 0; i < candidatesCount; i++) {
546                         MethodBinding methodBinding = candidates[i];
547                         if (methodBinding.canBeSeenBy(receiverType, invocationSite, this)) {
548                                 if (visiblesCount != i) {
549                                         candidates[i] = null;
550                                         candidates[visiblesCount] = methodBinding;
551                                 }
552                                 visiblesCount++;
553                         }
554                 }
555                 if (visiblesCount == 1) {
556                         compilationUnitScope().recordTypeReferences(candidates[0].thrownExceptions);
557                         return candidates[0];
558                 }
559                 if (visiblesCount == 0) {
560                         MethodBinding interfaceMethod =
561                                 findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, matchingMethod, found);
562                         if (interfaceMethod != null) return interfaceMethod;
563                         return new ProblemMethodBinding(candidates[0], candidates[0].selector, candidates[0].parameters, NotVisible);
564                 }       
565                 if (candidates[0].declaringClass.isClass()) {
566                         return mostSpecificClassMethodBinding(candidates, visiblesCount);
567                 } else {
568                         return mostSpecificInterfaceMethodBinding(candidates, visiblesCount);
569                 }
570         }
571
572         // abstract method lookup lookup (since maybe missing default abstract methods)
573         public MethodBinding findDefaultAbstractMethod(
574                 ReferenceBinding receiverType, 
575                 char[] selector,
576                 TypeBinding[] argumentTypes,
577                 InvocationSite invocationSite,
578                 ReferenceBinding classHierarchyStart,
579                 MethodBinding matchingMethod,
580                 ObjectVector found) {
581
582                 int startFoundSize = found.size;
583                 ReferenceBinding currentType = classHierarchyStart;
584                 while (currentType != null) {
585                         matchingMethod = findMethodInSuperInterfaces(currentType, selector, found, matchingMethod);
586                         currentType = currentType.superclass();
587                 }
588                 int foundSize = found.size;
589                 if (foundSize == startFoundSize) return matchingMethod; // maybe null
590
591                 MethodBinding[] candidates = new MethodBinding[foundSize - startFoundSize];
592                 int candidatesCount = 0;
593                 // argument type compatibility check
594                 for (int i = startFoundSize; i < foundSize; i++) {
595                         MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
596                         if (areParametersAssignable(methodBinding.parameters, argumentTypes))
597                                 candidates[candidatesCount++] = methodBinding;
598                 }
599                 if (candidatesCount == 1) {
600                         compilationUnitScope().recordTypeReferences(candidates[0].thrownExceptions);
601                         return candidates[0]; 
602                 }
603                 if (candidatesCount == 0) { // try to find a close match when the parameter order is wrong or missing some parameters
604                         int argLength = argumentTypes.length;
605                         nextMethod : for (int i = 0; i < foundSize; i++) {
606                                 MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
607                                 TypeBinding[] params = methodBinding.parameters;
608                                 int paramLength = params.length;
609                                 nextArg: for (int a = 0; a < argLength; a++) {
610                                         TypeBinding arg = argumentTypes[a];
611                                         for (int p = 0; p < paramLength; p++)
612                                                 if (params[p] == arg)
613                                                         continue nextArg;
614                                         continue nextMethod;
615                                 }
616                                 return methodBinding;
617                         }
618                         return (MethodBinding) found.elementAt(0); // no good match so just use the first one found
619                 }
620                 // no need to check for visibility - interface methods are public
621                 return mostSpecificInterfaceMethodBinding(candidates, candidatesCount);
622         }
623
624         public MethodBinding findMethodInSuperInterfaces(
625                 ReferenceBinding currentType,
626                 char[] selector,
627                 ObjectVector found,
628                 MethodBinding matchingMethod) {
629
630                 ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
631                 if (itsInterfaces != NoSuperInterfaces) {
632                         ReferenceBinding[][] interfacesToVisit = new ReferenceBinding[5][];
633                         int lastPosition = -1;
634                         if (++lastPosition == interfacesToVisit.length)
635                                 System.arraycopy(
636                                         interfacesToVisit, 0,
637                                         interfacesToVisit = new ReferenceBinding[lastPosition * 2][], 0,
638                                         lastPosition);
639                         interfacesToVisit[lastPosition] = itsInterfaces;
640
641                         for (int i = 0; i <= lastPosition; i++) {
642                                 ReferenceBinding[] interfaces = interfacesToVisit[i];
643                                 for (int j = 0, length = interfaces.length; j < length; j++) {
644                                         currentType = interfaces[j];
645                                         if ((currentType.tagBits & InterfaceVisited) == 0) {
646                                                 // if interface as not already been visited
647                                                 currentType.tagBits |= InterfaceVisited;
648
649                                                 MethodBinding[] currentMethods = currentType.getMethods(selector);
650                                                 int currentLength = currentMethods.length;
651                                                 if (currentLength == 1 && matchingMethod == null && found.size == 0) {
652                                                         matchingMethod = currentMethods[0];
653                                                 } else if (currentLength > 0) {
654                                                         if (matchingMethod != null) {
655                                                                 found.add(matchingMethod);
656                                                                 matchingMethod = null;
657                                                         }
658                                                         found.addAll(currentMethods);
659                                                 }
660                                                 itsInterfaces = currentType.superInterfaces();
661                                                 if (itsInterfaces != NoSuperInterfaces) {
662                                                         if (++lastPosition == interfacesToVisit.length)
663                                                                 System.arraycopy(
664                                                                         interfacesToVisit, 0,
665                                                                         interfacesToVisit = new ReferenceBinding[lastPosition * 2][], 0,
666                                                                         lastPosition);
667                                                         interfacesToVisit[lastPosition] = itsInterfaces;
668                                                 }
669                                         }
670                                 }
671                         }
672
673                         // bit reinitialization
674                         for (int i = 0; i <= lastPosition; i++) {
675                                 ReferenceBinding[] interfaces = interfacesToVisit[i];
676                                 for (int j = 0, length = interfaces.length; j < length; j++)
677                                         interfaces[j].tagBits &= ~InterfaceVisited;
678                         }
679                 }
680                 return matchingMethod;
681         }
682         
683         // Internal use only
684         public MethodBinding findMethodForArray(
685                 ArrayBinding receiverType,
686                 char[] selector,
687                 TypeBinding[] argumentTypes,
688                 InvocationSite invocationSite) {
689
690                 TypeBinding leafType = receiverType.leafComponentType();
691                 if (leafType instanceof ReferenceBinding) {
692                         if (!((ReferenceBinding) leafType).canBeSeenBy(this))
693                                 return new ProblemMethodBinding(selector, MethodBinding.NoParameters, (ReferenceBinding)leafType, ReceiverTypeNotVisible);
694                 }
695
696                 ReferenceBinding object = getJavaLangObject();
697                 MethodBinding methodBinding = object.getExactMethod(selector, argumentTypes);
698                 if (methodBinding != null) {
699                         // handle the method clone() specially... cannot be protected or throw exceptions
700                         if (argumentTypes == NoParameters && CharOperation.equals(selector, CLONE))
701                                 return new MethodBinding(
702                                         (methodBinding.modifiers ^ AccProtected) | AccPublic,
703                                         CLONE,
704                                         methodBinding.returnType,
705                                         argumentTypes,
706                                         null,
707                                         object);
708                         if (methodBinding.canBeSeenBy(receiverType, invocationSite, this))
709                                 return methodBinding;
710                 }
711                 // answers closest approximation, may not check argumentTypes or visibility
712                 methodBinding = findMethod(object, selector, argumentTypes, invocationSite);
713                 if (methodBinding == null)
714                         return new ProblemMethodBinding(selector, argumentTypes, NotFound);
715                 if (methodBinding.isValidBinding()) {
716                         if (!areParametersAssignable(methodBinding.parameters, argumentTypes))
717                                 return new ProblemMethodBinding(
718                                         methodBinding,
719                                         selector,
720                                         argumentTypes,
721                                         NotFound);
722                         if (!methodBinding.canBeSeenBy(receiverType, invocationSite, this))
723                                 return new ProblemMethodBinding(
724                                         methodBinding,
725                                         selector,
726                                         methodBinding.parameters,
727                                         NotVisible);
728                 }
729                 return methodBinding;
730         }
731
732         // Internal use only
733         public ReferenceBinding findType(
734                 char[] typeName,
735                 PackageBinding declarationPackage,
736                 PackageBinding invocationPackage) {
737
738                 compilationUnitScope().recordReference(declarationPackage.compoundName, typeName);
739                 ReferenceBinding typeBinding = declarationPackage.getType(typeName);
740                 if (typeBinding == null)
741                         return null;
742
743                 if (typeBinding.isValidBinding()) {
744                         if (declarationPackage != invocationPackage && !typeBinding.canBeSeenBy(invocationPackage))
745                                 return new ProblemReferenceBinding(typeName, typeBinding, NotVisible);
746                 }
747                 return typeBinding;
748         }
749
750         public TypeBinding getBaseType(char[] name) {
751                 // list should be optimized (with most often used first)
752                 int length = name.length;
753                 if (length > 2 && length < 8) {
754                         switch (name[0]) {
755                                 case 'i' :
756                                         if (length == 3 && name[1] == 'n' && name[2] == 't')
757                                                 return IntBinding;
758                                         break;
759                                 case 'v' :
760                                         if (length == 4 && name[1] == 'o' && name[2] == 'i' && name[3] == 'd')
761                                                 return VoidBinding;
762                                         break;
763                                 case 'b' :
764                                         if (length == 7
765                                                 && name[1] == 'o'
766                                                 && name[2] == 'o'
767                                                 && name[3] == 'l'
768                                                 && name[4] == 'e'
769                                                 && name[5] == 'a'
770                                                 && name[6] == 'n')
771                                                 return BooleanBinding;
772                                         if (length == 4 && name[1] == 'y' && name[2] == 't' && name[3] == 'e')
773                                                 return ByteBinding;
774                                         break;
775                                 case 'c' :
776                                         if (length == 4 && name[1] == 'h' && name[2] == 'a' && name[3] == 'r')
777                                                 return CharBinding;
778                                         break;
779                                 case 'd' :
780                                         if (length == 6
781                                                 && name[1] == 'o'
782                                                 && name[2] == 'u'
783                                                 && name[3] == 'b'
784                                                 && name[4] == 'l'
785                                                 && name[5] == 'e')
786                                                 return DoubleBinding;
787                                         break;
788                                 case 'f' :
789                                         if (length == 5
790                                                 && name[1] == 'l'
791                                                 && name[2] == 'o'
792                                                 && name[3] == 'a'
793                                                 && name[4] == 't')
794                                                 return FloatBinding;
795                                         break;
796                                 case 'l' :
797                                         if (length == 4 && name[1] == 'o' && name[2] == 'n' && name[3] == 'g')
798                                                 return LongBinding;
799                                         break;
800                                 case 's' :
801                                         if (length == 5
802                                                 && name[1] == 'h'
803                                                 && name[2] == 'o'
804                                                 && name[3] == 'r'
805                                                 && name[4] == 't')
806                                                 return ShortBinding;
807                         }
808                 }
809                 return null;
810         }
811
812         public final PackageBinding getCurrentPackage() {
813                 Scope scope, unitScope = this;
814                 while ((scope = unitScope.parent) != null)
815                         unitScope = scope;
816                 return ((CompilationUnitScope) unitScope).fPackage;
817         }
818
819         public final ReferenceBinding getJavaIoSerializable() {
820                 compilationUnitScope().recordQualifiedReference(JAVA_IO_SERIALIZABLE);
821                 ReferenceBinding type = environment().getType(JAVA_IO_SERIALIZABLE);
822                 if (type != null) return type;
823         
824                 problemReporter().isClassPathCorrect(JAVA_IO_SERIALIZABLE, referenceCompilationUnit());
825                 return null; // will not get here since the above error aborts the compilation
826         }
827
828         public final ReferenceBinding getJavaLangClass() {
829                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_CLASS);
830                 ReferenceBinding type = environment().getType(JAVA_LANG_CLASS);
831                 if (type != null) return type;
832         
833                 problemReporter().isClassPathCorrect(JAVA_LANG_CLASS, referenceCompilationUnit());
834                 return null; // will not get here since the above error aborts the compilation
835         }
836
837         public final ReferenceBinding getJavaLangCloneable() {
838                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_CLONEABLE);
839                 ReferenceBinding type = environment().getType(JAVA_LANG_CLONEABLE);
840                 if (type != null) return type;
841         
842                 problemReporter().isClassPathCorrect(JAVA_LANG_CLONEABLE, referenceCompilationUnit());
843                 return null; // will not get here since the above error aborts the compilation
844         }
845
846         public final ReferenceBinding getJavaLangError() {
847                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_ERROR);
848                 ReferenceBinding type = environment().getType(JAVA_LANG_ERROR);
849                 if (type != null) return type;
850         
851                 problemReporter().isClassPathCorrect(JAVA_LANG_ERROR, referenceCompilationUnit());
852                 return null; // will not get here since the above error aborts the compilation
853         }
854
855         public final ReferenceBinding getJavaLangAssertionError() {
856                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_ASSERTIONERROR);
857                 ReferenceBinding type = environment().getType(JAVA_LANG_ASSERTIONERROR);
858                 if (type != null) return type;
859                 problemReporter().isClassPathCorrect(JAVA_LANG_ASSERTIONERROR, referenceCompilationUnit());
860                 return null; // will not get here since the above error aborts the compilation
861         }
862
863         public final ReferenceBinding getJavaLangObject() {
864                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_OBJECT);
865                 ReferenceBinding type = environment().getType(JAVA_LANG_OBJECT);
866                 if (type != null) return type;
867         
868                 problemReporter().isClassPathCorrect(JAVA_LANG_OBJECT, referenceCompilationUnit());
869                 return null; // will not get here since the above error aborts the compilation
870         }
871
872         public final ReferenceBinding getJavaLangRuntimeException() {
873                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_RUNTIMEEXCEPTION);
874                 ReferenceBinding type = environment().getType(JAVA_LANG_RUNTIMEEXCEPTION);
875                 if (type != null) return type;
876         
877                 problemReporter().isClassPathCorrect(JAVA_LANG_RUNTIMEEXCEPTION, referenceCompilationUnit());
878                 return null; // will not get here since the above error aborts the compilation
879         }
880
881         public final ReferenceBinding getJavaLangString() {
882                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_STRING);
883                 ReferenceBinding type = environment().getType(JAVA_LANG_STRING);
884                 if (type != null) return type;
885         
886                 problemReporter().isClassPathCorrect(JAVA_LANG_STRING, referenceCompilationUnit());
887                 return null; // will not get here since the above error aborts the compilation
888         }
889
890         public final ReferenceBinding getJavaLangThrowable() {
891                 compilationUnitScope().recordQualifiedReference(JAVA_LANG_THROWABLE);
892                 ReferenceBinding type = environment().getType(JAVA_LANG_THROWABLE);
893                 if (type != null) return type;
894         
895 //              problemReporter().isClassPathCorrect(JAVA_LANG_THROWABLE, referenceCompilationUnit());
896                 return null; // will not get here since the above error aborts the compilation
897         }
898
899         /* Answer the type binding corresponding to the typeName argument, relative to the enclosingType.
900         */
901         public final ReferenceBinding getMemberType(char[] typeName, ReferenceBinding enclosingType) {
902                 ReferenceBinding memberType = findMemberType(typeName, enclosingType);
903                 if (memberType != null) return memberType;
904                 return new ProblemReferenceBinding(typeName, NotFound);
905         }
906
907         /* Answer the type binding corresponding to the compoundName.
908         *
909         * NOTE: If a problem binding is returned, senders should extract the compound name
910         * from the binding & not assume the problem applies to the entire compoundName.
911         */
912         public final TypeBinding getType(char[][] compoundName) {
913                 int typeNameLength = compoundName.length;
914                 if (typeNameLength == 1) {
915                         // Would like to remove this test and require senders to specially handle base types
916                         TypeBinding binding = getBaseType(compoundName[0]);
917                         if (binding != null) return binding;
918                 }
919
920                 compilationUnitScope().recordQualifiedReference(compoundName);
921                 Binding binding =
922                         getTypeOrPackage(compoundName[0], typeNameLength == 1 ? TYPE : TYPE | PACKAGE);
923                 if (binding == null)
924                         return new ProblemReferenceBinding(compoundName[0], NotFound);
925                 if (!binding.isValidBinding())
926                         return (ReferenceBinding) binding;
927
928                 int currentIndex = 1;
929                 boolean checkVisibility = false;
930                 if (binding instanceof PackageBinding) {
931                         PackageBinding packageBinding = (PackageBinding) binding;
932                         while (currentIndex < typeNameLength) {
933                                 binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]); // does not check visibility
934                                 if (binding == null)
935                                         return new ProblemReferenceBinding(
936                                                 CharOperation.subarray(compoundName, 0, currentIndex),
937                                                 NotFound);
938                                 if (!binding.isValidBinding())
939                                         return new ProblemReferenceBinding(
940                                                 CharOperation.subarray(compoundName, 0, currentIndex),
941                                                 binding.problemId());
942                                 if (!(binding instanceof PackageBinding))
943                                         break;
944                                 packageBinding = (PackageBinding) binding;
945                         }
946                         if (binding instanceof PackageBinding)
947                                 return new ProblemReferenceBinding(
948                                         CharOperation.subarray(compoundName, 0, currentIndex),
949                                         NotFound);
950                         checkVisibility = true;
951                 }
952
953                 // binding is now a ReferenceBinding
954                 ReferenceBinding typeBinding = (ReferenceBinding) binding;
955                 compilationUnitScope().recordTypeReference(typeBinding); // to record supertypes
956                 if (checkVisibility) // handles the fall through case
957                         if (!typeBinding.canBeSeenBy(this))
958                                 return new ProblemReferenceBinding(
959                                         CharOperation.subarray(compoundName, 0, currentIndex),
960                                         typeBinding,
961                                         NotVisible);
962
963                 while (currentIndex < typeNameLength) {
964                         typeBinding = getMemberType(compoundName[currentIndex++], typeBinding);
965                         if (!typeBinding.isValidBinding())
966                                 return new ProblemReferenceBinding(
967                                         CharOperation.subarray(compoundName, 0, currentIndex),
968                                         typeBinding.problemId());
969                 }
970                 return typeBinding;
971         }
972
973         /* Answer the type binding that corresponds the given name, starting the lookup in the receiver.
974         * The name provided is a simple source name (e.g., "Object" , "Point", ...)
975         */
976         // The return type of this method could be ReferenceBinding if we did not answer base types.
977         // NOTE: We could support looking for Base Types last in the search, however any code using
978         // this feature would be extraordinarily slow.  Therefore we don't do this
979         public final TypeBinding getType(char[] name) {
980                 // Would like to remove this test and require senders to specially handle base types
981                 TypeBinding binding = getBaseType(name);
982                 if (binding != null) return binding;
983                 return (ReferenceBinding) getTypeOrPackage(name, TYPE);
984         }
985
986         // Added for code assist... NOT Public API
987         public final Binding getTypeOrPackage(char[][] compoundName) {
988                 int nameLength = compoundName.length;
989                 if (nameLength == 1) {
990                         TypeBinding binding = getBaseType(compoundName[0]);
991                         if (binding != null) return binding;
992                 }
993                 Binding binding = getTypeOrPackage(compoundName[0], TYPE | PACKAGE);
994                 if (!binding.isValidBinding()) return binding;
995
996                 int currentIndex = 1;
997                 boolean checkVisibility = false;
998                 if (binding instanceof PackageBinding) {
999                         PackageBinding packageBinding = (PackageBinding) binding;
1000
1001                         while (currentIndex < nameLength) {
1002                                 binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]);
1003                                 if (binding == null)
1004                                         return new ProblemReferenceBinding(
1005                                                 CharOperation.subarray(compoundName, 0, currentIndex),
1006                                                 NotFound);
1007                                 if (!binding.isValidBinding())
1008                                         return new ProblemReferenceBinding(
1009                                                 CharOperation.subarray(compoundName, 0, currentIndex),
1010                                                 binding.problemId());
1011                                 if (!(binding instanceof PackageBinding))
1012                                         break;
1013                                 packageBinding = (PackageBinding) binding;
1014                         }
1015                         if (binding instanceof PackageBinding) return binding;
1016                         checkVisibility = true;
1017                 }
1018                 // binding is now a ReferenceBinding
1019                 ReferenceBinding typeBinding = (ReferenceBinding) binding;
1020                 if (checkVisibility) // handles the fall through case
1021                         if (!typeBinding.canBeSeenBy(this))
1022                                 return new ProblemReferenceBinding(
1023                                         CharOperation.subarray(compoundName, 0, currentIndex),
1024                                         typeBinding,
1025                                         NotVisible);
1026
1027                 while (currentIndex < nameLength) {
1028                         typeBinding = getMemberType(compoundName[currentIndex++], typeBinding);
1029                         // checks visibility
1030                         if (!typeBinding.isValidBinding())
1031                                 return new ProblemReferenceBinding(
1032                                         CharOperation.subarray(compoundName, 0, currentIndex),
1033                                         typeBinding.problemId());
1034                 }
1035                 return typeBinding;
1036         }
1037
1038         /* Internal use only 
1039         */
1040         final Binding getTypeOrPackage(char[] name, int mask) {
1041                 Scope scope = this;
1042                 ReferenceBinding foundType = null;
1043                 if ((mask & TYPE) == 0) {
1044                         Scope next = scope;
1045                         while ((next = scope.parent) != null)
1046                                 scope = next;
1047                 } else {
1048                         done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
1049                                 switch (scope.kind) {
1050                                         case METHOD_SCOPE :
1051                                         case BLOCK_SCOPE :
1052                                                 ReferenceBinding localType = ((BlockScope) scope).findLocalType(name); // looks in this scope only
1053                                                 if (localType != null) {
1054                                                         if (foundType != null && foundType != localType)
1055                                                                 return new ProblemReferenceBinding(name, InheritedNameHidesEnclosingName);
1056                                                         return localType;
1057                                                 }
1058                                                 break;
1059                                         case CLASS_SCOPE :
1060                                                 SourceTypeBinding sourceType = ((ClassScope) scope).referenceContext.binding;
1061                                                 // 6.5.5.1 - simple name favors member type over top-level type in same unit
1062                                                 ReferenceBinding memberType = findMemberType(name, sourceType);
1063                                                 if (memberType != null) { // skip it if we did not find anything
1064                                                         if (memberType.problemId() == Ambiguous) {
1065                                                                 if (foundType == null || foundType.problemId() == NotVisible)
1066                                                                         // supercedes any potential InheritedNameHidesEnclosingName problem
1067                                                                         return memberType;
1068                                                                 else
1069                                                                         // make the user qualify the type, likely wants the first inherited type
1070                                                                         return new ProblemReferenceBinding(name, InheritedNameHidesEnclosingName);
1071                                                         }
1072 //                                                      if (memberType.isValidBinding()) {
1073 //                                                              if (sourceType == memberType.enclosingType()
1074 //                                                                              || environment().options.complianceLevel >= CompilerOptions.JDK1_4) {
1075 //                                                                      // found a valid type in the 'immediate' scope (ie. not inherited)
1076 //                                                                      // OR in 1.4 mode (inherited shadows enclosing)
1077 //                                                                      if (foundType == null)
1078 //                                                                              return memberType; 
1079 //                                                                      if (foundType.isValidBinding())
1080 //                                                                              // if a valid type was found, complain when another is found in an 'immediate' enclosing type (ie. not inherited)
1081 //                                                                              if (foundType != memberType)
1082 //                                                                                      return new ProblemReferenceBinding(name, InheritedNameHidesEnclosingName);
1083 //                                                              }
1084 //                                                      }
1085                                                         if (foundType == null || (foundType.problemId() == NotVisible && memberType.problemId() != NotVisible))
1086                                                                 // only remember the memberType if its the first one found or the previous one was not visible & memberType is...
1087                                                                 foundType = memberType;
1088                                                 }
1089                                                 if (CharOperation.equals(sourceType.sourceName, name)) {
1090                                                         if (foundType != null && foundType != sourceType && foundType.problemId() != NotVisible)
1091                                                                 return new ProblemReferenceBinding(name, InheritedNameHidesEnclosingName);
1092                                                         return sourceType;
1093                                                 }
1094                                                 break;
1095                                         case COMPILATION_UNIT_SCOPE :
1096                                                 break done;
1097                                 }
1098                                 scope = scope.parent;
1099                         }
1100                         if (foundType != null && foundType.problemId() != NotVisible)
1101                                 return foundType;
1102                 }
1103
1104                 // at this point the scope is a compilation unit scope
1105                 CompilationUnitScope unitScope = (CompilationUnitScope) scope;
1106                 PackageBinding currentPackage = unitScope.fPackage; 
1107                 // ask for the imports + name
1108                 if ((mask & TYPE) != 0) {
1109                         // check single type imports.
1110                         ImportBinding[] imports = unitScope.imports;
1111                         if (imports != null) {
1112                                 // copy the list, since single type imports are removed if they cannot be resolved
1113                                 for (int i = 0, length = imports.length; i < length; i++) {
1114                                         ImportBinding typeImport = imports[i];
1115                                         if (!typeImport.onDemand) {
1116                                                 if (CharOperation.equals(typeImport.compoundName[typeImport.compoundName.length - 1], name)) {
1117                                                         if (unitScope.resolveSingleTypeImport(typeImport) != null) {
1118                                                                 ImportReference importReference = typeImport.reference;
1119                                                                 if (importReference != null) importReference.used = true;
1120                                                                 return typeImport.resolvedImport; // already know its visible
1121                                                         }
1122                                                 }
1123                                         }
1124                                 }
1125                         }
1126                         // check if the name is in the current package, skip it if its a sub-package
1127                         unitScope.recordReference(currentPackage.compoundName, name);
1128                         Binding binding = currentPackage.getTypeOrPackage(name);
1129                         if (binding instanceof ReferenceBinding) return binding; // type is always visible to its own package
1130
1131                         // check on demand imports
1132                         boolean foundInImport = false;
1133                         ReferenceBinding type = null;
1134                         if (imports != null) {
1135                                 for (int i = 0, length = imports.length; i < length; i++) {
1136                                         ImportBinding someImport = imports[i];
1137                                         if (someImport.onDemand) {
1138                                                 Binding resolvedImport = someImport.resolvedImport;
1139                                                 ReferenceBinding temp = resolvedImport instanceof PackageBinding
1140                                                                 ? findType(name, (PackageBinding) resolvedImport, currentPackage)
1141                                                                 : findDirectMemberType(name, (ReferenceBinding) resolvedImport);
1142                                                 if (temp != null && temp.isValidBinding()) {
1143 //                                                      ImportReference importReference = someImport.reference;
1144 //                                                      if (importReference != null) importReference.used = true;
1145                                                         if (foundInImport)
1146                                                                 // Answer error binding -- import on demand conflict; name found in two import on demand packages.
1147                                                                 return new ProblemReferenceBinding(name, Ambiguous);
1148                                                         type = temp;
1149                                                         foundInImport = true;
1150                                                 }
1151                                         }
1152                                 }
1153                         }
1154                         if (type != null) return type;
1155                 }
1156
1157                 unitScope.recordSimpleReference(name);
1158                 if ((mask & PACKAGE) != 0) {
1159                         PackageBinding packageBinding = unitScope.environment.getTopLevelPackage(name);
1160                         if (packageBinding != null) return packageBinding;
1161                 }
1162
1163                 // Answer error binding -- could not find name
1164                 if (foundType != null) return foundType; // problem type from above
1165                 return new ProblemReferenceBinding(name, NotFound);
1166         }
1167
1168         /* Answer whether the type is defined in the same compilation unit as the receiver
1169         */
1170         public final boolean isDefinedInSameUnit(ReferenceBinding type) {
1171                 // find the outer most enclosing type
1172                 ReferenceBinding enclosingType = type;
1173                 while ((type = enclosingType.enclosingType()) != null)
1174                         enclosingType = type;
1175
1176                 // find the compilation unit scope
1177                 Scope scope, unitScope = this;
1178                 while ((scope = unitScope.parent) != null)
1179                         unitScope = scope;
1180
1181                 // test that the enclosingType is not part of the compilation unit
1182                 SourceTypeBinding[] topLevelTypes =
1183                         ((CompilationUnitScope) unitScope).topLevelTypes;
1184                 for (int i = topLevelTypes.length; --i >= 0;)
1185                         if (topLevelTypes[i] == enclosingType)
1186                                 return true;
1187                 return false;
1188         }
1189
1190         /* Answer true if the scope is nested inside a given field declaration.
1191      * Note: it works as long as the scope.fieldDeclarationIndex is reflecting the field being traversed 
1192      * e.g. during name resolution.
1193         */
1194         public final boolean isDefinedInField(FieldBinding field) {
1195                 Scope scope = this;
1196                 do {
1197                         if (scope instanceof MethodScope) {
1198                                 MethodScope methodScope = (MethodScope) scope;
1199                                 ReferenceContext refContext = methodScope.referenceContext;
1200                                 if (refContext instanceof TypeDeclaration
1201                                                 && ((TypeDeclaration)refContext).binding == field.declaringClass
1202                                                 && methodScope.fieldDeclarationIndex == field.id) {
1203                                         return true;
1204                                 }
1205                         }
1206                         scope = scope.parent;
1207                 } while (scope != null);
1208                 return false;
1209         }
1210
1211         /* Answer true if the scope is nested inside a given method declaration
1212         */
1213         public final boolean isDefinedInMethod(MethodBinding method) {
1214                 Scope scope = this;
1215                 do {
1216                         if (scope instanceof MethodScope) {
1217                                 ReferenceContext refContext = ((MethodScope) scope).referenceContext;
1218                                 if (refContext instanceof AbstractMethodDeclaration
1219                                                 && ((AbstractMethodDeclaration)refContext).binding == method) {
1220                                         return true;
1221                                 }
1222                         }
1223                         scope = scope.parent;
1224                 } while (scope != null);
1225                 return false;
1226         }
1227                 
1228         /* Answer true if the scope is nested inside a given type declaration
1229         */
1230         public final boolean isDefinedInType(ReferenceBinding type) {
1231                 Scope scope = this;
1232                 do {
1233                         if (scope instanceof ClassScope)
1234                                 if (((ClassScope) scope).referenceContext.binding == type){
1235                                         return true;
1236                                 }
1237                         scope = scope.parent;
1238                 } while (scope != null);
1239                 return false;
1240         }
1241
1242         public boolean isInsideDeprecatedCode(){
1243                 switch(kind){
1244                         case Scope.BLOCK_SCOPE :
1245                         case Scope.METHOD_SCOPE :
1246                                 MethodScope methodScope = methodScope();
1247                                 if (!methodScope.isInsideInitializer()){
1248                                         // check method modifiers to see if deprecated
1249                                         MethodBinding context = ((AbstractMethodDeclaration)methodScope.referenceContext).binding;
1250                                         if (context != null && context.isViewedAsDeprecated()) {
1251                                                 return true;
1252                                         }
1253                                 } else {
1254                                         SourceTypeBinding type = ((BlockScope)this).referenceType().binding;
1255
1256                                         // inside field declaration ? check field modifier to see if deprecated
1257                                         if (methodScope.fieldDeclarationIndex != MethodScope.NotInFieldDecl) {
1258                                                 for (int i = 0; i < type.fields.length; i++){
1259                                                         if (type.fields[i].id == methodScope.fieldDeclarationIndex) {
1260                                                                 // currently inside this field initialization
1261                                                                 if (type.fields[i].isViewedAsDeprecated()){
1262                                                                         return true;
1263                                                                 }
1264                                                                 break;
1265                                                         }
1266                                                 }
1267                                         }
1268                                         if (type != null && type.isViewedAsDeprecated()) {
1269                                                 return true;
1270                                         }
1271                                 }
1272                                 break;
1273                         case Scope.CLASS_SCOPE :
1274                                 ReferenceBinding context = ((ClassScope)this).referenceType().binding;
1275                                 if (context != null && context.isViewedAsDeprecated()) {
1276                                         return true;
1277                                 }
1278                                 break;
1279                 }
1280                 return false;
1281         }
1282         
1283         public final boolean isJavaIoSerializable(TypeBinding tb) {
1284                 return tb == getJavaIoSerializable();
1285         }
1286
1287         public final boolean isJavaLangCloneable(TypeBinding tb) {
1288                 return tb == getJavaLangCloneable();
1289         }
1290
1291         public final boolean isJavaLangObject(TypeBinding type) {
1292                 return type.id == T_JavaLangObject;
1293         }
1294
1295         public final MethodScope methodScope() {
1296                 Scope scope = this;
1297                 do {
1298                         if (scope instanceof MethodScope)
1299                                 return (MethodScope) scope;
1300                         scope = scope.parent;
1301                 } while (scope != null);
1302                 return null;
1303         }
1304
1305         // Internal use only
1306         /* All methods in visible are acceptable matches for the method in question...
1307         * The methods defined by the receiver type appear before those defined by its
1308         * superclass and so on. We want to find the one which matches best.
1309         *
1310         * Since the receiver type is a class, we know each method's declaring class is
1311         * either the receiver type or one of its superclasses. It is an error if the best match
1312         * is defined by a superclass, when a lesser match is defined by the receiver type
1313         * or a closer superclass.
1314         */
1315         protected final MethodBinding mostSpecificClassMethodBinding(MethodBinding[] visible, int visibleSize) {
1316
1317                 MethodBinding method = null;
1318                 MethodBinding previous = null;
1319
1320                 nextVisible : for (int i = 0; i < visibleSize; i++) {
1321                         method = visible[i];
1322                                                 
1323                         if (previous != null && method.declaringClass != previous.declaringClass)
1324                                 break; // cannot answer a method farther up the hierarchy than the first method found
1325                         previous = method;
1326                         for (int j = 0; j < visibleSize; j++) {
1327                                 if (i == j) continue;
1328                                 MethodBinding next = visible[j];
1329                                 if (!areParametersAssignable(next.parameters, method.parameters))
1330                                         continue nextVisible;
1331                         }
1332                         compilationUnitScope().recordTypeReferences(method.thrownExceptions);
1333                         return method;
1334                 }
1335                 return new ProblemMethodBinding(visible[0].selector, visible[0].parameters, Ambiguous);
1336         }
1337
1338         // Internal use only
1339         /* All methods in visible are acceptable matches for the method in question...
1340         * Since the receiver type is an interface, we ignore the possibility that 2 inherited
1341         * but unrelated superinterfaces may define the same method in acceptable but
1342         * not identical ways... we just take the best match that we find since any class which
1343         * implements the receiver interface MUST implement all signatures for the method...
1344         * in which case the best match is correct.
1345         *
1346         * NOTE: This is different than javac... in the following example, the message send of
1347         * bar(X) in class Y is supposed to be ambiguous. But any class which implements the
1348         * interface I MUST implement both signatures for bar. If this class was the receiver of
1349         * the message send instead of the interface I, then no problem would be reported.
1350         *
1351         interface I1 {
1352                 void bar(J j);
1353         }
1354         interface I2 {
1355         //      void bar(J j);
1356                 void bar(Object o);
1357         }
1358         interface I extends I1, I2 {}
1359         interface J {}
1360         
1361         class X implements J {}
1362         
1363         class Y extends X {
1364                 public void foo(I i, X x) { i.bar(x); }
1365         }
1366         */
1367         protected final MethodBinding mostSpecificInterfaceMethodBinding(MethodBinding[] visible, int visibleSize) {
1368                 MethodBinding method = null;
1369                 nextVisible : for (int i = 0; i < visibleSize; i++) {
1370                         method = visible[i];
1371                         for (int j = 0; j < visibleSize; j++) {
1372                                 if (i == j) continue;
1373                                 MethodBinding next = visible[j];
1374                                 if (!areParametersAssignable(next.parameters, method.parameters))
1375                                         continue nextVisible;
1376                         }
1377                         compilationUnitScope().recordTypeReferences(method.thrownExceptions);
1378                         return method;
1379                 }
1380                 return new ProblemMethodBinding(visible[0].selector, visible[0].parameters, Ambiguous);
1381         }
1382
1383         public final ClassScope outerMostClassScope() {
1384                 ClassScope lastClassScope = null;
1385                 Scope scope = this;
1386                 do {
1387                         if (scope instanceof ClassScope)
1388                                 lastClassScope = (ClassScope) scope;
1389                         scope = scope.parent;
1390                 } while (scope != null);
1391                 return lastClassScope; // may answer null if no class around
1392         }
1393
1394         public final MethodScope outerMostMethodScope() {
1395                 MethodScope lastMethodScope = null;
1396                 Scope scope = this;
1397                 do {
1398                         if (scope instanceof MethodScope)
1399                                 lastMethodScope = (MethodScope) scope;
1400                         scope = scope.parent;
1401                 } while (scope != null);
1402                 return lastMethodScope; // may answer null if no method around
1403         }
1404
1405         public final CompilationUnitDeclaration referenceCompilationUnit() {
1406                 Scope scope, unitScope = this;
1407                 while ((scope = unitScope.parent) != null)
1408                         unitScope = scope;
1409                 return ((CompilationUnitScope) unitScope).referenceContext;
1410         }
1411         // start position in this scope - for ordering scopes vs. variables
1412         int startIndex() {
1413                 return 0;
1414         }
1415 }