Wed, 06 Feb 2013 14:03:39 +0000
8007463: Cleanup inference related classes
Summary: Make Infer.InferenceContext an inner class; adjust bound replacement logic in Type.UndetVar
Reviewed-by: jjg
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.code; |
duke@1 | 27 | |
jjg@988 | 28 | import java.util.Collections; |
mcimadamore@1338 | 29 | import java.util.EnumMap; |
mcimadamore@1338 | 30 | import java.util.EnumSet; |
mcimadamore@1338 | 31 | import java.util.Map; |
mcimadamore@1338 | 32 | import java.util.Set; |
mcimadamore@1342 | 33 | |
jjg@1521 | 34 | import javax.lang.model.element.AnnotationMirror; |
duke@1 | 35 | import javax.lang.model.type.*; |
duke@1 | 36 | |
jjg@1357 | 37 | import com.sun.tools.javac.code.Symbol.*; |
jjg@1357 | 38 | import com.sun.tools.javac.util.*; |
mcimadamore@1342 | 39 | import static com.sun.tools.javac.code.BoundKind.*; |
duke@1 | 40 | import static com.sun.tools.javac.code.Flags.*; |
duke@1 | 41 | import static com.sun.tools.javac.code.Kinds.*; |
jjg@1374 | 42 | import static com.sun.tools.javac.code.TypeTag.*; |
duke@1 | 43 | |
duke@1 | 44 | /** This class represents Java types. The class itself defines the behavior of |
duke@1 | 45 | * the following types: |
duke@1 | 46 | * <pre> |
duke@1 | 47 | * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN), |
duke@1 | 48 | * type `void' (tag: VOID), |
duke@1 | 49 | * the bottom type (tag: BOT), |
duke@1 | 50 | * the missing type (tag: NONE). |
duke@1 | 51 | * </pre> |
duke@1 | 52 | * <p>The behavior of the following types is defined in subclasses, which are |
duke@1 | 53 | * all static inner classes of this class: |
duke@1 | 54 | * <pre> |
duke@1 | 55 | * class types (tag: CLASS, class: ClassType), |
duke@1 | 56 | * array types (tag: ARRAY, class: ArrayType), |
duke@1 | 57 | * method types (tag: METHOD, class: MethodType), |
duke@1 | 58 | * package types (tag: PACKAGE, class: PackageType), |
duke@1 | 59 | * type variables (tag: TYPEVAR, class: TypeVar), |
duke@1 | 60 | * type arguments (tag: WILDCARD, class: WildcardType), |
mcimadamore@1268 | 61 | * generic method types (tag: FORALL, class: ForAll), |
duke@1 | 62 | * the error type (tag: ERROR, class: ErrorType). |
duke@1 | 63 | * </pre> |
duke@1 | 64 | * |
jjg@581 | 65 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 66 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 67 | * This code and its internal interfaces are subject to change or |
duke@1 | 68 | * deletion without notice.</b> |
duke@1 | 69 | * |
jjg@1374 | 70 | * @see TypeTag |
duke@1 | 71 | */ |
duke@1 | 72 | public class Type implements PrimitiveType { |
duke@1 | 73 | |
duke@1 | 74 | /** Constant type: no type at all. */ |
duke@1 | 75 | public static final JCNoType noType = new JCNoType(NONE); |
duke@1 | 76 | |
mcimadamore@1347 | 77 | /** Constant type: special type to be used during recovery of deferred expressions. */ |
mcimadamore@1347 | 78 | public static final JCNoType recoveryType = new JCNoType(NONE); |
mcimadamore@1347 | 79 | |
duke@1 | 80 | /** If this switch is turned on, the names of type variables |
duke@1 | 81 | * and anonymous classes are printed with hashcodes appended. |
duke@1 | 82 | */ |
duke@1 | 83 | public static boolean moreInfo = false; |
duke@1 | 84 | |
duke@1 | 85 | /** The tag of this type. |
duke@1 | 86 | * |
jjg@1374 | 87 | * @see TypeTag |
duke@1 | 88 | */ |
jjg@1374 | 89 | protected TypeTag tag; |
duke@1 | 90 | |
jjg@1521 | 91 | /** The defining class / interface / package / type variable. |
duke@1 | 92 | */ |
duke@1 | 93 | public TypeSymbol tsym; |
duke@1 | 94 | |
duke@1 | 95 | /** |
jjg@1374 | 96 | * Checks if the current type tag is equal to the given tag. |
jjg@1374 | 97 | * @return true if tag is equal to the current type tag. |
jjg@1374 | 98 | */ |
jjg@1374 | 99 | public boolean hasTag(TypeTag tag) { |
jjg@1374 | 100 | return this.tag == tag; |
jjg@1374 | 101 | } |
jjg@1374 | 102 | |
jjg@1374 | 103 | /** |
jjg@1374 | 104 | * Returns the current type tag. |
jjg@1374 | 105 | * @return the value of the current type tag. |
jjg@1374 | 106 | */ |
jjg@1374 | 107 | public TypeTag getTag() { |
jjg@1374 | 108 | return tag; |
jjg@1374 | 109 | } |
jjg@1374 | 110 | |
jjg@1374 | 111 | public boolean isNumeric() { |
jjg@1374 | 112 | switch (tag) { |
jjg@1374 | 113 | case BYTE: case CHAR: |
jjg@1374 | 114 | case SHORT: |
jjg@1374 | 115 | case INT: case LONG: |
jjg@1374 | 116 | case FLOAT: case DOUBLE: |
jjg@1374 | 117 | return true; |
jjg@1374 | 118 | default: |
jjg@1374 | 119 | return false; |
jjg@1374 | 120 | } |
jjg@1374 | 121 | } |
jjg@1374 | 122 | |
jjg@1374 | 123 | public boolean isPrimitive() { |
jjg@1374 | 124 | return (isNumeric() || tag == BOOLEAN); |
jjg@1374 | 125 | } |
jjg@1374 | 126 | |
jjg@1374 | 127 | public boolean isPrimitiveOrVoid() { |
jjg@1374 | 128 | return (isPrimitive() || tag == VOID); |
jjg@1374 | 129 | } |
jjg@1374 | 130 | |
jjg@1374 | 131 | public boolean isReference() { |
jjg@1374 | 132 | switch (tag) { |
jjg@1374 | 133 | case CLASS: |
jjg@1374 | 134 | case ARRAY: |
jjg@1374 | 135 | case TYPEVAR: |
jjg@1374 | 136 | case WILDCARD: |
jjg@1374 | 137 | case ERROR: |
jjg@1374 | 138 | return true; |
jjg@1374 | 139 | default: |
jjg@1374 | 140 | return false; |
jjg@1374 | 141 | } |
jjg@1374 | 142 | } |
jjg@1374 | 143 | |
jjg@1374 | 144 | public boolean isNullOrReference() { |
jjg@1374 | 145 | return (tag == BOT || isReference()); |
jjg@1374 | 146 | } |
jjg@1374 | 147 | |
jjg@1374 | 148 | public boolean isPartial() { |
jjg@1374 | 149 | switch(tag) { |
jjg@1374 | 150 | case ERROR: case UNKNOWN: case UNDETVAR: |
jjg@1374 | 151 | return true; |
jjg@1374 | 152 | default: |
jjg@1374 | 153 | return false; |
jjg@1374 | 154 | } |
jjg@1374 | 155 | } |
jjg@1374 | 156 | |
jjg@1374 | 157 | /** |
duke@1 | 158 | * The constant value of this type, null if this type does not |
duke@1 | 159 | * have a constant value attribute. Only primitive types and |
duke@1 | 160 | * strings (ClassType) can have a constant value attribute. |
duke@1 | 161 | * @return the constant value attribute of this type |
duke@1 | 162 | */ |
duke@1 | 163 | public Object constValue() { |
duke@1 | 164 | return null; |
duke@1 | 165 | } |
duke@1 | 166 | |
jjg@904 | 167 | /** |
jjg@904 | 168 | * Get the representation of this type used for modelling purposes. |
jjg@904 | 169 | * By default, this is itself. For ErrorType, a different value |
jjg@1521 | 170 | * may be provided. |
jjg@904 | 171 | */ |
jjg@904 | 172 | public Type getModelType() { |
jjg@904 | 173 | return this; |
jjg@904 | 174 | } |
jjg@904 | 175 | |
jjg@904 | 176 | public static List<Type> getModelTypes(List<Type> ts) { |
jjg@904 | 177 | ListBuffer<Type> lb = new ListBuffer<Type>(); |
jjg@904 | 178 | for (Type t: ts) |
jjg@904 | 179 | lb.append(t.getModelType()); |
jjg@904 | 180 | return lb.toList(); |
jjg@904 | 181 | } |
jjg@904 | 182 | |
duke@1 | 183 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); } |
duke@1 | 184 | |
duke@1 | 185 | /** Define a type given its tag and type symbol |
duke@1 | 186 | */ |
jjg@1374 | 187 | public Type(TypeTag tag, TypeSymbol tsym) { |
duke@1 | 188 | this.tag = tag; |
duke@1 | 189 | this.tsym = tsym; |
duke@1 | 190 | } |
duke@1 | 191 | |
duke@1 | 192 | /** An abstract class for mappings from types to types |
duke@1 | 193 | */ |
duke@1 | 194 | public static abstract class Mapping { |
duke@1 | 195 | private String name; |
duke@1 | 196 | public Mapping(String name) { |
duke@1 | 197 | this.name = name; |
duke@1 | 198 | } |
duke@1 | 199 | public abstract Type apply(Type t); |
duke@1 | 200 | public String toString() { |
duke@1 | 201 | return name; |
duke@1 | 202 | } |
duke@1 | 203 | } |
duke@1 | 204 | |
duke@1 | 205 | /** map a type function over all immediate descendants of this type |
duke@1 | 206 | */ |
duke@1 | 207 | public Type map(Mapping f) { |
duke@1 | 208 | return this; |
duke@1 | 209 | } |
duke@1 | 210 | |
duke@1 | 211 | /** map a type function over a list of types |
duke@1 | 212 | */ |
duke@1 | 213 | public static List<Type> map(List<Type> ts, Mapping f) { |
duke@1 | 214 | if (ts.nonEmpty()) { |
duke@1 | 215 | List<Type> tail1 = map(ts.tail, f); |
duke@1 | 216 | Type t = f.apply(ts.head); |
duke@1 | 217 | if (tail1 != ts.tail || t != ts.head) |
duke@1 | 218 | return tail1.prepend(t); |
duke@1 | 219 | } |
duke@1 | 220 | return ts; |
duke@1 | 221 | } |
duke@1 | 222 | |
duke@1 | 223 | /** Define a constant type, of the same kind as this type |
duke@1 | 224 | * and with given constant value |
duke@1 | 225 | */ |
duke@1 | 226 | public Type constType(Object constValue) { |
duke@1 | 227 | final Object value = constValue; |
jjg@1374 | 228 | Assert.check(isPrimitive()); |
duke@1 | 229 | return new Type(tag, tsym) { |
duke@1 | 230 | @Override |
duke@1 | 231 | public Object constValue() { |
duke@1 | 232 | return value; |
duke@1 | 233 | } |
duke@1 | 234 | @Override |
duke@1 | 235 | public Type baseType() { |
duke@1 | 236 | return tsym.type; |
duke@1 | 237 | } |
duke@1 | 238 | }; |
duke@1 | 239 | } |
duke@1 | 240 | |
duke@1 | 241 | /** |
duke@1 | 242 | * If this is a constant type, return its underlying type. |
duke@1 | 243 | * Otherwise, return the type itself. |
duke@1 | 244 | */ |
duke@1 | 245 | public Type baseType() { |
duke@1 | 246 | return this; |
duke@1 | 247 | } |
duke@1 | 248 | |
jjg@1521 | 249 | /** |
jjg@1521 | 250 | * If this is an annotated type, return the underlying type. |
jjg@1521 | 251 | * Otherwise, return the type itself. |
jjg@1521 | 252 | */ |
jjg@1521 | 253 | public Type unannotatedType() { |
jjg@1521 | 254 | return this; |
jjg@1521 | 255 | } |
jjg@1521 | 256 | |
duke@1 | 257 | /** Return the base types of a list of types. |
duke@1 | 258 | */ |
duke@1 | 259 | public static List<Type> baseTypes(List<Type> ts) { |
duke@1 | 260 | if (ts.nonEmpty()) { |
duke@1 | 261 | Type t = ts.head.baseType(); |
duke@1 | 262 | List<Type> baseTypes = baseTypes(ts.tail); |
duke@1 | 263 | if (t != ts.head || baseTypes != ts.tail) |
duke@1 | 264 | return baseTypes.prepend(t); |
duke@1 | 265 | } |
duke@1 | 266 | return ts; |
duke@1 | 267 | } |
duke@1 | 268 | |
duke@1 | 269 | /** The Java source which this type represents. |
duke@1 | 270 | */ |
duke@1 | 271 | public String toString() { |
duke@1 | 272 | String s = (tsym == null || tsym.name == null) |
duke@1 | 273 | ? "<none>" |
duke@1 | 274 | : tsym.name.toString(); |
duke@1 | 275 | if (moreInfo && tag == TYPEVAR) s = s + hashCode(); |
duke@1 | 276 | return s; |
duke@1 | 277 | } |
duke@1 | 278 | |
duke@1 | 279 | /** |
duke@1 | 280 | * The Java source which this type list represents. A List is |
duke@1 | 281 | * represented as a comma-spearated listing of the elements in |
duke@1 | 282 | * that list. |
duke@1 | 283 | */ |
duke@1 | 284 | public static String toString(List<Type> ts) { |
duke@1 | 285 | if (ts.isEmpty()) { |
duke@1 | 286 | return ""; |
duke@1 | 287 | } else { |
jjg@904 | 288 | StringBuilder buf = new StringBuilder(); |
duke@1 | 289 | buf.append(ts.head.toString()); |
duke@1 | 290 | for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail) |
duke@1 | 291 | buf.append(",").append(l.head.toString()); |
duke@1 | 292 | return buf.toString(); |
duke@1 | 293 | } |
duke@1 | 294 | } |
duke@1 | 295 | |
duke@1 | 296 | /** |
duke@1 | 297 | * The constant value of this type, converted to String |
duke@1 | 298 | */ |
duke@1 | 299 | public String stringValue() { |
jjg@816 | 300 | Object cv = Assert.checkNonNull(constValue()); |
duke@1 | 301 | if (tag == BOOLEAN) |
jjg@816 | 302 | return ((Integer) cv).intValue() == 0 ? "false" : "true"; |
duke@1 | 303 | else if (tag == CHAR) |
jjg@816 | 304 | return String.valueOf((char) ((Integer) cv).intValue()); |
duke@1 | 305 | else |
jjg@816 | 306 | return cv.toString(); |
duke@1 | 307 | } |
duke@1 | 308 | |
duke@1 | 309 | /** |
duke@1 | 310 | * This method is analogous to isSameType, but weaker, since we |
duke@1 | 311 | * never complete classes. Where isSameType would complete a |
duke@1 | 312 | * class, equals assumes that the two types are different. |
duke@1 | 313 | */ |
vromero@1452 | 314 | @Override |
duke@1 | 315 | public boolean equals(Object t) { |
duke@1 | 316 | return super.equals(t); |
duke@1 | 317 | } |
duke@1 | 318 | |
vromero@1452 | 319 | @Override |
duke@1 | 320 | public int hashCode() { |
duke@1 | 321 | return super.hashCode(); |
duke@1 | 322 | } |
duke@1 | 323 | |
duke@1 | 324 | /** Is this a constant type whose value is false? |
duke@1 | 325 | */ |
duke@1 | 326 | public boolean isFalse() { |
duke@1 | 327 | return |
duke@1 | 328 | tag == BOOLEAN && |
duke@1 | 329 | constValue() != null && |
duke@1 | 330 | ((Integer)constValue()).intValue() == 0; |
duke@1 | 331 | } |
duke@1 | 332 | |
duke@1 | 333 | /** Is this a constant type whose value is true? |
duke@1 | 334 | */ |
duke@1 | 335 | public boolean isTrue() { |
duke@1 | 336 | return |
duke@1 | 337 | tag == BOOLEAN && |
duke@1 | 338 | constValue() != null && |
duke@1 | 339 | ((Integer)constValue()).intValue() != 0; |
duke@1 | 340 | } |
duke@1 | 341 | |
duke@1 | 342 | public String argtypes(boolean varargs) { |
duke@1 | 343 | List<Type> args = getParameterTypes(); |
duke@1 | 344 | if (!varargs) return args.toString(); |
jjg@789 | 345 | StringBuilder buf = new StringBuilder(); |
duke@1 | 346 | while (args.tail.nonEmpty()) { |
duke@1 | 347 | buf.append(args.head); |
duke@1 | 348 | args = args.tail; |
duke@1 | 349 | buf.append(','); |
duke@1 | 350 | } |
jjg@1521 | 351 | if (args.head.unannotatedType().tag == ARRAY) { |
jjg@1521 | 352 | buf.append(((ArrayType)args.head.unannotatedType()).elemtype); |
jjg@1521 | 353 | if (args.head.getAnnotations().nonEmpty()) { |
jjg@1521 | 354 | buf.append(args.head.getAnnotations()); |
jjg@1521 | 355 | } |
duke@1 | 356 | buf.append("..."); |
duke@1 | 357 | } else { |
duke@1 | 358 | buf.append(args.head); |
duke@1 | 359 | } |
duke@1 | 360 | return buf.toString(); |
duke@1 | 361 | } |
duke@1 | 362 | |
duke@1 | 363 | /** Access methods. |
duke@1 | 364 | */ |
jjg@1521 | 365 | public List<? extends AnnotationMirror> getAnnotations() { return List.nil(); } |
duke@1 | 366 | public List<Type> getTypeArguments() { return List.nil(); } |
jjg@1521 | 367 | public Type getEnclosingType() { return null; } |
duke@1 | 368 | public List<Type> getParameterTypes() { return List.nil(); } |
duke@1 | 369 | public Type getReturnType() { return null; } |
jjg@1521 | 370 | public Type getReceiverType() { return null; } |
duke@1 | 371 | public List<Type> getThrownTypes() { return List.nil(); } |
duke@1 | 372 | public Type getUpperBound() { return null; } |
duke@1 | 373 | public Type getLowerBound() { return null; } |
duke@1 | 374 | |
duke@1 | 375 | /** Navigation methods, these will work for classes, type variables, |
duke@1 | 376 | * foralls, but will return null for arrays and methods. |
duke@1 | 377 | */ |
duke@1 | 378 | |
duke@1 | 379 | /** Return all parameters of this type and all its outer types in order |
duke@1 | 380 | * outer (first) to inner (last). |
duke@1 | 381 | */ |
duke@1 | 382 | public List<Type> allparams() { return List.nil(); } |
duke@1 | 383 | |
duke@1 | 384 | /** Does this type contain "error" elements? |
duke@1 | 385 | */ |
duke@1 | 386 | public boolean isErroneous() { |
duke@1 | 387 | return false; |
duke@1 | 388 | } |
duke@1 | 389 | |
duke@1 | 390 | public static boolean isErroneous(List<Type> ts) { |
duke@1 | 391 | for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
duke@1 | 392 | if (l.head.isErroneous()) return true; |
duke@1 | 393 | return false; |
duke@1 | 394 | } |
duke@1 | 395 | |
duke@1 | 396 | /** Is this type parameterized? |
duke@1 | 397 | * A class type is parameterized if it has some parameters. |
duke@1 | 398 | * An array type is parameterized if its element type is parameterized. |
duke@1 | 399 | * All other types are not parameterized. |
duke@1 | 400 | */ |
duke@1 | 401 | public boolean isParameterized() { |
duke@1 | 402 | return false; |
duke@1 | 403 | } |
duke@1 | 404 | |
duke@1 | 405 | /** Is this type a raw type? |
duke@1 | 406 | * A class type is a raw type if it misses some of its parameters. |
duke@1 | 407 | * An array type is a raw type if its element type is raw. |
duke@1 | 408 | * All other types are not raw. |
duke@1 | 409 | * Type validation will ensure that the only raw types |
duke@1 | 410 | * in a program are types that miss all their type variables. |
duke@1 | 411 | */ |
duke@1 | 412 | public boolean isRaw() { |
duke@1 | 413 | return false; |
duke@1 | 414 | } |
duke@1 | 415 | |
duke@1 | 416 | public boolean isCompound() { |
duke@1 | 417 | return tsym.completer == null |
duke@1 | 418 | // Compound types can't have a completer. Calling |
duke@1 | 419 | // flags() will complete the symbol causing the |
duke@1 | 420 | // compiler to load classes unnecessarily. This led |
duke@1 | 421 | // to regression 6180021. |
duke@1 | 422 | && (tsym.flags() & COMPOUND) != 0; |
duke@1 | 423 | } |
duke@1 | 424 | |
duke@1 | 425 | public boolean isInterface() { |
duke@1 | 426 | return (tsym.flags() & INTERFACE) != 0; |
duke@1 | 427 | } |
duke@1 | 428 | |
mcimadamore@640 | 429 | public boolean isFinal() { |
mcimadamore@640 | 430 | return (tsym.flags() & FINAL) != 0; |
mcimadamore@640 | 431 | } |
mcimadamore@640 | 432 | |
duke@1 | 433 | /** |
duke@1 | 434 | * Does this type contain occurrences of type t? |
duke@1 | 435 | */ |
duke@1 | 436 | public boolean contains(Type t) { |
duke@1 | 437 | return t == this; |
duke@1 | 438 | } |
duke@1 | 439 | |
duke@1 | 440 | public static boolean contains(List<Type> ts, Type t) { |
duke@1 | 441 | for (List<Type> l = ts; |
duke@1 | 442 | l.tail != null /*inlined: l.nonEmpty()*/; |
duke@1 | 443 | l = l.tail) |
duke@1 | 444 | if (l.head.contains(t)) return true; |
duke@1 | 445 | return false; |
duke@1 | 446 | } |
duke@1 | 447 | |
mcimadamore@635 | 448 | /** Does this type contain an occurrence of some type in 'ts'? |
duke@1 | 449 | */ |
mcimadamore@635 | 450 | public boolean containsAny(List<Type> ts) { |
mcimadamore@635 | 451 | for (Type t : ts) |
mcimadamore@635 | 452 | if (this.contains(t)) return true; |
mcimadamore@635 | 453 | return false; |
mcimadamore@635 | 454 | } |
mcimadamore@635 | 455 | |
mcimadamore@635 | 456 | public static boolean containsAny(List<Type> ts1, List<Type> ts2) { |
mcimadamore@635 | 457 | for (Type t : ts1) |
mcimadamore@635 | 458 | if (t.containsAny(ts2)) return true; |
duke@1 | 459 | return false; |
duke@1 | 460 | } |
duke@1 | 461 | |
mcimadamore@828 | 462 | public static List<Type> filter(List<Type> ts, Filter<Type> tf) { |
mcimadamore@828 | 463 | ListBuffer<Type> buf = ListBuffer.lb(); |
mcimadamore@828 | 464 | for (Type t : ts) { |
mcimadamore@828 | 465 | if (tf.accepts(t)) { |
mcimadamore@828 | 466 | buf.append(t); |
mcimadamore@828 | 467 | } |
mcimadamore@828 | 468 | } |
mcimadamore@828 | 469 | return buf.toList(); |
mcimadamore@828 | 470 | } |
mcimadamore@828 | 471 | |
duke@1 | 472 | public boolean isSuperBound() { return false; } |
duke@1 | 473 | public boolean isExtendsBound() { return false; } |
duke@1 | 474 | public boolean isUnbound() { return false; } |
duke@1 | 475 | public Type withTypeVar(Type t) { return this; } |
duke@1 | 476 | |
duke@1 | 477 | /** The underlying method type of this type. |
duke@1 | 478 | */ |
duke@1 | 479 | public MethodType asMethodType() { throw new AssertionError(); } |
duke@1 | 480 | |
duke@1 | 481 | /** Complete loading all classes in this type. |
duke@1 | 482 | */ |
duke@1 | 483 | public void complete() {} |
duke@1 | 484 | |
duke@1 | 485 | public TypeSymbol asElement() { |
duke@1 | 486 | return tsym; |
duke@1 | 487 | } |
duke@1 | 488 | |
duke@1 | 489 | public TypeKind getKind() { |
duke@1 | 490 | switch (tag) { |
duke@1 | 491 | case BYTE: return TypeKind.BYTE; |
duke@1 | 492 | case CHAR: return TypeKind.CHAR; |
duke@1 | 493 | case SHORT: return TypeKind.SHORT; |
duke@1 | 494 | case INT: return TypeKind.INT; |
duke@1 | 495 | case LONG: return TypeKind.LONG; |
duke@1 | 496 | case FLOAT: return TypeKind.FLOAT; |
duke@1 | 497 | case DOUBLE: return TypeKind.DOUBLE; |
duke@1 | 498 | case BOOLEAN: return TypeKind.BOOLEAN; |
duke@1 | 499 | case VOID: return TypeKind.VOID; |
duke@1 | 500 | case BOT: return TypeKind.NULL; |
duke@1 | 501 | case NONE: return TypeKind.NONE; |
duke@1 | 502 | default: return TypeKind.OTHER; |
duke@1 | 503 | } |
duke@1 | 504 | } |
duke@1 | 505 | |
duke@1 | 506 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 507 | if (isPrimitive()) |
duke@1 | 508 | return v.visitPrimitive(this, p); |
duke@1 | 509 | else |
duke@1 | 510 | throw new AssertionError(); |
duke@1 | 511 | } |
duke@1 | 512 | |
duke@1 | 513 | public static class WildcardType extends Type |
duke@1 | 514 | implements javax.lang.model.type.WildcardType { |
duke@1 | 515 | |
duke@1 | 516 | public Type type; |
duke@1 | 517 | public BoundKind kind; |
duke@1 | 518 | public TypeVar bound; |
duke@1 | 519 | |
duke@1 | 520 | @Override |
duke@1 | 521 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 522 | return v.visitWildcardType(this, s); |
duke@1 | 523 | } |
duke@1 | 524 | |
duke@1 | 525 | public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) { |
duke@1 | 526 | super(WILDCARD, tsym); |
jjg@816 | 527 | this.type = Assert.checkNonNull(type); |
duke@1 | 528 | this.kind = kind; |
duke@1 | 529 | } |
duke@1 | 530 | public WildcardType(WildcardType t, TypeVar bound) { |
duke@1 | 531 | this(t.type, t.kind, t.tsym, bound); |
duke@1 | 532 | } |
duke@1 | 533 | |
duke@1 | 534 | public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) { |
duke@1 | 535 | this(type, kind, tsym); |
duke@1 | 536 | this.bound = bound; |
duke@1 | 537 | } |
duke@1 | 538 | |
mcimadamore@635 | 539 | public boolean contains(Type t) { |
mcimadamore@635 | 540 | return kind != UNBOUND && type.contains(t); |
mcimadamore@635 | 541 | } |
mcimadamore@635 | 542 | |
duke@1 | 543 | public boolean isSuperBound() { |
duke@1 | 544 | return kind == SUPER || |
duke@1 | 545 | kind == UNBOUND; |
duke@1 | 546 | } |
duke@1 | 547 | public boolean isExtendsBound() { |
duke@1 | 548 | return kind == EXTENDS || |
duke@1 | 549 | kind == UNBOUND; |
duke@1 | 550 | } |
duke@1 | 551 | public boolean isUnbound() { |
duke@1 | 552 | return kind == UNBOUND; |
duke@1 | 553 | } |
duke@1 | 554 | |
duke@1 | 555 | public Type withTypeVar(Type t) { |
duke@1 | 556 | //-System.err.println(this+".withTypeVar("+t+");");//DEBUG |
duke@1 | 557 | if (bound == t) |
duke@1 | 558 | return this; |
duke@1 | 559 | bound = (TypeVar)t; |
duke@1 | 560 | return this; |
duke@1 | 561 | } |
duke@1 | 562 | |
duke@1 | 563 | boolean isPrintingBound = false; |
duke@1 | 564 | public String toString() { |
jjg@904 | 565 | StringBuilder s = new StringBuilder(); |
duke@1 | 566 | s.append(kind.toString()); |
duke@1 | 567 | if (kind != UNBOUND) |
duke@1 | 568 | s.append(type); |
duke@1 | 569 | if (moreInfo && bound != null && !isPrintingBound) |
duke@1 | 570 | try { |
duke@1 | 571 | isPrintingBound = true; |
duke@1 | 572 | s.append("{:").append(bound.bound).append(":}"); |
duke@1 | 573 | } finally { |
duke@1 | 574 | isPrintingBound = false; |
duke@1 | 575 | } |
duke@1 | 576 | return s.toString(); |
duke@1 | 577 | } |
duke@1 | 578 | |
duke@1 | 579 | public Type map(Mapping f) { |
duke@1 | 580 | //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG |
duke@1 | 581 | Type t = type; |
duke@1 | 582 | if (t != null) |
duke@1 | 583 | t = f.apply(t); |
duke@1 | 584 | if (t == type) |
duke@1 | 585 | return this; |
duke@1 | 586 | else |
duke@1 | 587 | return new WildcardType(t, kind, tsym, bound); |
duke@1 | 588 | } |
duke@1 | 589 | |
duke@1 | 590 | public Type getExtendsBound() { |
duke@1 | 591 | if (kind == EXTENDS) |
duke@1 | 592 | return type; |
duke@1 | 593 | else |
duke@1 | 594 | return null; |
duke@1 | 595 | } |
duke@1 | 596 | |
duke@1 | 597 | public Type getSuperBound() { |
duke@1 | 598 | if (kind == SUPER) |
duke@1 | 599 | return type; |
duke@1 | 600 | else |
duke@1 | 601 | return null; |
duke@1 | 602 | } |
duke@1 | 603 | |
duke@1 | 604 | public TypeKind getKind() { |
duke@1 | 605 | return TypeKind.WILDCARD; |
duke@1 | 606 | } |
duke@1 | 607 | |
duke@1 | 608 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 609 | return v.visitWildcard(this, p); |
duke@1 | 610 | } |
duke@1 | 611 | } |
duke@1 | 612 | |
duke@1 | 613 | public static class ClassType extends Type implements DeclaredType { |
duke@1 | 614 | |
duke@1 | 615 | /** The enclosing type of this type. If this is the type of an inner |
duke@1 | 616 | * class, outer_field refers to the type of its enclosing |
jjg@1521 | 617 | * instance class, in all other cases it refers to noType. |
duke@1 | 618 | */ |
duke@1 | 619 | private Type outer_field; |
duke@1 | 620 | |
duke@1 | 621 | /** The type parameters of this type (to be set once class is loaded). |
duke@1 | 622 | */ |
duke@1 | 623 | public List<Type> typarams_field; |
duke@1 | 624 | |
duke@1 | 625 | /** A cache variable for the type parameters of this type, |
duke@1 | 626 | * appended to all parameters of its enclosing class. |
duke@1 | 627 | * @see #allparams |
duke@1 | 628 | */ |
duke@1 | 629 | public List<Type> allparams_field; |
duke@1 | 630 | |
duke@1 | 631 | /** The supertype of this class (to be set once class is loaded). |
duke@1 | 632 | */ |
duke@1 | 633 | public Type supertype_field; |
duke@1 | 634 | |
duke@1 | 635 | /** The interfaces of this class (to be set once class is loaded). |
duke@1 | 636 | */ |
duke@1 | 637 | public List<Type> interfaces_field; |
duke@1 | 638 | |
jjg@904 | 639 | /** All the interfaces of this class, including missing ones. |
jjg@904 | 640 | */ |
jjg@904 | 641 | public List<Type> all_interfaces_field; |
jjg@904 | 642 | |
duke@1 | 643 | public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) { |
duke@1 | 644 | super(CLASS, tsym); |
duke@1 | 645 | this.outer_field = outer; |
duke@1 | 646 | this.typarams_field = typarams; |
duke@1 | 647 | this.allparams_field = null; |
duke@1 | 648 | this.supertype_field = null; |
duke@1 | 649 | this.interfaces_field = null; |
duke@1 | 650 | /* |
duke@1 | 651 | // this can happen during error recovery |
duke@1 | 652 | assert |
duke@1 | 653 | outer.isParameterized() ? |
duke@1 | 654 | typarams.length() == tsym.type.typarams().length() : |
duke@1 | 655 | outer.isRaw() ? |
duke@1 | 656 | typarams.length() == 0 : |
duke@1 | 657 | true; |
duke@1 | 658 | */ |
duke@1 | 659 | } |
duke@1 | 660 | |
duke@1 | 661 | @Override |
duke@1 | 662 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 663 | return v.visitClassType(this, s); |
duke@1 | 664 | } |
duke@1 | 665 | |
duke@1 | 666 | public Type constType(Object constValue) { |
duke@1 | 667 | final Object value = constValue; |
duke@1 | 668 | return new ClassType(getEnclosingType(), typarams_field, tsym) { |
duke@1 | 669 | @Override |
duke@1 | 670 | public Object constValue() { |
duke@1 | 671 | return value; |
duke@1 | 672 | } |
duke@1 | 673 | @Override |
duke@1 | 674 | public Type baseType() { |
duke@1 | 675 | return tsym.type; |
duke@1 | 676 | } |
duke@1 | 677 | }; |
duke@1 | 678 | } |
duke@1 | 679 | |
duke@1 | 680 | /** The Java source which this type represents. |
duke@1 | 681 | */ |
duke@1 | 682 | public String toString() { |
jjg@904 | 683 | StringBuilder buf = new StringBuilder(); |
duke@1 | 684 | if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) { |
duke@1 | 685 | buf.append(getEnclosingType().toString()); |
duke@1 | 686 | buf.append("."); |
duke@1 | 687 | buf.append(className(tsym, false)); |
duke@1 | 688 | } else { |
duke@1 | 689 | buf.append(className(tsym, true)); |
duke@1 | 690 | } |
duke@1 | 691 | if (getTypeArguments().nonEmpty()) { |
duke@1 | 692 | buf.append('<'); |
duke@1 | 693 | buf.append(getTypeArguments().toString()); |
duke@1 | 694 | buf.append(">"); |
duke@1 | 695 | } |
duke@1 | 696 | return buf.toString(); |
duke@1 | 697 | } |
duke@1 | 698 | //where |
duke@1 | 699 | private String className(Symbol sym, boolean longform) { |
jjg@113 | 700 | if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) { |
jjg@904 | 701 | StringBuilder s = new StringBuilder(supertype_field.toString()); |
duke@1 | 702 | for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) { |
duke@1 | 703 | s.append("&"); |
duke@1 | 704 | s.append(is.head.toString()); |
duke@1 | 705 | } |
duke@1 | 706 | return s.toString(); |
jjg@113 | 707 | } else if (sym.name.isEmpty()) { |
duke@1 | 708 | String s; |
duke@1 | 709 | ClassType norm = (ClassType) tsym.type; |
duke@1 | 710 | if (norm == null) { |
duke@1 | 711 | s = Log.getLocalizedString("anonymous.class", (Object)null); |
duke@1 | 712 | } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) { |
duke@1 | 713 | s = Log.getLocalizedString("anonymous.class", |
duke@1 | 714 | norm.interfaces_field.head); |
duke@1 | 715 | } else { |
duke@1 | 716 | s = Log.getLocalizedString("anonymous.class", |
duke@1 | 717 | norm.supertype_field); |
duke@1 | 718 | } |
duke@1 | 719 | if (moreInfo) |
duke@1 | 720 | s += String.valueOf(sym.hashCode()); |
duke@1 | 721 | return s; |
duke@1 | 722 | } else if (longform) { |
duke@1 | 723 | return sym.getQualifiedName().toString(); |
duke@1 | 724 | } else { |
duke@1 | 725 | return sym.name.toString(); |
duke@1 | 726 | } |
duke@1 | 727 | } |
duke@1 | 728 | |
duke@1 | 729 | public List<Type> getTypeArguments() { |
duke@1 | 730 | if (typarams_field == null) { |
duke@1 | 731 | complete(); |
duke@1 | 732 | if (typarams_field == null) |
duke@1 | 733 | typarams_field = List.nil(); |
duke@1 | 734 | } |
duke@1 | 735 | return typarams_field; |
duke@1 | 736 | } |
duke@1 | 737 | |
mcimadamore@30 | 738 | public boolean hasErasedSupertypes() { |
mcimadamore@30 | 739 | return isRaw(); |
mcimadamore@30 | 740 | } |
mcimadamore@30 | 741 | |
duke@1 | 742 | public Type getEnclosingType() { |
duke@1 | 743 | return outer_field; |
duke@1 | 744 | } |
duke@1 | 745 | |
duke@1 | 746 | public void setEnclosingType(Type outer) { |
duke@1 | 747 | outer_field = outer; |
duke@1 | 748 | } |
duke@1 | 749 | |
duke@1 | 750 | public List<Type> allparams() { |
duke@1 | 751 | if (allparams_field == null) { |
duke@1 | 752 | allparams_field = getTypeArguments().prependList(getEnclosingType().allparams()); |
duke@1 | 753 | } |
duke@1 | 754 | return allparams_field; |
duke@1 | 755 | } |
duke@1 | 756 | |
duke@1 | 757 | public boolean isErroneous() { |
duke@1 | 758 | return |
duke@1 | 759 | getEnclosingType().isErroneous() || |
duke@1 | 760 | isErroneous(getTypeArguments()) || |
duke@1 | 761 | this != tsym.type && tsym.type.isErroneous(); |
duke@1 | 762 | } |
duke@1 | 763 | |
duke@1 | 764 | public boolean isParameterized() { |
duke@1 | 765 | return allparams().tail != null; |
duke@1 | 766 | // optimization, was: allparams().nonEmpty(); |
duke@1 | 767 | } |
duke@1 | 768 | |
duke@1 | 769 | /** A cache for the rank. */ |
duke@1 | 770 | int rank_field = -1; |
duke@1 | 771 | |
duke@1 | 772 | /** A class type is raw if it misses some |
duke@1 | 773 | * of its type parameter sections. |
duke@1 | 774 | * After validation, this is equivalent to: |
jjg@1326 | 775 | * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); } |
duke@1 | 776 | */ |
duke@1 | 777 | public boolean isRaw() { |
duke@1 | 778 | return |
duke@1 | 779 | this != tsym.type && // necessary, but not sufficient condition |
duke@1 | 780 | tsym.type.allparams().nonEmpty() && |
duke@1 | 781 | allparams().isEmpty(); |
duke@1 | 782 | } |
duke@1 | 783 | |
duke@1 | 784 | public Type map(Mapping f) { |
duke@1 | 785 | Type outer = getEnclosingType(); |
duke@1 | 786 | Type outer1 = f.apply(outer); |
duke@1 | 787 | List<Type> typarams = getTypeArguments(); |
duke@1 | 788 | List<Type> typarams1 = map(typarams, f); |
duke@1 | 789 | if (outer1 == outer && typarams1 == typarams) return this; |
duke@1 | 790 | else return new ClassType(outer1, typarams1, tsym); |
duke@1 | 791 | } |
duke@1 | 792 | |
duke@1 | 793 | public boolean contains(Type elem) { |
duke@1 | 794 | return |
duke@1 | 795 | elem == this |
duke@1 | 796 | || (isParameterized() |
mcimadamore@635 | 797 | && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))) |
mcimadamore@635 | 798 | || (isCompound() |
mcimadamore@635 | 799 | && (supertype_field.contains(elem) || contains(interfaces_field, elem))); |
duke@1 | 800 | } |
duke@1 | 801 | |
duke@1 | 802 | public void complete() { |
duke@1 | 803 | if (tsym.completer != null) tsym.complete(); |
duke@1 | 804 | } |
duke@1 | 805 | |
duke@1 | 806 | public TypeKind getKind() { |
duke@1 | 807 | return TypeKind.DECLARED; |
duke@1 | 808 | } |
duke@1 | 809 | |
duke@1 | 810 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 811 | return v.visitDeclared(this, p); |
duke@1 | 812 | } |
duke@1 | 813 | } |
duke@1 | 814 | |
mcimadamore@30 | 815 | public static class ErasedClassType extends ClassType { |
mcimadamore@30 | 816 | public ErasedClassType(Type outer, TypeSymbol tsym) { |
mcimadamore@30 | 817 | super(outer, List.<Type>nil(), tsym); |
mcimadamore@30 | 818 | } |
mcimadamore@30 | 819 | |
mcimadamore@30 | 820 | @Override |
mcimadamore@30 | 821 | public boolean hasErasedSupertypes() { |
mcimadamore@30 | 822 | return true; |
mcimadamore@30 | 823 | } |
mcimadamore@30 | 824 | } |
mcimadamore@30 | 825 | |
jjg@988 | 826 | // a clone of a ClassType that knows about the alternatives of a union type. |
jjg@988 | 827 | public static class UnionClassType extends ClassType implements UnionType { |
jjg@988 | 828 | final List<? extends Type> alternatives_field; |
jjg@988 | 829 | |
jjg@988 | 830 | public UnionClassType(ClassType ct, List<? extends Type> alternatives) { |
jjg@988 | 831 | super(ct.outer_field, ct.typarams_field, ct.tsym); |
jjg@988 | 832 | allparams_field = ct.allparams_field; |
jjg@988 | 833 | supertype_field = ct.supertype_field; |
jjg@988 | 834 | interfaces_field = ct.interfaces_field; |
jjg@988 | 835 | all_interfaces_field = ct.interfaces_field; |
jjg@988 | 836 | alternatives_field = alternatives; |
jjg@988 | 837 | } |
jjg@988 | 838 | |
jjg@988 | 839 | public Type getLub() { |
jjg@988 | 840 | return tsym.type; |
jjg@988 | 841 | } |
jjg@988 | 842 | |
jjg@988 | 843 | public java.util.List<? extends TypeMirror> getAlternatives() { |
jjg@988 | 844 | return Collections.unmodifiableList(alternatives_field); |
jjg@988 | 845 | } |
jjg@988 | 846 | |
jjg@988 | 847 | @Override |
jjg@988 | 848 | public TypeKind getKind() { |
jjg@988 | 849 | return TypeKind.UNION; |
jjg@988 | 850 | } |
jjg@988 | 851 | |
jjg@988 | 852 | @Override |
jjg@988 | 853 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
jjg@988 | 854 | return v.visitUnion(this, p); |
jjg@988 | 855 | } |
jjg@988 | 856 | } |
jjg@988 | 857 | |
mcimadamore@1436 | 858 | // a clone of a ClassType that knows about the bounds of an intersection type. |
mcimadamore@1436 | 859 | public static class IntersectionClassType extends ClassType implements IntersectionType { |
mcimadamore@1436 | 860 | |
mcimadamore@1436 | 861 | public boolean allInterfaces; |
mcimadamore@1436 | 862 | |
mcimadamore@1436 | 863 | public enum IntersectionKind { |
mcimadamore@1436 | 864 | EXPLICIT, |
mcimadamore@1436 | 865 | IMPLICT; |
mcimadamore@1436 | 866 | } |
mcimadamore@1436 | 867 | |
mcimadamore@1436 | 868 | public IntersectionKind intersectionKind; |
mcimadamore@1436 | 869 | |
mcimadamore@1436 | 870 | public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) { |
mcimadamore@1436 | 871 | super(Type.noType, List.<Type>nil(), csym); |
mcimadamore@1436 | 872 | this.allInterfaces = allInterfaces; |
mcimadamore@1436 | 873 | Assert.check((csym.flags() & COMPOUND) != 0); |
mcimadamore@1436 | 874 | supertype_field = bounds.head; |
mcimadamore@1436 | 875 | interfaces_field = bounds.tail; |
mcimadamore@1436 | 876 | Assert.check(supertype_field.tsym.completer != null || |
mcimadamore@1436 | 877 | !supertype_field.isInterface(), supertype_field); |
mcimadamore@1436 | 878 | } |
mcimadamore@1436 | 879 | |
mcimadamore@1436 | 880 | public java.util.List<? extends TypeMirror> getBounds() { |
mcimadamore@1436 | 881 | return Collections.unmodifiableList(getComponents()); |
mcimadamore@1436 | 882 | } |
mcimadamore@1436 | 883 | |
mcimadamore@1436 | 884 | public List<Type> getComponents() { |
mcimadamore@1436 | 885 | return interfaces_field.prepend(supertype_field); |
mcimadamore@1436 | 886 | } |
mcimadamore@1436 | 887 | |
mcimadamore@1436 | 888 | @Override |
mcimadamore@1436 | 889 | public TypeKind getKind() { |
mcimadamore@1436 | 890 | return TypeKind.INTERSECTION; |
mcimadamore@1436 | 891 | } |
mcimadamore@1436 | 892 | |
mcimadamore@1436 | 893 | @Override |
mcimadamore@1436 | 894 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
mcimadamore@1436 | 895 | return intersectionKind == IntersectionKind.EXPLICIT ? |
mcimadamore@1436 | 896 | v.visitIntersection(this, p) : |
mcimadamore@1436 | 897 | v.visitDeclared(this, p); |
mcimadamore@1436 | 898 | } |
mcimadamore@1436 | 899 | } |
mcimadamore@1436 | 900 | |
duke@1 | 901 | public static class ArrayType extends Type |
duke@1 | 902 | implements javax.lang.model.type.ArrayType { |
duke@1 | 903 | |
duke@1 | 904 | public Type elemtype; |
duke@1 | 905 | |
duke@1 | 906 | public ArrayType(Type elemtype, TypeSymbol arrayClass) { |
duke@1 | 907 | super(ARRAY, arrayClass); |
duke@1 | 908 | this.elemtype = elemtype; |
duke@1 | 909 | } |
duke@1 | 910 | |
duke@1 | 911 | @Override |
duke@1 | 912 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 913 | return v.visitArrayType(this, s); |
duke@1 | 914 | } |
duke@1 | 915 | |
duke@1 | 916 | public String toString() { |
duke@1 | 917 | return elemtype + "[]"; |
duke@1 | 918 | } |
duke@1 | 919 | |
duke@1 | 920 | public boolean equals(Object obj) { |
duke@1 | 921 | return |
duke@1 | 922 | this == obj || |
duke@1 | 923 | (obj instanceof ArrayType && |
duke@1 | 924 | this.elemtype.equals(((ArrayType)obj).elemtype)); |
duke@1 | 925 | } |
duke@1 | 926 | |
duke@1 | 927 | public int hashCode() { |
jjg@1374 | 928 | return (ARRAY.ordinal() << 5) + elemtype.hashCode(); |
duke@1 | 929 | } |
duke@1 | 930 | |
mcimadamore@795 | 931 | public boolean isVarargs() { |
mcimadamore@795 | 932 | return false; |
mcimadamore@795 | 933 | } |
mcimadamore@795 | 934 | |
duke@1 | 935 | public List<Type> allparams() { return elemtype.allparams(); } |
duke@1 | 936 | |
duke@1 | 937 | public boolean isErroneous() { |
duke@1 | 938 | return elemtype.isErroneous(); |
duke@1 | 939 | } |
duke@1 | 940 | |
duke@1 | 941 | public boolean isParameterized() { |
duke@1 | 942 | return elemtype.isParameterized(); |
duke@1 | 943 | } |
duke@1 | 944 | |
duke@1 | 945 | public boolean isRaw() { |
duke@1 | 946 | return elemtype.isRaw(); |
duke@1 | 947 | } |
duke@1 | 948 | |
mcimadamore@795 | 949 | public ArrayType makeVarargs() { |
mcimadamore@795 | 950 | return new ArrayType(elemtype, tsym) { |
mcimadamore@795 | 951 | @Override |
mcimadamore@795 | 952 | public boolean isVarargs() { |
mcimadamore@795 | 953 | return true; |
mcimadamore@795 | 954 | } |
mcimadamore@795 | 955 | }; |
mcimadamore@795 | 956 | } |
mcimadamore@795 | 957 | |
duke@1 | 958 | public Type map(Mapping f) { |
duke@1 | 959 | Type elemtype1 = f.apply(elemtype); |
duke@1 | 960 | if (elemtype1 == elemtype) return this; |
duke@1 | 961 | else return new ArrayType(elemtype1, tsym); |
duke@1 | 962 | } |
duke@1 | 963 | |
duke@1 | 964 | public boolean contains(Type elem) { |
duke@1 | 965 | return elem == this || elemtype.contains(elem); |
duke@1 | 966 | } |
duke@1 | 967 | |
duke@1 | 968 | public void complete() { |
duke@1 | 969 | elemtype.complete(); |
duke@1 | 970 | } |
duke@1 | 971 | |
duke@1 | 972 | public Type getComponentType() { |
duke@1 | 973 | return elemtype; |
duke@1 | 974 | } |
duke@1 | 975 | |
duke@1 | 976 | public TypeKind getKind() { |
duke@1 | 977 | return TypeKind.ARRAY; |
duke@1 | 978 | } |
duke@1 | 979 | |
duke@1 | 980 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 981 | return v.visitArray(this, p); |
duke@1 | 982 | } |
duke@1 | 983 | } |
duke@1 | 984 | |
dlsmith@880 | 985 | public static class MethodType extends Type implements ExecutableType { |
duke@1 | 986 | |
duke@1 | 987 | public List<Type> argtypes; |
duke@1 | 988 | public Type restype; |
duke@1 | 989 | public List<Type> thrown; |
duke@1 | 990 | |
jjg@1521 | 991 | /** The type annotations on the method receiver. |
jjg@1521 | 992 | */ |
jjg@1521 | 993 | public Type recvtype; |
jjg@1521 | 994 | |
duke@1 | 995 | public MethodType(List<Type> argtypes, |
duke@1 | 996 | Type restype, |
duke@1 | 997 | List<Type> thrown, |
duke@1 | 998 | TypeSymbol methodClass) { |
duke@1 | 999 | super(METHOD, methodClass); |
duke@1 | 1000 | this.argtypes = argtypes; |
duke@1 | 1001 | this.restype = restype; |
duke@1 | 1002 | this.thrown = thrown; |
duke@1 | 1003 | } |
duke@1 | 1004 | |
duke@1 | 1005 | @Override |
duke@1 | 1006 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1007 | return v.visitMethodType(this, s); |
duke@1 | 1008 | } |
duke@1 | 1009 | |
duke@1 | 1010 | /** The Java source which this type represents. |
duke@1 | 1011 | * |
duke@1 | 1012 | * XXX 06/09/99 iris This isn't correct Java syntax, but it probably |
duke@1 | 1013 | * should be. |
duke@1 | 1014 | */ |
duke@1 | 1015 | public String toString() { |
duke@1 | 1016 | return "(" + argtypes + ")" + restype; |
duke@1 | 1017 | } |
duke@1 | 1018 | |
duke@1 | 1019 | public List<Type> getParameterTypes() { return argtypes; } |
duke@1 | 1020 | public Type getReturnType() { return restype; } |
jjg@1521 | 1021 | public Type getReceiverType() { return recvtype; } |
duke@1 | 1022 | public List<Type> getThrownTypes() { return thrown; } |
duke@1 | 1023 | |
duke@1 | 1024 | public boolean isErroneous() { |
duke@1 | 1025 | return |
duke@1 | 1026 | isErroneous(argtypes) || |
duke@1 | 1027 | restype != null && restype.isErroneous(); |
duke@1 | 1028 | } |
duke@1 | 1029 | |
duke@1 | 1030 | public Type map(Mapping f) { |
duke@1 | 1031 | List<Type> argtypes1 = map(argtypes, f); |
duke@1 | 1032 | Type restype1 = f.apply(restype); |
duke@1 | 1033 | List<Type> thrown1 = map(thrown, f); |
duke@1 | 1034 | if (argtypes1 == argtypes && |
duke@1 | 1035 | restype1 == restype && |
duke@1 | 1036 | thrown1 == thrown) return this; |
duke@1 | 1037 | else return new MethodType(argtypes1, restype1, thrown1, tsym); |
duke@1 | 1038 | } |
duke@1 | 1039 | |
duke@1 | 1040 | public boolean contains(Type elem) { |
duke@1 | 1041 | return elem == this || contains(argtypes, elem) || restype.contains(elem); |
duke@1 | 1042 | } |
duke@1 | 1043 | |
duke@1 | 1044 | public MethodType asMethodType() { return this; } |
duke@1 | 1045 | |
duke@1 | 1046 | public void complete() { |
duke@1 | 1047 | for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) |
duke@1 | 1048 | l.head.complete(); |
duke@1 | 1049 | restype.complete(); |
jjg@1521 | 1050 | recvtype.complete(); |
duke@1 | 1051 | for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
duke@1 | 1052 | l.head.complete(); |
duke@1 | 1053 | } |
duke@1 | 1054 | |
duke@1 | 1055 | public List<TypeVar> getTypeVariables() { |
duke@1 | 1056 | return List.nil(); |
duke@1 | 1057 | } |
duke@1 | 1058 | |
duke@1 | 1059 | public TypeSymbol asElement() { |
duke@1 | 1060 | return null; |
duke@1 | 1061 | } |
duke@1 | 1062 | |
duke@1 | 1063 | public TypeKind getKind() { |
duke@1 | 1064 | return TypeKind.EXECUTABLE; |
duke@1 | 1065 | } |
duke@1 | 1066 | |
duke@1 | 1067 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1068 | return v.visitExecutable(this, p); |
duke@1 | 1069 | } |
duke@1 | 1070 | } |
duke@1 | 1071 | |
duke@1 | 1072 | public static class PackageType extends Type implements NoType { |
duke@1 | 1073 | |
duke@1 | 1074 | PackageType(TypeSymbol tsym) { |
duke@1 | 1075 | super(PACKAGE, tsym); |
duke@1 | 1076 | } |
duke@1 | 1077 | |
duke@1 | 1078 | @Override |
duke@1 | 1079 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1080 | return v.visitPackageType(this, s); |
duke@1 | 1081 | } |
duke@1 | 1082 | |
duke@1 | 1083 | public String toString() { |
duke@1 | 1084 | return tsym.getQualifiedName().toString(); |
duke@1 | 1085 | } |
duke@1 | 1086 | |
duke@1 | 1087 | public TypeKind getKind() { |
duke@1 | 1088 | return TypeKind.PACKAGE; |
duke@1 | 1089 | } |
duke@1 | 1090 | |
duke@1 | 1091 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1092 | return v.visitNoType(this, p); |
duke@1 | 1093 | } |
duke@1 | 1094 | } |
duke@1 | 1095 | |
duke@1 | 1096 | public static class TypeVar extends Type implements TypeVariable { |
duke@1 | 1097 | |
jjg@789 | 1098 | /** The upper bound of this type variable; set from outside. |
duke@1 | 1099 | * Must be nonempty once it is set. |
duke@1 | 1100 | * For a bound, `bound' is the bound type itself. |
duke@1 | 1101 | * Multiple bounds are expressed as a single class type which has the |
duke@1 | 1102 | * individual bounds as superclass, respectively interfaces. |
duke@1 | 1103 | * The class type then has as `tsym' a compiler generated class `c', |
duke@1 | 1104 | * which has a flag COMPOUND and whose owner is the type variable |
duke@1 | 1105 | * itself. Furthermore, the erasure_field of the class |
duke@1 | 1106 | * points to the first class or interface bound. |
duke@1 | 1107 | */ |
duke@1 | 1108 | public Type bound = null; |
jjg@789 | 1109 | |
jjg@789 | 1110 | /** The lower bound of this type variable. |
jjg@789 | 1111 | * TypeVars don't normally have a lower bound, so it is normally set |
jjg@789 | 1112 | * to syms.botType. |
jjg@789 | 1113 | * Subtypes, such as CapturedType, may provide a different value. |
jjg@789 | 1114 | */ |
duke@1 | 1115 | public Type lower; |
duke@1 | 1116 | |
duke@1 | 1117 | public TypeVar(Name name, Symbol owner, Type lower) { |
duke@1 | 1118 | super(TYPEVAR, null); |
duke@1 | 1119 | tsym = new TypeSymbol(0, name, this, owner); |
duke@1 | 1120 | this.lower = lower; |
duke@1 | 1121 | } |
duke@1 | 1122 | |
duke@1 | 1123 | public TypeVar(TypeSymbol tsym, Type bound, Type lower) { |
duke@1 | 1124 | super(TYPEVAR, tsym); |
duke@1 | 1125 | this.bound = bound; |
duke@1 | 1126 | this.lower = lower; |
duke@1 | 1127 | } |
duke@1 | 1128 | |
duke@1 | 1129 | @Override |
duke@1 | 1130 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1131 | return v.visitTypeVar(this, s); |
duke@1 | 1132 | } |
duke@1 | 1133 | |
jjg@789 | 1134 | @Override |
jjg@1521 | 1135 | public Type getUpperBound() { |
jjg@1521 | 1136 | if ((bound == null || bound.tag == NONE) && this != tsym.type) |
jjg@1521 | 1137 | bound = tsym.type.getUpperBound(); |
jjg@1521 | 1138 | return bound; |
jjg@1521 | 1139 | } |
duke@1 | 1140 | |
duke@1 | 1141 | int rank_field = -1; |
duke@1 | 1142 | |
jjg@789 | 1143 | @Override |
duke@1 | 1144 | public Type getLowerBound() { |
duke@1 | 1145 | return lower; |
duke@1 | 1146 | } |
duke@1 | 1147 | |
duke@1 | 1148 | public TypeKind getKind() { |
duke@1 | 1149 | return TypeKind.TYPEVAR; |
duke@1 | 1150 | } |
duke@1 | 1151 | |
mcimadamore@79 | 1152 | public boolean isCaptured() { |
mcimadamore@79 | 1153 | return false; |
mcimadamore@79 | 1154 | } |
mcimadamore@79 | 1155 | |
duke@1 | 1156 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1157 | return v.visitTypeVariable(this, p); |
duke@1 | 1158 | } |
duke@1 | 1159 | } |
duke@1 | 1160 | |
duke@1 | 1161 | /** A captured type variable comes from wildcards which can have |
duke@1 | 1162 | * both upper and lower bound. CapturedType extends TypeVar with |
duke@1 | 1163 | * a lower bound. |
duke@1 | 1164 | */ |
duke@1 | 1165 | public static class CapturedType extends TypeVar { |
duke@1 | 1166 | |
duke@1 | 1167 | public WildcardType wildcard; |
duke@1 | 1168 | |
duke@1 | 1169 | public CapturedType(Name name, |
duke@1 | 1170 | Symbol owner, |
duke@1 | 1171 | Type upper, |
duke@1 | 1172 | Type lower, |
duke@1 | 1173 | WildcardType wildcard) { |
duke@1 | 1174 | super(name, owner, lower); |
jjg@816 | 1175 | this.lower = Assert.checkNonNull(lower); |
duke@1 | 1176 | this.bound = upper; |
duke@1 | 1177 | this.wildcard = wildcard; |
duke@1 | 1178 | } |
duke@1 | 1179 | |
duke@1 | 1180 | @Override |
duke@1 | 1181 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1182 | return v.visitCapturedType(this, s); |
duke@1 | 1183 | } |
duke@1 | 1184 | |
duke@1 | 1185 | @Override |
mcimadamore@79 | 1186 | public boolean isCaptured() { |
mcimadamore@79 | 1187 | return true; |
mcimadamore@79 | 1188 | } |
mcimadamore@79 | 1189 | |
mcimadamore@79 | 1190 | @Override |
duke@1 | 1191 | public String toString() { |
duke@1 | 1192 | return "capture#" |
mcimadamore@288 | 1193 | + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME |
duke@1 | 1194 | + " of " |
duke@1 | 1195 | + wildcard; |
duke@1 | 1196 | } |
duke@1 | 1197 | } |
duke@1 | 1198 | |
duke@1 | 1199 | public static abstract class DelegatedType extends Type { |
duke@1 | 1200 | public Type qtype; |
jjg@1374 | 1201 | public DelegatedType(TypeTag tag, Type qtype) { |
duke@1 | 1202 | super(tag, qtype.tsym); |
duke@1 | 1203 | this.qtype = qtype; |
duke@1 | 1204 | } |
duke@1 | 1205 | public String toString() { return qtype.toString(); } |
duke@1 | 1206 | public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } |
duke@1 | 1207 | public Type getEnclosingType() { return qtype.getEnclosingType(); } |
duke@1 | 1208 | public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } |
duke@1 | 1209 | public Type getReturnType() { return qtype.getReturnType(); } |
jjg@1521 | 1210 | public Type getReceiverType() { return qtype.getReceiverType(); } |
duke@1 | 1211 | public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } |
duke@1 | 1212 | public List<Type> allparams() { return qtype.allparams(); } |
duke@1 | 1213 | public Type getUpperBound() { return qtype.getUpperBound(); } |
duke@1 | 1214 | public boolean isErroneous() { return qtype.isErroneous(); } |
duke@1 | 1215 | } |
duke@1 | 1216 | |
mcimadamore@1268 | 1217 | /** |
mcimadamore@1268 | 1218 | * The type of a generic method type. It consists of a method type and |
mcimadamore@1268 | 1219 | * a list of method type-parameters that are used within the method |
mcimadamore@1268 | 1220 | * type. |
mcimadamore@1268 | 1221 | */ |
dlsmith@880 | 1222 | public static class ForAll extends DelegatedType implements ExecutableType { |
duke@1 | 1223 | public List<Type> tvars; |
duke@1 | 1224 | |
duke@1 | 1225 | public ForAll(List<Type> tvars, Type qtype) { |
mcimadamore@1268 | 1226 | super(FORALL, (MethodType)qtype); |
duke@1 | 1227 | this.tvars = tvars; |
duke@1 | 1228 | } |
duke@1 | 1229 | |
duke@1 | 1230 | @Override |
duke@1 | 1231 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1232 | return v.visitForAll(this, s); |
duke@1 | 1233 | } |
duke@1 | 1234 | |
duke@1 | 1235 | public String toString() { |
duke@1 | 1236 | return "<" + tvars + ">" + qtype; |
duke@1 | 1237 | } |
duke@1 | 1238 | |
duke@1 | 1239 | public List<Type> getTypeArguments() { return tvars; } |
duke@1 | 1240 | |
duke@1 | 1241 | public boolean isErroneous() { |
duke@1 | 1242 | return qtype.isErroneous(); |
duke@1 | 1243 | } |
duke@1 | 1244 | |
duke@1 | 1245 | public Type map(Mapping f) { |
duke@1 | 1246 | return f.apply(qtype); |
duke@1 | 1247 | } |
duke@1 | 1248 | |
duke@1 | 1249 | public boolean contains(Type elem) { |
duke@1 | 1250 | return qtype.contains(elem); |
duke@1 | 1251 | } |
duke@1 | 1252 | |
duke@1 | 1253 | public MethodType asMethodType() { |
mcimadamore@1268 | 1254 | return (MethodType)qtype; |
duke@1 | 1255 | } |
duke@1 | 1256 | |
duke@1 | 1257 | public void complete() { |
duke@1 | 1258 | for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) { |
duke@1 | 1259 | ((TypeVar)l.head).bound.complete(); |
duke@1 | 1260 | } |
duke@1 | 1261 | qtype.complete(); |
duke@1 | 1262 | } |
duke@1 | 1263 | |
duke@1 | 1264 | public List<TypeVar> getTypeVariables() { |
duke@1 | 1265 | return List.convert(TypeVar.class, getTypeArguments()); |
duke@1 | 1266 | } |
duke@1 | 1267 | |
duke@1 | 1268 | public TypeKind getKind() { |
duke@1 | 1269 | return TypeKind.EXECUTABLE; |
duke@1 | 1270 | } |
duke@1 | 1271 | |
duke@1 | 1272 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1273 | return v.visitExecutable(this, p); |
duke@1 | 1274 | } |
duke@1 | 1275 | } |
duke@1 | 1276 | |
mcimadamore@1338 | 1277 | /** A class for inference variables, for use during method/diamond type |
mcimadamore@1338 | 1278 | * inference. An inference variable has upper/lower bounds and a set |
mcimadamore@1338 | 1279 | * of equality constraints. Such bounds are set during subtyping, type-containment, |
mcimadamore@1338 | 1280 | * type-equality checks, when the types being tested contain inference variables. |
mcimadamore@1338 | 1281 | * A change listener can be attached to an inference variable, to receive notifications |
mcimadamore@1338 | 1282 | * whenever the bounds of an inference variable change. |
duke@1 | 1283 | */ |
duke@1 | 1284 | public static class UndetVar extends DelegatedType { |
mcimadamore@1338 | 1285 | |
mcimadamore@1338 | 1286 | /** Inference variable change listener. The listener method is called |
mcimadamore@1338 | 1287 | * whenever a change to the inference variable's bounds occurs |
mcimadamore@1338 | 1288 | */ |
mcimadamore@1338 | 1289 | public interface UndetVarListener { |
mcimadamore@1338 | 1290 | /** called when some inference variable bounds (of given kinds ibs) change */ |
mcimadamore@1338 | 1291 | void varChanged(UndetVar uv, Set<InferenceBound> ibs); |
mcimadamore@1338 | 1292 | } |
mcimadamore@1338 | 1293 | |
mcimadamore@1338 | 1294 | /** |
mcimadamore@1338 | 1295 | * Inference variable bound kinds |
mcimadamore@1338 | 1296 | */ |
mcimadamore@1338 | 1297 | public enum InferenceBound { |
mcimadamore@1338 | 1298 | /** upper bounds */ |
mcimadamore@1338 | 1299 | UPPER, |
mcimadamore@1338 | 1300 | /** lower bounds */ |
mcimadamore@1338 | 1301 | LOWER, |
mcimadamore@1338 | 1302 | /** equality constraints */ |
mcimadamore@1338 | 1303 | EQ; |
mcimadamore@1338 | 1304 | } |
mcimadamore@1338 | 1305 | |
mcimadamore@1338 | 1306 | /** inference variable bounds */ |
mcimadamore@1338 | 1307 | private Map<InferenceBound, List<Type>> bounds; |
mcimadamore@1338 | 1308 | |
mcimadamore@1338 | 1309 | /** inference variable's inferred type (set from Infer.java) */ |
duke@1 | 1310 | public Type inst = null; |
duke@1 | 1311 | |
mcimadamore@1550 | 1312 | /** number of declared (upper) bounds */ |
mcimadamore@1550 | 1313 | public int declaredCount; |
mcimadamore@1550 | 1314 | |
mcimadamore@1338 | 1315 | /** inference variable's change listener */ |
mcimadamore@1338 | 1316 | public UndetVarListener listener = null; |
mcimadamore@1338 | 1317 | |
duke@1 | 1318 | @Override |
duke@1 | 1319 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1320 | return v.visitUndetVar(this, s); |
duke@1 | 1321 | } |
duke@1 | 1322 | |
mcimadamore@1338 | 1323 | public UndetVar(TypeVar origin, Types types) { |
duke@1 | 1324 | super(UNDETVAR, origin); |
mcimadamore@1338 | 1325 | bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class); |
mcimadamore@1550 | 1326 | List<Type> declaredBounds = types.getBounds(origin); |
mcimadamore@1550 | 1327 | declaredCount = declaredBounds.length(); |
mcimadamore@1550 | 1328 | bounds.put(InferenceBound.UPPER, declaredBounds); |
mcimadamore@1338 | 1329 | bounds.put(InferenceBound.LOWER, List.<Type>nil()); |
mcimadamore@1338 | 1330 | bounds.put(InferenceBound.EQ, List.<Type>nil()); |
duke@1 | 1331 | } |
duke@1 | 1332 | |
duke@1 | 1333 | public String toString() { |
duke@1 | 1334 | if (inst != null) return inst.toString(); |
duke@1 | 1335 | else return qtype + "?"; |
duke@1 | 1336 | } |
duke@1 | 1337 | |
duke@1 | 1338 | public Type baseType() { |
duke@1 | 1339 | if (inst != null) return inst.baseType(); |
duke@1 | 1340 | else return this; |
duke@1 | 1341 | } |
mcimadamore@1338 | 1342 | |
mcimadamore@1338 | 1343 | /** get all bounds of a given kind */ |
mcimadamore@1550 | 1344 | public List<Type> getBounds(InferenceBound... ibs) { |
mcimadamore@1550 | 1345 | ListBuffer<Type> buf = ListBuffer.lb(); |
mcimadamore@1550 | 1346 | for (InferenceBound ib : ibs) { |
mcimadamore@1550 | 1347 | buf.appendList(bounds.get(ib)); |
mcimadamore@1550 | 1348 | } |
mcimadamore@1550 | 1349 | return buf.toList(); |
mcimadamore@1550 | 1350 | } |
mcimadamore@1550 | 1351 | |
mcimadamore@1550 | 1352 | /** get the list of declared (upper) bounds */ |
mcimadamore@1550 | 1353 | public List<Type> getDeclaredBounds() { |
mcimadamore@1550 | 1354 | ListBuffer<Type> buf = ListBuffer.lb(); |
mcimadamore@1550 | 1355 | int count = 0; |
mcimadamore@1550 | 1356 | for (Type b : getBounds(InferenceBound.UPPER)) { |
mcimadamore@1550 | 1357 | if (count++ == declaredCount) break; |
mcimadamore@1550 | 1358 | buf.append(b); |
mcimadamore@1550 | 1359 | } |
mcimadamore@1550 | 1360 | return buf.toList(); |
mcimadamore@1338 | 1361 | } |
mcimadamore@1338 | 1362 | |
mcimadamore@1338 | 1363 | /** add a bound of a given kind - this might trigger listener notification */ |
mcimadamore@1338 | 1364 | public void addBound(InferenceBound ib, Type bound, Types types) { |
mcimadamore@1550 | 1365 | Type bound2 = toTypeVarMap.apply(bound); |
mcimadamore@1338 | 1366 | List<Type> prevBounds = bounds.get(ib); |
mcimadamore@1338 | 1367 | for (Type b : prevBounds) { |
mcimadamore@1550 | 1368 | //check for redundancy - use strict version of isSameType on tvars |
mcimadamore@1550 | 1369 | //(as the standard version will lead to false positives w.r.t. clones ivars) |
mcimadamore@1550 | 1370 | if (types.isSameType(b, bound2, true)) return; |
mcimadamore@1338 | 1371 | } |
mcimadamore@1550 | 1372 | bounds.put(ib, prevBounds.prepend(bound2)); |
mcimadamore@1338 | 1373 | notifyChange(EnumSet.of(ib)); |
mcimadamore@1338 | 1374 | } |
mcimadamore@1550 | 1375 | //where |
mcimadamore@1550 | 1376 | Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") { |
mcimadamore@1550 | 1377 | @Override |
mcimadamore@1550 | 1378 | public Type apply(Type t) { |
mcimadamore@1550 | 1379 | if (t.hasTag(UNDETVAR)) { |
mcimadamore@1550 | 1380 | UndetVar uv = (UndetVar)t; |
mcimadamore@1550 | 1381 | return uv.qtype; |
mcimadamore@1550 | 1382 | } else { |
mcimadamore@1550 | 1383 | return t.map(this); |
mcimadamore@1550 | 1384 | } |
mcimadamore@1550 | 1385 | } |
mcimadamore@1550 | 1386 | }; |
mcimadamore@1338 | 1387 | |
mcimadamore@1338 | 1388 | /** replace types in all bounds - this might trigger listener notification */ |
mcimadamore@1338 | 1389 | public void substBounds(List<Type> from, List<Type> to, Types types) { |
mcimadamore@1550 | 1390 | List<Type> instVars = from.diff(to); |
mcimadamore@1550 | 1391 | //if set of instantiated ivars is empty, there's nothing to do! |
mcimadamore@1550 | 1392 | if (instVars.isEmpty()) return; |
mcimadamore@1550 | 1393 | final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class); |
mcimadamore@1550 | 1394 | UndetVarListener prevListener = listener; |
mcimadamore@1550 | 1395 | try { |
mcimadamore@1550 | 1396 | //setup new listener for keeping track of changed bounds |
mcimadamore@1550 | 1397 | listener = new UndetVarListener() { |
mcimadamore@1550 | 1398 | public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { |
mcimadamore@1550 | 1399 | boundsChanged.addAll(ibs); |
mcimadamore@1550 | 1400 | } |
mcimadamore@1550 | 1401 | }; |
mcimadamore@1550 | 1402 | for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) { |
mcimadamore@1550 | 1403 | InferenceBound ib = _entry.getKey(); |
mcimadamore@1550 | 1404 | List<Type> prevBounds = _entry.getValue(); |
mcimadamore@1550 | 1405 | ListBuffer<Type> newBounds = ListBuffer.lb(); |
mcimadamore@1550 | 1406 | ListBuffer<Type> deps = ListBuffer.lb(); |
mcimadamore@1550 | 1407 | //step 1 - re-add bounds that are not dependent on ivars |
mcimadamore@1550 | 1408 | for (Type t : prevBounds) { |
mcimadamore@1550 | 1409 | if (!t.containsAny(instVars)) { |
mcimadamore@1550 | 1410 | newBounds.append(t); |
mcimadamore@1550 | 1411 | } else { |
mcimadamore@1550 | 1412 | deps.append(t); |
mcimadamore@1550 | 1413 | } |
mcimadamore@1550 | 1414 | } |
mcimadamore@1550 | 1415 | //step 2 - replace bounds |
mcimadamore@1550 | 1416 | bounds.put(ib, newBounds.toList()); |
mcimadamore@1550 | 1417 | //step 3 - for each dependency, add new replaced bound |
mcimadamore@1550 | 1418 | for (Type dep : deps) { |
mcimadamore@1550 | 1419 | addBound(ib, types.subst(dep, from, to), types); |
mcimadamore@1550 | 1420 | } |
mcimadamore@1338 | 1421 | } |
mcimadamore@1550 | 1422 | } finally { |
mcimadamore@1550 | 1423 | listener = prevListener; |
mcimadamore@1550 | 1424 | if (!boundsChanged.isEmpty()) { |
mcimadamore@1550 | 1425 | notifyChange(boundsChanged); |
mcimadamore@1550 | 1426 | } |
mcimadamore@1338 | 1427 | } |
mcimadamore@1338 | 1428 | } |
mcimadamore@1338 | 1429 | |
mcimadamore@1338 | 1430 | private void notifyChange(EnumSet<InferenceBound> ibs) { |
mcimadamore@1338 | 1431 | if (listener != null) { |
mcimadamore@1338 | 1432 | listener.varChanged(this, ibs); |
mcimadamore@1338 | 1433 | } |
mcimadamore@1338 | 1434 | } |
duke@1 | 1435 | } |
duke@1 | 1436 | |
duke@1 | 1437 | /** Represents VOID or NONE. |
duke@1 | 1438 | */ |
duke@1 | 1439 | static class JCNoType extends Type implements NoType { |
jjg@1374 | 1440 | public JCNoType(TypeTag tag) { |
duke@1 | 1441 | super(tag, null); |
duke@1 | 1442 | } |
duke@1 | 1443 | |
duke@1 | 1444 | @Override |
duke@1 | 1445 | public TypeKind getKind() { |
duke@1 | 1446 | switch (tag) { |
duke@1 | 1447 | case VOID: return TypeKind.VOID; |
duke@1 | 1448 | case NONE: return TypeKind.NONE; |
duke@1 | 1449 | default: |
duke@1 | 1450 | throw new AssertionError("Unexpected tag: " + tag); |
duke@1 | 1451 | } |
duke@1 | 1452 | } |
duke@1 | 1453 | |
duke@1 | 1454 | @Override |
duke@1 | 1455 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1456 | return v.visitNoType(this, p); |
duke@1 | 1457 | } |
duke@1 | 1458 | } |
duke@1 | 1459 | |
duke@1 | 1460 | static class BottomType extends Type implements NullType { |
duke@1 | 1461 | public BottomType() { |
jjg@1374 | 1462 | super(BOT, null); |
duke@1 | 1463 | } |
duke@1 | 1464 | |
duke@1 | 1465 | @Override |
duke@1 | 1466 | public TypeKind getKind() { |
duke@1 | 1467 | return TypeKind.NULL; |
duke@1 | 1468 | } |
duke@1 | 1469 | |
duke@1 | 1470 | @Override |
duke@1 | 1471 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1472 | return v.visitNull(this, p); |
duke@1 | 1473 | } |
duke@1 | 1474 | |
duke@1 | 1475 | @Override |
duke@1 | 1476 | public Type constType(Object value) { |
duke@1 | 1477 | return this; |
duke@1 | 1478 | } |
duke@1 | 1479 | |
duke@1 | 1480 | @Override |
duke@1 | 1481 | public String stringValue() { |
duke@1 | 1482 | return "null"; |
duke@1 | 1483 | } |
duke@1 | 1484 | } |
duke@1 | 1485 | |
duke@1 | 1486 | public static class ErrorType extends ClassType |
duke@1 | 1487 | implements javax.lang.model.type.ErrorType { |
duke@1 | 1488 | |
jjg@110 | 1489 | private Type originalType = null; |
jjg@110 | 1490 | |
jjg@110 | 1491 | public ErrorType(Type originalType, TypeSymbol tsym) { |
duke@1 | 1492 | super(noType, List.<Type>nil(), null); |
duke@1 | 1493 | tag = ERROR; |
jjg@110 | 1494 | this.tsym = tsym; |
jjg@110 | 1495 | this.originalType = (originalType == null ? noType : originalType); |
duke@1 | 1496 | } |
duke@1 | 1497 | |
jjg@110 | 1498 | public ErrorType(ClassSymbol c, Type originalType) { |
jjg@110 | 1499 | this(originalType, c); |
duke@1 | 1500 | c.type = this; |
duke@1 | 1501 | c.kind = ERR; |
duke@1 | 1502 | c.members_field = new Scope.ErrorScope(c); |
duke@1 | 1503 | } |
duke@1 | 1504 | |
jjg@110 | 1505 | public ErrorType(Name name, TypeSymbol container, Type originalType) { |
jjg@110 | 1506 | this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType); |
duke@1 | 1507 | } |
duke@1 | 1508 | |
duke@1 | 1509 | @Override |
duke@1 | 1510 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
duke@1 | 1511 | return v.visitErrorType(this, s); |
duke@1 | 1512 | } |
duke@1 | 1513 | |
duke@1 | 1514 | public Type constType(Object constValue) { return this; } |
jjg@1521 | 1515 | public Type getEnclosingType() { return this; } |
duke@1 | 1516 | public Type getReturnType() { return this; } |
duke@1 | 1517 | public Type asSub(Symbol sym) { return this; } |
duke@1 | 1518 | public Type map(Mapping f) { return this; } |
duke@1 | 1519 | |
duke@1 | 1520 | public boolean isGenType(Type t) { return true; } |
duke@1 | 1521 | public boolean isErroneous() { return true; } |
duke@1 | 1522 | public boolean isCompound() { return false; } |
duke@1 | 1523 | public boolean isInterface() { return false; } |
duke@1 | 1524 | |
duke@1 | 1525 | public List<Type> allparams() { return List.nil(); } |
duke@1 | 1526 | public List<Type> getTypeArguments() { return List.nil(); } |
duke@1 | 1527 | |
duke@1 | 1528 | public TypeKind getKind() { |
duke@1 | 1529 | return TypeKind.ERROR; |
duke@1 | 1530 | } |
duke@1 | 1531 | |
jjg@110 | 1532 | public Type getOriginalType() { |
jjg@110 | 1533 | return originalType; |
jjg@110 | 1534 | } |
jjg@110 | 1535 | |
duke@1 | 1536 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
duke@1 | 1537 | return v.visitError(this, p); |
duke@1 | 1538 | } |
duke@1 | 1539 | } |
duke@1 | 1540 | |
jjg@1521 | 1541 | public static class AnnotatedType extends Type |
jjg@1521 | 1542 | implements javax.lang.model.type.AnnotatedType { |
jjg@1521 | 1543 | /** The type annotations on this type. |
jjg@1521 | 1544 | */ |
jjg@1521 | 1545 | public List<Attribute.TypeCompound> typeAnnotations; |
jjg@1521 | 1546 | |
jjg@1521 | 1547 | /** The underlying type that is annotated. |
jjg@1521 | 1548 | */ |
jjg@1521 | 1549 | public Type underlyingType; |
jjg@1521 | 1550 | |
jjg@1521 | 1551 | public AnnotatedType(Type underlyingType) { |
jjg@1521 | 1552 | super(underlyingType.tag, underlyingType.tsym); |
jjg@1521 | 1553 | this.typeAnnotations = List.nil(); |
jjg@1521 | 1554 | this.underlyingType = underlyingType; |
jjg@1521 | 1555 | Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED, |
jjg@1521 | 1556 | "Can't annotate already annotated type: " + underlyingType); |
jjg@1521 | 1557 | } |
jjg@1521 | 1558 | |
jjg@1521 | 1559 | public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations, |
jjg@1521 | 1560 | Type underlyingType) { |
jjg@1521 | 1561 | super(underlyingType.tag, underlyingType.tsym); |
jjg@1521 | 1562 | this.typeAnnotations = typeAnnotations; |
jjg@1521 | 1563 | this.underlyingType = underlyingType; |
jjg@1521 | 1564 | Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED, |
jjg@1521 | 1565 | "Can't annotate already annotated type: " + underlyingType + |
jjg@1521 | 1566 | "; adding: " + typeAnnotations); |
jjg@1521 | 1567 | } |
jjg@1521 | 1568 | |
jjg@1521 | 1569 | @Override |
jjg@1521 | 1570 | public TypeKind getKind() { |
jjg@1521 | 1571 | return TypeKind.ANNOTATED; |
jjg@1521 | 1572 | } |
jjg@1521 | 1573 | |
jjg@1521 | 1574 | @Override |
jjg@1521 | 1575 | public List<? extends AnnotationMirror> getAnnotations() { |
jjg@1521 | 1576 | return typeAnnotations; |
jjg@1521 | 1577 | } |
jjg@1521 | 1578 | |
jjg@1521 | 1579 | @Override |
jjg@1521 | 1580 | public TypeMirror getUnderlyingType() { |
jjg@1521 | 1581 | return underlyingType; |
jjg@1521 | 1582 | } |
jjg@1521 | 1583 | |
jjg@1521 | 1584 | @Override |
jjg@1521 | 1585 | public Type unannotatedType() { |
jjg@1521 | 1586 | return underlyingType; |
jjg@1521 | 1587 | } |
jjg@1521 | 1588 | |
jjg@1521 | 1589 | @Override |
jjg@1521 | 1590 | public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
jjg@1521 | 1591 | return v.visitAnnotatedType(this, s); |
jjg@1521 | 1592 | } |
jjg@1521 | 1593 | |
jjg@1521 | 1594 | @Override |
jjg@1521 | 1595 | public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
jjg@1521 | 1596 | return v.visitAnnotated(this, p); |
jjg@1521 | 1597 | } |
jjg@1521 | 1598 | |
jjg@1521 | 1599 | @Override |
jjg@1521 | 1600 | public Type map(Mapping f) { |
jjg@1521 | 1601 | underlyingType.map(f); |
jjg@1521 | 1602 | return this; |
jjg@1521 | 1603 | } |
jjg@1521 | 1604 | |
jjg@1521 | 1605 | @Override |
jjg@1521 | 1606 | public Type constType(Object constValue) { return underlyingType.constType(constValue); } |
jjg@1521 | 1607 | @Override |
jjg@1521 | 1608 | public Type getEnclosingType() { return underlyingType.getEnclosingType(); } |
jjg@1521 | 1609 | |
jjg@1521 | 1610 | @Override |
jjg@1521 | 1611 | public Type getReturnType() { return underlyingType.getReturnType(); } |
jjg@1521 | 1612 | @Override |
jjg@1521 | 1613 | public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); } |
jjg@1521 | 1614 | @Override |
jjg@1521 | 1615 | public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); } |
jjg@1521 | 1616 | @Override |
jjg@1521 | 1617 | public Type getReceiverType() { return underlyingType.getReceiverType(); } |
jjg@1521 | 1618 | @Override |
jjg@1521 | 1619 | public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); } |
jjg@1521 | 1620 | @Override |
jjg@1521 | 1621 | public Type getUpperBound() { return underlyingType.getUpperBound(); } |
jjg@1521 | 1622 | @Override |
jjg@1521 | 1623 | public Type getLowerBound() { return underlyingType.getLowerBound(); } |
jjg@1521 | 1624 | |
jjg@1521 | 1625 | @Override |
jjg@1521 | 1626 | public boolean isErroneous() { return underlyingType.isErroneous(); } |
jjg@1521 | 1627 | @Override |
jjg@1521 | 1628 | public boolean isCompound() { return underlyingType.isCompound(); } |
jjg@1521 | 1629 | @Override |
jjg@1521 | 1630 | public boolean isInterface() { return underlyingType.isInterface(); } |
jjg@1521 | 1631 | @Override |
jjg@1521 | 1632 | public List<Type> allparams() { return underlyingType.allparams(); } |
jjg@1521 | 1633 | @Override |
jjg@1521 | 1634 | public boolean isNumeric() { return underlyingType.isNumeric(); } |
jjg@1521 | 1635 | @Override |
jjg@1521 | 1636 | public boolean isReference() { return underlyingType.isReference(); } |
jjg@1521 | 1637 | @Override |
jjg@1521 | 1638 | public boolean isParameterized() { return underlyingType.isParameterized(); } |
jjg@1521 | 1639 | @Override |
jjg@1521 | 1640 | public boolean isRaw() { return underlyingType.isRaw(); } |
jjg@1521 | 1641 | @Override |
jjg@1521 | 1642 | public boolean isFinal() { return underlyingType.isFinal(); } |
jjg@1521 | 1643 | @Override |
jjg@1521 | 1644 | public boolean isSuperBound() { return underlyingType.isSuperBound(); } |
jjg@1521 | 1645 | @Override |
jjg@1521 | 1646 | public boolean isExtendsBound() { return underlyingType.isExtendsBound(); } |
jjg@1521 | 1647 | @Override |
jjg@1521 | 1648 | public boolean isUnbound() { return underlyingType.isUnbound(); } |
jjg@1521 | 1649 | |
jjg@1521 | 1650 | @Override |
jjg@1521 | 1651 | public String toString() { |
jjg@1521 | 1652 | // TODO more logic for arrays, etc. |
jjg@1521 | 1653 | if (typeAnnotations != null && |
jjg@1521 | 1654 | !typeAnnotations.isEmpty()) { |
jjg@1521 | 1655 | return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")"; |
jjg@1521 | 1656 | } else { |
jjg@1521 | 1657 | return "({} :: " + underlyingType.toString() +")"; |
jjg@1521 | 1658 | } |
jjg@1521 | 1659 | } |
jjg@1521 | 1660 | |
jjg@1521 | 1661 | @Override |
jjg@1521 | 1662 | public boolean contains(Type t) { return underlyingType.contains(t); } |
jjg@1521 | 1663 | |
jjg@1521 | 1664 | // TODO: attach annotations? |
jjg@1521 | 1665 | @Override |
jjg@1521 | 1666 | public Type withTypeVar(Type t) { return underlyingType.withTypeVar(t); } |
jjg@1521 | 1667 | |
jjg@1521 | 1668 | // TODO: attach annotations? |
jjg@1521 | 1669 | @Override |
jjg@1521 | 1670 | public TypeSymbol asElement() { return underlyingType.asElement(); } |
jjg@1521 | 1671 | |
jjg@1521 | 1672 | // TODO: attach annotations? |
jjg@1521 | 1673 | @Override |
jjg@1521 | 1674 | public MethodType asMethodType() { return underlyingType.asMethodType(); } |
jjg@1521 | 1675 | |
jjg@1521 | 1676 | @Override |
jjg@1521 | 1677 | public void complete() { underlyingType.complete(); } |
jjg@1521 | 1678 | |
jjg@1521 | 1679 | @Override |
jjg@1521 | 1680 | public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); } |
jjg@1521 | 1681 | |
jjg@1521 | 1682 | // The result is an ArrayType, but only in the model sense, not the Type sense. |
jjg@1521 | 1683 | public AnnotatedType makeVarargs() { |
jjg@1521 | 1684 | AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs()); |
jjg@1521 | 1685 | atype.typeAnnotations = this.typeAnnotations; |
jjg@1521 | 1686 | return atype; |
jjg@1521 | 1687 | } |
jjg@1521 | 1688 | |
jjg@1521 | 1689 | @Override |
jjg@1521 | 1690 | public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); } |
jjg@1521 | 1691 | @Override |
jjg@1521 | 1692 | public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); } |
jjg@1521 | 1693 | } |
jjg@1521 | 1694 | |
duke@1 | 1695 | /** |
duke@1 | 1696 | * A visitor for types. A visitor is used to implement operations |
duke@1 | 1697 | * (or relations) on types. Most common operations on types are |
duke@1 | 1698 | * binary relations and this interface is designed for binary |
jjg@1521 | 1699 | * relations, that is, operations of the form |
duke@1 | 1700 | * Type × S → R. |
duke@1 | 1701 | * <!-- In plain text: Type x S -> R --> |
duke@1 | 1702 | * |
duke@1 | 1703 | * @param <R> the return type of the operation implemented by this |
duke@1 | 1704 | * visitor; use Void if no return type is needed. |
duke@1 | 1705 | * @param <S> the type of the second argument (the first being the |
duke@1 | 1706 | * type itself) of the operation implemented by this visitor; use |
duke@1 | 1707 | * Void if a second argument is not needed. |
duke@1 | 1708 | */ |
duke@1 | 1709 | public interface Visitor<R,S> { |
duke@1 | 1710 | R visitClassType(ClassType t, S s); |
duke@1 | 1711 | R visitWildcardType(WildcardType t, S s); |
duke@1 | 1712 | R visitArrayType(ArrayType t, S s); |
duke@1 | 1713 | R visitMethodType(MethodType t, S s); |
duke@1 | 1714 | R visitPackageType(PackageType t, S s); |
duke@1 | 1715 | R visitTypeVar(TypeVar t, S s); |
duke@1 | 1716 | R visitCapturedType(CapturedType t, S s); |
duke@1 | 1717 | R visitForAll(ForAll t, S s); |
duke@1 | 1718 | R visitUndetVar(UndetVar t, S s); |
duke@1 | 1719 | R visitErrorType(ErrorType t, S s); |
jjg@1521 | 1720 | R visitAnnotatedType(AnnotatedType t, S s); |
duke@1 | 1721 | R visitType(Type t, S s); |
duke@1 | 1722 | } |
duke@1 | 1723 | } |