langtools/src/share/classes/com/sun/tools/javac/code/Types.java
changeset 21712 d363f367d4c0
parent 21041 99f5e5e97425
child 22163 3651128c74eb
equal deleted inserted replaced
21711:1118b43b5111 21712:d363f367d4c0
   242 
   242 
   243             @Override
   243             @Override
   244             public Type visitClassType(ClassType t, Symbol sym) {
   244             public Type visitClassType(ClassType t, Symbol sym) {
   245                 if (t.tsym == sym)
   245                 if (t.tsym == sym)
   246                     return t;
   246                     return t;
   247                 Type base = asSuper(sym.type, t);
   247                 Type base = asSuper(sym.type, t.tsym);
   248                 if (base == null)
   248                 if (base == null)
   249                     return null;
   249                     return null;
   250                 ListBuffer<Type> from = new ListBuffer<Type>();
   250                 ListBuffer<Type> from = new ListBuffer<Type>();
   251                 ListBuffer<Type> to = new ListBuffer<Type>();
   251                 ListBuffer<Type> to = new ListBuffer<Type>();
   252                 try {
   252                 try {
   685                         t.name != names.init &&
   685                         t.name != names.init &&
   686                         t.name != names.clinit &&
   686                         t.name != names.clinit &&
   687                         (t.flags() & SYNTHETIC) == 0;
   687                         (t.flags() & SYNTHETIC) == 0;
   688             }
   688             }
   689         };
   689         };
   690         private boolean pendingBridges(ClassSymbol origin, TypeSymbol sym) {
   690         private boolean pendingBridges(ClassSymbol origin, TypeSymbol s) {
   691             //a symbol will be completed from a classfile if (a) symbol has
   691             //a symbol will be completed from a classfile if (a) symbol has
   692             //an associated file object with CLASS kind and (b) the symbol has
   692             //an associated file object with CLASS kind and (b) the symbol has
   693             //not been entered
   693             //not been entered
   694             if (origin.classfile != null &&
   694             if (origin.classfile != null &&
   695                     origin.classfile.getKind() == JavaFileObject.Kind.CLASS &&
   695                     origin.classfile.getKind() == JavaFileObject.Kind.CLASS &&
   696                     enter.getEnv(origin) == null) {
   696                     enter.getEnv(origin) == null) {
   697                 return false;
   697                 return false;
   698             }
   698             }
   699             if (origin == sym) {
   699             if (origin == s) {
   700                 return true;
   700                 return true;
   701             }
   701             }
   702             for (Type t : interfaces(origin.type)) {
   702             for (Type t : interfaces(origin.type)) {
   703                 if (pendingBridges((ClassSymbol)t.tsym, sym)) {
   703                 if (pendingBridges((ClassSymbol)t.tsym, s)) {
   704                     return true;
   704                     return true;
   705                 }
   705                 }
   706             }
   706             }
   707             return false;
   707             return false;
   708         }
   708         }
   759             } else if (isSubtype(t, s)) {
   759             } else if (isSubtype(t, s)) {
   760                 return true;
   760                 return true;
   761             } else if (t.hasTag(TYPEVAR)) {
   761             } else if (t.hasTag(TYPEVAR)) {
   762                 return isSubtypeUnchecked(t.getUpperBound(), s, warn);
   762                 return isSubtypeUnchecked(t.getUpperBound(), s, warn);
   763             } else if (!s.isRaw()) {
   763             } else if (!s.isRaw()) {
   764                 Type t2 = asSuper(t, s);
   764                 Type t2 = asSuper(t, s.tsym);
   765                 if (t2 != null && t2.isRaw()) {
   765                 if (t2 != null && t2.isRaw()) {
   766                     if (isReifiable(s)) {
   766                     if (isReifiable(s)) {
   767                         warn.silentWarn(LintCategory.UNCHECKED);
   767                         warn.silentWarn(LintCategory.UNCHECKED);
   768                     } else {
   768                     } else {
   769                         warn.warn(LintCategory.UNCHECKED);
   769                         warn.warn(LintCategory.UNCHECKED);
   912                     return t;
   912                     return t;
   913             }
   913             }
   914 
   914 
   915             @Override
   915             @Override
   916             public Boolean visitClassType(ClassType t, Type s) {
   916             public Boolean visitClassType(ClassType t, Type s) {
   917                 Type sup = asSuper(t, s);
   917                 Type sup = asSuper(t, s.tsym);
   918                 return sup != null
   918                 return sup != null
   919                     && sup.tsym == s.tsym
   919                     && sup.tsym == s.tsym
   920                     // You're not allowed to write
   920                     // You're not allowed to write
   921                     //     Vector<Object> vec = new Vector<String>();
   921                     //     Vector<Object> vec = new Vector<String>();
   922                     // But with wildcards you can write
   922                     // But with wildcards you can write
  1933      * given symbol.  If none exists, return null.
  1933      * given symbol.  If none exists, return null.
  1934      *
  1934      *
  1935      * @param t a type
  1935      * @param t a type
  1936      * @param sym a symbol
  1936      * @param sym a symbol
  1937      */
  1937      */
  1938     public Type asSuper(Type t, Symbol s) {
  1938     public Type asSuper(Type t, Symbol sym) {
  1939         return asSuper(t, s.type);
  1939         return asSuper.visit(t, sym);
  1940     }
       
  1941 
       
  1942     public Type asSuper(Type t, Type s) {
       
  1943         return asSuper.visit(t, s);
       
  1944     }
  1940     }
  1945     // where
  1941     // where
  1946         private SimpleVisitor<Type,Type> asSuper = new SimpleVisitor<Type,Type>() {
  1942         private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
  1947 
  1943 
  1948             public Type visitType(Type t, Type s) {
  1944             public Type visitType(Type t, Symbol sym) {
  1949                 return null;
  1945                 return null;
  1950             }
  1946             }
  1951 
  1947 
  1952             @Override
  1948             @Override
  1953             public Type visitClassType(ClassType t, Type s) {
  1949             public Type visitClassType(ClassType t, Symbol sym) {
  1954                 if (t.tsym == s.tsym)
  1950                 if (t.tsym == sym)
  1955                     return t;
  1951                     return t;
  1956 
  1952 
  1957                 Type st = supertype(t);
  1953                 Type st = supertype(t);
  1958 
  1954                 if (st.hasTag(CLASS) || st.hasTag(TYPEVAR) || st.hasTag(ERROR)) {
  1959                 switch(st.getTag()) {
  1955                     Type x = asSuper(st, sym);
  1960                 default: break;
       
  1961                 case CLASS:
       
  1962                 case ARRAY:
       
  1963                 case TYPEVAR:
       
  1964                 case ERROR: {
       
  1965                     Type x = asSuper(st, s);
       
  1966                     if (x != null)
  1956                     if (x != null)
  1967                         return x;
  1957                         return x;
  1968                 } break;
  1958                 }
  1969                 }
  1959                 if ((sym.flags() & INTERFACE) != 0) {
  1970 
       
  1971                 if ((s.tsym.flags() & INTERFACE) != 0) {
       
  1972                     for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
  1960                     for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
  1973                         Type x = asSuper(l.head, s);
  1961                         Type x = asSuper(l.head, sym);
  1974                         if (x != null)
  1962                         if (x != null)
  1975                             return x;
  1963                             return x;
  1976                     }
  1964                     }
  1977                 }
  1965                 }
  1978                 return null;
  1966                 return null;
  1979             }
  1967             }
  1980 
  1968 
  1981             @Override
  1969             @Override
  1982             public Type visitArrayType(ArrayType t, Type s) {
  1970             public Type visitArrayType(ArrayType t, Symbol sym) {
  1983                 return isSubtype(t, s) ? s : null;
  1971                 return isSubtype(t, sym.type) ? sym.type : null;
  1984             }
  1972             }
  1985 
  1973 
  1986             @Override
  1974             @Override
  1987             public Type visitTypeVar(TypeVar t, Type s) {
  1975             public Type visitTypeVar(TypeVar t, Symbol sym) {
  1988                 if (t.tsym == s.tsym)
  1976                 if (t.tsym == sym)
  1989                     return t;
  1977                     return t;
  1990                 else
  1978                 else
  1991                     return asSuper(t.bound, s);
  1979                     return asSuper(t.bound, sym);
  1992             }
  1980             }
  1993 
  1981 
  1994             @Override
  1982             @Override
  1995             public Type visitErrorType(ErrorType t, Type s) { return t; }
  1983             public Type visitErrorType(ErrorType t, Symbol sym) {
       
  1984                 return t;
       
  1985             }
  1996         };
  1986         };
  1997 
  1987 
  1998     /**
  1988     /**
  1999      * Return the base type of t or any of its outer types that starts
  1989      * Return the base type of t or any of its outer types that starts
  2000      * with the given symbol.  If none exists, return null.
  1990      * with the given symbol.  If none exists, return null.
  3571             //step 2 - compute minimal erased candidate set (MEC)
  3561             //step 2 - compute minimal erased candidate set (MEC)
  3572             List<Type> mec = closureMin(cl);
  3562             List<Type> mec = closureMin(cl);
  3573             //step 3 - for each element G in MEC, compute lci(Inv(G))
  3563             //step 3 - for each element G in MEC, compute lci(Inv(G))
  3574             List<Type> candidates = List.nil();
  3564             List<Type> candidates = List.nil();
  3575             for (Type erasedSupertype : mec) {
  3565             for (Type erasedSupertype : mec) {
  3576                 List<Type> lci = List.of(asSuper(ts.head, erasedSupertype));
  3566                 List<Type> lci = List.of(asSuper(ts.head, erasedSupertype.tsym));
  3577                 for (Type t : ts) {
  3567                 for (Type t : ts) {
  3578                     lci = intersect(lci, List.of(asSuper(t, erasedSupertype)));
  3568                     lci = intersect(lci, List.of(asSuper(t, erasedSupertype.tsym)));
  3579                 }
  3569                 }
  3580                 candidates = candidates.appendList(lci);
  3570                 candidates = candidates.appendList(lci);
  3581             }
  3571             }
  3582             //step 4 - let MEC be { G1, G2 ... Gn }, then we have that
  3572             //step 4 - let MEC be { G1, G2 ... Gn }, then we have that
  3583             //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn))
  3573             //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn))
  3993         List<Type> commonSupers = superClosure(to, erasure(from));
  3983         List<Type> commonSupers = superClosure(to, erasure(from));
  3994         boolean giveWarning = commonSupers.isEmpty();
  3984         boolean giveWarning = commonSupers.isEmpty();
  3995         // The arguments to the supers could be unified here to
  3985         // The arguments to the supers could be unified here to
  3996         // get a more accurate analysis
  3986         // get a more accurate analysis
  3997         while (commonSupers.nonEmpty()) {
  3987         while (commonSupers.nonEmpty()) {
  3998             Type t1 = asSuper(from, commonSupers.head);
  3988             Type t1 = asSuper(from, commonSupers.head.tsym);
  3999             Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
  3989             Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
  4000             if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
  3990             if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
  4001                 return false;
  3991                 return false;
  4002             giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
  3992             giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
  4003             commonSupers = commonSupers.tail;
  3993             commonSupers = commonSupers.tail;
  4024             reverse = true;
  4014             reverse = true;
  4025             to = from;
  4015             to = from;
  4026             from = target;
  4016             from = target;
  4027         }
  4017         }
  4028         Assert.check((from.tsym.flags() & FINAL) != 0);
  4018         Assert.check((from.tsym.flags() & FINAL) != 0);
  4029         Type t1 = asSuper(from, to);
  4019         Type t1 = asSuper(from, to.tsym);
  4030         if (t1 == null) return false;
  4020         if (t1 == null) return false;
  4031         Type t2 = to;
  4021         Type t2 = to;
  4032         if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
  4022         if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
  4033             return false;
  4023             return false;
  4034         if (!allowCovariantReturns)
  4024         if (!allowCovariantReturns)