src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 51047 860a3648c494
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    41 import com.sun.tools.javac.comp.Resolve.ReferenceLookupResult.StaticKind;
    41 import com.sun.tools.javac.comp.Resolve.ReferenceLookupResult.StaticKind;
    42 import com.sun.tools.javac.jvm.*;
    42 import com.sun.tools.javac.jvm.*;
    43 import com.sun.tools.javac.main.Option;
    43 import com.sun.tools.javac.main.Option;
    44 import com.sun.tools.javac.resources.CompilerProperties.Errors;
    44 import com.sun.tools.javac.resources.CompilerProperties.Errors;
    45 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
    45 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
       
    46 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    46 import com.sun.tools.javac.tree.*;
    47 import com.sun.tools.javac.tree.*;
    47 import com.sun.tools.javac.tree.JCTree.*;
    48 import com.sun.tools.javac.tree.JCTree.*;
    48 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
    49 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
    49 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
    50 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
    50 import com.sun.tools.javac.util.*;
    51 import com.sun.tools.javac.util.*;
    51 import com.sun.tools.javac.util.DefinedBy.Api;
    52 import com.sun.tools.javac.util.DefinedBy.Api;
    52 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    53 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    53 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    54 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    54 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
    55 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
       
    56 import com.sun.tools.javac.util.JCDiagnostic.Warning;
    55 
    57 
    56 import java.util.Arrays;
    58 import java.util.Arrays;
    57 import java.util.Collection;
    59 import java.util.Collection;
    58 import java.util.EnumSet;
    60 import java.util.EnumSet;
    59 import java.util.HashSet;
    61 import java.util.HashSet;
    61 import java.util.LinkedHashMap;
    63 import java.util.LinkedHashMap;
    62 import java.util.Map;
    64 import java.util.Map;
    63 import java.util.Set;
    65 import java.util.Set;
    64 import java.util.function.BiFunction;
    66 import java.util.function.BiFunction;
    65 import java.util.function.BiPredicate;
    67 import java.util.function.BiPredicate;
       
    68 import java.util.function.Consumer;
    66 import java.util.function.Function;
    69 import java.util.function.Function;
    67 import java.util.function.Predicate;
    70 import java.util.function.Predicate;
    68 import java.util.stream.Stream;
    71 import java.util.stream.Stream;
    69 
    72 
    70 import javax.lang.model.element.ElementVisitor;
    73 import javax.lang.model.element.ElementVisitor;
   103     public final boolean allowFunctionalInterfaceMostSpecific;
   106     public final boolean allowFunctionalInterfaceMostSpecific;
   104     public final boolean allowModules;
   107     public final boolean allowModules;
   105     public final boolean checkVarargsAccessAfterResolution;
   108     public final boolean checkVarargsAccessAfterResolution;
   106     private final boolean compactMethodDiags;
   109     private final boolean compactMethodDiags;
   107     private final boolean allowLocalVariableTypeInference;
   110     private final boolean allowLocalVariableTypeInference;
       
   111     private final boolean allowYieldStatement;
   108     final EnumSet<VerboseResolutionMode> verboseResolutionMode;
   112     final EnumSet<VerboseResolutionMode> verboseResolutionMode;
   109 
   113 
   110     WriteableScope polymorphicSignatureScope;
   114     WriteableScope polymorphicSignatureScope;
   111 
   115 
   112     protected Resolve(Context context) {
   116     protected Resolve(Context context) {
   126         infer = Infer.instance(context);
   130         infer = Infer.instance(context);
   127         finder = ClassFinder.instance(context);
   131         finder = ClassFinder.instance(context);
   128         moduleFinder = ModuleFinder.instance(context);
   132         moduleFinder = ModuleFinder.instance(context);
   129         types = Types.instance(context);
   133         types = Types.instance(context);
   130         diags = JCDiagnostic.Factory.instance(context);
   134         diags = JCDiagnostic.Factory.instance(context);
       
   135         Preview preview = Preview.instance(context);
   131         Source source = Source.instance(context);
   136         Source source = Source.instance(context);
   132         Options options = Options.instance(context);
   137         Options options = Options.instance(context);
   133         compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
   138         compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
   134                 options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
   139                 options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
   135         verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
   140         verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
   136         Target target = Target.instance(context);
   141         Target target = Target.instance(context);
   137         allowFunctionalInterfaceMostSpecific = Feature.FUNCTIONAL_INTERFACE_MOST_SPECIFIC.allowedInSource(source);
   142         allowFunctionalInterfaceMostSpecific = Feature.FUNCTIONAL_INTERFACE_MOST_SPECIFIC.allowedInSource(source);
   138         allowLocalVariableTypeInference = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source);
   143         allowLocalVariableTypeInference = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source);
       
   144         allowYieldStatement = (!preview.isPreview(Feature.SWITCH_EXPRESSION) || preview.isEnabled()) &&
       
   145                 Feature.SWITCH_EXPRESSION.allowedInSource(source);
   139         checkVarargsAccessAfterResolution =
   146         checkVarargsAccessAfterResolution =
   140                 Feature.POST_APPLICABILITY_VARARGS_ACCESS_CHECK.allowedInSource(source);
   147                 Feature.POST_APPLICABILITY_VARARGS_ACCESS_CHECK.allowedInSource(source);
   141         polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
   148         polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
   142         allowModules = Feature.MODULES.allowedInSource(source);
   149         allowModules = Feature.MODULES.allowedInSource(source);
   143     }
   150     }
  1378     }
  1385     }
  1379 
  1386 
  1380     public static class InapplicableMethodException extends RuntimeException {
  1387     public static class InapplicableMethodException extends RuntimeException {
  1381         private static final long serialVersionUID = 0;
  1388         private static final long serialVersionUID = 0;
  1382 
  1389 
  1383         JCDiagnostic diagnostic;
  1390         transient JCDiagnostic diagnostic;
  1384 
  1391 
  1385         InapplicableMethodException(JCDiagnostic diag) {
  1392         InapplicableMethodException(JCDiagnostic diag) {
  1386             this.diagnostic = diag;
  1393             this.diagnostic = diag;
  1387         }
  1394         }
  1388 
  1395 
  2328 
  2335 
  2329         return bestSoFar;
  2336         return bestSoFar;
  2330     }
  2337     }
  2331 
  2338 
  2332     /** Find an unqualified identifier which matches a specified kind set.
  2339     /** Find an unqualified identifier which matches a specified kind set.
       
  2340      *  @param pos       position on which report warnings, if any;
       
  2341      *                   null warnings should not be reported
  2333      *  @param env       The current environment.
  2342      *  @param env       The current environment.
  2334      *  @param name      The identifier's name.
  2343      *  @param name      The identifier's name.
  2335      *  @param kind      Indicates the possible symbol kinds
  2344      *  @param kind      Indicates the possible symbol kinds
  2336      *                   (a subset of VAL, TYP, PCK).
  2345      *                   (a subset of VAL, TYP, PCK).
  2337      */
  2346      */
  2338     Symbol findIdent(Env<AttrContext> env, Name name, KindSelector kind) {
  2347     Symbol findIdent(DiagnosticPosition pos, Env<AttrContext> env, Name name, KindSelector kind) {
  2339         return checkVarType(findIdentInternal(env, name, kind), name);
  2348         return checkRestrictedType(pos, findIdentInternal(env, name, kind), name);
  2340     }
  2349     }
  2341 
  2350 
  2342     Symbol findIdentInternal(Env<AttrContext> env, Name name, KindSelector kind) {
  2351     Symbol findIdentInternal(Env<AttrContext> env, Name name, KindSelector kind) {
  2343         Symbol bestSoFar = typeNotFound;
  2352         Symbol bestSoFar = typeNotFound;
  2344         Symbol sym;
  2353         Symbol sym;
  2360             return lookupPackage(env, name);
  2369             return lookupPackage(env, name);
  2361         else return bestSoFar;
  2370         else return bestSoFar;
  2362     }
  2371     }
  2363 
  2372 
  2364     /** Find an identifier in a package which matches a specified kind set.
  2373     /** Find an identifier in a package which matches a specified kind set.
       
  2374      *  @param pos       position on which report warnings, if any;
       
  2375      *                   null warnings should not be reported
  2365      *  @param env       The current environment.
  2376      *  @param env       The current environment.
  2366      *  @param name      The identifier's name.
  2377      *  @param name      The identifier's name.
  2367      *  @param kind      Indicates the possible symbol kinds
  2378      *  @param kind      Indicates the possible symbol kinds
  2368      *                   (a nonempty subset of TYP, PCK).
  2379      *                   (a nonempty subset of TYP, PCK).
  2369      */
  2380      */
  2370     Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
  2381     Symbol findIdentInPackage(DiagnosticPosition pos,
       
  2382                               Env<AttrContext> env, TypeSymbol pck,
  2371                               Name name, KindSelector kind) {
  2383                               Name name, KindSelector kind) {
  2372         return checkVarType(findIdentInPackageInternal(env, pck, name, kind), name);
  2384         return checkRestrictedType(pos, findIdentInPackageInternal(env, pck, name, kind), name);
  2373     }
  2385     }
  2374 
  2386 
  2375     Symbol findIdentInPackageInternal(Env<AttrContext> env, TypeSymbol pck,
  2387     Symbol findIdentInPackageInternal(Env<AttrContext> env, TypeSymbol pck,
  2376                               Name name, KindSelector kind) {
  2388                               Name name, KindSelector kind) {
  2377         Name fullname = TypeSymbol.formFullName(name, pck);
  2389         Name fullname = TypeSymbol.formFullName(name, pck);
  2378         Symbol bestSoFar = typeNotFound;
  2390         Symbol bestSoFar = typeNotFound;
  2379         if (kind.contains(KindSelector.TYP)) {
  2391         if (kind.contains(KindSelector.TYP)) {
  2380             RecoveryLoadClass recoveryLoadClass =
  2392             RecoveryLoadClass recoveryLoadClass =
  2381                     allowModules && !kind.contains(KindSelector.PCK) &&
  2393                     allowModules && !kind.contains(KindSelector.PCK) &&
  2382                     !pck.exists() && !env.info.isSpeculative ?
  2394                     !pck.exists() && !env.info.attributionMode.isSpeculative ?
  2383                         doRecoveryLoadClass : noRecovery;
  2395                         doRecoveryLoadClass : noRecovery;
  2384             Symbol sym = loadClass(env, fullname, recoveryLoadClass);
  2396             Symbol sym = loadClass(env, fullname, recoveryLoadClass);
  2385             if (sym.exists()) {
  2397             if (sym.exists()) {
  2386                 // don't allow programs to use flatnames
  2398                 // don't allow programs to use flatnames
  2387                 if (name == sym.name) return sym;
  2399                 if (name == sym.name) return sym;
  2393         }
  2405         }
  2394         return bestSoFar;
  2406         return bestSoFar;
  2395     }
  2407     }
  2396 
  2408 
  2397     /** Find an identifier among the members of a given type `site'.
  2409     /** Find an identifier among the members of a given type `site'.
       
  2410      *  @param pos       position on which report warnings, if any;
       
  2411      *                   null warnings should not be reported
  2398      *  @param env       The current environment.
  2412      *  @param env       The current environment.
  2399      *  @param site      The type containing the symbol to be found.
  2413      *  @param site      The type containing the symbol to be found.
  2400      *  @param name      The identifier's name.
  2414      *  @param name      The identifier's name.
  2401      *  @param kind      Indicates the possible symbol kinds
  2415      *  @param kind      Indicates the possible symbol kinds
  2402      *                   (a subset of VAL, TYP).
  2416      *                   (a subset of VAL, TYP).
  2403      */
  2417      */
  2404     Symbol findIdentInType(Env<AttrContext> env, Type site,
  2418     Symbol findIdentInType(DiagnosticPosition pos,
       
  2419                            Env<AttrContext> env, Type site,
  2405                            Name name, KindSelector kind) {
  2420                            Name name, KindSelector kind) {
  2406         return checkVarType(findIdentInTypeInternal(env, site, name, kind), name);
  2421         return checkRestrictedType(pos, findIdentInTypeInternal(env, site, name, kind), name);
  2407     }
  2422     }
  2408 
  2423 
  2409     Symbol findIdentInTypeInternal(Env<AttrContext> env, Type site,
  2424     Symbol findIdentInTypeInternal(Env<AttrContext> env, Type site,
  2410                            Name name, KindSelector kind) {
  2425                            Name name, KindSelector kind) {
  2411         Symbol bestSoFar = typeNotFound;
  2426         Symbol bestSoFar = typeNotFound;
  2422             else bestSoFar = bestOf(bestSoFar, sym);
  2437             else bestSoFar = bestOf(bestSoFar, sym);
  2423         }
  2438         }
  2424         return bestSoFar;
  2439         return bestSoFar;
  2425     }
  2440     }
  2426 
  2441 
  2427     private Symbol checkVarType(Symbol bestSoFar, Name name) {
  2442     private Symbol checkRestrictedType(DiagnosticPosition pos, Symbol bestSoFar, Name name) {
  2428         if (allowLocalVariableTypeInference && name.equals(names.var) &&
  2443         if (bestSoFar.kind == TYP || bestSoFar.kind == ABSENT_TYP) {
  2429                 (bestSoFar.kind == TYP || bestSoFar.kind == ABSENT_TYP)) {
  2444             if (allowLocalVariableTypeInference && name.equals(names.var)) {
  2430             bestSoFar = new BadVarTypeError();
  2445                 bestSoFar = new BadRestrictedTypeError(names.var);
       
  2446             } else if (name.equals(names.yield)) {
       
  2447                 if (allowYieldStatement) {
       
  2448                     bestSoFar = new BadRestrictedTypeError(names.yield);
       
  2449                 } else if (pos != null) {
       
  2450                     log.warning(pos, Warnings.IllegalRefToRestrictedType(names.yield));
       
  2451                 }
       
  2452             }
  2431         }
  2453         }
  2432         return bestSoFar;
  2454         return bestSoFar;
  2433     }
  2455     }
  2434 
  2456 
  2435 /* ***************************************************************************
  2457 /* ***************************************************************************
  2595      *  @param kind      The set of admissible symbol kinds for the identifier.
  2617      *  @param kind      The set of admissible symbol kinds for the identifier.
  2596      */
  2618      */
  2597     Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
  2619     Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
  2598                         Name name, KindSelector kind) {
  2620                         Name name, KindSelector kind) {
  2599         return accessBase(
  2621         return accessBase(
  2600             findIdent(env, name, kind),
  2622             findIdent(pos, env, name, kind),
  2601             pos, env.enclClass.sym.type, name, false);
  2623             pos, env.enclClass.sym.type, name, false);
  2602     }
  2624     }
  2603 
  2625 
  2604     /** Resolve an unqualified method identifier.
  2626     /** Resolve an unqualified method identifier.
  2605      *  @param pos       The position to use for error reporting.
  2627      *  @param pos       The position to use for error reporting.
  3831             else
  3853             else
  3832                 return sym;
  3854                 return sym;
  3833         }
  3855         }
  3834     }
  3856     }
  3835 
  3857 
  3836     class BadVarTypeError extends ResolveError {
  3858     class BadRestrictedTypeError extends ResolveError {
  3837         BadVarTypeError() {
  3859         private final Name typeName;
  3838             super(Kind.BAD_VAR, "bad var use");
  3860         BadRestrictedTypeError(Name typeName) {
       
  3861             super(Kind.BAD_RESTRICTED_TYPE, "bad var use");
       
  3862             this.typeName = typeName;
  3839         }
  3863         }
  3840 
  3864 
  3841         @Override
  3865         @Override
  3842         JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
  3866         JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
  3843             return diags.create(dkind, log.currentSource(), pos, "illegal.ref.to.var.type");
  3867             return diags.create(dkind, log.currentSource(), pos, "illegal.ref.to.restricted.type", typeName);
  3844         }
  3868         }
  3845     }
  3869     }
  3846 
  3870 
  3847     /**
  3871     /**
  3848      * InvalidSymbolError error class indicating that a symbol matching a
  3872      * InvalidSymbolError error class indicating that a symbol matching a