src/share/classes/com/sun/tools/javac/code/Types.java

changeset 2400
0e026d3f2786
parent 2398
7c925f35f81c
child 2407
8a5512cb5e9d
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Tue May 27 21:15:06 2014 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Tue May 27 17:30:48 2014 -0600
     1.3 @@ -122,37 +122,34 @@
     1.4      }
     1.5      // </editor-fold>
     1.6  
     1.7 -    // <editor-fold defaultstate="collapsed" desc="upperBound">
     1.8 -    /**
     1.9 -     * The "rvalue conversion".<br>
    1.10 -     * The upper bound of most types is the type
    1.11 -     * itself.  Wildcards, on the other hand have upper
    1.12 -     * and lower bounds.
    1.13 -     * @param t a type
    1.14 -     * @return the upper bound of the given type
    1.15 -     */
    1.16 -    public Type upperBound(Type t) {
    1.17 -        return upperBound.visit(t).unannotatedType();
    1.18 -    }
    1.19 -    // where
    1.20 -        private final MapVisitor<Void> upperBound = new MapVisitor<Void>() {
    1.21 -
    1.22 -            @Override
    1.23 -            public Type visitWildcardType(WildcardType t, Void ignored) {
    1.24 -                if (t.isSuperBound())
    1.25 -                    return t.bound == null ? syms.objectType : t.bound.bound;
    1.26 -                else
    1.27 -                    return visit(t.type);
    1.28 -            }
    1.29 -
    1.30 -            @Override
    1.31 -            public Type visitCapturedType(CapturedType t, Void ignored) {
    1.32 -                return visit(t.bound);
    1.33 -            }
    1.34 -        };
    1.35 -    // </editor-fold>
    1.36 -
    1.37 -    // <editor-fold defaultstate="collapsed" desc="wildLowerBound">
    1.38 +     // <editor-fold defaultstate="collapsed" desc="bounds">
    1.39 +     /**
    1.40 +      * Get a wildcard's upper bound, returning non-wildcards unchanged.
    1.41 +      * @param t a type argument, either a wildcard or a type
    1.42 +      */
    1.43 +     public Type wildUpperBound(Type t) {
    1.44 +         if (t.hasTag(WILDCARD)) {
    1.45 +             WildcardType w = (WildcardType) t.unannotatedType();
    1.46 +             if (w.isSuperBound())
    1.47 +                 return w.bound == null ? syms.objectType : w.bound.bound;
    1.48 +             else
    1.49 +                 return wildUpperBound(w.type);
    1.50 +         }
    1.51 +         else return t;
    1.52 +     }
    1.53 +
    1.54 +     /**
    1.55 +      * Get a capture variable's upper bound, returning other types unchanged.
    1.56 +      * @param t a type
    1.57 +      */
    1.58 +     public Type cvarUpperBound(Type t) {
    1.59 +         if (t.hasTag(TYPEVAR)) {
    1.60 +             TypeVar v = (TypeVar) t.unannotatedType();
    1.61 +             return v.isCaptured() ? cvarUpperBound(v.bound) : v;
    1.62 +         }
    1.63 +         else return t;
    1.64 +     }
    1.65 +
    1.66      /**
    1.67       * Get a wildcard's lower bound, returning non-wildcards unchanged.
    1.68       * @param t a type argument, either a wildcard or a type
    1.69 @@ -164,9 +161,7 @@
    1.70          }
    1.71          else return t;
    1.72      }
    1.73 -    // </editor-fold>
    1.74 -
    1.75 -    // <editor-fold defaultstate="collapsed" desc="cvarLowerBound">
    1.76 +
    1.77      /**
    1.78       * Get a capture variable's lower bound, returning other types unchanged.
    1.79       * @param t a type
    1.80 @@ -904,7 +899,7 @@
    1.81                                               syms.boundClass);
    1.82                          changed = true;
    1.83                      } else if (s != orig) {
    1.84 -                        s = new WildcardType(upperBound(s),
    1.85 +                        s = new WildcardType(wildUpperBound(s),
    1.86                                               BoundKind.EXTENDS,
    1.87                                               syms.boundClass);
    1.88                          changed = true;
    1.89 @@ -1113,7 +1108,7 @@
    1.90                          //check that u == t, where u has been set by Type.withTypeVar
    1.91                          return s.isSuperBound() &&
    1.92                                  !s.isExtendsBound() &&
    1.93 -                                visit(t, upperBound(s));
    1.94 +                                visit(t, wildUpperBound(s));
    1.95                      }
    1.96                  }
    1.97                  default:
    1.98 @@ -1140,7 +1135,7 @@
    1.99                      return visit(s, t);
   1.100  
   1.101                  if (s.isSuperBound() && !s.isExtendsBound())
   1.102 -                    return visit(t, upperBound(s)) && visit(t, wildLowerBound(s));
   1.103 +                    return visit(t, wildUpperBound(s)) && visit(t, wildLowerBound(s));
   1.104  
   1.105                  if (t.isCompound() && s.isCompound()) {
   1.106                      if (!visit(supertype(t), supertype(s)))
   1.107 @@ -1290,7 +1285,7 @@
   1.108                  switch(wt.kind) {
   1.109                      case UNBOUND: //similar to ? extends Object
   1.110                      case EXTENDS: {
   1.111 -                        Type bound = upperBound(s);
   1.112 +                        Type bound = wildUpperBound(s);
   1.113                          undetvar.addBound(InferenceBound.UPPER, bound, this);
   1.114                          break;
   1.115                      }
   1.116 @@ -1351,28 +1346,6 @@
   1.117      // where
   1.118          private TypeRelation containsType = new TypeRelation() {
   1.119  
   1.120 -            private Type U(Type t) {
   1.121 -                while (t.hasTag(WILDCARD)) {
   1.122 -                    WildcardType w = (WildcardType)t.unannotatedType();
   1.123 -                    if (w.isSuperBound())
   1.124 -                        return w.bound == null ? syms.objectType : w.bound.bound;
   1.125 -                    else
   1.126 -                        t = w.type;
   1.127 -                }
   1.128 -                return t;
   1.129 -            }
   1.130 -
   1.131 -            private Type L(Type t) {
   1.132 -                while (t.hasTag(WILDCARD)) {
   1.133 -                    WildcardType w = (WildcardType)t.unannotatedType();
   1.134 -                    if (w.isExtendsBound())
   1.135 -                        return syms.botType;
   1.136 -                    else
   1.137 -                        t = w.type;
   1.138 -                }
   1.139 -                return t;
   1.140 -            }
   1.141 -
   1.142              public Boolean visitType(Type t, Type s) {
   1.143                  if (s.isPartial())
   1.144                      return containedBy(s, t);
   1.145 @@ -1384,13 +1357,13 @@
   1.146  //                System.err.println();
   1.147  //                System.err.format(" does %s contain %s?%n", t, s);
   1.148  //                System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
   1.149 -//                                  upperBound(s), s, t, U(t),
   1.150 +//                                  wildUpperBound(s), s, t, wildUpperBound(t),
   1.151  //                                  t.isSuperBound()
   1.152 -//                                  || isSubtypeNoCapture(upperBound(s), U(t)));
   1.153 +//                                  || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t)));
   1.154  //                System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
   1.155 -//                                  L(t), t, s, wildLowerBound(s),
   1.156 +//                                  wildLowerBound(t), t, s, wildLowerBound(s),
   1.157  //                                  t.isExtendsBound()
   1.158 -//                                  || isSubtypeNoCapture(L(t), wildLowerBound(s)));
   1.159 +//                                  || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s)));
   1.160  //                System.err.println();
   1.161  //            }
   1.162  
   1.163 @@ -1402,8 +1375,9 @@
   1.164  //                    debugContainsType(t, s);
   1.165                      return isSameWildcard(t, s)
   1.166                          || isCaptureOf(s, t)
   1.167 -                        || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), wildLowerBound(s))) &&
   1.168 -                            (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t))));
   1.169 +                        || ((t.isExtendsBound() || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s))) &&
   1.170 +                            // TODO: JDK-8039214, cvarUpperBound call here is incorrect
   1.171 +                            (t.isSuperBound() || isSubtypeNoCapture(cvarUpperBound(wildUpperBound(s)), wildUpperBound(t))));
   1.172                  }
   1.173              }
   1.174  
   1.175 @@ -1521,7 +1495,7 @@
   1.176  
   1.177              @Override
   1.178              public Boolean visitWildcardType(WildcardType t, Type s) {
   1.179 -                return isCastable(upperBound(t), s, warnStack.head);
   1.180 +                return isCastable(wildUpperBound(t), s, warnStack.head);
   1.181              }
   1.182  
   1.183              @Override
   1.184 @@ -1762,12 +1736,12 @@
   1.185  
   1.186                  if (t.isExtendsBound()) {
   1.187                      if (s.isExtendsBound())
   1.188 -                        return !isCastableRecursive(t.type, upperBound(s));
   1.189 +                        return !isCastableRecursive(t.type, wildUpperBound(s));
   1.190                      else if (s.isSuperBound())
   1.191                          return notSoftSubtypeRecursive(wildLowerBound(s), t.type);
   1.192                  } else if (t.isSuperBound()) {
   1.193                      if (s.isExtendsBound())
   1.194 -                        return notSoftSubtypeRecursive(t.type, upperBound(s));
   1.195 +                        return notSoftSubtypeRecursive(t.type, wildUpperBound(s));
   1.196                  }
   1.197                  return false;
   1.198              }
   1.199 @@ -1803,7 +1777,7 @@
   1.200                                 noWarnings);
   1.201          }
   1.202          if (!s.hasTag(WILDCARD))
   1.203 -            s = upperBound(s);
   1.204 +            s = cvarUpperBound(s);
   1.205  
   1.206          return !isSubtype(t, relaxBound(s));
   1.207      }
   1.208 @@ -1860,7 +1834,7 @@
   1.209      // <editor-fold defaultstate="collapsed" desc="Array Utils">
   1.210      public boolean isArray(Type t) {
   1.211          while (t.hasTag(WILDCARD))
   1.212 -            t = upperBound(t);
   1.213 +            t = wildUpperBound(t);
   1.214          return t.hasTag(ARRAY);
   1.215      }
   1.216  
   1.217 @@ -1870,7 +1844,7 @@
   1.218      public Type elemtype(Type t) {
   1.219          switch (t.getTag()) {
   1.220          case WILDCARD:
   1.221 -            return elemtype(upperBound(t));
   1.222 +            return elemtype(wildUpperBound(t));
   1.223          case ARRAY:
   1.224              t = t.unannotatedType();
   1.225              return ((ArrayType)t).elemtype;
   1.226 @@ -2073,7 +2047,7 @@
   1.227  
   1.228              @Override
   1.229              public Type visitWildcardType(WildcardType t, Symbol sym) {
   1.230 -                return memberType(upperBound(t), sym);
   1.231 +                return memberType(wildUpperBound(t), sym);
   1.232              }
   1.233  
   1.234              @Override
   1.235 @@ -2192,7 +2166,7 @@
   1.236  
   1.237              @Override
   1.238              public Type visitWildcardType(WildcardType t, Boolean recurse) {
   1.239 -                return erasure(upperBound(t), recurse);
   1.240 +                return erasure(wildUpperBound(t), recurse);
   1.241              }
   1.242  
   1.243              @Override
   1.244 @@ -2401,8 +2375,7 @@
   1.245                          if (t.hasErasedSupertypes()) {
   1.246                              t.interfaces_field = erasureRecursive(interfaces);
   1.247                          } else if (formals.nonEmpty()) {
   1.248 -                            t.interfaces_field =
   1.249 -                                upperBounds(subst(interfaces, formals, actuals));
   1.250 +                            t.interfaces_field = subst(interfaces, formals, actuals);
   1.251                          }
   1.252                          else {
   1.253                              t.interfaces_field = interfaces;
   1.254 @@ -2971,7 +2944,7 @@
   1.255                      return new ClassType(outer1, typarams1, t.tsym);
   1.256              } else {
   1.257                  Type st = subst(supertype(t));
   1.258 -                List<Type> is = upperBounds(subst(interfaces(t)));
   1.259 +                List<Type> is = subst(interfaces(t));
   1.260                  if (st == supertype(t) && is == interfaces(t))
   1.261                      return t;
   1.262                  else
   1.263 @@ -2988,7 +2961,7 @@
   1.264                  return t;
   1.265              } else {
   1.266                  if (t.isExtendsBound() && bound.isExtendsBound())
   1.267 -                    bound = upperBound(bound);
   1.268 +                    bound = wildUpperBound(bound);
   1.269                  return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
   1.270              }
   1.271          }
   1.272 @@ -3438,8 +3411,8 @@
   1.273                      TypePair pair = new TypePair(c1, c2);
   1.274                      Type m;
   1.275                      if (mergeCache.add(pair)) {
   1.276 -                        m = new WildcardType(lub(upperBound(act1.head),
   1.277 -                                                 upperBound(act2.head)),
   1.278 +                        m = new WildcardType(lub(wildUpperBound(act1.head),
   1.279 +                                                 wildUpperBound(act2.head)),
   1.280                                               BoundKind.EXTENDS,
   1.281                                               syms.boundClass);
   1.282                          mergeCache.remove(pair);
   1.283 @@ -4015,16 +3988,6 @@
   1.284      // </editor-fold>
   1.285  
   1.286      // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
   1.287 -    private List<Type> upperBounds(List<Type> ss) {
   1.288 -        if (ss.isEmpty()) return ss;
   1.289 -        Type head = upperBound(ss.head);
   1.290 -        List<Type> tail = upperBounds(ss.tail);
   1.291 -        if (head != ss.head || tail != ss.tail)
   1.292 -            return tail.prepend(head);
   1.293 -        else
   1.294 -            return ss;
   1.295 -    }
   1.296 -
   1.297      private boolean sideCast(Type from, Type to, Warner warn) {
   1.298          // We are casting from type $from$ to type $to$, which are
   1.299          // non-final unrelated types.  This method
   1.300 @@ -4181,7 +4144,7 @@
   1.301          @Override
   1.302          public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
   1.303              if (source.isExtendsBound())
   1.304 -                adaptRecursive(upperBound(source), upperBound(target));
   1.305 +                adaptRecursive(wildUpperBound(source), wildUpperBound(target));
   1.306              else if (source.isSuperBound())
   1.307                  adaptRecursive(wildLowerBound(source), wildLowerBound(target));
   1.308              return null;
   1.309 @@ -4198,7 +4161,7 @@
   1.310                      val = isSubtype(wildLowerBound(val), wildLowerBound(target))
   1.311                          ? target : val;
   1.312                  } else if (val.isExtendsBound() && target.isExtendsBound()) {
   1.313 -                    val = isSubtype(upperBound(val), upperBound(target))
   1.314 +                    val = isSubtype(wildUpperBound(val), wildUpperBound(target))
   1.315                          ? val : target;
   1.316                  } else if (!isSameType(val, target)) {
   1.317                      throw new AdaptFailure();
   1.318 @@ -4309,7 +4272,7 @@
   1.319          }
   1.320  
   1.321          public Type visitType(Type t, Void s) {
   1.322 -            return high ? upperBound(t) : t;
   1.323 +            return t;
   1.324          }
   1.325  
   1.326          @Override

mercurial