src/jdk.compiler/share/classes/com/sun/tools/javac/comp/DeferredAttr.java
changeset 50898 12133a6e2613
parent 50564 ef7c4c77d9fa
child 51563 de411d537aae
equal deleted inserted replaced
50897:a4d7eaf58623 50898:12133a6e2613
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2018, 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
    26 package com.sun.tools.javac.comp;
    26 package com.sun.tools.javac.comp;
    27 
    27 
    28 import com.sun.source.tree.LambdaExpressionTree.BodyKind;
    28 import com.sun.source.tree.LambdaExpressionTree.BodyKind;
    29 import com.sun.source.tree.NewClassTree;
    29 import com.sun.source.tree.NewClassTree;
    30 import com.sun.tools.javac.code.*;
    30 import com.sun.tools.javac.code.*;
       
    31 import com.sun.tools.javac.code.Type.ErrorType;
       
    32 import com.sun.tools.javac.code.Type.MethodType;
    31 import com.sun.tools.javac.code.Type.StructuralTypeMapping;
    33 import com.sun.tools.javac.code.Type.StructuralTypeMapping;
    32 import com.sun.tools.javac.code.Types.TypeMapping;
    34 import com.sun.tools.javac.code.Types.TypeMapping;
    33 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
    35 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
    34 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
    36 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
    35 import com.sun.tools.javac.comp.Resolve.ResolveError;
    37 import com.sun.tools.javac.comp.Resolve.ResolveError;
    57 import java.util.Set;
    59 import java.util.Set;
    58 import java.util.WeakHashMap;
    60 import java.util.WeakHashMap;
    59 import java.util.function.Function;
    61 import java.util.function.Function;
    60 
    62 
    61 import com.sun.source.tree.MemberReferenceTree;
    63 import com.sun.source.tree.MemberReferenceTree;
       
    64 import com.sun.tools.javac.code.Type;
    62 import com.sun.tools.javac.tree.JCTree.JCMemberReference.OverloadKind;
    65 import com.sun.tools.javac.tree.JCTree.JCMemberReference.OverloadKind;
    63 
    66 
    64 import static com.sun.tools.javac.code.TypeTag.*;
    67 import static com.sun.tools.javac.code.TypeTag.*;
    65 import static com.sun.tools.javac.tree.JCTree.Tag.*;
    68 import static com.sun.tools.javac.tree.JCTree.Tag.*;
    66 
    69 
  1000      * Map a list of types possibly containing one or more deferred types
  1003      * Map a list of types possibly containing one or more deferred types
  1001      * into a list of ordinary types. Each deferred type D is mapped into a type T,
  1004      * into a list of ordinary types. Each deferred type D is mapped into a type T,
  1002      * where T is computed by retrieving the type that has already been
  1005      * where T is computed by retrieving the type that has already been
  1003      * computed for D during a previous deferred attribution round of the given kind.
  1006      * computed for D during a previous deferred attribution round of the given kind.
  1004      */
  1007      */
  1005     class DeferredTypeMap extends StructuralTypeMapping<Void> {
  1008     class DeferredTypeMap<T> extends StructuralTypeMapping<T> {
  1006         DeferredAttrContext deferredAttrContext;
  1009         DeferredAttrContext deferredAttrContext;
  1007 
  1010 
  1008         protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
  1011         protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
  1009             this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase,
  1012             this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase,
  1010                     infer.emptyContext, emptyDeferredAttrContext, types.noWarnings);
  1013                     infer.emptyContext, emptyDeferredAttrContext, types.noWarnings);
  1011         }
  1014         }
  1012 
  1015 
  1013         @Override
  1016         @Override
  1014         public Type visitType(Type t, Void _unused) {
  1017         public Type visitType(Type t, T p) {
  1015             if (!t.hasTag(DEFERRED)) {
  1018             if (!t.hasTag(DEFERRED)) {
  1016                 return super.visitType(t, null);
  1019                 return super.visitType(t, p);
  1017             } else {
  1020             } else {
  1018                 DeferredType dt = (DeferredType)t;
  1021                 DeferredType dt = (DeferredType)t;
  1019                 return typeOf(dt);
  1022                 return typeOf(dt, p);
  1020             }
  1023             }
  1021         }
  1024         }
  1022 
  1025 
  1023         protected Type typeOf(DeferredType dt) {
  1026         protected Type typeOf(DeferredType dt, T p) {
  1024             switch (deferredAttrContext.mode) {
  1027             switch (deferredAttrContext.mode) {
  1025                 case CHECK:
  1028                 case CHECK:
  1026                     return dt.tree.type == null ? Type.noType : dt.tree.type;
  1029                     return dt.tree.type == null ? Type.noType : dt.tree.type;
  1027                 case SPECULATIVE:
  1030                 case SPECULATIVE:
  1028                     return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
  1031                     return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
  1037      * Each deferred type D is mapped into a type T, where T is computed either by
  1040      * Each deferred type D is mapped into a type T, where T is computed either by
  1038      * (i) retrieving the type that has already been computed for D during a previous
  1041      * (i) retrieving the type that has already been computed for D during a previous
  1039      * attribution round (as before), or (ii) by synthesizing a new type R for D
  1042      * attribution round (as before), or (ii) by synthesizing a new type R for D
  1040      * (the latter step is useful in a recovery scenario).
  1043      * (the latter step is useful in a recovery scenario).
  1041      */
  1044      */
  1042     public class RecoveryDeferredTypeMap extends DeferredTypeMap {
  1045     public class RecoveryDeferredTypeMap extends DeferredTypeMap<Type> {
  1043 
  1046 
  1044         public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
  1047         public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
  1045             super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX);
  1048             super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX);
  1046         }
  1049         }
  1047 
  1050 
  1048         @Override
  1051         @Override
  1049         protected Type typeOf(DeferredType dt) {
  1052         protected Type typeOf(DeferredType dt, Type pt) {
  1050             Type owntype = super.typeOf(dt);
  1053             Type owntype = super.typeOf(dt, pt);
  1051             return owntype == Type.noType ?
  1054             return owntype == Type.noType ?
  1052                         recover(dt) : owntype;
  1055                         recover(dt, pt) : owntype;
       
  1056         }
       
  1057 
       
  1058         @Override
       
  1059         public Type visitMethodType(Type.MethodType t, Type pt) {
       
  1060             if (t.hasTag(METHOD) && deferredAttrContext.mode == AttrMode.CHECK) {
       
  1061                 Type mtype = deferredAttrContext.msym.type;
       
  1062                 mtype = mtype.hasTag(ERROR) ? ((ErrorType)mtype).getOriginalType() : null;
       
  1063                 if (mtype != null && mtype.hasTag(METHOD)) {
       
  1064                     List<Type> argtypes1 = map(t.getParameterTypes(), mtype.getParameterTypes());
       
  1065                     Type restype1 = visit(t.getReturnType(), mtype.getReturnType());
       
  1066                     List<Type> thrown1 = map(t.getThrownTypes(), mtype.getThrownTypes());
       
  1067                     if (argtypes1 == t.getParameterTypes() &&
       
  1068                         restype1 == t.getReturnType() &&
       
  1069                         thrown1 == t.getThrownTypes()) return t;
       
  1070                     else return new MethodType(argtypes1, restype1, thrown1, t.tsym);
       
  1071                 }
       
  1072             }
       
  1073             return super.visitMethodType(t, pt);
  1053         }
  1074         }
  1054 
  1075 
  1055         /**
  1076         /**
  1056          * Synthesize a type for a deferred type that hasn't been previously
  1077          * Synthesize a type for a deferred type that hasn't been previously
  1057          * reduced to an ordinary type. Functional deferred types and conditionals
  1078          * reduced to an ordinary type. Functional deferred types and conditionals
  1058          * are mapped to themselves, in order to have a richer diagnostic
  1079          * are mapped to themselves, in order to have a richer diagnostic
  1059          * representation. Remaining deferred types are attributed using
  1080          * representation. Remaining deferred types are attributed using
  1060          * a default expected type (j.l.Object).
  1081          * a default expected type (j.l.Object).
  1061          */
  1082          */
  1062         private Type recover(DeferredType dt) {
  1083         private Type recover(DeferredType dt, Type pt) {
  1063             dt.check(attr.new RecoveryInfo(deferredAttrContext) {
  1084             dt.check(attr.new RecoveryInfo(deferredAttrContext, pt != null ? pt : Type.recoveryType) {
  1064                 @Override
  1085                 @Override
  1065                 protected Type check(DiagnosticPosition pos, Type found) {
  1086                 protected Type check(DiagnosticPosition pos, Type found) {
  1066                     return chk.checkNonVoid(pos, super.check(pos, found));
  1087                     return chk.checkNonVoid(pos, super.check(pos, found));
  1067                 }
  1088                 }
  1068             });
  1089             });
  1069             return super.visit(dt);
  1090             return super.visit(dt);
       
  1091         }
       
  1092 
       
  1093         private List<Type> map(List<Type> ts, List<Type> pts) {
       
  1094             if (ts.nonEmpty()) {
       
  1095                 List<Type> tail1 = map(ts.tail, pts != null ? pts.tail : null);
       
  1096                 Type t = visit(ts.head, pts != null && pts.nonEmpty() ? pts.head : null);
       
  1097                 if (tail1 != ts.tail || t != ts.head)
       
  1098                     return tail1.prepend(t);
       
  1099             }
       
  1100             return ts;
  1070         }
  1101         }
  1071     }
  1102     }
  1072 
  1103 
  1073     /**
  1104     /**
  1074      * A special tree scanner that would only visit portions of a given tree.
  1105      * A special tree scanner that would only visit portions of a given tree.