1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.lookup;
13 public class NestedTypeBinding extends SourceTypeBinding {
15 public SourceTypeBinding enclosingType;
17 public SyntheticArgumentBinding[] enclosingInstances;
18 public SyntheticArgumentBinding[] outerLocalVariables;
19 public int enclosingInstancesSlotSize; // amount of slots used by synthetic enclosing instances
20 public int outerLocalVariablesSlotSize; // amount of slots used by synthetic outer local variables
22 public NestedTypeBinding(char[][] typeName, ClassScope scope, SourceTypeBinding enclosingType) {
23 super(typeName, enclosingType.fPackage, scope);
24 this.tagBits |= IsNestedType;
25 this.enclosingType = enclosingType;
28 /* Add a new synthetic argument for <actualOuterLocalVariable>.
29 * Answer the new argument or the existing argument if one already existed.
31 public SyntheticArgumentBinding addSyntheticArgument(LocalVariableBinding actualOuterLocalVariable) {
32 SyntheticArgumentBinding synthLocal = null;
34 if (outerLocalVariables == null) {
35 synthLocal = new SyntheticArgumentBinding(actualOuterLocalVariable);
36 outerLocalVariables = new SyntheticArgumentBinding[] {synthLocal};
38 int size = outerLocalVariables.length;
39 int newArgIndex = size;
40 for (int i = size; --i >= 0;) { // must search backwards
41 if (outerLocalVariables[i].actualOuterLocalVariable == actualOuterLocalVariable)
42 return outerLocalVariables[i]; // already exists
43 if (outerLocalVariables[i].id > actualOuterLocalVariable.id)
46 SyntheticArgumentBinding[] synthLocals = new SyntheticArgumentBinding[size + 1];
47 System.arraycopy(outerLocalVariables, 0, synthLocals, 0, newArgIndex);
48 synthLocals[newArgIndex] = synthLocal = new SyntheticArgumentBinding(actualOuterLocalVariable);
49 System.arraycopy(outerLocalVariables, newArgIndex, synthLocals, newArgIndex + 1, size - newArgIndex);
50 outerLocalVariables = synthLocals;
52 //System.out.println("Adding synth arg for local var: " + new String(actualOuterLocalVariable.name) + " to: " + new String(this.readableName()));
53 if (scope.referenceCompilationUnit().isPropagatingInnerClassEmulation)
54 this.updateInnerEmulationDependents();
58 /* Add a new synthetic argument for <enclosingType>.
59 * Answer the new argument or the existing argument if one already existed.
61 public SyntheticArgumentBinding addSyntheticArgument(ReferenceBinding targetEnclosingType) {
62 SyntheticArgumentBinding synthLocal = null;
63 if (enclosingInstances == null) {
64 synthLocal = new SyntheticArgumentBinding(targetEnclosingType);
65 enclosingInstances = new SyntheticArgumentBinding[] {synthLocal};
67 int size = enclosingInstances.length;
68 int newArgIndex = size;
69 for (int i = size; --i >= 0;) {
70 if (enclosingInstances[i].type == targetEnclosingType)
71 return enclosingInstances[i]; // already exists
72 if (this.enclosingType() == targetEnclosingType)
75 SyntheticArgumentBinding[] newInstances = new SyntheticArgumentBinding[size + 1];
76 System.arraycopy(enclosingInstances, 0, newInstances, newArgIndex == 0 ? 1 : 0, size);
77 newInstances[newArgIndex] = synthLocal = new SyntheticArgumentBinding(targetEnclosingType);
78 enclosingInstances = newInstances;
80 //System.out.println("Adding synth arg for enclosing type: " + new String(enclosingType.readableName()) + " to: " + new String(this.readableName()));
81 if (scope.referenceCompilationUnit().isPropagatingInnerClassEmulation)
82 this.updateInnerEmulationDependents();
86 /* Add a new synthetic argument and field for <actualOuterLocalVariable>.
87 * Answer the new argument or the existing argument if one already existed.
89 public SyntheticArgumentBinding addSyntheticArgumentAndField(LocalVariableBinding actualOuterLocalVariable) {
90 SyntheticArgumentBinding synthLocal = addSyntheticArgument(actualOuterLocalVariable);
91 if (synthLocal == null) return null;
93 if (synthLocal.matchingField == null)
94 synthLocal.matchingField = addSyntheticField(actualOuterLocalVariable);
98 /* Add a new synthetic argument and field for <enclosingType>.
99 * Answer the new argument or the existing argument if one already existed.
101 public SyntheticArgumentBinding addSyntheticArgumentAndField(ReferenceBinding targetEnclosingType) {
102 SyntheticArgumentBinding synthLocal = addSyntheticArgument(targetEnclosingType);
103 if (synthLocal == null) return null;
105 if (synthLocal.matchingField == null)
106 synthLocal.matchingField = addSyntheticField(targetEnclosingType);
111 * Compute the resolved positions for all the synthetic arguments
113 final public void computeSyntheticArgumentSlotSizes() {
116 // insert enclosing instances first, followed by the outerLocals
117 int enclosingInstancesCount = this.enclosingInstances == null ? 0 : this.enclosingInstances.length;
118 for (int i = 0; i < enclosingInstancesCount; i++){
119 SyntheticArgumentBinding argument = this.enclosingInstances[i];
120 // position the enclosing instance synthetic arg
121 argument.resolvedPosition = slotSize + 1; // shift by 1 to leave room for aload0==this
122 if (slotSize + 1 > 0xFF) { // no more than 255 words of arguments
123 this.scope.problemReporter().noMoreAvailableSpaceForArgument(argument, this.scope.referenceType());
125 if ((argument.type == LongBinding) || (argument.type == DoubleBinding)){
131 this.enclosingInstancesSlotSize = slotSize;
133 slotSize = 0; // reset, outer local are not positionned yet, since will be appended to user arguments
134 int outerLocalsCount = this.outerLocalVariables == null ? 0 : this.outerLocalVariables.length;
135 for (int i = 0; i < outerLocalsCount; i++){
136 SyntheticArgumentBinding argument = this.outerLocalVariables[i];
137 // do NOT position the outerlocal synthetic arg yet, since will be appended to user arguments
138 if ((argument.type == LongBinding) || (argument.type == DoubleBinding)){
144 this.outerLocalVariablesSlotSize = slotSize;
147 /* Answer the receiver's enclosing type... null if the receiver is a top level type.
149 public ReferenceBinding enclosingType() {
151 return enclosingType;
154 /* Answer the synthetic argument for <actualOuterLocalVariable> or null if one does not exist.
156 public SyntheticArgumentBinding getSyntheticArgument(LocalVariableBinding actualOuterLocalVariable) {
158 if (outerLocalVariables == null) return null; // is null if no outer local variables are known
160 for (int i = outerLocalVariables.length; --i >= 0;)
161 if (outerLocalVariables[i].actualOuterLocalVariable == actualOuterLocalVariable)
162 return outerLocalVariables[i];
166 public SyntheticArgumentBinding[] syntheticEnclosingInstances() {
167 return enclosingInstances; // is null if no enclosing instances are required
170 public ReferenceBinding[] syntheticEnclosingInstanceTypes() {
171 if (enclosingInstances == null)
174 int length = enclosingInstances.length;
175 ReferenceBinding types[] = new ReferenceBinding[length];
176 for (int i = 0; i < length; i++)
177 types[i] = (ReferenceBinding) enclosingInstances[i].type;
181 public SyntheticArgumentBinding[] syntheticOuterLocalVariables() {
183 return outerLocalVariables; // is null if no outer locals are required
187 * Trigger the dependency mechanism forcing the innerclass emulation
188 * to be propagated to all dependent source types.
190 public void updateInnerEmulationDependents() {
191 // nothing to do in general, only local types are doing anything
194 /* Answer the synthetic argument for <targetEnclosingType> or null if one does not exist.
196 public SyntheticArgumentBinding getSyntheticArgument(ReferenceBinding targetEnclosingType, boolean onlyExactMatch) {
198 if (enclosingInstances == null) return null; // is null if no enclosing instances are known
201 for (int i = enclosingInstances.length; --i >= 0;)
202 if (enclosingInstances[i].type == targetEnclosingType)
203 if (enclosingInstances[i].actualOuterLocalVariable == null)
204 return enclosingInstances[i];
206 // type compatibility : to handle cases such as
207 // class T { class M{}}
208 // class S extends T { class N extends M {}} --> need to use S as a default enclosing instance for the super constructor call in N().
209 if (!onlyExactMatch){
210 for (int i = enclosingInstances.length; --i >= 0;)
211 if (enclosingInstances[i].actualOuterLocalVariable == null)
212 if (targetEnclosingType.isSuperclassOf((ReferenceBinding) enclosingInstances[i].type))
213 return enclosingInstances[i];