65 TreeInfo treeinfo; |
65 TreeInfo treeinfo; |
66 Types types; |
66 Types types; |
67 JCDiagnostic.Factory diags; |
67 JCDiagnostic.Factory diags; |
68 public final boolean boxingEnabled; // = source.allowBoxing(); |
68 public final boolean boxingEnabled; // = source.allowBoxing(); |
69 public final boolean varargsEnabled; // = source.allowVarargs(); |
69 public final boolean varargsEnabled; // = source.allowVarargs(); |
70 public final boolean allowInvokedynamic; // = options.get("invokedynamic"); |
70 public final boolean allowPolymorphicSignature; |
71 private final boolean debugResolve; |
71 private final boolean debugResolve; |
72 |
72 |
73 public static Resolve instance(Context context) { |
73 public static Resolve instance(Context context) { |
74 Resolve instance = context.get(resolveKey); |
74 Resolve instance = context.get(resolveKey); |
75 if (instance == null) |
75 if (instance == null) |
103 Source source = Source.instance(context); |
103 Source source = Source.instance(context); |
104 boxingEnabled = source.allowBoxing(); |
104 boxingEnabled = source.allowBoxing(); |
105 varargsEnabled = source.allowVarargs(); |
105 varargsEnabled = source.allowVarargs(); |
106 Options options = Options.instance(context); |
106 Options options = Options.instance(context); |
107 debugResolve = options.get("debugresolve") != null; |
107 debugResolve = options.get("debugresolve") != null; |
108 allowInvokedynamic = options.get("invokedynamic") != null; |
108 allowPolymorphicSignature = source.allowPolymorphicSignature() || options.get("invokedynamic") != null; |
109 } |
109 } |
110 |
110 |
111 /** error symbols, which are returned when resolution fails |
111 /** error symbols, which are returned when resolution fails |
112 */ |
112 */ |
113 final SymbolNotFoundError varNotFound; |
113 final SymbolNotFoundError varNotFound; |
299 List<Type> typeargtypes, |
299 List<Type> typeargtypes, |
300 boolean allowBoxing, |
300 boolean allowBoxing, |
301 boolean useVarargs, |
301 boolean useVarargs, |
302 Warner warn) |
302 Warner warn) |
303 throws Infer.InferenceException { |
303 throws Infer.InferenceException { |
|
304 assert ((m.flags() & (POLYMORPHIC_SIGNATURE|HYPOTHETICAL)) != POLYMORPHIC_SIGNATURE); |
304 if (useVarargs && (m.flags() & VARARGS) == 0) return null; |
305 if (useVarargs && (m.flags() & VARARGS) == 0) return null; |
305 Type mt = types.memberType(site, m); |
306 Type mt = types.memberType(site, m); |
306 |
307 |
307 // tvars is the list of formal type variables for which type arguments |
308 // tvars is the list of formal type variables for which type arguments |
308 // need to inferred. |
309 // need to inferred. |
573 boolean useVarargs, |
574 boolean useVarargs, |
574 boolean operator) { |
575 boolean operator) { |
575 if (sym.kind == ERR) return bestSoFar; |
576 if (sym.kind == ERR) return bestSoFar; |
576 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar; |
577 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar; |
577 assert sym.kind < AMBIGUOUS; |
578 assert sym.kind < AMBIGUOUS; |
|
579 if ((sym.flags() & POLYMORPHIC_SIGNATURE) != 0 && allowPolymorphicSignature) { |
|
580 assert(site.tag == CLASS); |
|
581 // Never match a MethodHandle.invoke directly. |
|
582 if (useVarargs | allowBoxing | operator) |
|
583 return bestSoFar; |
|
584 // Supply an exactly-typed implicit method instead. |
|
585 sym = findPolymorphicSignatureInstance(env, sym.owner.type, sym.name, (MethodSymbol) sym, argtypes, typeargtypes); |
|
586 } |
578 try { |
587 try { |
579 if (rawInstantiate(env, site, sym, argtypes, typeargtypes, |
588 if (rawInstantiate(env, site, sym, argtypes, typeargtypes, |
580 allowBoxing, useVarargs, Warner.noWarnings) == null) { |
589 allowBoxing, useVarargs, Warner.noWarnings) == null) { |
581 // inapplicable |
590 // inapplicable |
582 switch (bestSoFar.kind) { |
591 switch (bestSoFar.kind) { |
743 List<Type> argtypes, |
752 List<Type> argtypes, |
744 List<Type> typeargtypes, |
753 List<Type> typeargtypes, |
745 boolean allowBoxing, |
754 boolean allowBoxing, |
746 boolean useVarargs, |
755 boolean useVarargs, |
747 boolean operator) { |
756 boolean operator) { |
|
757 Symbol bestSoFar = methodNotFound; |
|
758 if ((site.tsym.flags() & POLYMORPHIC_SIGNATURE) != 0 && |
|
759 allowPolymorphicSignature && |
|
760 site.tag == CLASS && |
|
761 !(useVarargs | allowBoxing | operator)) { |
|
762 // supply an exactly-typed implicit method in java.dyn.InvokeDynamic |
|
763 bestSoFar = findPolymorphicSignatureInstance(env, site, name, null, argtypes, typeargtypes); |
|
764 } |
748 return findMethod(env, |
765 return findMethod(env, |
749 site, |
766 site, |
750 name, |
767 name, |
751 argtypes, |
768 argtypes, |
752 typeargtypes, |
769 typeargtypes, |
753 site.tsym.type, |
770 site.tsym.type, |
754 true, |
771 true, |
755 methodNotFound, |
772 bestSoFar, |
756 allowBoxing, |
773 allowBoxing, |
757 useVarargs, |
774 useVarargs, |
758 operator); |
775 operator); |
759 } |
776 } |
760 // where |
777 // where |
894 * takes place. |
911 * takes place. |
895 * @param name The method's name. |
912 * @param name The method's name. |
896 * @param argtypes The method's value arguments. |
913 * @param argtypes The method's value arguments. |
897 * @param typeargtypes The method's type arguments |
914 * @param typeargtypes The method's type arguments |
898 */ |
915 */ |
899 Symbol findImplicitMethod(Env<AttrContext> env, |
916 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, |
900 Type site, |
917 Type site, |
901 Name name, |
918 Name name, |
902 List<Type> argtypes, |
919 MethodSymbol spMethod, // sig. poly. method or null if none |
903 List<Type> typeargtypes) { |
920 List<Type> argtypes, |
904 assert allowInvokedynamic; |
921 List<Type> typeargtypes) { |
905 assert site == syms.invokeDynamicType || (site == syms.methodHandleType && name == names.invoke); |
922 assert allowPolymorphicSignature; |
|
923 //assert site == syms.invokeDynamicType || site == syms.methodHandleType : site; |
906 ClassSymbol c = (ClassSymbol) site.tsym; |
924 ClassSymbol c = (ClassSymbol) site.tsym; |
907 Scope implicit = c.members().next; |
925 Scope implicit = c.members().next; |
908 if (implicit == null) { |
926 if (implicit == null) { |
909 c.members().next = implicit = new Scope(c); |
927 c.members().next = implicit = new Scope(c); |
910 } |
928 } |
915 restype = typeargtypes.head; |
933 restype = typeargtypes.head; |
916 if (!typeargtypes.tail.isEmpty()) |
934 if (!typeargtypes.tail.isEmpty()) |
917 return methodNotFound; |
935 return methodNotFound; |
918 } |
936 } |
919 List<Type> paramtypes = Type.map(argtypes, implicitArgType); |
937 List<Type> paramtypes = Type.map(argtypes, implicitArgType); |
|
938 long flags; |
|
939 List<Type> exType; |
|
940 if (spMethod != null) { |
|
941 exType = spMethod.getThrownTypes(); |
|
942 flags = spMethod.flags() & AccessFlags; |
|
943 } else { |
|
944 // make it throw all exceptions |
|
945 //assert(site == syms.invokeDynamicType); |
|
946 exType = List.of(syms.throwableType); |
|
947 flags = PUBLIC | STATIC; |
|
948 } |
920 MethodType mtype = new MethodType(paramtypes, |
949 MethodType mtype = new MethodType(paramtypes, |
921 restype, |
950 restype, |
922 List.<Type>nil(), |
951 exType, |
923 syms.methodClass); |
952 syms.methodClass); |
924 int flags = PUBLIC | ABSTRACT; |
953 flags |= ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE; |
925 if (site == syms.invokeDynamicType) flags |= STATIC; |
|
926 Symbol m = null; |
954 Symbol m = null; |
927 for (Scope.Entry e = implicit.lookup(name); |
955 for (Scope.Entry e = implicit.lookup(name); |
928 e.scope != null; |
956 e.scope != null; |
929 e = e.next()) { |
957 e = e.next()) { |
930 Symbol sym = e.sym; |
958 Symbol sym = e.sym; |
1334 sym = findMethod(env, site, name, argtypes, typeargtypes, |
1362 sym = findMethod(env, site, name, argtypes, typeargtypes, |
1335 steps.head.isBoxingRequired(), |
1363 steps.head.isBoxingRequired(), |
1336 env.info.varArgs = steps.head.isVarargsRequired(), false); |
1364 env.info.varArgs = steps.head.isVarargsRequired(), false); |
1337 methodResolutionCache.put(steps.head, sym); |
1365 methodResolutionCache.put(steps.head, sym); |
1338 steps = steps.tail; |
1366 steps = steps.tail; |
1339 } |
|
1340 if (sym.kind >= AMBIGUOUS && |
|
1341 allowInvokedynamic && |
|
1342 (site == syms.invokeDynamicType || |
|
1343 site == syms.methodHandleType && name == names.invoke)) { |
|
1344 // lookup failed; supply an exactly-typed implicit method |
|
1345 sym = findImplicitMethod(env, site, name, argtypes, typeargtypes); |
|
1346 env.info.varArgs = false; |
|
1347 } |
1367 } |
1348 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error |
1368 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error |
1349 MethodResolutionPhase errPhase = |
1369 MethodResolutionPhase errPhase = |
1350 firstErroneousResolutionPhase(); |
1370 firstErroneousResolutionPhase(); |
1351 sym = access(methodResolutionCache.get(errPhase), |
1371 sym = access(methodResolutionCache.get(errPhase), |