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. |
574 boolean useVarargs, |
575 boolean useVarargs, |
575 boolean operator) { |
576 boolean operator) { |
576 if (sym.kind == ERR) return bestSoFar; |
577 if (sym.kind == ERR) return bestSoFar; |
577 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar; |
578 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar; |
578 assert sym.kind < AMBIGUOUS; |
579 assert sym.kind < AMBIGUOUS; |
|
580 if ((sym.flags() & POLYMORPHIC_SIGNATURE) != 0 && allowPolymorphicSignature) { |
|
581 assert(site.tag == CLASS); |
|
582 // Never match a MethodHandle.invoke directly. |
|
583 if (useVarargs | allowBoxing | operator) |
|
584 return bestSoFar; |
|
585 // Supply an exactly-typed implicit method instead. |
|
586 sym = findPolymorphicSignatureInstance(env, sym.owner.type, sym.name, (MethodSymbol) sym, argtypes, typeargtypes); |
|
587 } |
579 try { |
588 try { |
580 if (rawInstantiate(env, site, sym, argtypes, typeargtypes, |
589 if (rawInstantiate(env, site, sym, argtypes, typeargtypes, |
581 allowBoxing, useVarargs, Warner.noWarnings) == null) { |
590 allowBoxing, useVarargs, Warner.noWarnings) == null) { |
582 // inapplicable |
591 // inapplicable |
583 switch (bestSoFar.kind) { |
592 switch (bestSoFar.kind) { |
744 List<Type> argtypes, |
753 List<Type> argtypes, |
745 List<Type> typeargtypes, |
754 List<Type> typeargtypes, |
746 boolean allowBoxing, |
755 boolean allowBoxing, |
747 boolean useVarargs, |
756 boolean useVarargs, |
748 boolean operator) { |
757 boolean operator) { |
|
758 Symbol bestSoFar = methodNotFound; |
|
759 if ((site.tsym.flags() & POLYMORPHIC_SIGNATURE) != 0 && |
|
760 allowPolymorphicSignature && |
|
761 site.tag == CLASS && |
|
762 !(useVarargs | allowBoxing | operator)) { |
|
763 // supply an exactly-typed implicit method in java.dyn.InvokeDynamic |
|
764 bestSoFar = findPolymorphicSignatureInstance(env, site, name, null, argtypes, typeargtypes); |
|
765 } |
749 return findMethod(env, |
766 return findMethod(env, |
750 site, |
767 site, |
751 name, |
768 name, |
752 argtypes, |
769 argtypes, |
753 typeargtypes, |
770 typeargtypes, |
754 site.tsym.type, |
771 site.tsym.type, |
755 true, |
772 true, |
756 methodNotFound, |
773 bestSoFar, |
757 allowBoxing, |
774 allowBoxing, |
758 useVarargs, |
775 useVarargs, |
759 operator); |
776 operator); |
760 } |
777 } |
761 // where |
778 // where |
895 * takes place. |
912 * takes place. |
896 * @param name The method's name. |
913 * @param name The method's name. |
897 * @param argtypes The method's value arguments. |
914 * @param argtypes The method's value arguments. |
898 * @param typeargtypes The method's type arguments |
915 * @param typeargtypes The method's type arguments |
899 */ |
916 */ |
900 Symbol findImplicitMethod(Env<AttrContext> env, |
917 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, |
901 Type site, |
918 Type site, |
902 Name name, |
919 Name name, |
903 List<Type> argtypes, |
920 MethodSymbol spMethod, // sig. poly. method or null if none |
904 List<Type> typeargtypes) { |
921 List<Type> argtypes, |
905 assert allowInvokedynamic; |
922 List<Type> typeargtypes) { |
906 assert site == syms.invokeDynamicType || (site == syms.methodHandleType && name == names.invoke); |
923 assert allowPolymorphicSignature; |
|
924 //assert site == syms.invokeDynamicType || site == syms.methodHandleType : site; |
907 ClassSymbol c = (ClassSymbol) site.tsym; |
925 ClassSymbol c = (ClassSymbol) site.tsym; |
908 Scope implicit = c.members().next; |
926 Scope implicit = c.members().next; |
909 if (implicit == null) { |
927 if (implicit == null) { |
910 c.members().next = implicit = new Scope(c); |
928 c.members().next = implicit = new Scope(c); |
911 } |
929 } |
916 restype = typeargtypes.head; |
934 restype = typeargtypes.head; |
917 if (!typeargtypes.tail.isEmpty()) |
935 if (!typeargtypes.tail.isEmpty()) |
918 return methodNotFound; |
936 return methodNotFound; |
919 } |
937 } |
920 List<Type> paramtypes = Type.map(argtypes, implicitArgType); |
938 List<Type> paramtypes = Type.map(argtypes, implicitArgType); |
|
939 long flags; |
|
940 List<Type> exType; |
|
941 if (spMethod != null) { |
|
942 exType = spMethod.getThrownTypes(); |
|
943 flags = spMethod.flags() & AccessFlags; |
|
944 } else { |
|
945 // make it throw all exceptions |
|
946 //assert(site == syms.invokeDynamicType); |
|
947 exType = List.of(syms.throwableType); |
|
948 flags = PUBLIC | STATIC; |
|
949 } |
921 MethodType mtype = new MethodType(paramtypes, |
950 MethodType mtype = new MethodType(paramtypes, |
922 restype, |
951 restype, |
923 List.<Type>nil(), |
952 exType, |
924 syms.methodClass); |
953 syms.methodClass); |
925 int flags = PUBLIC | ABSTRACT; |
954 flags |= ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE; |
926 if (site == syms.invokeDynamicType) flags |= STATIC; |
|
927 Symbol m = null; |
955 Symbol m = null; |
928 for (Scope.Entry e = implicit.lookup(name); |
956 for (Scope.Entry e = implicit.lookup(name); |
929 e.scope != null; |
957 e.scope != null; |
930 e = e.next()) { |
958 e = e.next()) { |
931 Symbol sym = e.sym; |
959 Symbol sym = e.sym; |
1335 sym = findMethod(env, site, name, argtypes, typeargtypes, |
1363 sym = findMethod(env, site, name, argtypes, typeargtypes, |
1336 steps.head.isBoxingRequired(), |
1364 steps.head.isBoxingRequired(), |
1337 env.info.varArgs = steps.head.isVarargsRequired(), false); |
1365 env.info.varArgs = steps.head.isVarargsRequired(), false); |
1338 methodResolutionCache.put(steps.head, sym); |
1366 methodResolutionCache.put(steps.head, sym); |
1339 steps = steps.tail; |
1367 steps = steps.tail; |
1340 } |
|
1341 if (sym.kind >= AMBIGUOUS && |
|
1342 allowInvokedynamic && |
|
1343 (site == syms.invokeDynamicType || |
|
1344 site == syms.methodHandleType && name == names.invoke)) { |
|
1345 // lookup failed; supply an exactly-typed implicit method |
|
1346 sym = findImplicitMethod(env, site, name, argtypes, typeargtypes); |
|
1347 env.info.varArgs = false; |
|
1348 } |
1368 } |
1349 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error |
1369 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error |
1350 MethodResolutionPhase errPhase = |
1370 MethodResolutionPhase errPhase = |
1351 firstErroneousResolutionPhase(); |
1371 firstErroneousResolutionPhase(); |
1352 sym = access(methodResolutionCache.get(errPhase), |
1372 sym = access(methodResolutionCache.get(errPhase), |