Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BlockScope.java 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Common Public License v0.5
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/cpl-v05.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. * Palo Alto Research Center, Incorporated - AspectJ adaptation
  11. ******************************************************************************/
  12. package org.eclipse.jdt.internal.compiler.lookup;
  13. import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  14. import org.eclipse.jdt.internal.compiler.ast.Argument;
  15. import org.eclipse.jdt.internal.compiler.ast.AstNode;
  16. import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
  17. import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  18. import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
  19. import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  20. import org.eclipse.jdt.internal.compiler.impl.Constant;
  21. import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
  22. import org.eclipse.jdt.internal.compiler.util.CharOperation;
  23. /**
  24. * AspectJ - added hook to use classScope.addDepth() in lookup
  25. */
  26. public class BlockScope extends Scope {
  27. // Local variable management
  28. public LocalVariableBinding[] locals;
  29. public int localIndex; // position for next variable
  30. public int startIndex; // start position in this scope - for ordering scopes vs. variables
  31. public int offset; // for variable allocation throughout scopes
  32. public int maxOffset; // for variable allocation throughout scopes
  33. // finally scopes must be shifted behind respective try scope
  34. public BlockScope[] shiftScopes;
  35. public final static VariableBinding[] EmulationPathToImplicitThis = {};
  36. public Scope[] subscopes = new Scope[1]; // need access from code assist
  37. public int scopeIndex = 0; // need access from code assist
  38. protected BlockScope(int kind, Scope parent) {
  39. super(kind, parent);
  40. }
  41. public BlockScope(BlockScope parent) {
  42. this(parent, true);
  43. }
  44. public BlockScope(BlockScope parent, boolean addToParentScope) {
  45. this(BLOCK_SCOPE, parent);
  46. locals = new LocalVariableBinding[5];
  47. if (addToParentScope) parent.addSubscope(this);
  48. this.startIndex = parent.localIndex;
  49. }
  50. public BlockScope(BlockScope parent, int variableCount) {
  51. this(BLOCK_SCOPE, parent);
  52. locals = new LocalVariableBinding[variableCount];
  53. parent.addSubscope(this);
  54. this.startIndex = parent.localIndex;
  55. }
  56. /* Create the class scope & binding for the anonymous type.
  57. */
  58. public final void addAnonymousType(
  59. TypeDeclaration anonymousType,
  60. ReferenceBinding superBinding) {
  61. ClassScope anonymousClassScope = new ClassScope(this, anonymousType);
  62. anonymousClassScope.buildAnonymousTypeBinding(
  63. enclosingSourceType(),
  64. superBinding);
  65. }
  66. /* Create the class scope & binding for the local type.
  67. */
  68. public final void addLocalType(TypeDeclaration localType) {
  69. // check that the localType does not conflict with an enclosing type
  70. ReferenceBinding type = enclosingSourceType();
  71. do {
  72. if (CharOperation.equals(type.sourceName, localType.name)) {
  73. problemReporter().hidingEnclosingType(localType);
  74. return;
  75. }
  76. type = type.enclosingType();
  77. } while (type != null);
  78. // check that the localType does not conflict with another sibling local type
  79. Scope scope = this;
  80. do {
  81. if (((BlockScope) scope).findLocalType(localType.name) != null) {
  82. problemReporter().duplicateNestedType(localType);
  83. return;
  84. }
  85. } while ((scope = scope.parent) instanceof BlockScope);
  86. ClassScope localTypeScope = new ClassScope(this, localType);
  87. localTypeScope.buildLocalTypeBinding(enclosingSourceType());
  88. addSubscope(localTypeScope);
  89. }
  90. /* Insert a local variable into a given scope, updating its position
  91. * and checking there are not too many locals or arguments allocated.
  92. */
  93. public final void addLocalVariable(LocalVariableBinding binding) {
  94. checkAndSetModifiersForVariable(binding);
  95. // insert local in scope
  96. if (localIndex == locals.length)
  97. System.arraycopy(
  98. locals,
  99. 0,
  100. (locals = new LocalVariableBinding[localIndex * 2]),
  101. 0,
  102. localIndex);
  103. locals[localIndex++] = binding;
  104. // update local variable binding
  105. binding.declaringScope = this;
  106. binding.id = this.outerMostMethodScope().analysisIndex++;
  107. // share the outermost method scope analysisIndex
  108. }
  109. public void addSubscope(Scope childScope) {
  110. if (scopeIndex == subscopes.length)
  111. System.arraycopy(
  112. subscopes,
  113. 0,
  114. (subscopes = new Scope[scopeIndex * 2]),
  115. 0,
  116. scopeIndex);
  117. subscopes[scopeIndex++] = childScope;
  118. }
  119. /* Answer true if the receiver is suitable for assigning final blank fields.
  120. *
  121. * i.e. is inside an initializer, a constructor or a clinit
  122. */
  123. public final boolean allowBlankFinalFieldAssignment(FieldBinding binding) {
  124. if (enclosingSourceType() != binding.declaringClass)
  125. return false;
  126. MethodScope methodScope = methodScope();
  127. if (methodScope.isStatic != binding.isStatic())
  128. return false;
  129. return methodScope.isInsideInitializer() // inside initializer
  130. || ((AbstractMethodDeclaration) methodScope.referenceContext)
  131. .isInitializationMethod();
  132. // inside constructor or clinit
  133. }
  134. String basicToString(int tab) {
  135. String newLine = "\n"; //$NON-NLS-1$
  136. for (int i = tab; --i >= 0;)
  137. newLine += "\t"; //$NON-NLS-1$
  138. String s = newLine + "--- Block Scope ---"; //$NON-NLS-1$
  139. newLine += "\t"; //$NON-NLS-1$
  140. s += newLine + "locals:"; //$NON-NLS-1$
  141. for (int i = 0; i < localIndex; i++)
  142. s += newLine + "\t" + locals[i].toString(); //$NON-NLS-1$
  143. s += newLine + "startIndex = " + startIndex; //$NON-NLS-1$
  144. return s;
  145. }
  146. private void checkAndSetModifiersForVariable(LocalVariableBinding varBinding) {
  147. int modifiers = varBinding.modifiers;
  148. if ((modifiers & AccAlternateModifierProblem) != 0 && varBinding.declaration != null){
  149. problemReporter().duplicateModifierForVariable(varBinding.declaration, this instanceof MethodScope);
  150. }
  151. int realModifiers = modifiers & AccJustFlag;
  152. int unexpectedModifiers = ~AccFinal;
  153. if ((realModifiers & unexpectedModifiers) != 0 && varBinding.declaration != null){
  154. problemReporter().illegalModifierForVariable(varBinding.declaration, this instanceof MethodScope);
  155. }
  156. varBinding.modifiers = modifiers;
  157. }
  158. /* Compute variable positions in scopes given an initial position offset
  159. * ignoring unused local variables.
  160. *
  161. * Special treatment to have Try secret return address variables located at non
  162. * colliding positions. Return addresses are not allocated initially, but gathered
  163. * and allocated behind all other variables.
  164. */
  165. public void computeLocalVariablePositions(
  166. int initOffset,
  167. CodeStream codeStream) {
  168. this.offset = initOffset;
  169. this.maxOffset = initOffset;
  170. // local variable init
  171. int ilocal = 0, maxLocals = 0, localsLength = locals.length;
  172. while ((maxLocals < localsLength) && (locals[maxLocals] != null))
  173. maxLocals++;
  174. boolean hasMoreVariables = maxLocals > 0;
  175. // scope init
  176. int iscope = 0, maxScopes = 0, subscopesLength = subscopes.length;
  177. while ((maxScopes < subscopesLength) && (subscopes[maxScopes] != null))
  178. maxScopes++;
  179. boolean hasMoreScopes = maxScopes > 0;
  180. // iterate scopes and variables in parallel
  181. while (hasMoreVariables || hasMoreScopes) {
  182. if (hasMoreScopes
  183. && (!hasMoreVariables || (subscopes[iscope].startIndex() <= ilocal))) {
  184. // consider subscope first
  185. if (subscopes[iscope] instanceof BlockScope) {
  186. BlockScope subscope = (BlockScope) subscopes[iscope];
  187. int subOffset = subscope.shiftScopes == null ? this.offset : subscope.maxShiftedOffset();
  188. subscope.computeLocalVariablePositions(subOffset, codeStream);
  189. if (subscope.maxOffset > this.maxOffset)
  190. this.maxOffset = subscope.maxOffset;
  191. }
  192. hasMoreScopes = ++iscope < maxScopes;
  193. } else {
  194. // consider variable first
  195. LocalVariableBinding local = locals[ilocal];
  196. // check if variable is actually used, and may force it to be preserved
  197. boolean generatesLocal =
  198. (local.used && (local.constant == Constant.NotAConstant)) || local.isArgument;
  199. if (!local.used
  200. && (local.declaration != null) // unused (and non secret) local
  201. && ((local.declaration.bits & AstNode.IsLocalDeclarationReachableMASK) != 0)) { // declaration is reachable
  202. if (local.isArgument) // method argument
  203. this.problemReporter().unusedArgument(local.declaration);
  204. else if (!(local.declaration instanceof Argument)) // do not report unused catch arguments
  205. this.problemReporter().unusedLocalVariable(local.declaration);
  206. }
  207. if (!generatesLocal) {
  208. if (local.declaration != null
  209. && environment().options.preserveAllLocalVariables) {
  210. generatesLocal = true; // force it to be preserved in the generated code
  211. local.used = true;
  212. }
  213. }
  214. if (generatesLocal) {
  215. if (local.declaration != null) {
  216. codeStream.record(local);
  217. // record user local variables for attribute generation
  218. }
  219. // allocate variable position
  220. local.resolvedPosition = this.offset;
  221. // check for too many arguments/local variables
  222. if (local.isArgument) {
  223. if (this.offset > 0xFF) { // no more than 255 words of arguments
  224. this.problemReporter().noMoreAvailableSpaceForArgument(local, local.declaration);
  225. }
  226. } else {
  227. if (this.offset > 0xFFFF) { // no more than 65535 words of locals
  228. this.problemReporter().noMoreAvailableSpaceForLocal(
  229. local, local.declaration == null ? (AstNode)this.methodScope().referenceContext : local.declaration);
  230. }
  231. }
  232. // increment offset
  233. if ((local.type == LongBinding) || (local.type == DoubleBinding)) {
  234. this.offset += 2;
  235. } else {
  236. this.offset++;
  237. }
  238. } else {
  239. local.resolvedPosition = -1; // not generated
  240. }
  241. hasMoreVariables = ++ilocal < maxLocals;
  242. }
  243. }
  244. if (this.offset > this.maxOffset)
  245. this.maxOffset = this.offset;
  246. }
  247. /* Answer true if the variable name already exists within the receiver's scope.
  248. */
  249. public final LocalVariableBinding duplicateName(char[] name) {
  250. for (int i = 0; i < localIndex; i++)
  251. if (CharOperation.equals(name, locals[i].name))
  252. return locals[i];
  253. if (this instanceof MethodScope)
  254. return null;
  255. else
  256. return ((BlockScope) parent).duplicateName(name);
  257. }
  258. /*
  259. * Record the suitable binding denoting a synthetic field or constructor argument,
  260. * mapping to the actual outer local variable in the scope context.
  261. * Note that this may not need any effect, in case the outer local variable does not
  262. * need to be emulated and can directly be used as is (using its back pointer to its
  263. * declaring scope).
  264. */
  265. public void emulateOuterAccess(LocalVariableBinding outerLocalVariable) {
  266. MethodScope currentMethodScope;
  267. if ((currentMethodScope = this.methodScope())
  268. != outerLocalVariable.declaringScope.methodScope()) {
  269. NestedTypeBinding currentType = (NestedTypeBinding) this.enclosingSourceType();
  270. //do nothing for member types, pre emulation was performed already
  271. if (!currentType.isLocalType()) {
  272. return;
  273. }
  274. // must also add a synthetic field if we're not inside a constructor
  275. if (!currentMethodScope.isInsideInitializerOrConstructor()) {
  276. currentType.addSyntheticArgumentAndField(outerLocalVariable);
  277. } else {
  278. currentType.addSyntheticArgument(outerLocalVariable);
  279. }
  280. }
  281. }
  282. /*
  283. * Record the suitable binding denoting a synthetic field or constructor argument,
  284. * mapping to a given actual enclosing instance type in the scope context.
  285. * Skip it if the enclosingType is actually the current scope's enclosing type.
  286. */
  287. public void emulateOuterAccess(
  288. ReferenceBinding targetEnclosingType,
  289. boolean useDirectReference) {
  290. ReferenceBinding currentType = enclosingSourceType();
  291. if (currentType.isNestedType()
  292. && currentType != targetEnclosingType){
  293. /*&& !targetEnclosingType.isSuperclassOf(currentType)*/
  294. if (useDirectReference) {
  295. // the target enclosing type is not in scope, we directly refer it
  296. // must also add a synthetic field if we're not inside a constructor
  297. NestedTypeBinding currentNestedType = (NestedTypeBinding) currentType;
  298. if (methodScope().isInsideInitializerOrConstructor())
  299. currentNestedType.addSyntheticArgument(targetEnclosingType);
  300. else
  301. currentNestedType.addSyntheticArgumentAndField(targetEnclosingType);
  302. } else { // indirect reference sequence
  303. int depth = 0;
  304. // saturate all the way up until reaching compatible enclosing type
  305. while (currentType.isLocalType()){
  306. NestedTypeBinding currentNestedType = (NestedTypeBinding) currentType;
  307. currentType = currentNestedType.enclosingType;
  308. if (depth == 0){
  309. if (methodScope().isInsideInitializerOrConstructor()) {
  310. // must also add a synthetic field if we're not inside a constructor
  311. currentNestedType.addSyntheticArgument(currentType);
  312. } else {
  313. currentNestedType.addSyntheticArgumentAndField(currentType);
  314. }
  315. } else if (currentNestedType == targetEnclosingType
  316. || targetEnclosingType.isSuperclassOf(currentNestedType)) {
  317. break;
  318. } else {
  319. currentNestedType.addSyntheticArgumentAndField(currentType);
  320. }
  321. depth++;
  322. }
  323. }
  324. }
  325. }
  326. /* Note that it must never produce a direct access to the targetEnclosingType,
  327. * but instead a field sequence (this$2.this$1.this$0) so as to handle such a test case:
  328. *
  329. * class XX {
  330. * void foo() {
  331. * class A {
  332. * class B {
  333. * class C {
  334. * boolean foo() {
  335. * return (Object) A.this == (Object) B.this;
  336. * }
  337. * }
  338. * }
  339. * }
  340. * new A().new B().new C();
  341. * }
  342. * }
  343. * where we only want to deal with ONE enclosing instance for C (could not figure out an A for C)
  344. */
  345. public final ReferenceBinding findLocalType(char[] name) {
  346. for (int i = 0, length = scopeIndex; i < length; i++) {
  347. if (subscopes[i] instanceof ClassScope) {
  348. SourceTypeBinding sourceType =
  349. ((ClassScope) subscopes[i]).referenceContext.binding;
  350. if (CharOperation.equals(sourceType.sourceName(), name))
  351. return sourceType;
  352. }
  353. }
  354. return null;
  355. }
  356. public LocalVariableBinding findVariable(char[] variable) {
  357. int variableLength = variable.length;
  358. for (int i = 0, length = locals.length; i < length; i++) {
  359. LocalVariableBinding local = locals[i];
  360. if (local == null)
  361. return null;
  362. if (local.name.length == variableLength
  363. && CharOperation.prefixEquals(local.name, variable))
  364. return local;
  365. }
  366. return null;
  367. }
  368. /* API
  369. * flag is a mask of the following values VARIABLE (= FIELD or LOCAL), TYPE.
  370. * Only bindings corresponding to the mask will be answered.
  371. *
  372. * if the VARIABLE mask is set then
  373. * If the first name provided is a field (or local) then the field (or local) is answered
  374. * Otherwise, package names and type names are consumed until a field is found.
  375. * In this case, the field is answered.
  376. *
  377. * if the TYPE mask is set,
  378. * package names and type names are consumed until the end of the input.
  379. * Only if all of the input is consumed is the type answered
  380. *
  381. * All other conditions are errors, and a problem binding is returned.
  382. *
  383. * NOTE: If a problem binding is returned, senders should extract the compound name
  384. * from the binding & not assume the problem applies to the entire compoundName.
  385. *
  386. * The VARIABLE mask has precedence over the TYPE mask.
  387. *
  388. * InvocationSite implements
  389. * isSuperAccess(); this is used to determine if the discovered field is visible.
  390. * setFieldIndex(int); this is used to record the number of names that were consumed.
  391. *
  392. * For example, getBinding({"foo","y","q", VARIABLE, site) will answer
  393. * the binding for the field or local named "foo" (or an error binding if none exists).
  394. * In addition, setFieldIndex(1) will be sent to the invocation site.
  395. * If a type named "foo" exists, it will not be detected (and an error binding will be answered)
  396. *
  397. * IMPORTANT NOTE: This method is written under the assumption that compoundName is longer than length 1.
  398. */
  399. public Binding getBinding(char[][] compoundName, int mask, InvocationSite invocationSite) {
  400. Binding binding = getBinding(compoundName[0], mask | TYPE | PACKAGE, invocationSite);
  401. invocationSite.setFieldIndex(1);
  402. if (binding instanceof VariableBinding) return binding;
  403. compilationUnitScope().recordSimpleReference(compoundName[0]);
  404. if (!binding.isValidBinding()) return binding;
  405. int length = compoundName.length;
  406. int currentIndex = 1;
  407. foundType : if (binding instanceof PackageBinding) {
  408. PackageBinding packageBinding = (PackageBinding) binding;
  409. while (currentIndex < length) {
  410. compilationUnitScope().recordReference(packageBinding.compoundName, compoundName[currentIndex]);
  411. binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]);
  412. invocationSite.setFieldIndex(currentIndex);
  413. if (binding == null) {
  414. if (currentIndex == length)
  415. // must be a type if its the last name, otherwise we have no idea if its a package or type
  416. return new ProblemReferenceBinding(
  417. CharOperation.subarray(compoundName, 0, currentIndex),
  418. NotFound);
  419. else
  420. return new ProblemBinding(
  421. CharOperation.subarray(compoundName, 0, currentIndex),
  422. NotFound);
  423. }
  424. if (binding instanceof ReferenceBinding) {
  425. if (!binding.isValidBinding())
  426. return new ProblemReferenceBinding(
  427. CharOperation.subarray(compoundName, 0, currentIndex),
  428. binding.problemId());
  429. if (!((ReferenceBinding) binding).canBeSeenBy(this))
  430. return new ProblemReferenceBinding(
  431. CharOperation.subarray(compoundName, 0, currentIndex),
  432. binding,
  433. NotVisible);
  434. break foundType;
  435. }
  436. packageBinding = (PackageBinding) binding;
  437. }
  438. // It is illegal to request a PACKAGE from this method.
  439. return new ProblemReferenceBinding(
  440. CharOperation.subarray(compoundName, 0, currentIndex),
  441. NotFound);
  442. }
  443. // know binding is now a ReferenceBinding
  444. while (currentIndex < length) {
  445. ReferenceBinding typeBinding = (ReferenceBinding) binding;
  446. char[] nextName = compoundName[currentIndex++];
  447. invocationSite.setFieldIndex(currentIndex);
  448. invocationSite.setActualReceiverType(typeBinding);
  449. if ((binding = findField(typeBinding, nextName, invocationSite)) != null) {
  450. if (!binding.isValidBinding())
  451. return new ProblemFieldBinding(
  452. ((FieldBinding) binding).declaringClass,
  453. CharOperation.subarray(compoundName, 0, currentIndex),
  454. binding.problemId());
  455. break; // binding is now a field
  456. }
  457. if ((binding = findMemberType(nextName, typeBinding)) == null)
  458. return new ProblemBinding(
  459. CharOperation.subarray(compoundName, 0, currentIndex),
  460. typeBinding,
  461. NotFound);
  462. if (!binding.isValidBinding())
  463. return new ProblemReferenceBinding(
  464. CharOperation.subarray(compoundName, 0, currentIndex),
  465. binding.problemId());
  466. }
  467. if ((mask & FIELD) != 0 && (binding instanceof FieldBinding)) {
  468. // was looking for a field and found a field
  469. FieldBinding field = (FieldBinding) binding;
  470. if (!field.isStatic())
  471. return new ProblemFieldBinding(
  472. field.declaringClass,
  473. CharOperation.subarray(compoundName, 0, currentIndex),
  474. NonStaticReferenceInStaticContext);
  475. return binding;
  476. }
  477. if ((mask & TYPE) != 0 && (binding instanceof ReferenceBinding)) {
  478. // was looking for a type and found a type
  479. return binding;
  480. }
  481. // handle the case when a field or type was asked for but we resolved the compoundName to a type or field
  482. return new ProblemBinding(
  483. CharOperation.subarray(compoundName, 0, currentIndex),
  484. NotFound);
  485. }
  486. // Added for code assist... NOT Public API
  487. public final Binding getBinding(
  488. char[][] compoundName,
  489. InvocationSite invocationSite) {
  490. int currentIndex = 0;
  491. int length = compoundName.length;
  492. Binding binding =
  493. getBinding(
  494. compoundName[currentIndex++],
  495. VARIABLE | TYPE | PACKAGE,
  496. invocationSite);
  497. if (!binding.isValidBinding())
  498. return binding;
  499. foundType : if (binding instanceof PackageBinding) {
  500. while (currentIndex < length) {
  501. PackageBinding packageBinding = (PackageBinding) binding;
  502. binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]);
  503. if (binding == null) {
  504. if (currentIndex == length)
  505. // must be a type if its the last name, otherwise we have no idea if its a package or type
  506. return new ProblemReferenceBinding(
  507. CharOperation.subarray(compoundName, 0, currentIndex),
  508. NotFound);
  509. else
  510. return new ProblemBinding(
  511. CharOperation.subarray(compoundName, 0, currentIndex),
  512. NotFound);
  513. }
  514. if (binding instanceof ReferenceBinding) {
  515. if (!binding.isValidBinding())
  516. return new ProblemReferenceBinding(
  517. CharOperation.subarray(compoundName, 0, currentIndex),
  518. binding.problemId());
  519. if (!((ReferenceBinding) binding).canBeSeenBy(this))
  520. return new ProblemReferenceBinding(
  521. CharOperation.subarray(compoundName, 0, currentIndex),
  522. binding,
  523. NotVisible);
  524. break foundType;
  525. }
  526. }
  527. return binding;
  528. }
  529. foundField : if (binding instanceof ReferenceBinding) {
  530. while (currentIndex < length) {
  531. ReferenceBinding typeBinding = (ReferenceBinding) binding;
  532. char[] nextName = compoundName[currentIndex++];
  533. if ((binding = findField(typeBinding, nextName, invocationSite)) != null) {
  534. if (!binding.isValidBinding())
  535. return new ProblemFieldBinding(
  536. ((FieldBinding) binding).declaringClass,
  537. CharOperation.subarray(compoundName, 0, currentIndex),
  538. binding.problemId());
  539. if (!((FieldBinding) binding).isStatic())
  540. return new ProblemFieldBinding(
  541. ((FieldBinding) binding).declaringClass,
  542. CharOperation.subarray(compoundName, 0, currentIndex),
  543. NonStaticReferenceInStaticContext);
  544. break foundField; // binding is now a field
  545. }
  546. if ((binding = findMemberType(nextName, typeBinding)) == null)
  547. return new ProblemBinding(
  548. CharOperation.subarray(compoundName, 0, currentIndex),
  549. typeBinding,
  550. NotFound);
  551. if (!binding.isValidBinding())
  552. return new ProblemReferenceBinding(
  553. CharOperation.subarray(compoundName, 0, currentIndex),
  554. binding.problemId());
  555. }
  556. return binding;
  557. }
  558. VariableBinding variableBinding = (VariableBinding) binding;
  559. while (currentIndex < length) {
  560. TypeBinding typeBinding = variableBinding.type;
  561. if (typeBinding == null)
  562. return new ProblemFieldBinding(
  563. null,
  564. CharOperation.subarray(compoundName, 0, currentIndex + 1),
  565. NotFound);
  566. variableBinding =
  567. findField(typeBinding, compoundName[currentIndex++], invocationSite);
  568. if (variableBinding == null)
  569. return new ProblemFieldBinding(
  570. null,
  571. CharOperation.subarray(compoundName, 0, currentIndex),
  572. NotFound);
  573. if (!variableBinding.isValidBinding())
  574. return variableBinding;
  575. }
  576. return variableBinding;
  577. }
  578. /* API
  579. *
  580. * Answer the binding that corresponds to the argument name.
  581. * flag is a mask of the following values VARIABLE (= FIELD or LOCAL), TYPE, PACKAGE.
  582. * Only bindings corresponding to the mask can be answered.
  583. *
  584. * For example, getBinding("foo", VARIABLE, site) will answer
  585. * the binding for the field or local named "foo" (or an error binding if none exists).
  586. * If a type named "foo" exists, it will not be detected (and an error binding will be answered)
  587. *
  588. * The VARIABLE mask has precedence over the TYPE mask.
  589. *
  590. * If the VARIABLE mask is not set, neither fields nor locals will be looked for.
  591. *
  592. * InvocationSite implements:
  593. * isSuperAccess(); this is used to determine if the discovered field is visible.
  594. *
  595. * Limitations: cannot request FIELD independently of LOCAL, or vice versa
  596. */
  597. public Binding getBinding(char[] name, int mask, InvocationSite invocationSite) {
  598. Binding binding = null;
  599. FieldBinding problemField = null;
  600. if ((mask & VARIABLE) != 0) {
  601. if (this.kind == BLOCK_SCOPE || this.kind == METHOD_SCOPE) {
  602. LocalVariableBinding variableBinding = findVariable(name);
  603. // looks in this scope only
  604. if (variableBinding != null) return variableBinding;
  605. }
  606. boolean insideStaticContext = false;
  607. boolean insideConstructorCall = false;
  608. if (this.kind == METHOD_SCOPE) {
  609. MethodScope methodScope = (MethodScope) this;
  610. insideStaticContext |= methodScope.isStatic;
  611. insideConstructorCall |= methodScope.isConstructorCall;
  612. }
  613. FieldBinding foundField = null;
  614. // can be a problem field which is answered if a valid field is not found
  615. ProblemFieldBinding foundInsideProblem = null;
  616. // inside Constructor call or inside static context
  617. Scope scope = parent;
  618. int depth = 0;
  619. int foundDepth = 0;
  620. ReferenceBinding foundActualReceiverType = null;
  621. done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
  622. switch (scope.kind) {
  623. case METHOD_SCOPE :
  624. MethodScope methodScope = (MethodScope) scope;
  625. insideStaticContext |= methodScope.isStatic;
  626. insideConstructorCall |= methodScope.isConstructorCall;
  627. // Fall through... could duplicate the code below to save a cast - questionable optimization
  628. case BLOCK_SCOPE :
  629. LocalVariableBinding variableBinding = ((BlockScope) scope).findVariable(name);
  630. // looks in this scope only
  631. if (variableBinding != null) {
  632. if (foundField != null && foundField.isValidBinding())
  633. return new ProblemFieldBinding(
  634. foundField.declaringClass,
  635. name,
  636. InheritedNameHidesEnclosingName);
  637. if (depth > 0)
  638. invocationSite.setDepth(depth);
  639. return variableBinding;
  640. }
  641. break;
  642. case CLASS_SCOPE :
  643. ClassScope classScope = (ClassScope) scope;
  644. SourceTypeBinding enclosingType = classScope.referenceContext.binding;
  645. FieldBinding fieldBinding =
  646. classScope.findField(enclosingType, name, invocationSite);
  647. // Use next line instead if willing to enable protected access accross inner types
  648. // FieldBinding fieldBinding = findField(enclosingType, name, invocationSite);
  649. if (fieldBinding != null) { // skip it if we did not find anything
  650. if (fieldBinding.problemId() == Ambiguous) {
  651. if (foundField == null || foundField.problemId() == NotVisible)
  652. // supercedes any potential InheritedNameHidesEnclosingName problem
  653. return fieldBinding;
  654. else
  655. // make the user qualify the field, likely wants the first inherited field (javac generates an ambiguous error instead)
  656. return new ProblemFieldBinding(
  657. fieldBinding.declaringClass,
  658. name,
  659. InheritedNameHidesEnclosingName);
  660. }
  661. ProblemFieldBinding insideProblem = null;
  662. if (fieldBinding.isValidBinding()) {
  663. if (!fieldBinding.isStatic()) {
  664. if (insideConstructorCall) {
  665. insideProblem =
  666. new ProblemFieldBinding(
  667. fieldBinding.declaringClass,
  668. name,
  669. NonStaticReferenceInConstructorInvocation);
  670. } else if (insideStaticContext) {
  671. insideProblem =
  672. new ProblemFieldBinding(
  673. fieldBinding.declaringClass,
  674. name,
  675. NonStaticReferenceInStaticContext);
  676. }
  677. }
  678. if (enclosingType == fieldBinding.declaringClass
  679. || environment().options.complianceLevel >= CompilerOptions.JDK1_4){
  680. // found a valid field in the 'immediate' scope (ie. not inherited)
  681. // OR in 1.4 mode (inherited shadows enclosing)
  682. if (foundField == null) {
  683. if (depth > 0){
  684. invocationSite.setDepth(depth);
  685. invocationSite.setActualReceiverType(enclosingType);
  686. }
  687. // return the fieldBinding if it is not declared in a superclass of the scope's binding (i.e. "inherited")
  688. return insideProblem == null ? fieldBinding : insideProblem;
  689. }
  690. if (foundField.isValidBinding())
  691. // if a valid field was found, complain when another is found in an 'immediate' enclosing type (ie. not inherited)
  692. if (foundField.declaringClass != fieldBinding.declaringClass)
  693. // ie. have we found the same field - do not trust field identity yet
  694. return new ProblemFieldBinding(
  695. fieldBinding.declaringClass,
  696. name,
  697. InheritedNameHidesEnclosingName);
  698. }
  699. }
  700. if (foundField == null
  701. || (foundField.problemId() == NotVisible
  702. && fieldBinding.problemId() != NotVisible)) {
  703. // only remember the fieldBinding if its the first one found or the previous one was not visible & fieldBinding is...
  704. foundDepth = depth;
  705. foundActualReceiverType = enclosingType;
  706. foundInsideProblem = insideProblem;
  707. foundField = fieldBinding;
  708. }
  709. }
  710. depth+=classScope.addDepth();
  711. insideStaticContext |= enclosingType.isStatic();
  712. // 1EX5I8Z - accessing outer fields within a constructor call is permitted
  713. // in order to do so, we change the flag as we exit from the type, not the method
  714. // itself, because the class scope is used to retrieve the fields.
  715. MethodScope enclosingMethodScope = scope.methodScope();
  716. insideConstructorCall =
  717. enclosingMethodScope == null ? false : enclosingMethodScope.isConstructorCall;
  718. break;
  719. case COMPILATION_UNIT_SCOPE :
  720. break done;
  721. }
  722. scope = scope.parent;
  723. }
  724. if (foundInsideProblem != null){
  725. return foundInsideProblem;
  726. }
  727. if (foundField != null) {
  728. if (foundField.isValidBinding()){
  729. if (foundDepth > 0){
  730. invocationSite.setDepth(foundDepth);
  731. invocationSite.setActualReceiverType(foundActualReceiverType);
  732. }
  733. return foundField;
  734. }
  735. problemField = foundField;
  736. }
  737. }
  738. // We did not find a local or instance variable.
  739. if ((mask & TYPE) != 0) {
  740. if ((binding = getBaseType(name)) != null)
  741. return binding;
  742. binding = getTypeOrPackage(name, (mask & PACKAGE) == 0 ? TYPE : TYPE | PACKAGE);
  743. if (binding.isValidBinding() || mask == TYPE)
  744. return binding;
  745. // answer the problem type binding if we are only looking for a type
  746. } else if ((mask & PACKAGE) != 0) {
  747. compilationUnitScope().recordSimpleReference(name);
  748. if ((binding = environment().getTopLevelPackage(name)) != null)
  749. return binding;
  750. }
  751. if (problemField != null)
  752. return problemField;
  753. else
  754. return new ProblemBinding(name, enclosingSourceType(), NotFound);
  755. }
  756. /*
  757. * This retrieves the argument that maps to an enclosing instance of the suitable type,
  758. * if not found then answers nil -- do not create one
  759. *
  760. * #implicitThis : the implicit this will be ok
  761. * #((arg) this$n) : available as a constructor arg
  762. * #((arg) this$n access$m... access$p) : available as as a constructor arg + a sequence of synthetic accessors to synthetic fields
  763. * #((fieldDescr) this$n access#m... access$p) : available as a first synthetic field + a sequence of synthetic accessors to synthetic fields
  764. * nil : not found
  765. *
  766. */
  767. public Object[] getCompatibleEmulationPath(ReferenceBinding targetEnclosingType) {
  768. MethodScope currentMethodScope = this.methodScope();
  769. SourceTypeBinding sourceType = currentMethodScope.enclosingSourceType();
  770. // identity check
  771. if (!currentMethodScope.isStatic
  772. && !currentMethodScope.isConstructorCall
  773. && (sourceType == targetEnclosingType
  774. || targetEnclosingType.isSuperclassOf(sourceType))) {
  775. return EmulationPathToImplicitThis; // implicit this is good enough
  776. }
  777. if (!sourceType.isNestedType()
  778. || sourceType.isStatic()) { // no emulation from within non-inner types
  779. return null;
  780. }
  781. boolean insideConstructor =
  782. currentMethodScope.isInsideInitializerOrConstructor();
  783. // use synthetic constructor arguments if possible
  784. if (insideConstructor) {
  785. SyntheticArgumentBinding syntheticArg;
  786. if ((syntheticArg = ((NestedTypeBinding) sourceType).getSyntheticArgument(targetEnclosingType, this, false)) != null) {
  787. return new Object[] { syntheticArg };
  788. }
  789. }
  790. // use a direct synthetic field then
  791. if (!currentMethodScope.isStatic) {
  792. FieldBinding syntheticField;
  793. if ((syntheticField = sourceType.getSyntheticField(targetEnclosingType, this, false)) != null) {
  794. return new Object[] { syntheticField };
  795. }
  796. // could be reached through a sequence of enclosing instance link (nested members)
  797. Object[] path = new Object[2]; // probably at least 2 of them
  798. ReferenceBinding currentType = sourceType.enclosingType();
  799. if (insideConstructor) {
  800. path[0] = ((NestedTypeBinding) sourceType).getSyntheticArgument((SourceTypeBinding) currentType, this, false);
  801. } else {
  802. path[0] =
  803. sourceType.getSyntheticField((SourceTypeBinding) currentType, this, false);
  804. }
  805. if (path[0] != null) { // keep accumulating
  806. int count = 1;
  807. ReferenceBinding currentEnclosingType;
  808. while ((currentEnclosingType = currentType.enclosingType()) != null) {
  809. //done?
  810. if (currentType == targetEnclosingType
  811. || targetEnclosingType.isSuperclassOf(currentType))
  812. break;
  813. syntheticField = ((NestedTypeBinding) currentType).getSyntheticField((SourceTypeBinding) currentEnclosingType, this, false);
  814. if (syntheticField == null)
  815. break;
  816. // append inside the path
  817. if (count == path.length) {
  818. System.arraycopy(path, 0, (path = new Object[count + 1]), 0, count);
  819. }
  820. // private access emulation is necessary since synthetic field is private
  821. path[count++] = ((SourceTypeBinding) syntheticField.declaringClass).addSyntheticMethod(syntheticField, true);
  822. currentType = currentEnclosingType;
  823. }
  824. if (currentType == targetEnclosingType
  825. || targetEnclosingType.isSuperclassOf(currentType)) {
  826. return path;
  827. }
  828. }
  829. }
  830. return null;
  831. }
  832. /* API
  833. *
  834. * Answer the constructor binding that corresponds to receiverType, argumentTypes.
  835. *
  836. * InvocationSite implements
  837. * isSuperAccess(); this is used to determine if the discovered constructor is visible.
  838. *
  839. * If no visible constructor is discovered, an error binding is answered.
  840. */
  841. public MethodBinding getConstructor(
  842. ReferenceBinding receiverType,
  843. TypeBinding[] argumentTypes,
  844. InvocationSite invocationSite) {
  845. IPrivilegedHandler handler = findPrivilegedHandler(invocationType());
  846. compilationUnitScope().recordTypeReference(receiverType);
  847. compilationUnitScope().recordTypeReferences(argumentTypes);
  848. MethodBinding methodBinding = receiverType.getExactConstructor(argumentTypes);
  849. if (methodBinding != null) {
  850. if (methodBinding.canBeSeenBy(invocationSite, this)) {
  851. return methodBinding;
  852. } else if (handler != null) {
  853. return handler.getPrivilegedAccessMethod(methodBinding);
  854. }
  855. }
  856. MethodBinding[] methods =
  857. receiverType.getMethods(ConstructorDeclaration.ConstantPoolName);
  858. if (methods == NoMethods)
  859. return new ProblemMethodBinding(
  860. ConstructorDeclaration.ConstantPoolName,
  861. argumentTypes,
  862. NotFound);
  863. MethodBinding[] compatible = new MethodBinding[methods.length];
  864. int compatibleIndex = 0;
  865. for (int i = 0, length = methods.length; i < length; i++)
  866. if (areParametersAssignable(methods[i].parameters, argumentTypes))
  867. compatible[compatibleIndex++] = methods[i];
  868. if (compatibleIndex == 0)
  869. return new ProblemMethodBinding(
  870. ConstructorDeclaration.ConstantPoolName,
  871. argumentTypes,
  872. NotFound);
  873. // need a more descriptive error... cannot convert from X to Y
  874. MethodBinding[] visible = new MethodBinding[compatibleIndex];
  875. int visibleIndex = 0;
  876. for (int i = 0; i < compatibleIndex; i++) {
  877. MethodBinding method = compatible[i];
  878. if (method.canBeSeenBy(invocationSite, this))
  879. visible[visibleIndex++] = method;
  880. }
  881. if (visibleIndex == 1)
  882. return visible[0];
  883. if (visibleIndex == 0)
  884. return new ProblemMethodBinding(
  885. ConstructorDeclaration.ConstantPoolName,
  886. argumentTypes,
  887. NotVisible);
  888. return mostSpecificClassMethodBinding(visible, visibleIndex);
  889. }
  890. /*
  891. * This retrieves the argument that maps to an enclosing instance of the suitable type,
  892. * if not found then answers nil -- do not create one
  893. *
  894. * #implicitThis : the implicit this will be ok
  895. * #((arg) this$n) : available as a constructor arg
  896. * #((arg) this$n ... this$p) : available as as a constructor arg + a sequence of fields
  897. * #((fieldDescr) this$n ... this$p) : available as a sequence of fields
  898. * nil : not found
  899. *
  900. * Note that this algorithm should answer the shortest possible sequence when
  901. * shortcuts are available:
  902. * this$0 . this$0 . this$0
  903. * instead of
  904. * this$2 . this$1 . this$0 . this$1 . this$0
  905. * thus the code generation will be more compact and runtime faster
  906. */
  907. public VariableBinding[] getEmulationPath(LocalVariableBinding outerLocalVariable) {
  908. MethodScope currentMethodScope = this.methodScope();
  909. SourceTypeBinding sourceType = currentMethodScope.enclosingSourceType();
  910. // identity check
  911. if (currentMethodScope == outerLocalVariable.declaringScope.methodScope()) {
  912. return new VariableBinding[] { outerLocalVariable };
  913. // implicit this is good enough
  914. }
  915. // use synthetic constructor arguments if possible
  916. if (currentMethodScope.isInsideInitializerOrConstructor()
  917. && (sourceType.isNestedType())) {
  918. SyntheticArgumentBinding syntheticArg;
  919. if ((syntheticArg = ((NestedTypeBinding) sourceType).getSyntheticArgument(outerLocalVariable)) != null) {
  920. return new VariableBinding[] { syntheticArg };
  921. }
  922. }
  923. // use a synthetic field then
  924. if (!currentMethodScope.isStatic) {
  925. FieldBinding syntheticField;
  926. if ((syntheticField = sourceType.getSyntheticField(outerLocalVariable)) != null) {
  927. return new VariableBinding[] { syntheticField };
  928. }
  929. }
  930. return null;
  931. }
  932. /*
  933. * This retrieves the argument that maps to an enclosing instance of the suitable type,
  934. * if not found then answers nil -- do not create one
  935. *
  936. * #implicitThis : the implicit this will be ok
  937. * #((arg) this$n) : available as a constructor arg
  938. * #((arg) this$n access$m... access$p) : available as as a constructor arg + a sequence of synthetic accessors to synthetic fields
  939. * #((fieldDescr) this$n access#m... access$p) : available as a first synthetic field + a sequence of synthetic accessors to synthetic fields
  940. * nil : not found
  941. *
  942. * EXACT MATCH VERSION - no type compatibility is performed
  943. */
  944. public Object[] getExactEmulationPath(ReferenceBinding targetEnclosingType) {
  945. MethodScope currentMethodScope = this.methodScope();
  946. SourceTypeBinding sourceType = currentMethodScope.enclosingSourceType();
  947. // identity check
  948. if (!currentMethodScope.isStatic
  949. && !currentMethodScope.isConstructorCall
  950. && (sourceType == targetEnclosingType)) {
  951. return EmulationPathToImplicitThis; // implicit this is good enough
  952. }
  953. if (!sourceType.isNestedType()
  954. || sourceType.isStatic()) { // no emulation from within non-inner types
  955. return null;
  956. }
  957. boolean insideConstructor =
  958. currentMethodScope.isInsideInitializerOrConstructor();
  959. // use synthetic constructor arguments if possible
  960. if (insideConstructor) {
  961. SyntheticArgumentBinding syntheticArg;
  962. if ((syntheticArg = ((NestedTypeBinding) sourceType).getSyntheticArgument(targetEnclosingType, this, true)) != null) {
  963. return new Object[] { syntheticArg };
  964. }
  965. }
  966. // use a direct synthetic field then
  967. if (!currentMethodScope.isStatic) {
  968. FieldBinding syntheticField;
  969. if ((syntheticField = sourceType.getSyntheticField(targetEnclosingType, this, true)) != null) {
  970. return new Object[] { syntheticField };
  971. }
  972. // could be reached through a sequence of enclosing instance link (nested members)
  973. Object[] path = new Object[2]; // probably at least 2 of them
  974. ReferenceBinding currentType = sourceType.enclosingType();
  975. if (insideConstructor) {
  976. path[0] =
  977. ((NestedTypeBinding) sourceType).getSyntheticArgument((SourceTypeBinding) currentType, this, true);
  978. } else {
  979. path[0] =
  980. sourceType.getSyntheticField((SourceTypeBinding) currentType, this, true);
  981. }
  982. if (path[0] != null) { // keep accumulating
  983. int count = 1;
  984. ReferenceBinding currentEnclosingType;
  985. while ((currentEnclosingType = currentType.enclosingType()) != null) {
  986. //done?
  987. if (currentType == targetEnclosingType)
  988. break;
  989. syntheticField =
  990. ((NestedTypeBinding) currentType).getSyntheticField(
  991. (SourceTypeBinding) currentEnclosingType,
  992. this,
  993. true);
  994. if (syntheticField == null)
  995. break;
  996. // append inside the path
  997. if (count == path.length) {
  998. System.arraycopy(path, 0, (path = new Object[count + 1]), 0, count);
  999. }
  1000. // private access emulation is necessary since synthetic field is private
  1001. path[count++] = ((SourceTypeBinding) syntheticField.declaringClass).addSyntheticMethod(syntheticField, true);
  1002. currentType = currentEnclosingType;
  1003. }
  1004. if (currentType == targetEnclosingType) {
  1005. return path;
  1006. }
  1007. }
  1008. }
  1009. return null;
  1010. }
  1011. /* API
  1012. *
  1013. * Answer the field binding that corresponds to fieldName.
  1014. * Start the lookup at the receiverType.
  1015. * InvocationSite implements
  1016. * isSuperAccess(); this is used to determine if the discovered field is visible.
  1017. * Only fields defined by the receiverType or its supertypes are answered;
  1018. * a field of an enclosing type will not be found using this API.
  1019. *
  1020. * If no visible field is discovered, an error binding is answered.
  1021. */
  1022. public FieldBinding getField(
  1023. TypeBinding receiverType,
  1024. char[] fieldName,
  1025. InvocationSite invocationSite) {
  1026. FieldBinding field = findField(receiverType, fieldName, invocationSite);
  1027. if (field == null)
  1028. return new ProblemFieldBinding(
  1029. receiverType instanceof ReferenceBinding
  1030. ? (ReferenceBinding) receiverType
  1031. : null,
  1032. fieldName,
  1033. NotFound);
  1034. else
  1035. return field;
  1036. }
  1037. /* API
  1038. *
  1039. * Answer the method binding that corresponds to selector, argumentTypes.
  1040. * Start the lookup at the enclosing type of the receiver.
  1041. * InvocationSite implements
  1042. * isSuperAccess(); this is used to determine if the discovered method is visible.
  1043. * setDepth(int); this is used to record the depth of the discovered method
  1044. * relative to the enclosing type of the receiver. (If the method is defined
  1045. * in the enclosing type of the receiver, the depth is 0; in the next enclosing
  1046. * type, the depth is 1; and so on
  1047. *
  1048. * If no visible method is discovered, an error binding is answered.
  1049. */
  1050. public MethodBinding getImplicitMethod(
  1051. char[] selector,
  1052. TypeBinding[] argumentTypes,
  1053. InvocationSite invocationSite) {
  1054. boolean insideStaticContext = false;
  1055. boolean insideConstructorCall = false;
  1056. MethodBinding foundMethod = null;
  1057. ProblemMethodBinding foundFuzzyProblem = null;
  1058. // the weird method lookup case (matches method name in scope, then arg types, then visibility)
  1059. ProblemMethodBinding foundInsideProblem = null;
  1060. // inside Constructor call or inside static context
  1061. Scope scope = this;
  1062. int depth = 0;
  1063. done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
  1064. switch (scope.kind) {
  1065. case METHOD_SCOPE :
  1066. MethodScope methodScope = (MethodScope) scope;
  1067. insideStaticContext |= methodScope.isStatic;
  1068. insideConstructorCall |= methodScope.isConstructorCall;
  1069. break;
  1070. case CLASS_SCOPE :
  1071. ClassScope classScope = (ClassScope) scope;
  1072. SourceTypeBinding receiverType = classScope.referenceContext.binding;
  1073. boolean isExactMatch = true;
  1074. // retrieve an exact visible match (if possible)
  1075. MethodBinding methodBinding =
  1076. (foundMethod == null)
  1077. ? classScope.findExactMethod(
  1078. receiverType,
  1079. selector,
  1080. argumentTypes,
  1081. invocationSite)
  1082. : classScope.findExactMethod(
  1083. receiverType,
  1084. foundMethod.selector,
  1085. foundMethod.parameters,
  1086. invocationSite);
  1087. // ? findExactMethod(receiverType, selector, argumentTypes, invocationSite)
  1088. // : findExactMethod(receiverType, foundMethod.selector, foundMethod.parameters, invocationSite);
  1089. if (methodBinding == null) {
  1090. // answers closest approximation, may not check argumentTypes or visibility
  1091. isExactMatch = false;
  1092. methodBinding =
  1093. classScope.findMethod(receiverType, selector, argumentTypes, invocationSite);
  1094. // methodBinding = findMethod(receiverType, selector, argumentTypes, invocationSite);
  1095. }
  1096. if (methodBinding != null) { // skip it if we did not find anything
  1097. if (methodBinding.problemId() == Ambiguous) {
  1098. if (foundMethod == null || foundMethod.problemId() == NotVisible)
  1099. // supercedes any potential InheritedNameHidesEnclosingName problem
  1100. return methodBinding;
  1101. else
  1102. // make the user qualify the method, likely wants the first inherited method (javac generates an ambiguous error instead)
  1103. return new ProblemMethodBinding(
  1104. selector,
  1105. argumentTypes,
  1106. InheritedNameHidesEnclosingName);
  1107. }
  1108. ProblemMethodBinding fuzzyProblem = null;
  1109. ProblemMethodBinding insideProblem = null;
  1110. if (methodBinding.isValidBinding()) {
  1111. if (!isExactMatch) {
  1112. if (!areParametersAssignable(methodBinding.parameters, argumentTypes)) {
  1113. if (foundMethod == null || foundMethod.problemId() == NotVisible){
  1114. // inherited mismatch is reported directly, not looking at enclosing matches
  1115. return new ProblemMethodBinding(methodBinding, selector, argumentTypes, NotFound);
  1116. }
  1117. // make the user qualify the method, likely wants the first inherited method (javac generates an ambiguous error instead)
  1118. fuzzyProblem = new ProblemMethodBinding(selector, argumentTypes, InheritedNameHidesEnclosingName);
  1119. } else if (!methodBinding.canBeSeenBy(receiverType, invocationSite, classScope)) {
  1120. // using <classScope> instead of <this> for visibility check does grant all access to innerclass
  1121. fuzzyProblem =
  1122. new ProblemMethodBinding(
  1123. selector,
  1124. argumentTypes,
  1125. methodBinding.declaringClass,
  1126. NotVisible);
  1127. }
  1128. }
  1129. if (fuzzyProblem == null && !methodBinding.isStatic()) {
  1130. if (insideConstructorCall) {
  1131. insideProblem =
  1132. new ProblemMethodBinding(
  1133. methodBinding.selector,
  1134. methodBinding.parameters,
  1135. NonStaticReferenceInConstructorInvocation);
  1136. } else if (insideStaticContext) {
  1137. insideProblem =
  1138. new ProblemMethodBinding(
  1139. methodBinding.selector,
  1140. methodBinding.parameters,
  1141. NonStaticReferenceInStaticContext);
  1142. }
  1143. }
  1144. if (receiverType == methodBinding.declaringClass
  1145. || (receiverType.getMethods(selector)) != NoMethods
  1146. || ((fuzzyProblem == null || fuzzyProblem.problemId() != NotVisible) && environment().options.complianceLevel >= CompilerOptions.JDK1_4)){
  1147. // found a valid method in the 'immediate' scope (ie. not inherited)
  1148. // OR the receiverType implemented a method with the correct name
  1149. // OR in 1.4 mode (inherited visible shadows enclosing)
  1150. if (foundMethod == null) {
  1151. if (depth > 0){
  1152. invocationSite.setDepth(depth);
  1153. invocationSite.setActualReceiverType(receiverType);
  1154. }
  1155. // return the methodBinding if it is not declared in a superclass of the scope's binding (i.e. "inherited")
  1156. if (fuzzyProblem != null)
  1157. return fuzzyProblem;
  1158. if (insideProblem != null)
  1159. return insideProblem;
  1160. return methodBinding;
  1161. }
  1162. // if a method was found, complain when another is found in an 'immediate' enclosing type (ie. not inherited)
  1163. // NOTE: Unlike fields, a non visible method hides a visible method
  1164. if (foundMethod.declaringClass != methodBinding.declaringClass)
  1165. // ie. have we found the same method - do not trust field identity yet
  1166. return new ProblemMethodBinding(
  1167. methodBinding.selector,
  1168. methodBinding.parameters,
  1169. InheritedNameHidesEnclosingName);
  1170. }
  1171. }
  1172. if (foundMethod == null
  1173. || (foundMethod.problemId() == NotVisible
  1174. && methodBinding.problemId() != NotVisible)) {
  1175. // only remember the methodBinding if its the first one found or the previous one was not visible & methodBinding is...
  1176. // remember that private methods are visible if defined directly by an enclosing class
  1177. if (depth > 0){
  1178. invocationSite.setDepth(depth);
  1179. invocationSite.setActualReceiverType(receiverType);
  1180. }
  1181. foundFuzzyProblem = fuzzyProblem;
  1182. foundInsideProblem = insideProblem;
  1183. if (fuzzyProblem == null)
  1184. foundMethod = methodBinding; // only keep it if no error was found
  1185. }
  1186. }
  1187. depth++;
  1188. insideStaticContext |= receiverType.isStatic();
  1189. // 1EX5I8Z - accessing outer fields within a constructor call is permitted
  1190. // in order to do so, we change the flag as we exit from the type, not the method
  1191. // itself, because the class scope is used to retrieve the fields.
  1192. MethodScope enclosingMethodScope = scope.methodScope();
  1193. insideConstructorCall =
  1194. enclosingMethodScope == null ? false : enclosingMethodScope.isConstructorCall;
  1195. break;
  1196. case COMPILATION_UNIT_SCOPE :
  1197. break done;
  1198. }
  1199. scope = scope.parent;
  1200. }
  1201. if (foundFuzzyProblem != null)
  1202. return foundFuzzyProblem;
  1203. if (foundInsideProblem != null)
  1204. return foundInsideProblem;
  1205. if (foundMethod != null)
  1206. return foundMethod;
  1207. return new ProblemMethodBinding(selector, argumentTypes, NotFound);
  1208. }
  1209. /* API
  1210. *
  1211. * Answer the method binding that corresponds to selector, argumentTypes.
  1212. * Start the lookup at the receiverType.
  1213. * InvocationSite implements
  1214. * isSuperAccess(); this is used to determine if the discovered method is visible.
  1215. *
  1216. * Only methods defined by the receiverType or its supertypes are answered;
  1217. * use getImplicitMethod() to discover methods of enclosing types.
  1218. *
  1219. * If no visible method is discovered, an error binding is answered.
  1220. */
  1221. public MethodBinding getMethod(
  1222. TypeBinding receiverType,
  1223. char[] selector,
  1224. TypeBinding[] argumentTypes,
  1225. InvocationSite invocationSite) {
  1226. if (receiverType.isArrayType())
  1227. return findMethodForArray(
  1228. (ArrayBinding) receiverType,
  1229. selector,
  1230. argumentTypes,
  1231. invocationSite);
  1232. if (receiverType.isBaseType())
  1233. return new ProblemMethodBinding(selector, argumentTypes, NotFound);
  1234. ReferenceBinding currentType = (ReferenceBinding) receiverType;
  1235. if (!currentType.canBeSeenBy(this))
  1236. return new ProblemMethodBinding(selector, argumentTypes, NotVisible);
  1237. // *** Need a new problem id - TypeNotVisible?
  1238. // retrieve an exact visible match (if possible)
  1239. MethodBinding methodBinding =
  1240. findExactMethod(currentType, selector, argumentTypes, invocationSite);
  1241. if (methodBinding != null)
  1242. return methodBinding;
  1243. // answers closest approximation, may not check argumentTypes or visibility
  1244. methodBinding =
  1245. findMethod(currentType, selector, argumentTypes, invocationSite);
  1246. if (methodBinding == null)
  1247. return new ProblemMethodBinding(selector, argumentTypes, NotFound);
  1248. if (methodBinding.isValidBinding()) {
  1249. if (!areParametersAssignable(methodBinding.parameters, argumentTypes))
  1250. return new ProblemMethodBinding(
  1251. methodBinding,
  1252. selector,
  1253. argumentTypes,
  1254. NotFound);
  1255. if (!methodBinding.canBeSeenBy(currentType, invocationSite, this))
  1256. return new ProblemMethodBinding(
  1257. selector,
  1258. argumentTypes,
  1259. methodBinding.declaringClass,
  1260. NotVisible);
  1261. }
  1262. return methodBinding;
  1263. }
  1264. public int maxShiftedOffset() {
  1265. int max = -1;
  1266. if (this.shiftScopes != null){
  1267. for (int i = 0, length = this.shiftScopes.length; i < length; i++){
  1268. int subMaxOffset = this.shiftScopes[i].maxOffset;
  1269. if (subMaxOffset > max) max = subMaxOffset;
  1270. }
  1271. }
  1272. return max;
  1273. }
  1274. /* Answer the problem reporter to use for raising new problems.
  1275. *
  1276. * Note that as a side-effect, this updates the current reference context
  1277. * (unit, type or method) in case the problem handler decides it is necessary
  1278. * to abort.
  1279. */
  1280. public ProblemReporter problemReporter() {
  1281. return outerMostMethodScope().problemReporter();
  1282. }
  1283. /*
  1284. * Code responsible to request some more emulation work inside the invocation type, so as to supply
  1285. * correct synthetic arguments to any allocation of the target type.
  1286. */
  1287. public void propagateInnerEmulation(
  1288. ReferenceBinding targetType,
  1289. boolean isEnclosingInstanceSupplied,
  1290. boolean useDirectReference) {
  1291. // perform some emulation work in case there is some and we are inside a local type only
  1292. // propage emulation of the enclosing instances
  1293. ReferenceBinding[] syntheticArgumentTypes;
  1294. if ((syntheticArgumentTypes = targetType.syntheticEnclosingInstanceTypes())
  1295. != null) {
  1296. for (int i = 0, max = syntheticArgumentTypes.length; i < max; i++) {
  1297. ReferenceBinding syntheticArgType = syntheticArgumentTypes[i];
  1298. // need to filter out the one that could match a supplied enclosing instance
  1299. if (!(isEnclosingInstanceSupplied
  1300. && (syntheticArgType == targetType.enclosingType()))) {
  1301. this.emulateOuterAccess(syntheticArgType, useDirectReference);
  1302. }
  1303. }
  1304. }
  1305. SyntheticArgumentBinding[] syntheticArguments;
  1306. if ((syntheticArguments = targetType.syntheticOuterLocalVariables()) != null) {
  1307. for (int i = 0, max = syntheticArguments.length; i < max; i++) {
  1308. SyntheticArgumentBinding syntheticArg = syntheticArguments[i];
  1309. // need to filter out the one that could match a supplied enclosing instance
  1310. if (!(isEnclosingInstanceSupplied
  1311. && (syntheticArg.type == targetType.enclosingType()))) {
  1312. this.emulateOuterAccess(syntheticArg.actualOuterLocalVariable);
  1313. }
  1314. }
  1315. }
  1316. }
  1317. /* Answer the reference type of this scope.
  1318. *
  1319. * i.e. the nearest enclosing type of this scope.
  1320. */
  1321. public TypeDeclaration referenceType() {
  1322. return methodScope().referenceType();
  1323. }
  1324. // start position in this scope - for ordering scopes vs. variables
  1325. int startIndex() {
  1326. return startIndex;
  1327. }
  1328. public String toString() {
  1329. return toString(0);
  1330. }
  1331. public String toString(int tab) {
  1332. String s = basicToString(tab);
  1333. for (int i = 0; i < scopeIndex; i++)
  1334. if (subscopes[i] instanceof BlockScope)
  1335. s += ((BlockScope) subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$
  1336. return s;
  1337. }
  1338. }