35 import com.sun.tools.javac.code.Type.*; |
35 import com.sun.tools.javac.code.Type.*; |
36 import com.sun.tools.javac.comp.Attr.ResultInfo; |
36 import com.sun.tools.javac.comp.Attr.ResultInfo; |
37 import com.sun.tools.javac.comp.Infer.InferenceContext; |
37 import com.sun.tools.javac.comp.Infer.InferenceContext; |
38 import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase; |
38 import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase; |
39 import com.sun.tools.javac.tree.JCTree.*; |
39 import com.sun.tools.javac.tree.JCTree.*; |
|
40 import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler; |
40 |
41 |
41 import java.util.ArrayList; |
42 import java.util.ArrayList; |
42 import java.util.Collections; |
43 import java.util.Collections; |
43 import java.util.EnumSet; |
44 import java.util.EnumSet; |
44 import java.util.LinkedHashMap; |
45 import java.util.LinkedHashMap; |
45 import java.util.LinkedHashSet; |
46 import java.util.LinkedHashSet; |
46 import java.util.Map; |
47 import java.util.Map; |
47 import java.util.Set; |
48 import java.util.Set; |
48 import java.util.WeakHashMap; |
49 import java.util.WeakHashMap; |
|
50 import java.util.function.Function; |
49 |
51 |
50 import static com.sun.tools.javac.code.TypeTag.*; |
52 import static com.sun.tools.javac.code.TypeTag.*; |
51 import static com.sun.tools.javac.tree.JCTree.Tag.*; |
53 import static com.sun.tools.javac.tree.JCTree.Tag.*; |
52 import static com.sun.tools.javac.code.Kinds.*; |
54 import static com.sun.tools.javac.code.Kinds.*; |
53 import static com.sun.tools.javac.code.Kinds.Kind.*; |
55 import static com.sun.tools.javac.code.Kinds.Kind.*; |
362 * cloned (to avoid side-effects cause by Attr) and compiler state is |
364 * cloned (to avoid side-effects cause by Attr) and compiler state is |
363 * restored after type-checking. All diagnostics (but critical ones) are |
365 * restored after type-checking. All diagnostics (but critical ones) are |
364 * disabled during speculative type-checking. |
366 * disabled during speculative type-checking. |
365 */ |
367 */ |
366 JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
368 JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
367 final JCTree newTree = new TreeCopier<>(make).copy(tree); |
369 return attribSpeculative(tree, env, resultInfo, new TreeCopier<>(make), |
|
370 (newTree)->new DeferredAttrDiagHandler(log, newTree)); |
|
371 } |
|
372 |
|
373 <Z> JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, TreeCopier<Z> deferredCopier, |
|
374 Function<JCTree, DeferredDiagnosticHandler> diagHandlerCreator) { |
|
375 final JCTree newTree = deferredCopier.copy(tree); |
368 Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner))); |
376 Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner))); |
369 Log.DeferredDiagnosticHandler deferredDiagnosticHandler = |
377 speculativeEnv.info.isSpeculative = true; |
370 new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() { |
378 Log.DeferredDiagnosticHandler deferredDiagnosticHandler = diagHandlerCreator.apply(newTree); |
371 public boolean accepts(final JCDiagnostic d) { |
|
372 class PosScanner extends TreeScanner { |
|
373 boolean found = false; |
|
374 |
|
375 @Override |
|
376 public void scan(JCTree tree) { |
|
377 if (tree != null && |
|
378 tree.pos() == d.getDiagnosticPosition()) { |
|
379 found = true; |
|
380 } |
|
381 super.scan(tree); |
|
382 } |
|
383 } |
|
384 PosScanner posScanner = new PosScanner(); |
|
385 posScanner.scan(newTree); |
|
386 return posScanner.found; |
|
387 } |
|
388 }); |
|
389 try { |
379 try { |
390 attr.attribTree(newTree, speculativeEnv, resultInfo); |
380 attr.attribTree(newTree, speculativeEnv, resultInfo); |
391 unenterScanner.scan(newTree); |
381 unenterScanner.scan(newTree); |
392 return newTree; |
382 return newTree; |
393 } finally { |
383 } finally { |
408 if (csym == null) return; |
398 if (csym == null) return; |
409 typeEnvs.remove(csym); |
399 typeEnvs.remove(csym); |
410 chk.compiled.remove(csym.flatname); |
400 chk.compiled.remove(csym.flatname); |
411 syms.classes.remove(csym.flatname); |
401 syms.classes.remove(csym.flatname); |
412 super.visitClassDef(tree); |
402 super.visitClassDef(tree); |
|
403 } |
|
404 } |
|
405 |
|
406 static class DeferredAttrDiagHandler extends Log.DeferredDiagnosticHandler { |
|
407 |
|
408 static class PosScanner extends TreeScanner { |
|
409 DiagnosticPosition pos; |
|
410 boolean found = false; |
|
411 |
|
412 PosScanner(DiagnosticPosition pos) { |
|
413 this.pos = pos; |
|
414 } |
|
415 |
|
416 @Override |
|
417 public void scan(JCTree tree) { |
|
418 if (tree != null && |
|
419 tree.pos() == pos) { |
|
420 found = true; |
|
421 } |
|
422 super.scan(tree); |
|
423 } |
|
424 } |
|
425 |
|
426 DeferredAttrDiagHandler(Log log, JCTree newTree) { |
|
427 super(log, new Filter<JCDiagnostic>() { |
|
428 public boolean accepts(JCDiagnostic d) { |
|
429 PosScanner posScanner = new PosScanner(d.getDiagnosticPosition()); |
|
430 posScanner.scan(newTree); |
|
431 return posScanner.found; |
|
432 } |
|
433 }); |
413 } |
434 } |
414 } |
435 } |
415 |
436 |
416 /** |
437 /** |
417 * A deferred context is created on each method check. A deferred context is |
438 * A deferred context is created on each method check. A deferred context is |