54 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
55 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
56 import com.sun.tools.javac.tree.*;
57 import com.sun.tools.javac.tree.JCTree.*;
58 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
59 import com.sun.tools.javac.util.*;
60 import com.sun.tools.javac.util.DefinedBy.Api;
61 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
62 import com.sun.tools.javac.util.JCDiagnostic.Error;
63 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
64 import com.sun.tools.javac.util.JCDiagnostic.Warning;
65 import com.sun.tools.javac.util.List;
66
67 import static com.sun.tools.javac.code.Flags.*;
68 import static com.sun.tools.javac.code.Flags.ANNOTATION;
69 import static com.sun.tools.javac.code.Flags.BLOCK;
70 import static com.sun.tools.javac.code.Kinds.*;
71 import static com.sun.tools.javac.code.Kinds.Kind.*;
72 import static com.sun.tools.javac.code.TypeTag.*;
73 import static com.sun.tools.javac.code.TypeTag.WILDCARD;
74 import static com.sun.tools.javac.tree.JCTree.Tag.*;
75 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
76
77 /** This is the main context-dependent analysis phase in GJC. It
78 * encompasses name resolution, type checking and constant folding as
79 * subtasks. Some subtasks involve auxiliary classes.
80 * @see Check
81 * @see Resolve
82 * @see ConstFold
83 * @see Infer
84 *
85 * <p><b>This is NOT part of any supported API.
86 * If you write code that depends on this, you do so at your own risk.
87 * This code and its internal interfaces are subject to change or
88 * deletion without notice.</b>
89 */
90 public class Attr extends JCTree.Visitor {
91 protected static final Context.Key<Attr> attrKey = new Context.Key<>();
92
93 final Names names;
379 }
380
381 public Type attribType(JCTree node, TypeSymbol sym) {
382 Env<AttrContext> env = typeEnvs.get(sym);
383 Env<AttrContext> localEnv = env.dup(node, env.info.dup());
384 return attribTree(node, localEnv, unknownTypeInfo);
385 }
386
387 public Type attribImportQualifier(JCImport tree, Env<AttrContext> env) {
388 // Attribute qualifying package or class.
389 JCFieldAccess s = (JCFieldAccess)tree.qualid;
390 return attribTree(s.selected, env,
391 new ResultInfo(tree.staticImport ?
392 KindSelector.TYP : KindSelector.TYP_PCK,
393 Type.noType));
394 }
395
396 public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) {
397 breakTree = tree;
398 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
399 try {
400 attribExpr(expr, env);
401 } catch (BreakAttr b) {
402 return b.env;
403 } catch (AssertionError ae) {
404 if (ae.getCause() instanceof BreakAttr) {
405 return ((BreakAttr)(ae.getCause())).env;
406 } else {
407 throw ae;
408 }
409 } finally {
410 breakTree = null;
411 log.useSource(prev);
412 }
413 return env;
414 }
415
416 public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) {
417 breakTree = tree;
418 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
419 try {
420 attribStat(stmt, env);
421 } catch (BreakAttr b) {
422 return b.env;
423 } catch (AssertionError ae) {
424 if (ae.getCause() instanceof BreakAttr) {
425 return ((BreakAttr)(ae.getCause())).env;
426 } else {
427 throw ae;
428 }
429 } finally {
430 breakTree = null;
431 log.useSource(prev);
432 }
433 return env;
434 }
435
436 private JCTree breakTree = null;
437
438 private static class BreakAttr extends RuntimeException {
439 static final long serialVersionUID = -6924771130405446405L;
440 private Env<AttrContext> env;
441 private BreakAttr(Env<AttrContext> env) {
442 this.env = env;
443 }
444 }
445
446 /**
447 * Mode controlling behavior of Attr.Check
448 */
449 enum CheckMode {
450
451 NORMAL,
2745 ListBuffer<Type> supertypes = new ListBuffer<>();
2746 for (Type i : ict.interfaces_field) {
2747 if (i.isParameterized()) {
2748 targs.appendList(i.tsym.type.allparams());
2749 }
2750 supertypes.append(i.tsym.type);
2751 }
2752 IntersectionClassType notionalIntf = types.makeIntersectionType(supertypes.toList());
2753 notionalIntf.allparams_field = targs.toList();
2754 notionalIntf.tsym.flags_field |= INTERFACE;
2755 return notionalIntf.tsym;
2756 }
2757 };
2758
2759 private Type fallbackDescriptorType(JCExpression tree) {
2760 switch (tree.getTag()) {
2761 case LAMBDA:
2762 JCLambda lambda = (JCLambda)tree;
2763 List<Type> argtypes = List.nil();
2764 for (JCVariableDecl param : lambda.params) {
2765 argtypes = param.vartype != null ?
2766 argtypes.append(param.vartype.type) :
2767 argtypes.append(syms.errType);
2768 }
2769 return new MethodType(argtypes, Type.recoveryType,
2770 List.of(syms.throwableType), syms.methodClass);
2771 case REFERENCE:
2772 return new MethodType(List.nil(), Type.recoveryType,
2773 List.of(syms.throwableType), syms.methodClass);
2774 default:
2775 Assert.error("Cannot get here!");
2776 }
2777 return null;
2778 }
2779
2780 private void checkAccessibleTypes(final DiagnosticPosition pos, final Env<AttrContext> env,
2781 final InferenceContext inferenceContext, final Type... ts) {
2782 checkAccessibleTypes(pos, env, inferenceContext, List.from(ts));
2783 }
2784
2785 private void checkAccessibleTypes(final DiagnosticPosition pos, final Env<AttrContext> env,
|
54 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
55 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
56 import com.sun.tools.javac.tree.*;
57 import com.sun.tools.javac.tree.JCTree.*;
58 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
59 import com.sun.tools.javac.util.*;
60 import com.sun.tools.javac.util.DefinedBy.Api;
61 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
62 import com.sun.tools.javac.util.JCDiagnostic.Error;
63 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
64 import com.sun.tools.javac.util.JCDiagnostic.Warning;
65 import com.sun.tools.javac.util.List;
66
67 import static com.sun.tools.javac.code.Flags.*;
68 import static com.sun.tools.javac.code.Flags.ANNOTATION;
69 import static com.sun.tools.javac.code.Flags.BLOCK;
70 import static com.sun.tools.javac.code.Kinds.*;
71 import static com.sun.tools.javac.code.Kinds.Kind.*;
72 import static com.sun.tools.javac.code.TypeTag.*;
73 import static com.sun.tools.javac.code.TypeTag.WILDCARD;
74 import com.sun.tools.javac.comp.Analyzer.AnalyzerMode;
75 import static com.sun.tools.javac.tree.JCTree.Tag.*;
76 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
77
78 /** This is the main context-dependent analysis phase in GJC. It
79 * encompasses name resolution, type checking and constant folding as
80 * subtasks. Some subtasks involve auxiliary classes.
81 * @see Check
82 * @see Resolve
83 * @see ConstFold
84 * @see Infer
85 *
86 * <p><b>This is NOT part of any supported API.
87 * If you write code that depends on this, you do so at your own risk.
88 * This code and its internal interfaces are subject to change or
89 * deletion without notice.</b>
90 */
91 public class Attr extends JCTree.Visitor {
92 protected static final Context.Key<Attr> attrKey = new Context.Key<>();
93
94 final Names names;
380 }
381
382 public Type attribType(JCTree node, TypeSymbol sym) {
383 Env<AttrContext> env = typeEnvs.get(sym);
384 Env<AttrContext> localEnv = env.dup(node, env.info.dup());
385 return attribTree(node, localEnv, unknownTypeInfo);
386 }
387
388 public Type attribImportQualifier(JCImport tree, Env<AttrContext> env) {
389 // Attribute qualifying package or class.
390 JCFieldAccess s = (JCFieldAccess)tree.qualid;
391 return attribTree(s.selected, env,
392 new ResultInfo(tree.staticImport ?
393 KindSelector.TYP : KindSelector.TYP_PCK,
394 Type.noType));
395 }
396
397 public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) {
398 breakTree = tree;
399 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
400 EnumSet<AnalyzerMode> analyzerModes = EnumSet.copyOf(analyzer.analyzerModes);
401 try {
402 analyzer.analyzerModes.clear();
403 attribExpr(expr, env);
404 } catch (BreakAttr b) {
405 return b.env;
406 } catch (AssertionError ae) {
407 if (ae.getCause() instanceof BreakAttr) {
408 return ((BreakAttr)(ae.getCause())).env;
409 } else {
410 throw ae;
411 }
412 } finally {
413 breakTree = null;
414 log.useSource(prev);
415 analyzer.analyzerModes.addAll(analyzerModes);
416 }
417 return env;
418 }
419
420 public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) {
421 breakTree = tree;
422 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
423 EnumSet<AnalyzerMode> analyzerModes = EnumSet.copyOf(analyzer.analyzerModes);
424 try {
425 analyzer.analyzerModes.clear();
426 attribStat(stmt, env);
427 } catch (BreakAttr b) {
428 return b.env;
429 } catch (AssertionError ae) {
430 if (ae.getCause() instanceof BreakAttr) {
431 return ((BreakAttr)(ae.getCause())).env;
432 } else {
433 throw ae;
434 }
435 } finally {
436 breakTree = null;
437 log.useSource(prev);
438 analyzer.analyzerModes.addAll(analyzerModes);
439 }
440 return env;
441 }
442
443 private JCTree breakTree = null;
444
445 private static class BreakAttr extends RuntimeException {
446 static final long serialVersionUID = -6924771130405446405L;
447 private Env<AttrContext> env;
448 private BreakAttr(Env<AttrContext> env) {
449 this.env = env;
450 }
451 }
452
453 /**
454 * Mode controlling behavior of Attr.Check
455 */
456 enum CheckMode {
457
458 NORMAL,
2752 ListBuffer<Type> supertypes = new ListBuffer<>();
2753 for (Type i : ict.interfaces_field) {
2754 if (i.isParameterized()) {
2755 targs.appendList(i.tsym.type.allparams());
2756 }
2757 supertypes.append(i.tsym.type);
2758 }
2759 IntersectionClassType notionalIntf = types.makeIntersectionType(supertypes.toList());
2760 notionalIntf.allparams_field = targs.toList();
2761 notionalIntf.tsym.flags_field |= INTERFACE;
2762 return notionalIntf.tsym;
2763 }
2764 };
2765
2766 private Type fallbackDescriptorType(JCExpression tree) {
2767 switch (tree.getTag()) {
2768 case LAMBDA:
2769 JCLambda lambda = (JCLambda)tree;
2770 List<Type> argtypes = List.nil();
2771 for (JCVariableDecl param : lambda.params) {
2772 argtypes = param.vartype != null && param.vartype.type != null ?
2773 argtypes.append(param.vartype.type) :
2774 argtypes.append(syms.errType);
2775 }
2776 return new MethodType(argtypes, Type.recoveryType,
2777 List.of(syms.throwableType), syms.methodClass);
2778 case REFERENCE:
2779 return new MethodType(List.nil(), Type.recoveryType,
2780 List.of(syms.throwableType), syms.methodClass);
2781 default:
2782 Assert.error("Cannot get here!");
2783 }
2784 return null;
2785 }
2786
2787 private void checkAccessibleTypes(final DiagnosticPosition pos, final Env<AttrContext> env,
2788 final InferenceContext inferenceContext, final Type... ts) {
2789 checkAccessibleTypes(pos, env, inferenceContext, List.from(ts));
2790 }
2791
2792 private void checkAccessibleTypes(final DiagnosticPosition pos, final Env<AttrContext> env,
|