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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.lookup;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
15 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
16 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
17 import net.sourceforge.phpeclipse.internal.compiler.ast.Argument;
18 import net.sourceforge.phpeclipse.internal.compiler.ast.AstNode;
19 import net.sourceforge.phpeclipse.internal.compiler.ast.ConstructorDeclaration;
20 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
22 public class BlockScope extends Scope {
24 // Local variable management
25 public LocalVariableBinding[] locals;
26 public int localIndex; // position for next variable
27 public int startIndex; // start position in this scope - for ordering scopes vs. variables
28 public int offset; // for variable allocation throughout scopes
29 public int maxOffset; // for variable allocation throughout scopes
31 // finally scopes must be shifted behind respective try&catch scope(s) so as to avoid
32 // collisions of secret variables (return address, save value).
33 public BlockScope[] shiftScopes;
35 public final static VariableBinding[] EmulationPathToImplicitThis = {};
36 public final static VariableBinding[] NoEnclosingInstanceInConstructorCall = {};
37 public final static VariableBinding[] NoEnclosingInstanceInStaticContext = {};
39 public Scope[] subscopes = new Scope[1]; // need access from code assist
40 public int scopeIndex = 0; // need access from code assist
42 protected BlockScope(int kind, Scope parent) {
47 public BlockScope(BlockScope parent) {
52 public BlockScope(BlockScope parent, boolean addToParentScope) {
54 this(BLOCK_SCOPE, parent);
55 locals = new LocalVariableBinding[5];
56 if (addToParentScope) parent.addSubscope(this);
57 this.startIndex = parent.localIndex;
60 public BlockScope(BlockScope parent, int variableCount) {
62 this(BLOCK_SCOPE, parent);
63 locals = new LocalVariableBinding[variableCount];
64 parent.addSubscope(this);
65 this.startIndex = parent.localIndex;
68 /* Create the class scope & binding for the anonymous type.
70 public final void addAnonymousType(
71 TypeDeclaration anonymousType,
72 ReferenceBinding superBinding) {
74 ClassScope anonymousClassScope = new ClassScope(this, anonymousType);
75 anonymousClassScope.buildAnonymousTypeBinding(
76 enclosingSourceType(),
80 /* Create the class scope & binding for the local type.
82 public final void addLocalType(TypeDeclaration localType) {
84 // check that the localType does not conflict with an enclosing type
85 ReferenceBinding type = enclosingSourceType();
87 if (CharOperation.equals(type.sourceName, localType.name)) {
88 problemReporter().hidingEnclosingType(localType);
91 type = type.enclosingType();
92 } while (type != null);
94 // check that the localType does not conflict with another sibling local type
97 if (((BlockScope) scope).findLocalType(localType.name) != null) {
98 problemReporter().duplicateNestedType(localType);
101 } while ((scope = scope.parent) instanceof BlockScope);
103 ClassScope localTypeScope = new ClassScope(this, localType);
104 addSubscope(localTypeScope);
105 localTypeScope.buildLocalTypeBinding(enclosingSourceType());
108 /* Insert a local variable into a given scope, updating its position
109 * and checking there are not too many locals or arguments allocated.
111 public final void addLocalVariable(LocalVariableBinding binding) {
113 checkAndSetModifiersForVariable(binding);
115 // insert local in scope
116 if (localIndex == locals.length)
120 (locals = new LocalVariableBinding[localIndex * 2]),
123 locals[localIndex++] = binding;
125 // update local variable binding
126 binding.declaringScope = this;
127 binding.id = this.outerMostMethodScope().analysisIndex++;
128 // share the outermost method scope analysisIndex
131 public void addSubscope(Scope childScope) {
132 if (scopeIndex == subscopes.length)
136 (subscopes = new Scope[scopeIndex * 2]),
139 subscopes[scopeIndex++] = childScope;
142 /* Answer true if the receiver is suitable for assigning final blank fields.
144 * in other words, it is inside an initializer, a constructor or a clinit
146 public final boolean allowBlankFinalFieldAssignment(FieldBinding binding) {
148 if (enclosingSourceType() != binding.declaringClass)
151 MethodScope methodScope = methodScope();
152 if (methodScope.isStatic != binding.isStatic())
154 return methodScope.isInsideInitializer() // inside initializer
155 || ((AbstractMethodDeclaration) methodScope.referenceContext)
156 .isInitializationMethod(); // inside constructor or clinit
158 String basicToString(int tab) {
159 String newLine = "\n"; //$NON-NLS-1$
160 for (int i = tab; --i >= 0;)
161 newLine += "\t"; //$NON-NLS-1$
163 String s = newLine + "--- Block Scope ---"; //$NON-NLS-1$
164 newLine += "\t"; //$NON-NLS-1$
165 s += newLine + "locals:"; //$NON-NLS-1$
166 for (int i = 0; i < localIndex; i++)
167 s += newLine + "\t" + locals[i].toString(); //$NON-NLS-1$
168 s += newLine + "startIndex = " + startIndex; //$NON-NLS-1$
172 private void checkAndSetModifiersForVariable(LocalVariableBinding varBinding) {
174 int modifiers = varBinding.modifiers;
175 if ((modifiers & AccAlternateModifierProblem) != 0 && varBinding.declaration != null){
176 problemReporter().duplicateModifierForVariable(varBinding.declaration, this instanceof MethodScope);
178 int realModifiers = modifiers & AccJustFlag;
180 int unexpectedModifiers = ~AccFinal;
181 if ((realModifiers & unexpectedModifiers) != 0 && varBinding.declaration != null){
182 problemReporter().illegalModifierForVariable(varBinding.declaration, this instanceof MethodScope);
184 varBinding.modifiers = modifiers;
187 /* Compute variable positions in scopes given an initial position offset
188 * ignoring unused local variables.
190 * No argument is expected here (ilocal is the first non-argument local of the outermost scope)
191 * Arguments are managed by the MethodScope method
193 // void computeLocalVariablePositions(int ilocal, int initOffset, CodeStream codeStream) {
195 // this.offset = initOffset;
196 // this.maxOffset = initOffset;
198 // // local variable init
199 // int maxLocals = this.localIndex;
200 // boolean hasMoreVariables = ilocal < maxLocals;
203 // int iscope = 0, maxScopes = this.scopeIndex;
204 // boolean hasMoreScopes = maxScopes > 0;
206 // // iterate scopes and variables in parallel
207 // while (hasMoreVariables || hasMoreScopes) {
209 // && (!hasMoreVariables || (subscopes[iscope].startIndex() <= ilocal))) {
210 // // consider subscope first
211 // if (subscopes[iscope] instanceof BlockScope) {
212 // BlockScope subscope = (BlockScope) subscopes[iscope];
213 // int subOffset = subscope.shiftScopes == null ? this.offset : subscope.maxShiftedOffset();
214 // subscope.computeLocalVariablePositions(0, subOffset, codeStream);
215 // if (subscope.maxOffset > this.maxOffset)
216 // this.maxOffset = subscope.maxOffset;
218 // hasMoreScopes = ++iscope < maxScopes;
221 // // consider variable first
222 // LocalVariableBinding local = locals[ilocal]; // if no local at all, will be locals[ilocal]==null
224 // // check if variable is actually used, and may force it to be preserved
225 // boolean generateCurrentLocalVar = (local.useFlag == LocalVariableBinding.USED && (local.constant == Constant.NotAConstant));
227 // // do not report fake used variable
228 // if (local.useFlag == LocalVariableBinding.UNUSED
229 // && (local.declaration != null) // unused (and non secret) local
230 // && ((local.declaration.bits & AstNode.IsLocalDeclarationReachableMASK) != 0)) { // declaration is reachable
232 // if (!(local.declaration instanceof Argument)) // do not report unused catch arguments
233 // this.problemReporter().unusedLocalVariable(local.declaration);
236 // // could be optimized out, but does need to preserve unread variables ?
237 //// if (!generateCurrentLocalVar) {
238 //// if (local.declaration != null && environment().options.preserveAllLocalVariables) {
239 //// generateCurrentLocalVar = true; // force it to be preserved in the generated code
240 //// local.useFlag = LocalVariableBinding.USED;
244 // // allocate variable
245 // if (generateCurrentLocalVar) {
247 // if (local.declaration != null) {
248 // codeStream.record(local); // record user-defined local variables for attribute generation
250 // // assign variable position
251 // local.resolvedPosition = this.offset;
253 // if ((local.type == LongBinding) || (local.type == DoubleBinding)) {
258 // if (this.offset > 0xFFFF) { // no more than 65535 words of locals
259 // this.problemReporter().noMoreAvailableSpaceForLocal(
261 // local.declaration == null ? (AstNode)this.methodScope().referenceContext : local.declaration);
264 // local.resolvedPosition = -1; // not generated
266 // hasMoreVariables = ++ilocal < maxLocals;
269 // if (this.offset > this.maxOffset)
270 // this.maxOffset = this.offset;
273 /* Answer true if the variable name already exists within the receiver's scope.
275 public final LocalVariableBinding duplicateName(char[] name) {
276 for (int i = 0; i < localIndex; i++)
277 if (CharOperation.equals(name, locals[i].name))
280 if (this instanceof MethodScope)
283 return ((BlockScope) parent).duplicateName(name);
287 * Record the suitable binding denoting a synthetic field or constructor argument,
288 * mapping to the actual outer local variable in the scope context.
289 * Note that this may not need any effect, in case the outer local variable does not
290 * need to be emulated and can directly be used as is (using its back pointer to its
293 public void emulateOuterAccess(LocalVariableBinding outerLocalVariable) {
295 MethodScope currentMethodScope;
296 if ((currentMethodScope = this.methodScope())
297 != outerLocalVariable.declaringScope.methodScope()) {
298 NestedTypeBinding currentType = (NestedTypeBinding) this.enclosingSourceType();
300 //do nothing for member types, pre emulation was performed already
301 if (!currentType.isLocalType()) {
304 // must also add a synthetic field if we're not inside a constructor
305 if (!currentMethodScope.isInsideInitializerOrConstructor()) {
306 currentType.addSyntheticArgumentAndField(outerLocalVariable);
308 currentType.addSyntheticArgument(outerLocalVariable);
313 /* Note that it must never produce a direct access to the targetEnclosingType,
314 * but instead a field sequence (this$2.this$1.this$0) so as to handle such a test case:
322 * return (Object) A.this == (Object) B.this;
327 * new A().new B().new C();
330 * where we only want to deal with ONE enclosing instance for C (could not figure out an A for C)
332 public final ReferenceBinding findLocalType(char[] name) {
334 for (int i = 0, length = scopeIndex; i < length; i++) {
335 if (subscopes[i] instanceof ClassScope) {
336 SourceTypeBinding sourceType =
337 ((ClassScope) subscopes[i]).referenceContext.binding;
338 if (CharOperation.equals(sourceType.sourceName(), name))
345 public LocalVariableBinding findVariable(char[] variable) {
347 int variableLength = variable.length;
348 for (int i = 0, length = locals.length; i < length; i++) {
349 LocalVariableBinding local = locals[i];
352 if (local.name.length == variableLength
353 && CharOperation.prefixEquals(local.name, variable))
359 * flag is a mask of the following values VARIABLE (= FIELD or LOCAL), TYPE.
360 * Only bindings corresponding to the mask will be answered.
362 * if the VARIABLE mask is set then
363 * If the first name provided is a field (or local) then the field (or local) is answered
364 * Otherwise, package names and type names are consumed until a field is found.
365 * In this case, the field is answered.
367 * if the TYPE mask is set,
368 * package names and type names are consumed until the end of the input.
369 * Only if all of the input is consumed is the type answered
371 * All other conditions are errors, and a problem binding is returned.
373 * NOTE: If a problem binding is returned, senders should extract the compound name
374 * from the binding & not assume the problem applies to the entire compoundName.
376 * The VARIABLE mask has precedence over the TYPE mask.
378 * InvocationSite implements
379 * isSuperAccess(); this is used to determine if the discovered field is visible.
380 * setFieldIndex(int); this is used to record the number of names that were consumed.
382 * For example, getBinding({"foo","y","q", VARIABLE, site) will answer
383 * the binding for the field or local named "foo" (or an error binding if none exists).
384 * In addition, setFieldIndex(1) will be sent to the invocation site.
385 * If a type named "foo" exists, it will not be detected (and an error binding will be answered)
387 * IMPORTANT NOTE: This method is written under the assumption that compoundName is longer than length 1.
389 public Binding getBinding(char[][] compoundName, int mask, InvocationSite invocationSite) {
391 Binding binding = getBinding(compoundName[0], mask | TYPE | PACKAGE, invocationSite);
392 invocationSite.setFieldIndex(1);
393 if (binding instanceof VariableBinding) return binding;
394 compilationUnitScope().recordSimpleReference(compoundName[0]);
395 if (!binding.isValidBinding()) return binding;
397 int length = compoundName.length;
398 int currentIndex = 1;
399 foundType : if (binding instanceof PackageBinding) {
400 PackageBinding packageBinding = (PackageBinding) binding;
401 while (currentIndex < length) {
402 compilationUnitScope().recordReference(packageBinding.compoundName, compoundName[currentIndex]);
403 binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]);
404 invocationSite.setFieldIndex(currentIndex);
405 if (binding == null) {
406 if (currentIndex == length)
407 // must be a type if its the last name, otherwise we have no idea if its a package or type
408 return new ProblemReferenceBinding(
409 CharOperation.subarray(compoundName, 0, currentIndex),
412 return new ProblemBinding(
413 CharOperation.subarray(compoundName, 0, currentIndex),
416 if (binding instanceof ReferenceBinding) {
417 if (!binding.isValidBinding())
418 return new ProblemReferenceBinding(
419 CharOperation.subarray(compoundName, 0, currentIndex),
420 binding.problemId());
421 if (!((ReferenceBinding) binding).canBeSeenBy(this))
422 return new ProblemReferenceBinding(
423 CharOperation.subarray(compoundName, 0, currentIndex),
428 packageBinding = (PackageBinding) binding;
431 // It is illegal to request a PACKAGE from this method.
432 return new ProblemReferenceBinding(
433 CharOperation.subarray(compoundName, 0, currentIndex),
437 // know binding is now a ReferenceBinding
438 while (currentIndex < length) {
439 ReferenceBinding typeBinding = (ReferenceBinding) binding;
440 char[] nextName = compoundName[currentIndex++];
441 invocationSite.setFieldIndex(currentIndex);
442 invocationSite.setActualReceiverType(typeBinding);
443 if ((mask & FIELD) != 0 && (binding = findField(typeBinding, nextName, invocationSite)) != null) {
444 if (!binding.isValidBinding())
445 return new ProblemFieldBinding(
446 ((FieldBinding) binding).declaringClass,
447 CharOperation.subarray(compoundName, 0, currentIndex),
448 binding.problemId());
449 break; // binding is now a field
451 if ((binding = findMemberType(nextName, typeBinding)) == null) {
452 if ((mask & FIELD) != 0) {
453 return new ProblemBinding(
454 CharOperation.subarray(compoundName, 0, currentIndex),
458 return new ProblemReferenceBinding(
459 CharOperation.subarray(compoundName, 0, currentIndex),
464 if (!binding.isValidBinding())
465 return new ProblemReferenceBinding(
466 CharOperation.subarray(compoundName, 0, currentIndex),
467 binding.problemId());
470 if ((mask & FIELD) != 0 && (binding instanceof FieldBinding)) {
471 // was looking for a field and found a field
472 FieldBinding field = (FieldBinding) binding;
473 if (!field.isStatic())
474 return new ProblemFieldBinding(
475 field.declaringClass,
476 CharOperation.subarray(compoundName, 0, currentIndex),
477 NonStaticReferenceInStaticContext);
480 if ((mask & TYPE) != 0 && (binding instanceof ReferenceBinding)) {
481 // was looking for a type and found a type
485 // handle the case when a field or type was asked for but we resolved the compoundName to a type or field
486 return new ProblemBinding(
487 CharOperation.subarray(compoundName, 0, currentIndex),
491 // Added for code assist... NOT Public API
492 public final Binding getBinding(
493 char[][] compoundName,
494 InvocationSite invocationSite) {
495 int currentIndex = 0;
496 int length = compoundName.length;
499 compoundName[currentIndex++],
500 VARIABLE | TYPE | PACKAGE,
502 if (!binding.isValidBinding())
505 foundType : if (binding instanceof PackageBinding) {
506 while (currentIndex < length) {
507 PackageBinding packageBinding = (PackageBinding) binding;
508 binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]);
509 if (binding == null) {
510 if (currentIndex == length)
511 // must be a type if its the last name, otherwise we have no idea if its a package or type
512 return new ProblemReferenceBinding(
513 CharOperation.subarray(compoundName, 0, currentIndex),
516 return new ProblemBinding(
517 CharOperation.subarray(compoundName, 0, currentIndex),
520 if (binding instanceof ReferenceBinding) {
521 if (!binding.isValidBinding())
522 return new ProblemReferenceBinding(
523 CharOperation.subarray(compoundName, 0, currentIndex),
524 binding.problemId());
525 if (!((ReferenceBinding) binding).canBeSeenBy(this))
526 return new ProblemReferenceBinding(
527 CharOperation.subarray(compoundName, 0, currentIndex),
536 foundField : if (binding instanceof ReferenceBinding) {
537 while (currentIndex < length) {
538 ReferenceBinding typeBinding = (ReferenceBinding) binding;
539 char[] nextName = compoundName[currentIndex++];
540 if ((binding = findField(typeBinding, nextName, invocationSite)) != null) {
541 if (!binding.isValidBinding())
542 return new ProblemFieldBinding(
543 ((FieldBinding) binding).declaringClass,
544 CharOperation.subarray(compoundName, 0, currentIndex),
545 binding.problemId());
546 if (!((FieldBinding) binding).isStatic())
547 return new ProblemFieldBinding(
548 ((FieldBinding) binding).declaringClass,
549 CharOperation.subarray(compoundName, 0, currentIndex),
550 NonStaticReferenceInStaticContext);
551 break foundField; // binding is now a field
553 if ((binding = findMemberType(nextName, typeBinding)) == null)
554 return new ProblemBinding(
555 CharOperation.subarray(compoundName, 0, currentIndex),
558 if (!binding.isValidBinding())
559 return new ProblemReferenceBinding(
560 CharOperation.subarray(compoundName, 0, currentIndex),
561 binding.problemId());
566 VariableBinding variableBinding = (VariableBinding) binding;
567 while (currentIndex < length) {
568 TypeBinding typeBinding = variableBinding.type;
569 if (typeBinding == null)
570 return new ProblemFieldBinding(
572 CharOperation.subarray(compoundName, 0, currentIndex + 1),
575 findField(typeBinding, compoundName[currentIndex++], invocationSite);
576 if (variableBinding == null)
577 return new ProblemFieldBinding(
579 CharOperation.subarray(compoundName, 0, currentIndex),
581 if (!variableBinding.isValidBinding())
582 return variableBinding;
584 return variableBinding;
589 * Answer the binding that corresponds to the argument name.
590 * flag is a mask of the following values VARIABLE (= FIELD or LOCAL), TYPE, PACKAGE.
591 * Only bindings corresponding to the mask can be answered.
593 * For example, getBinding("foo", VARIABLE, site) will answer
594 * the binding for the field or local named "foo" (or an error binding if none exists).
595 * If a type named "foo" exists, it will not be detected (and an error binding will be answered)
597 * The VARIABLE mask has precedence over the TYPE mask.
599 * If the VARIABLE mask is not set, neither fields nor locals will be looked for.
601 * InvocationSite implements:
602 * isSuperAccess(); this is used to determine if the discovered field is visible.
604 * Limitations: cannot request FIELD independently of LOCAL, or vice versa
606 public Binding getBinding(char[] name, int mask, InvocationSite invocationSite) {
608 Binding binding = null;
609 FieldBinding problemField = null;
610 if ((mask & VARIABLE) != 0) {
611 if (this.kind == BLOCK_SCOPE || this.kind == METHOD_SCOPE) {
612 LocalVariableBinding variableBinding = findVariable(name);
613 // looks in this scope only
614 if (variableBinding != null) return variableBinding;
617 boolean insideStaticContext = false;
618 boolean insideConstructorCall = false;
619 if (this.kind == METHOD_SCOPE) {
620 MethodScope methodScope = (MethodScope) this;
621 insideStaticContext |= methodScope.isStatic;
622 insideConstructorCall |= methodScope.isConstructorCall;
625 FieldBinding foundField = null;
626 // can be a problem field which is answered if a valid field is not found
627 ProblemFieldBinding foundInsideProblem = null;
628 // inside Constructor call or inside static context
629 Scope scope = parent;
632 ReferenceBinding foundActualReceiverType = null;
633 done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
634 switch (scope.kind) {
636 MethodScope methodScope = (MethodScope) scope;
637 insideStaticContext |= methodScope.isStatic;
638 insideConstructorCall |= methodScope.isConstructorCall;
639 // Fall through... could duplicate the code below to save a cast - questionable optimization
641 LocalVariableBinding variableBinding = ((BlockScope) scope).findVariable(name);
642 // looks in this scope only
643 if (variableBinding != null) {
644 if (foundField != null && foundField.isValidBinding())
645 return new ProblemFieldBinding(
646 foundField.declaringClass,
648 InheritedNameHidesEnclosingName);
650 invocationSite.setDepth(depth);
651 return variableBinding;
655 ClassScope classScope = (ClassScope) scope;
656 SourceTypeBinding enclosingType = classScope.referenceContext.binding;
657 FieldBinding fieldBinding =
658 classScope.findField(enclosingType, name, invocationSite);
659 // Use next line instead if willing to enable protected access accross inner types
660 // FieldBinding fieldBinding = findField(enclosingType, name, invocationSite);
661 if (fieldBinding != null) { // skip it if we did not find anything
662 if (fieldBinding.problemId() == Ambiguous) {
663 if (foundField == null || foundField.problemId() == NotVisible)
664 // supercedes any potential InheritedNameHidesEnclosingName problem
667 // make the user qualify the field, likely wants the first inherited field (javac generates an ambiguous error instead)
668 return new ProblemFieldBinding(
669 fieldBinding.declaringClass,
671 InheritedNameHidesEnclosingName);
674 ProblemFieldBinding insideProblem = null;
675 if (fieldBinding.isValidBinding()) {
676 if (!fieldBinding.isStatic()) {
677 if (insideConstructorCall) {
679 new ProblemFieldBinding(
680 fieldBinding.declaringClass,
682 NonStaticReferenceInConstructorInvocation);
683 } else if (insideStaticContext) {
685 new ProblemFieldBinding(
686 fieldBinding.declaringClass,
688 NonStaticReferenceInStaticContext);
691 // if (enclosingType == fieldBinding.declaringClass
692 // || environment().options.complianceLevel >= CompilerOptions.JDK1_4){
693 // // found a valid field in the 'immediate' scope (ie. not inherited)
694 // // OR in 1.4 mode (inherited shadows enclosing)
695 // if (foundField == null) {
697 // invocationSite.setDepth(depth);
698 // invocationSite.setActualReceiverType(enclosingType);
700 // // return the fieldBinding if it is not declared in a superclass of the scope's binding (that is, inherited)
701 // return insideProblem == null ? fieldBinding : insideProblem;
703 // if (foundField.isValidBinding())
704 // // if a valid field was found, complain when another is found in an 'immediate' enclosing type (that is, not inherited)
705 // if (foundField.declaringClass != fieldBinding.declaringClass)
706 // // ie. have we found the same field - do not trust field identity yet
707 // return new ProblemFieldBinding(
708 // fieldBinding.declaringClass,
710 // InheritedNameHidesEnclosingName);
714 if (foundField == null
715 || (foundField.problemId() == NotVisible
716 && fieldBinding.problemId() != NotVisible)) {
717 // only remember the fieldBinding if its the first one found or the previous one was not visible & fieldBinding is...
719 foundActualReceiverType = enclosingType;
720 foundInsideProblem = insideProblem;
721 foundField = fieldBinding;
725 insideStaticContext |= enclosingType.isStatic();
726 // 1EX5I8Z - accessing outer fields within a constructor call is permitted
727 // in order to do so, we change the flag as we exit from the type, not the method
728 // itself, because the class scope is used to retrieve the fields.
729 MethodScope enclosingMethodScope = scope.methodScope();
730 insideConstructorCall =
731 enclosingMethodScope == null ? false : enclosingMethodScope.isConstructorCall;
733 case COMPILATION_UNIT_SCOPE :
736 scope = scope.parent;
739 if (foundInsideProblem != null){
740 return foundInsideProblem;
742 if (foundField != null) {
743 if (foundField.isValidBinding()){
745 invocationSite.setDepth(foundDepth);
746 invocationSite.setActualReceiverType(foundActualReceiverType);
750 problemField = foundField;
754 // We did not find a local or instance variable.
755 if ((mask & TYPE) != 0) {
756 if ((binding = getBaseType(name)) != null)
758 binding = getTypeOrPackage(name, (mask & PACKAGE) == 0 ? TYPE : TYPE | PACKAGE);
759 if (binding.isValidBinding() || mask == TYPE)
761 // answer the problem type binding if we are only looking for a type
762 } else if ((mask & PACKAGE) != 0) {
763 compilationUnitScope().recordSimpleReference(name);
764 if ((binding = environment().getTopLevelPackage(name)) != null)
767 if (problemField != null)
770 return new ProblemBinding(name, enclosingSourceType(), NotFound);
775 * Answer the constructor binding that corresponds to receiverType, argumentTypes.
777 * InvocationSite implements
778 * isSuperAccess(); this is used to determine if the discovered constructor is visible.
780 * If no visible constructor is discovered, an error binding is answered.
782 public MethodBinding getConstructor(
783 ReferenceBinding receiverType,
784 TypeBinding[] argumentTypes,
785 InvocationSite invocationSite) {
787 compilationUnitScope().recordTypeReference(receiverType);
788 compilationUnitScope().recordTypeReferences(argumentTypes);
789 MethodBinding methodBinding = receiverType.getExactConstructor(argumentTypes);
790 if (methodBinding != null) {
791 if (methodBinding.canBeSeenBy(invocationSite, this))
792 return methodBinding;
794 MethodBinding[] methods =
795 receiverType.getMethods(ConstructorDeclaration.ConstantPoolName);
796 if (methods == NoMethods) {
797 return new ProblemMethodBinding(
798 ConstructorDeclaration.ConstantPoolName,
802 MethodBinding[] compatible = new MethodBinding[methods.length];
803 int compatibleIndex = 0;
804 for (int i = 0, length = methods.length; i < length; i++)
805 if (areParametersAssignable(methods[i].parameters, argumentTypes))
806 compatible[compatibleIndex++] = methods[i];
807 if (compatibleIndex == 0)
808 return new ProblemMethodBinding(
809 ConstructorDeclaration.ConstantPoolName,
812 // need a more descriptive error... cannot convert from X to Y
814 MethodBinding[] visible = new MethodBinding[compatibleIndex];
815 int visibleIndex = 0;
816 for (int i = 0; i < compatibleIndex; i++) {
817 MethodBinding method = compatible[i];
818 if (method.canBeSeenBy(invocationSite, this))
819 visible[visibleIndex++] = method;
821 if (visibleIndex == 1)
823 if (visibleIndex == 0)
824 return new ProblemMethodBinding(
826 ConstructorDeclaration.ConstantPoolName,
827 compatible[0].parameters,
829 return mostSpecificClassMethodBinding(visible, visibleIndex);
833 * This retrieves the argument that maps to an enclosing instance of the suitable type,
834 * if not found then answers nil -- do not create one
836 * #implicitThis : the implicit this will be ok
837 * #((arg) this$n) : available as a constructor arg
838 * #((arg) this$n ... this$p) : available as as a constructor arg + a sequence of fields
839 * #((fieldDescr) this$n ... this$p) : available as a sequence of fields
842 * Note that this algorithm should answer the shortest possible sequence when
843 * shortcuts are available:
844 * this$0 . this$0 . this$0
846 * this$2 . this$1 . this$0 . this$1 . this$0
847 * thus the code generation will be more compact and runtime faster
849 public VariableBinding[] getEmulationPath(LocalVariableBinding outerLocalVariable) {
851 MethodScope currentMethodScope = this.methodScope();
852 SourceTypeBinding sourceType = currentMethodScope.enclosingSourceType();
855 if (currentMethodScope == outerLocalVariable.declaringScope.methodScope()) {
856 return new VariableBinding[] { outerLocalVariable };
857 // implicit this is good enough
859 // use synthetic constructor arguments if possible
860 if (currentMethodScope.isInsideInitializerOrConstructor()
861 && (sourceType.isNestedType())) {
862 SyntheticArgumentBinding syntheticArg;
863 if ((syntheticArg = ((NestedTypeBinding) sourceType).getSyntheticArgument(outerLocalVariable)) != null) {
864 return new VariableBinding[] { syntheticArg };
867 // use a synthetic field then
868 if (!currentMethodScope.isStatic) {
869 FieldBinding syntheticField;
870 if ((syntheticField = sourceType.getSyntheticField(outerLocalVariable)) != null) {
871 return new VariableBinding[] { syntheticField };
878 * This retrieves the argument that maps to an enclosing instance of the suitable type,
879 * if not found then answers nil -- do not create one
881 * #implicitThis : the implicit this will be ok
882 * #((arg) this$n) : available as a constructor arg
883 * #((arg) this$n access$m... access$p) : available as as a constructor arg + a sequence of synthetic accessors to synthetic fields
884 * #((fieldDescr) this$n access#m... access$p) : available as a first synthetic field + a sequence of synthetic accessors to synthetic fields
888 public Object[] getEmulationPath(
889 ReferenceBinding targetEnclosingType,
890 boolean onlyExactMatch,
891 boolean ignoreEnclosingArgInConstructorCall) {
892 //TODO: (philippe) investigate why exactly test76 fails if ignoreEnclosingArgInConstructorCall is always false
893 MethodScope currentMethodScope = this.methodScope();
894 SourceTypeBinding sourceType = currentMethodScope.enclosingSourceType();
897 if (!currentMethodScope.isStatic
898 && (!currentMethodScope.isConstructorCall || ignoreEnclosingArgInConstructorCall)
899 && (sourceType == targetEnclosingType
900 || (!onlyExactMatch && targetEnclosingType.isSuperclassOf(sourceType)))) {
901 if (currentMethodScope.isConstructorCall) {
902 return NoEnclosingInstanceInConstructorCall;
904 if (currentMethodScope.isStatic){
905 return NoEnclosingInstanceInStaticContext;
907 return EmulationPathToImplicitThis; // implicit this is good enough
909 if (!sourceType.isNestedType() || sourceType.isStatic()) { // no emulation from within non-inner types
910 if (currentMethodScope.isConstructorCall) {
911 return NoEnclosingInstanceInConstructorCall;
913 if (currentMethodScope.isStatic){
914 return NoEnclosingInstanceInStaticContext;
918 boolean insideConstructor = currentMethodScope.isInsideInitializerOrConstructor();
919 // use synthetic constructor arguments if possible
920 if (insideConstructor) {
921 SyntheticArgumentBinding syntheticArg;
922 if ((syntheticArg = ((NestedTypeBinding) sourceType).getSyntheticArgument(targetEnclosingType, onlyExactMatch)) != null) {
923 return new Object[] { syntheticArg };
927 // use a direct synthetic field then
928 if (currentMethodScope.isStatic) {
929 return NoEnclosingInstanceInStaticContext;
931 FieldBinding syntheticField = sourceType.getSyntheticField(targetEnclosingType, onlyExactMatch);
932 if (syntheticField != null) {
933 if (currentMethodScope.isConstructorCall){
934 return NoEnclosingInstanceInConstructorCall;
936 return new Object[] { syntheticField };
938 // could be reached through a sequence of enclosing instance link (nested members)
939 Object[] path = new Object[2]; // probably at least 2 of them
940 ReferenceBinding currentType = sourceType.enclosingType();
941 if (insideConstructor) {
942 path[0] = ((NestedTypeBinding) sourceType).getSyntheticArgument((SourceTypeBinding) currentType, onlyExactMatch);
944 if (currentMethodScope.isConstructorCall){
945 return NoEnclosingInstanceInConstructorCall;
947 path[0] = sourceType.getSyntheticField((SourceTypeBinding) currentType, onlyExactMatch);
949 if (path[0] != null) { // keep accumulating
952 ReferenceBinding currentEnclosingType;
953 while ((currentEnclosingType = currentType.enclosingType()) != null) {
956 if (currentType == targetEnclosingType
957 || (!onlyExactMatch && targetEnclosingType.isSuperclassOf(currentType))) break;
959 if (currentMethodScope != null) {
960 currentMethodScope = currentMethodScope.enclosingMethodScope();
961 if (currentMethodScope != null && currentMethodScope.isConstructorCall){
962 return NoEnclosingInstanceInConstructorCall;
964 if (currentMethodScope != null && currentMethodScope.isStatic){
965 return NoEnclosingInstanceInStaticContext;
969 syntheticField = ((NestedTypeBinding) currentType).getSyntheticField((SourceTypeBinding) currentEnclosingType, onlyExactMatch);
970 if (syntheticField == null) break;
972 // append inside the path
973 if (count == path.length) {
974 System.arraycopy(path, 0, (path = new Object[count + 1]), 0, count);
976 // private access emulation is necessary since synthetic field is private
977 path[count++] = ((SourceTypeBinding) syntheticField.declaringClass).addSyntheticMethod(syntheticField, true);
978 currentType = currentEnclosingType;
980 if (currentType == targetEnclosingType
981 || (!onlyExactMatch && targetEnclosingType.isSuperclassOf(currentType))) {
990 * Answer the field binding that corresponds to fieldName.
991 * Start the lookup at the receiverType.
992 * InvocationSite implements
993 * isSuperAccess(); this is used to determine if the discovered field is visible.
994 * Only fields defined by the receiverType or its supertypes are answered;
995 * a field of an enclosing type will not be found using this API.
997 * If no visible field is discovered, an error binding is answered.
999 public FieldBinding getField(
1000 TypeBinding receiverType,
1002 InvocationSite invocationSite) {
1004 FieldBinding field = findField(receiverType, fieldName, invocationSite);
1006 return new ProblemFieldBinding(
1007 receiverType instanceof ReferenceBinding
1008 ? (ReferenceBinding) receiverType
1018 * Answer the method binding that corresponds to selector, argumentTypes.
1019 * Start the lookup at the enclosing type of the receiver.
1020 * InvocationSite implements
1021 * isSuperAccess(); this is used to determine if the discovered method is visible.
1022 * setDepth(int); this is used to record the depth of the discovered method
1023 * relative to the enclosing type of the receiver. (If the method is defined
1024 * in the enclosing type of the receiver, the depth is 0; in the next enclosing
1025 * type, the depth is 1; and so on
1027 * If no visible method is discovered, an error binding is answered.
1029 public MethodBinding getImplicitMethod(
1031 TypeBinding[] argumentTypes,
1032 InvocationSite invocationSite) {
1034 boolean insideStaticContext = false;
1035 boolean insideConstructorCall = false;
1036 MethodBinding foundMethod = null;
1037 ProblemMethodBinding foundFuzzyProblem = null;
1038 // the weird method lookup case (matches method name in scope, then arg types, then visibility)
1039 ProblemMethodBinding foundInsideProblem = null;
1040 // inside Constructor call or inside static context
1043 done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
1044 switch (scope.kind) {
1046 MethodScope methodScope = (MethodScope) scope;
1047 insideStaticContext |= methodScope.isStatic;
1048 insideConstructorCall |= methodScope.isConstructorCall;
1051 ClassScope classScope = (ClassScope) scope;
1052 SourceTypeBinding receiverType = classScope.referenceContext.binding;
1053 boolean isExactMatch = true;
1054 // retrieve an exact visible match (if possible)
1055 MethodBinding methodBinding =
1056 (foundMethod == null)
1057 ? classScope.findExactMethod(
1062 : classScope.findExactMethod(
1064 foundMethod.selector,
1065 foundMethod.parameters,
1067 // ? findExactMethod(receiverType, selector, argumentTypes, invocationSite)
1068 // : findExactMethod(receiverType, foundMethod.selector, foundMethod.parameters, invocationSite);
1069 if (methodBinding == null) {
1070 // answers closest approximation, may not check argumentTypes or visibility
1071 isExactMatch = false;
1073 classScope.findMethod(receiverType, selector, argumentTypes, invocationSite);
1074 // methodBinding = findMethod(receiverType, selector, argumentTypes, invocationSite);
1076 if (methodBinding != null) { // skip it if we did not find anything
1077 if (methodBinding.problemId() == Ambiguous) {
1078 if (foundMethod == null || foundMethod.problemId() == NotVisible)
1079 // supercedes any potential InheritedNameHidesEnclosingName problem
1080 return methodBinding;
1082 // make the user qualify the method, likely wants the first inherited method (javac generates an ambiguous error instead)
1083 return new ProblemMethodBinding(
1086 InheritedNameHidesEnclosingName);
1089 ProblemMethodBinding fuzzyProblem = null;
1090 ProblemMethodBinding insideProblem = null;
1091 if (methodBinding.isValidBinding()) {
1092 if (!isExactMatch) {
1093 if (!areParametersAssignable(methodBinding.parameters, argumentTypes)) {
1094 if (foundMethod == null || foundMethod.problemId() == NotVisible){
1095 // inherited mismatch is reported directly, not looking at enclosing matches
1096 return new ProblemMethodBinding(methodBinding, selector, argumentTypes, NotFound);
1098 // make the user qualify the method, likely wants the first inherited method (javac generates an ambiguous error instead)
1099 fuzzyProblem = new ProblemMethodBinding(selector, methodBinding.parameters, InheritedNameHidesEnclosingName);
1101 } else if (!methodBinding.canBeSeenBy(receiverType, invocationSite, classScope)) {
1102 // using <classScope> instead of <this> for visibility check does grant all access to innerclass
1104 new ProblemMethodBinding(
1107 methodBinding.parameters,
1111 if (fuzzyProblem == null && !methodBinding.isStatic()) {
1112 if (insideConstructorCall) {
1114 new ProblemMethodBinding(
1115 methodBinding.selector,
1116 methodBinding.parameters,
1117 NonStaticReferenceInConstructorInvocation);
1118 } else if (insideStaticContext) {
1120 new ProblemMethodBinding(
1121 methodBinding.selector,
1122 methodBinding.parameters,
1123 NonStaticReferenceInStaticContext);
1127 // if (receiverType == methodBinding.declaringClass
1128 // || (receiverType.getMethods(selector)) != NoMethods
1129 // || ((fuzzyProblem == null || fuzzyProblem.problemId() != NotVisible) && environment().options.complianceLevel >= CompilerOptions.JDK1_4)){
1130 // // found a valid method in the 'immediate' scope (ie. not inherited)
1131 // // OR the receiverType implemented a method with the correct name
1132 // // OR in 1.4 mode (inherited visible shadows enclosing)
1133 // if (foundMethod == null) {
1135 // invocationSite.setDepth(depth);
1136 // invocationSite.setActualReceiverType(receiverType);
1138 // // return the methodBinding if it is not declared in a superclass of the scope's binding (that is, inherited)
1139 // if (fuzzyProblem != null)
1140 // return fuzzyProblem;
1141 // if (insideProblem != null)
1142 // return insideProblem;
1143 // return methodBinding;
1145 // // if a method was found, complain when another is found in an 'immediate' enclosing type (that is, not inherited)
1146 // // NOTE: Unlike fields, a non visible method hides a visible method
1147 // if (foundMethod.declaringClass != methodBinding.declaringClass)
1148 // // ie. have we found the same method - do not trust field identity yet
1149 // return new ProblemMethodBinding(
1150 // methodBinding.selector,
1151 // methodBinding.parameters,
1152 // InheritedNameHidesEnclosingName);
1156 if (foundMethod == null
1157 || (foundMethod.problemId() == NotVisible
1158 && methodBinding.problemId() != NotVisible)) {
1159 // only remember the methodBinding if its the first one found or the previous one was not visible & methodBinding is...
1160 // remember that private methods are visible if defined directly by an enclosing class
1162 invocationSite.setDepth(depth);
1163 invocationSite.setActualReceiverType(receiverType);
1165 foundFuzzyProblem = fuzzyProblem;
1166 foundInsideProblem = insideProblem;
1167 if (fuzzyProblem == null)
1168 foundMethod = methodBinding; // only keep it if no error was found
1172 insideStaticContext |= receiverType.isStatic();
1173 // 1EX5I8Z - accessing outer fields within a constructor call is permitted
1174 // in order to do so, we change the flag as we exit from the type, not the method
1175 // itself, because the class scope is used to retrieve the fields.
1176 MethodScope enclosingMethodScope = scope.methodScope();
1177 insideConstructorCall =
1178 enclosingMethodScope == null ? false : enclosingMethodScope.isConstructorCall;
1180 case COMPILATION_UNIT_SCOPE :
1183 scope = scope.parent;
1186 if (foundFuzzyProblem != null)
1187 return foundFuzzyProblem;
1188 if (foundInsideProblem != null)
1189 return foundInsideProblem;
1190 if (foundMethod != null)
1192 return new ProblemMethodBinding(selector, argumentTypes, NotFound);
1197 * Answer the method binding that corresponds to selector, argumentTypes.
1198 * Start the lookup at the receiverType.
1199 * InvocationSite implements
1200 * isSuperAccess(); this is used to determine if the discovered method is visible.
1202 * Only methods defined by the receiverType or its supertypes are answered;
1203 * use getImplicitMethod() to discover methods of enclosing types.
1205 * If no visible method is discovered, an error binding is answered.
1207 public MethodBinding getMethod(
1208 TypeBinding receiverType,
1210 TypeBinding[] argumentTypes,
1211 InvocationSite invocationSite) {
1213 if (receiverType.isArrayType())
1214 return findMethodForArray(
1215 (ArrayBinding) receiverType,
1219 if (receiverType.isBaseType())
1220 return new ProblemMethodBinding(selector, argumentTypes, NotFound);
1222 ReferenceBinding currentType = (ReferenceBinding) receiverType;
1223 if (!currentType.canBeSeenBy(this))
1224 return new ProblemMethodBinding(selector, argumentTypes, ReceiverTypeNotVisible);
1226 // retrieve an exact visible match (if possible)
1227 MethodBinding methodBinding =
1228 findExactMethod(currentType, selector, argumentTypes, invocationSite);
1229 if (methodBinding != null)
1230 return methodBinding;
1232 // answers closest approximation, may not check argumentTypes or visibility
1234 findMethod(currentType, selector, argumentTypes, invocationSite);
1235 if (methodBinding == null)
1236 return new ProblemMethodBinding(selector, argumentTypes, NotFound);
1237 if (methodBinding.isValidBinding()) {
1238 if (!areParametersAssignable(methodBinding.parameters, argumentTypes))
1239 return new ProblemMethodBinding(
1244 if (!methodBinding.canBeSeenBy(currentType, invocationSite, this))
1245 return new ProblemMethodBinding(
1248 methodBinding.parameters,
1251 return methodBinding;
1254 public int maxShiftedOffset() {
1256 if (this.shiftScopes != null){
1257 for (int i = 0, length = this.shiftScopes.length; i < length; i++){
1258 int subMaxOffset = this.shiftScopes[i].maxOffset;
1259 if (subMaxOffset > max) max = subMaxOffset;
1265 /* Answer the problem reporter to use for raising new problems.
1267 * Note that as a side-effect, this updates the current reference context
1268 * (unit, type or method) in case the problem handler decides it is necessary
1271 public ProblemReporter problemReporter() {
1273 return outerMostMethodScope().problemReporter();
1277 * Code responsible to request some more emulation work inside the invocation type, so as to supply
1278 * correct synthetic arguments to any allocation of the target type.
1280 public void propagateInnerEmulation(ReferenceBinding targetType, boolean isEnclosingInstanceSupplied) {
1282 // no need to propagate enclosing instances, they got eagerly allocated already.
1284 SyntheticArgumentBinding[] syntheticArguments;
1285 if ((syntheticArguments = targetType.syntheticOuterLocalVariables()) != null) {
1286 for (int i = 0, max = syntheticArguments.length; i < max; i++) {
1287 SyntheticArgumentBinding syntheticArg = syntheticArguments[i];
1288 // need to filter out the one that could match a supplied enclosing instance
1289 if (!(isEnclosingInstanceSupplied
1290 && (syntheticArg.type == targetType.enclosingType()))) {
1291 this.emulateOuterAccess(syntheticArg.actualOuterLocalVariable);
1297 /* Answer the reference type of this scope.
1299 * It is the nearest enclosing type of this scope.
1301 public TypeDeclaration referenceType() {
1303 return methodScope().referenceType();
1306 // start position in this scope - for ordering scopes vs. variables
1311 public String toString() {
1315 public String toString(int tab) {
1317 String s = basicToString(tab);
1318 for (int i = 0; i < scopeIndex; i++)
1319 if (subscopes[i] instanceof BlockScope)
1320 s += ((BlockScope) subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$