Mon, 16 Aug 2010 14:58:10 +0100
6369605: Unconstrained type variables fails to include bounds
Summary: unconstrained type-variables with recursive bounds are not inferred properly
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.code;
28 import com.sun.tools.javac.util.*;
29 import com.sun.tools.javac.code.Symbol.*;
31 import javax.lang.model.type.*;
33 import static com.sun.tools.javac.code.Flags.*;
34 import static com.sun.tools.javac.code.Kinds.*;
35 import static com.sun.tools.javac.code.BoundKind.*;
36 import static com.sun.tools.javac.code.TypeTags.*;
38 /** This class represents Java types. The class itself defines the behavior of
39 * the following types:
40 * <pre>
41 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
42 * type `void' (tag: VOID),
43 * the bottom type (tag: BOT),
44 * the missing type (tag: NONE).
45 * </pre>
46 * <p>The behavior of the following types is defined in subclasses, which are
47 * all static inner classes of this class:
48 * <pre>
49 * class types (tag: CLASS, class: ClassType),
50 * array types (tag: ARRAY, class: ArrayType),
51 * method types (tag: METHOD, class: MethodType),
52 * package types (tag: PACKAGE, class: PackageType),
53 * type variables (tag: TYPEVAR, class: TypeVar),
54 * type arguments (tag: WILDCARD, class: WildcardType),
55 * polymorphic types (tag: FORALL, class: ForAll),
56 * the error type (tag: ERROR, class: ErrorType).
57 * </pre>
58 *
59 * <p><b>This is NOT part of any supported API.
60 * If you write code that depends on this, you do so at your own risk.
61 * This code and its internal interfaces are subject to change or
62 * deletion without notice.</b>
63 *
64 * @see TypeTags
65 */
66 public class Type implements PrimitiveType {
68 /** Constant type: no type at all. */
69 public static final JCNoType noType = new JCNoType(NONE);
71 /** If this switch is turned on, the names of type variables
72 * and anonymous classes are printed with hashcodes appended.
73 */
74 public static boolean moreInfo = false;
76 /** The tag of this type.
77 *
78 * @see TypeTags
79 */
80 public int tag;
82 /** The defining class / interface / package / type variable
83 */
84 public TypeSymbol tsym;
86 /**
87 * The constant value of this type, null if this type does not
88 * have a constant value attribute. Only primitive types and
89 * strings (ClassType) can have a constant value attribute.
90 * @return the constant value attribute of this type
91 */
92 public Object constValue() {
93 return null;
94 }
96 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
98 /** Define a type given its tag and type symbol
99 */
100 public Type(int tag, TypeSymbol tsym) {
101 this.tag = tag;
102 this.tsym = tsym;
103 }
105 /** An abstract class for mappings from types to types
106 */
107 public static abstract class Mapping {
108 private String name;
109 public Mapping(String name) {
110 this.name = name;
111 }
112 public abstract Type apply(Type t);
113 public String toString() {
114 return name;
115 }
116 }
118 /** map a type function over all immediate descendants of this type
119 */
120 public Type map(Mapping f) {
121 return this;
122 }
124 /** map a type function over a list of types
125 */
126 public static List<Type> map(List<Type> ts, Mapping f) {
127 if (ts.nonEmpty()) {
128 List<Type> tail1 = map(ts.tail, f);
129 Type t = f.apply(ts.head);
130 if (tail1 != ts.tail || t != ts.head)
131 return tail1.prepend(t);
132 }
133 return ts;
134 }
136 /** Define a constant type, of the same kind as this type
137 * and with given constant value
138 */
139 public Type constType(Object constValue) {
140 final Object value = constValue;
141 assert tag <= BOOLEAN;
142 return new Type(tag, tsym) {
143 @Override
144 public Object constValue() {
145 return value;
146 }
147 @Override
148 public Type baseType() {
149 return tsym.type;
150 }
151 };
152 }
154 /**
155 * If this is a constant type, return its underlying type.
156 * Otherwise, return the type itself.
157 */
158 public Type baseType() {
159 return this;
160 }
162 /** Return the base types of a list of types.
163 */
164 public static List<Type> baseTypes(List<Type> ts) {
165 if (ts.nonEmpty()) {
166 Type t = ts.head.baseType();
167 List<Type> baseTypes = baseTypes(ts.tail);
168 if (t != ts.head || baseTypes != ts.tail)
169 return baseTypes.prepend(t);
170 }
171 return ts;
172 }
174 /** The Java source which this type represents.
175 */
176 public String toString() {
177 String s = (tsym == null || tsym.name == null)
178 ? "<none>"
179 : tsym.name.toString();
180 if (moreInfo && tag == TYPEVAR) s = s + hashCode();
181 return s;
182 }
184 /**
185 * The Java source which this type list represents. A List is
186 * represented as a comma-spearated listing of the elements in
187 * that list.
188 */
189 public static String toString(List<Type> ts) {
190 if (ts.isEmpty()) {
191 return "";
192 } else {
193 StringBuffer buf = new StringBuffer();
194 buf.append(ts.head.toString());
195 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
196 buf.append(",").append(l.head.toString());
197 return buf.toString();
198 }
199 }
201 /**
202 * The constant value of this type, converted to String
203 */
204 public String stringValue() {
205 assert constValue() != null;
206 if (tag == BOOLEAN)
207 return ((Integer) constValue()).intValue() == 0 ? "false" : "true";
208 else if (tag == CHAR)
209 return String.valueOf((char) ((Integer) constValue()).intValue());
210 else
211 return constValue().toString();
212 }
214 /**
215 * This method is analogous to isSameType, but weaker, since we
216 * never complete classes. Where isSameType would complete a
217 * class, equals assumes that the two types are different.
218 */
219 public boolean equals(Object t) {
220 return super.equals(t);
221 }
223 public int hashCode() {
224 return super.hashCode();
225 }
227 /** Is this a constant type whose value is false?
228 */
229 public boolean isFalse() {
230 return
231 tag == BOOLEAN &&
232 constValue() != null &&
233 ((Integer)constValue()).intValue() == 0;
234 }
236 /** Is this a constant type whose value is true?
237 */
238 public boolean isTrue() {
239 return
240 tag == BOOLEAN &&
241 constValue() != null &&
242 ((Integer)constValue()).intValue() != 0;
243 }
245 public String argtypes(boolean varargs) {
246 List<Type> args = getParameterTypes();
247 if (!varargs) return args.toString();
248 StringBuffer buf = new StringBuffer();
249 while (args.tail.nonEmpty()) {
250 buf.append(args.head);
251 args = args.tail;
252 buf.append(',');
253 }
254 if (args.head.tag == ARRAY) {
255 buf.append(((ArrayType)args.head).elemtype);
256 buf.append("...");
257 } else {
258 buf.append(args.head);
259 }
260 return buf.toString();
261 }
263 /** Access methods.
264 */
265 public List<Type> getTypeArguments() { return List.nil(); }
266 public Type getEnclosingType() { return null; }
267 public List<Type> getParameterTypes() { return List.nil(); }
268 public Type getReturnType() { return null; }
269 public List<Type> getThrownTypes() { return List.nil(); }
270 public Type getUpperBound() { return null; }
271 public Type getLowerBound() { return null; }
273 public void setThrown(List<Type> ts) {
274 throw new AssertionError();
275 }
277 /** Navigation methods, these will work for classes, type variables,
278 * foralls, but will return null for arrays and methods.
279 */
281 /** Return all parameters of this type and all its outer types in order
282 * outer (first) to inner (last).
283 */
284 public List<Type> allparams() { return List.nil(); }
286 /** Does this type contain "error" elements?
287 */
288 public boolean isErroneous() {
289 return false;
290 }
292 public static boolean isErroneous(List<Type> ts) {
293 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
294 if (l.head.isErroneous()) return true;
295 return false;
296 }
298 /** Is this type parameterized?
299 * A class type is parameterized if it has some parameters.
300 * An array type is parameterized if its element type is parameterized.
301 * All other types are not parameterized.
302 */
303 public boolean isParameterized() {
304 return false;
305 }
307 /** Is this type a raw type?
308 * A class type is a raw type if it misses some of its parameters.
309 * An array type is a raw type if its element type is raw.
310 * All other types are not raw.
311 * Type validation will ensure that the only raw types
312 * in a program are types that miss all their type variables.
313 */
314 public boolean isRaw() {
315 return false;
316 }
318 public boolean isCompound() {
319 return tsym.completer == null
320 // Compound types can't have a completer. Calling
321 // flags() will complete the symbol causing the
322 // compiler to load classes unnecessarily. This led
323 // to regression 6180021.
324 && (tsym.flags() & COMPOUND) != 0;
325 }
327 public boolean isInterface() {
328 return (tsym.flags() & INTERFACE) != 0;
329 }
331 public boolean isPrimitive() {
332 return tag < VOID;
333 }
335 /**
336 * Does this type contain occurrences of type t?
337 */
338 public boolean contains(Type t) {
339 return t == this;
340 }
342 public static boolean contains(List<Type> ts, Type t) {
343 for (List<Type> l = ts;
344 l.tail != null /*inlined: l.nonEmpty()*/;
345 l = l.tail)
346 if (l.head.contains(t)) return true;
347 return false;
348 }
350 /** Does this type contain an occurrence of some type in 'ts'?
351 */
352 public boolean containsAny(List<Type> ts) {
353 for (Type t : ts)
354 if (this.contains(t)) return true;
355 return false;
356 }
358 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
359 for (Type t : ts1)
360 if (t.containsAny(ts2)) return true;
361 return false;
362 }
364 public boolean isSuperBound() { return false; }
365 public boolean isExtendsBound() { return false; }
366 public boolean isUnbound() { return false; }
367 public Type withTypeVar(Type t) { return this; }
369 /** The underlying method type of this type.
370 */
371 public MethodType asMethodType() { throw new AssertionError(); }
373 /** Complete loading all classes in this type.
374 */
375 public void complete() {}
377 public Object clone() {
378 try {
379 return super.clone();
380 } catch (CloneNotSupportedException e) {
381 throw new AssertionError(e);
382 }
383 }
385 public TypeSymbol asElement() {
386 return tsym;
387 }
389 public TypeKind getKind() {
390 switch (tag) {
391 case BYTE: return TypeKind.BYTE;
392 case CHAR: return TypeKind.CHAR;
393 case SHORT: return TypeKind.SHORT;
394 case INT: return TypeKind.INT;
395 case LONG: return TypeKind.LONG;
396 case FLOAT: return TypeKind.FLOAT;
397 case DOUBLE: return TypeKind.DOUBLE;
398 case BOOLEAN: return TypeKind.BOOLEAN;
399 case VOID: return TypeKind.VOID;
400 case BOT: return TypeKind.NULL;
401 case NONE: return TypeKind.NONE;
402 default: return TypeKind.OTHER;
403 }
404 }
406 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
407 if (isPrimitive())
408 return v.visitPrimitive(this, p);
409 else
410 throw new AssertionError();
411 }
413 public static class WildcardType extends Type
414 implements javax.lang.model.type.WildcardType {
416 public Type type;
417 public BoundKind kind;
418 public TypeVar bound;
420 @Override
421 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
422 return v.visitWildcardType(this, s);
423 }
425 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
426 super(WILDCARD, tsym);
427 assert(type != null);
428 this.kind = kind;
429 this.type = type;
430 }
431 public WildcardType(WildcardType t, TypeVar bound) {
432 this(t.type, t.kind, t.tsym, bound);
433 }
435 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
436 this(type, kind, tsym);
437 this.bound = bound;
438 }
440 public boolean contains(Type t) {
441 return kind != UNBOUND && type.contains(t);
442 }
444 public boolean isSuperBound() {
445 return kind == SUPER ||
446 kind == UNBOUND;
447 }
448 public boolean isExtendsBound() {
449 return kind == EXTENDS ||
450 kind == UNBOUND;
451 }
452 public boolean isUnbound() {
453 return kind == UNBOUND;
454 }
456 public Type withTypeVar(Type t) {
457 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
458 if (bound == t)
459 return this;
460 bound = (TypeVar)t;
461 return this;
462 }
464 boolean isPrintingBound = false;
465 public String toString() {
466 StringBuffer s = new StringBuffer();
467 s.append(kind.toString());
468 if (kind != UNBOUND)
469 s.append(type);
470 if (moreInfo && bound != null && !isPrintingBound)
471 try {
472 isPrintingBound = true;
473 s.append("{:").append(bound.bound).append(":}");
474 } finally {
475 isPrintingBound = false;
476 }
477 return s.toString();
478 }
480 public Type map(Mapping f) {
481 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
482 Type t = type;
483 if (t != null)
484 t = f.apply(t);
485 if (t == type)
486 return this;
487 else
488 return new WildcardType(t, kind, tsym, bound);
489 }
491 public Type getExtendsBound() {
492 if (kind == EXTENDS)
493 return type;
494 else
495 return null;
496 }
498 public Type getSuperBound() {
499 if (kind == SUPER)
500 return type;
501 else
502 return null;
503 }
505 public TypeKind getKind() {
506 return TypeKind.WILDCARD;
507 }
509 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
510 return v.visitWildcard(this, p);
511 }
512 }
514 public static class ClassType extends Type implements DeclaredType {
516 /** The enclosing type of this type. If this is the type of an inner
517 * class, outer_field refers to the type of its enclosing
518 * instance class, in all other cases it referes to noType.
519 */
520 private Type outer_field;
522 /** The type parameters of this type (to be set once class is loaded).
523 */
524 public List<Type> typarams_field;
526 /** A cache variable for the type parameters of this type,
527 * appended to all parameters of its enclosing class.
528 * @see #allparams
529 */
530 public List<Type> allparams_field;
532 /** The supertype of this class (to be set once class is loaded).
533 */
534 public Type supertype_field;
536 /** The interfaces of this class (to be set once class is loaded).
537 */
538 public List<Type> interfaces_field;
540 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
541 super(CLASS, tsym);
542 this.outer_field = outer;
543 this.typarams_field = typarams;
544 this.allparams_field = null;
545 this.supertype_field = null;
546 this.interfaces_field = null;
547 /*
548 // this can happen during error recovery
549 assert
550 outer.isParameterized() ?
551 typarams.length() == tsym.type.typarams().length() :
552 outer.isRaw() ?
553 typarams.length() == 0 :
554 true;
555 */
556 }
558 @Override
559 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
560 return v.visitClassType(this, s);
561 }
563 public Type constType(Object constValue) {
564 final Object value = constValue;
565 return new ClassType(getEnclosingType(), typarams_field, tsym) {
566 @Override
567 public Object constValue() {
568 return value;
569 }
570 @Override
571 public Type baseType() {
572 return tsym.type;
573 }
574 };
575 }
577 /** The Java source which this type represents.
578 */
579 public String toString() {
580 StringBuffer buf = new StringBuffer();
581 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
582 buf.append(getEnclosingType().toString());
583 buf.append(".");
584 buf.append(className(tsym, false));
585 } else {
586 buf.append(className(tsym, true));
587 }
588 if (getTypeArguments().nonEmpty()) {
589 buf.append('<');
590 buf.append(getTypeArguments().toString());
591 buf.append(">");
592 }
593 return buf.toString();
594 }
595 //where
596 private String className(Symbol sym, boolean longform) {
597 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
598 StringBuffer s = new StringBuffer(supertype_field.toString());
599 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
600 s.append("&");
601 s.append(is.head.toString());
602 }
603 return s.toString();
604 } else if (sym.name.isEmpty()) {
605 String s;
606 ClassType norm = (ClassType) tsym.type;
607 if (norm == null) {
608 s = Log.getLocalizedString("anonymous.class", (Object)null);
609 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
610 s = Log.getLocalizedString("anonymous.class",
611 norm.interfaces_field.head);
612 } else {
613 s = Log.getLocalizedString("anonymous.class",
614 norm.supertype_field);
615 }
616 if (moreInfo)
617 s += String.valueOf(sym.hashCode());
618 return s;
619 } else if (longform) {
620 return sym.getQualifiedName().toString();
621 } else {
622 return sym.name.toString();
623 }
624 }
626 public List<Type> getTypeArguments() {
627 if (typarams_field == null) {
628 complete();
629 if (typarams_field == null)
630 typarams_field = List.nil();
631 }
632 return typarams_field;
633 }
635 public boolean hasErasedSupertypes() {
636 return isRaw();
637 }
639 public Type getEnclosingType() {
640 return outer_field;
641 }
643 public void setEnclosingType(Type outer) {
644 outer_field = outer;
645 }
647 public List<Type> allparams() {
648 if (allparams_field == null) {
649 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
650 }
651 return allparams_field;
652 }
654 public boolean isErroneous() {
655 return
656 getEnclosingType().isErroneous() ||
657 isErroneous(getTypeArguments()) ||
658 this != tsym.type && tsym.type.isErroneous();
659 }
661 public boolean isParameterized() {
662 return allparams().tail != null;
663 // optimization, was: allparams().nonEmpty();
664 }
666 /** A cache for the rank. */
667 int rank_field = -1;
669 /** A class type is raw if it misses some
670 * of its type parameter sections.
671 * After validation, this is equivalent to:
672 * allparams.isEmpty() && tsym.type.allparams.nonEmpty();
673 */
674 public boolean isRaw() {
675 return
676 this != tsym.type && // necessary, but not sufficient condition
677 tsym.type.allparams().nonEmpty() &&
678 allparams().isEmpty();
679 }
681 public Type map(Mapping f) {
682 Type outer = getEnclosingType();
683 Type outer1 = f.apply(outer);
684 List<Type> typarams = getTypeArguments();
685 List<Type> typarams1 = map(typarams, f);
686 if (outer1 == outer && typarams1 == typarams) return this;
687 else return new ClassType(outer1, typarams1, tsym);
688 }
690 public boolean contains(Type elem) {
691 return
692 elem == this
693 || (isParameterized()
694 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
695 || (isCompound()
696 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
697 }
699 public void complete() {
700 if (tsym.completer != null) tsym.complete();
701 }
703 public TypeKind getKind() {
704 return TypeKind.DECLARED;
705 }
707 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
708 return v.visitDeclared(this, p);
709 }
710 }
712 public static class ErasedClassType extends ClassType {
713 public ErasedClassType(Type outer, TypeSymbol tsym) {
714 super(outer, List.<Type>nil(), tsym);
715 }
717 @Override
718 public boolean hasErasedSupertypes() {
719 return true;
720 }
721 }
723 public static class ArrayType extends Type
724 implements javax.lang.model.type.ArrayType {
726 public Type elemtype;
728 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
729 super(ARRAY, arrayClass);
730 this.elemtype = elemtype;
731 }
733 @Override
734 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
735 return v.visitArrayType(this, s);
736 }
738 public String toString() {
739 return elemtype + "[]";
740 }
742 public boolean equals(Object obj) {
743 return
744 this == obj ||
745 (obj instanceof ArrayType &&
746 this.elemtype.equals(((ArrayType)obj).elemtype));
747 }
749 public int hashCode() {
750 return (ARRAY << 5) + elemtype.hashCode();
751 }
753 public List<Type> allparams() { return elemtype.allparams(); }
755 public boolean isErroneous() {
756 return elemtype.isErroneous();
757 }
759 public boolean isParameterized() {
760 return elemtype.isParameterized();
761 }
763 public boolean isRaw() {
764 return elemtype.isRaw();
765 }
767 public Type map(Mapping f) {
768 Type elemtype1 = f.apply(elemtype);
769 if (elemtype1 == elemtype) return this;
770 else return new ArrayType(elemtype1, tsym);
771 }
773 public boolean contains(Type elem) {
774 return elem == this || elemtype.contains(elem);
775 }
777 public void complete() {
778 elemtype.complete();
779 }
781 public Type getComponentType() {
782 return elemtype;
783 }
785 public TypeKind getKind() {
786 return TypeKind.ARRAY;
787 }
789 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
790 return v.visitArray(this, p);
791 }
792 }
794 public static class MethodType extends Type
795 implements Cloneable, ExecutableType {
797 public List<Type> argtypes;
798 public Type restype;
799 public List<Type> thrown;
801 public MethodType(List<Type> argtypes,
802 Type restype,
803 List<Type> thrown,
804 TypeSymbol methodClass) {
805 super(METHOD, methodClass);
806 this.argtypes = argtypes;
807 this.restype = restype;
808 this.thrown = thrown;
809 }
811 @Override
812 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
813 return v.visitMethodType(this, s);
814 }
816 /** The Java source which this type represents.
817 *
818 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
819 * should be.
820 */
821 public String toString() {
822 return "(" + argtypes + ")" + restype;
823 }
825 public boolean equals(Object obj) {
826 if (this == obj)
827 return true;
828 if (!(obj instanceof MethodType))
829 return false;
830 MethodType m = (MethodType)obj;
831 List<Type> args1 = argtypes;
832 List<Type> args2 = m.argtypes;
833 while (!args1.isEmpty() && !args2.isEmpty()) {
834 if (!args1.head.equals(args2.head))
835 return false;
836 args1 = args1.tail;
837 args2 = args2.tail;
838 }
839 if (!args1.isEmpty() || !args2.isEmpty())
840 return false;
841 return restype.equals(m.restype);
842 }
844 public int hashCode() {
845 int h = METHOD;
846 for (List<Type> thisargs = this.argtypes;
847 thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
848 thisargs = thisargs.tail)
849 h = (h << 5) + thisargs.head.hashCode();
850 return (h << 5) + this.restype.hashCode();
851 }
853 public List<Type> getParameterTypes() { return argtypes; }
854 public Type getReturnType() { return restype; }
855 public List<Type> getThrownTypes() { return thrown; }
857 public void setThrown(List<Type> t) {
858 thrown = t;
859 }
861 public boolean isErroneous() {
862 return
863 isErroneous(argtypes) ||
864 restype != null && restype.isErroneous();
865 }
867 public Type map(Mapping f) {
868 List<Type> argtypes1 = map(argtypes, f);
869 Type restype1 = f.apply(restype);
870 List<Type> thrown1 = map(thrown, f);
871 if (argtypes1 == argtypes &&
872 restype1 == restype &&
873 thrown1 == thrown) return this;
874 else return new MethodType(argtypes1, restype1, thrown1, tsym);
875 }
877 public boolean contains(Type elem) {
878 return elem == this || contains(argtypes, elem) || restype.contains(elem);
879 }
881 public MethodType asMethodType() { return this; }
883 public void complete() {
884 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
885 l.head.complete();
886 restype.complete();
887 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
888 l.head.complete();
889 }
891 public List<TypeVar> getTypeVariables() {
892 return List.nil();
893 }
895 public TypeSymbol asElement() {
896 return null;
897 }
899 public TypeKind getKind() {
900 return TypeKind.EXECUTABLE;
901 }
903 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
904 return v.visitExecutable(this, p);
905 }
906 }
908 public static class PackageType extends Type implements NoType {
910 PackageType(TypeSymbol tsym) {
911 super(PACKAGE, tsym);
912 }
914 @Override
915 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
916 return v.visitPackageType(this, s);
917 }
919 public String toString() {
920 return tsym.getQualifiedName().toString();
921 }
923 public TypeKind getKind() {
924 return TypeKind.PACKAGE;
925 }
927 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
928 return v.visitNoType(this, p);
929 }
930 }
932 public static class TypeVar extends Type implements TypeVariable {
934 /** The bound of this type variable; set from outside.
935 * Must be nonempty once it is set.
936 * For a bound, `bound' is the bound type itself.
937 * Multiple bounds are expressed as a single class type which has the
938 * individual bounds as superclass, respectively interfaces.
939 * The class type then has as `tsym' a compiler generated class `c',
940 * which has a flag COMPOUND and whose owner is the type variable
941 * itself. Furthermore, the erasure_field of the class
942 * points to the first class or interface bound.
943 */
944 public Type bound = null;
945 public Type lower;
947 public TypeVar(Name name, Symbol owner, Type lower) {
948 super(TYPEVAR, null);
949 tsym = new TypeSymbol(0, name, this, owner);
950 this.lower = lower;
951 }
953 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
954 super(TYPEVAR, tsym);
955 this.bound = bound;
956 this.lower = lower;
957 }
959 @Override
960 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
961 return v.visitTypeVar(this, s);
962 }
964 public Type getUpperBound() { return bound; }
966 int rank_field = -1;
968 public Type getLowerBound() {
969 return lower;
970 }
972 public TypeKind getKind() {
973 return TypeKind.TYPEVAR;
974 }
976 public boolean isCaptured() {
977 return false;
978 }
980 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
981 return v.visitTypeVariable(this, p);
982 }
983 }
985 /** A captured type variable comes from wildcards which can have
986 * both upper and lower bound. CapturedType extends TypeVar with
987 * a lower bound.
988 */
989 public static class CapturedType extends TypeVar {
991 public Type lower;
992 public WildcardType wildcard;
994 public CapturedType(Name name,
995 Symbol owner,
996 Type upper,
997 Type lower,
998 WildcardType wildcard) {
999 super(name, owner, lower);
1000 assert lower != null;
1001 this.bound = upper;
1002 this.lower = lower;
1003 this.wildcard = wildcard;
1004 }
1006 @Override
1007 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1008 return v.visitCapturedType(this, s);
1009 }
1011 public Type getLowerBound() {
1012 return lower;
1013 }
1015 @Override
1016 public boolean isCaptured() {
1017 return true;
1018 }
1020 @Override
1021 public String toString() {
1022 return "capture#"
1023 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1024 + " of "
1025 + wildcard;
1026 }
1027 }
1029 public static abstract class DelegatedType extends Type {
1030 public Type qtype;
1031 public DelegatedType(int tag, Type qtype) {
1032 super(tag, qtype.tsym);
1033 this.qtype = qtype;
1034 }
1035 public String toString() { return qtype.toString(); }
1036 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1037 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1038 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1039 public Type getReturnType() { return qtype.getReturnType(); }
1040 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1041 public List<Type> allparams() { return qtype.allparams(); }
1042 public Type getUpperBound() { return qtype.getUpperBound(); }
1043 public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
1044 public boolean isErroneous() { return qtype.isErroneous(); }
1045 }
1047 public static class ForAll extends DelegatedType
1048 implements Cloneable, ExecutableType {
1049 public List<Type> tvars;
1051 public ForAll(List<Type> tvars, Type qtype) {
1052 super(FORALL, qtype);
1053 this.tvars = tvars;
1054 }
1056 @Override
1057 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1058 return v.visitForAll(this, s);
1059 }
1061 public String toString() {
1062 return "<" + tvars + ">" + qtype;
1063 }
1065 public List<Type> getTypeArguments() { return tvars; }
1067 public void setThrown(List<Type> t) {
1068 qtype.setThrown(t);
1069 }
1071 public Object clone() {
1072 ForAll result = (ForAll)super.clone();
1073 result.qtype = (Type)result.qtype.clone();
1074 return result;
1075 }
1077 public boolean isErroneous() {
1078 return qtype.isErroneous();
1079 }
1081 /**
1082 * Replaces this ForAll's typevars with a set of concrete Java types
1083 * and returns the instantiated generic type. Subclasses should override
1084 * in order to check that the list of types is a valid instantiation
1085 * of the ForAll's typevars.
1086 *
1087 * @param actuals list of actual types
1088 * @param types types instance
1089 * @return qtype where all occurrences of tvars are replaced
1090 * by types in actuals
1091 */
1092 public Type inst(List<Type> actuals, Types types) {
1093 return types.subst(qtype, tvars, actuals);
1094 }
1096 /**
1097 * Kind of type-constraint derived during type inference
1098 */
1099 public enum ConstraintKind {
1100 /**
1101 * upper bound constraint (a type variable must be instantiated
1102 * with a type T, where T is a subtype of all the types specified by
1103 * its EXTENDS constraints).
1104 */
1105 EXTENDS,
1106 /**
1107 * lower bound constraint (a type variable must be instantiated
1108 * with a type T, where T is a supertype of all the types specified by
1109 * its SUPER constraints).
1110 */
1111 SUPER,
1112 /**
1113 * equality constraint (a type variable must be instantiated to the type
1114 * specified by its EQUAL constraint.
1115 */
1116 EQUAL;
1117 }
1119 /**
1120 * Get the type-constraints of a given kind for a given type-variable of
1121 * this ForAll type. Subclasses should override in order to return more
1122 * accurate sets of constraints.
1123 *
1124 * @param tv the type-variable for which the constraint is to be retrieved
1125 * @param ck the constraint kind to be retrieved
1126 * @return the list of types specified by the selected constraint
1127 */
1128 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
1129 return List.nil();
1130 }
1132 public Type map(Mapping f) {
1133 return f.apply(qtype);
1134 }
1136 public boolean contains(Type elem) {
1137 return qtype.contains(elem);
1138 }
1140 public MethodType asMethodType() {
1141 return qtype.asMethodType();
1142 }
1144 public void complete() {
1145 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1146 ((TypeVar)l.head).bound.complete();
1147 }
1148 qtype.complete();
1149 }
1151 public List<TypeVar> getTypeVariables() {
1152 return List.convert(TypeVar.class, getTypeArguments());
1153 }
1155 public TypeKind getKind() {
1156 return TypeKind.EXECUTABLE;
1157 }
1159 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1160 return v.visitExecutable(this, p);
1161 }
1162 }
1164 /** A class for instantiatable variables, for use during type
1165 * inference.
1166 */
1167 public static class UndetVar extends DelegatedType {
1168 public List<Type> lobounds = List.nil();
1169 public List<Type> hibounds = List.nil();
1170 public Type inst = null;
1172 @Override
1173 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1174 return v.visitUndetVar(this, s);
1175 }
1177 public UndetVar(Type origin) {
1178 super(UNDETVAR, origin);
1179 }
1181 public String toString() {
1182 if (inst != null) return inst.toString();
1183 else return qtype + "?";
1184 }
1186 public Type baseType() {
1187 if (inst != null) return inst.baseType();
1188 else return this;
1189 }
1190 }
1192 /** Represents VOID or NONE.
1193 */
1194 static class JCNoType extends Type implements NoType {
1195 public JCNoType(int tag) {
1196 super(tag, null);
1197 }
1199 @Override
1200 public TypeKind getKind() {
1201 switch (tag) {
1202 case VOID: return TypeKind.VOID;
1203 case NONE: return TypeKind.NONE;
1204 default:
1205 throw new AssertionError("Unexpected tag: " + tag);
1206 }
1207 }
1209 @Override
1210 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1211 return v.visitNoType(this, p);
1212 }
1213 }
1215 static class BottomType extends Type implements NullType {
1216 public BottomType() {
1217 super(TypeTags.BOT, null);
1218 }
1220 @Override
1221 public TypeKind getKind() {
1222 return TypeKind.NULL;
1223 }
1225 @Override
1226 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1227 return v.visitNull(this, p);
1228 }
1230 @Override
1231 public Type constType(Object value) {
1232 return this;
1233 }
1235 @Override
1236 public String stringValue() {
1237 return "null";
1238 }
1239 }
1241 public static class ErrorType extends ClassType
1242 implements javax.lang.model.type.ErrorType {
1244 private Type originalType = null;
1246 public ErrorType(Type originalType, TypeSymbol tsym) {
1247 super(noType, List.<Type>nil(), null);
1248 tag = ERROR;
1249 this.tsym = tsym;
1250 this.originalType = (originalType == null ? noType : originalType);
1251 }
1253 public ErrorType(ClassSymbol c, Type originalType) {
1254 this(originalType, c);
1255 c.type = this;
1256 c.kind = ERR;
1257 c.members_field = new Scope.ErrorScope(c);
1258 }
1260 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1261 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1262 }
1264 @Override
1265 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1266 return v.visitErrorType(this, s);
1267 }
1269 public Type constType(Object constValue) { return this; }
1270 public Type getEnclosingType() { return this; }
1271 public Type getReturnType() { return this; }
1272 public Type asSub(Symbol sym) { return this; }
1273 public Type map(Mapping f) { return this; }
1275 public boolean isGenType(Type t) { return true; }
1276 public boolean isErroneous() { return true; }
1277 public boolean isCompound() { return false; }
1278 public boolean isInterface() { return false; }
1280 public List<Type> allparams() { return List.nil(); }
1281 public List<Type> getTypeArguments() { return List.nil(); }
1283 public TypeKind getKind() {
1284 return TypeKind.ERROR;
1285 }
1287 public Type getOriginalType() {
1288 return originalType;
1289 }
1291 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1292 return v.visitError(this, p);
1293 }
1294 }
1296 /**
1297 * A visitor for types. A visitor is used to implement operations
1298 * (or relations) on types. Most common operations on types are
1299 * binary relations and this interface is designed for binary
1300 * relations, that is, operations on the form
1301 * Type × S → R.
1302 * <!-- In plain text: Type x S -> R -->
1303 *
1304 * @param <R> the return type of the operation implemented by this
1305 * visitor; use Void if no return type is needed.
1306 * @param <S> the type of the second argument (the first being the
1307 * type itself) of the operation implemented by this visitor; use
1308 * Void if a second argument is not needed.
1309 */
1310 public interface Visitor<R,S> {
1311 R visitClassType(ClassType t, S s);
1312 R visitWildcardType(WildcardType t, S s);
1313 R visitArrayType(ArrayType t, S s);
1314 R visitMethodType(MethodType t, S s);
1315 R visitPackageType(PackageType t, S s);
1316 R visitTypeVar(TypeVar t, S s);
1317 R visitCapturedType(CapturedType t, S s);
1318 R visitForAll(ForAll t, S s);
1319 R visitUndetVar(UndetVar t, S s);
1320 R visitErrorType(ErrorType t, S s);
1321 R visitType(Type t, S s);
1322 }
1323 }