Mon, 17 Dec 2012 07:47:05 -0800
8004832: Add new doclint package
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 1999, 2012, 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 java.util.Collections;
29 import java.util.EnumMap;
30 import java.util.EnumSet;
31 import java.util.Map;
32 import java.util.Set;
34 import javax.lang.model.type.*;
36 import com.sun.tools.javac.code.Symbol.*;
37 import com.sun.tools.javac.util.*;
38 import static com.sun.tools.javac.code.BoundKind.*;
39 import static com.sun.tools.javac.code.Flags.*;
40 import static com.sun.tools.javac.code.Kinds.*;
41 import static com.sun.tools.javac.code.TypeTag.*;
43 /** This class represents Java types. The class itself defines the behavior of
44 * the following types:
45 * <pre>
46 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
47 * type `void' (tag: VOID),
48 * the bottom type (tag: BOT),
49 * the missing type (tag: NONE).
50 * </pre>
51 * <p>The behavior of the following types is defined in subclasses, which are
52 * all static inner classes of this class:
53 * <pre>
54 * class types (tag: CLASS, class: ClassType),
55 * array types (tag: ARRAY, class: ArrayType),
56 * method types (tag: METHOD, class: MethodType),
57 * package types (tag: PACKAGE, class: PackageType),
58 * type variables (tag: TYPEVAR, class: TypeVar),
59 * type arguments (tag: WILDCARD, class: WildcardType),
60 * generic method types (tag: FORALL, class: ForAll),
61 * the error type (tag: ERROR, class: ErrorType).
62 * </pre>
63 *
64 * <p><b>This is NOT part of any supported API.
65 * If you write code that depends on this, you do so at your own risk.
66 * This code and its internal interfaces are subject to change or
67 * deletion without notice.</b>
68 *
69 * @see TypeTag
70 */
71 public class Type implements PrimitiveType {
73 /** Constant type: no type at all. */
74 public static final JCNoType noType = new JCNoType(NONE);
76 /** Constant type: special type to be used during recovery of deferred expressions. */
77 public static final JCNoType recoveryType = new JCNoType(NONE);
79 /** If this switch is turned on, the names of type variables
80 * and anonymous classes are printed with hashcodes appended.
81 */
82 public static boolean moreInfo = false;
84 /** The tag of this type.
85 *
86 * @see TypeTag
87 */
88 protected TypeTag tag;
90 /** The defining class / interface / package / type variable
91 */
92 public TypeSymbol tsym;
94 /**
95 * Checks if the current type tag is equal to the given tag.
96 * @return true if tag is equal to the current type tag.
97 */
98 public boolean hasTag(TypeTag tag) {
99 return this.tag == tag;
100 }
102 /**
103 * Returns the current type tag.
104 * @return the value of the current type tag.
105 */
106 public TypeTag getTag() {
107 return tag;
108 }
110 public boolean isNumeric() {
111 switch (tag) {
112 case BYTE: case CHAR:
113 case SHORT:
114 case INT: case LONG:
115 case FLOAT: case DOUBLE:
116 return true;
117 default:
118 return false;
119 }
120 }
122 public boolean isPrimitive() {
123 return (isNumeric() || tag == BOOLEAN);
124 }
126 public boolean isPrimitiveOrVoid() {
127 return (isPrimitive() || tag == VOID);
128 }
130 public boolean isReference() {
131 switch (tag) {
132 case CLASS:
133 case ARRAY:
134 case TYPEVAR:
135 case WILDCARD:
136 case ERROR:
137 return true;
138 default:
139 return false;
140 }
141 }
143 public boolean isNullOrReference() {
144 return (tag == BOT || isReference());
145 }
147 public boolean isPartial() {
148 switch(tag) {
149 case ERROR: case UNKNOWN: case UNDETVAR:
150 return true;
151 default:
152 return false;
153 }
154 }
156 /**
157 * The constant value of this type, null if this type does not
158 * have a constant value attribute. Only primitive types and
159 * strings (ClassType) can have a constant value attribute.
160 * @return the constant value attribute of this type
161 */
162 public Object constValue() {
163 return null;
164 }
166 /**
167 * Get the representation of this type used for modelling purposes.
168 * By default, this is itself. For ErrorType, a different value
169 * may be provided,
170 */
171 public Type getModelType() {
172 return this;
173 }
175 public static List<Type> getModelTypes(List<Type> ts) {
176 ListBuffer<Type> lb = new ListBuffer<Type>();
177 for (Type t: ts)
178 lb.append(t.getModelType());
179 return lb.toList();
180 }
182 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
184 /** Define a type given its tag and type symbol
185 */
186 public Type(TypeTag tag, TypeSymbol tsym) {
187 this.tag = tag;
188 this.tsym = tsym;
189 }
191 /** An abstract class for mappings from types to types
192 */
193 public static abstract class Mapping {
194 private String name;
195 public Mapping(String name) {
196 this.name = name;
197 }
198 public abstract Type apply(Type t);
199 public String toString() {
200 return name;
201 }
202 }
204 /** map a type function over all immediate descendants of this type
205 */
206 public Type map(Mapping f) {
207 return this;
208 }
210 /** map a type function over a list of types
211 */
212 public static List<Type> map(List<Type> ts, Mapping f) {
213 if (ts.nonEmpty()) {
214 List<Type> tail1 = map(ts.tail, f);
215 Type t = f.apply(ts.head);
216 if (tail1 != ts.tail || t != ts.head)
217 return tail1.prepend(t);
218 }
219 return ts;
220 }
222 /** Define a constant type, of the same kind as this type
223 * and with given constant value
224 */
225 public Type constType(Object constValue) {
226 final Object value = constValue;
227 Assert.check(isPrimitive());
228 return new Type(tag, tsym) {
229 @Override
230 public Object constValue() {
231 return value;
232 }
233 @Override
234 public Type baseType() {
235 return tsym.type;
236 }
237 };
238 }
240 /**
241 * If this is a constant type, return its underlying type.
242 * Otherwise, return the type itself.
243 */
244 public Type baseType() {
245 return this;
246 }
248 /** Return the base types of a list of types.
249 */
250 public static List<Type> baseTypes(List<Type> ts) {
251 if (ts.nonEmpty()) {
252 Type t = ts.head.baseType();
253 List<Type> baseTypes = baseTypes(ts.tail);
254 if (t != ts.head || baseTypes != ts.tail)
255 return baseTypes.prepend(t);
256 }
257 return ts;
258 }
260 /** The Java source which this type represents.
261 */
262 public String toString() {
263 String s = (tsym == null || tsym.name == null)
264 ? "<none>"
265 : tsym.name.toString();
266 if (moreInfo && tag == TYPEVAR) s = s + hashCode();
267 return s;
268 }
270 /**
271 * The Java source which this type list represents. A List is
272 * represented as a comma-spearated listing of the elements in
273 * that list.
274 */
275 public static String toString(List<Type> ts) {
276 if (ts.isEmpty()) {
277 return "";
278 } else {
279 StringBuilder buf = new StringBuilder();
280 buf.append(ts.head.toString());
281 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
282 buf.append(",").append(l.head.toString());
283 return buf.toString();
284 }
285 }
287 /**
288 * The constant value of this type, converted to String
289 */
290 public String stringValue() {
291 Object cv = Assert.checkNonNull(constValue());
292 if (tag == BOOLEAN)
293 return ((Integer) cv).intValue() == 0 ? "false" : "true";
294 else if (tag == CHAR)
295 return String.valueOf((char) ((Integer) cv).intValue());
296 else
297 return cv.toString();
298 }
300 /**
301 * This method is analogous to isSameType, but weaker, since we
302 * never complete classes. Where isSameType would complete a
303 * class, equals assumes that the two types are different.
304 */
305 @Override
306 public boolean equals(Object t) {
307 return super.equals(t);
308 }
310 @Override
311 public int hashCode() {
312 return super.hashCode();
313 }
315 /** Is this a constant type whose value is false?
316 */
317 public boolean isFalse() {
318 return
319 tag == BOOLEAN &&
320 constValue() != null &&
321 ((Integer)constValue()).intValue() == 0;
322 }
324 /** Is this a constant type whose value is true?
325 */
326 public boolean isTrue() {
327 return
328 tag == BOOLEAN &&
329 constValue() != null &&
330 ((Integer)constValue()).intValue() != 0;
331 }
333 public String argtypes(boolean varargs) {
334 List<Type> args = getParameterTypes();
335 if (!varargs) return args.toString();
336 StringBuilder buf = new StringBuilder();
337 while (args.tail.nonEmpty()) {
338 buf.append(args.head);
339 args = args.tail;
340 buf.append(',');
341 }
342 if (args.head.tag == ARRAY) {
343 buf.append(((ArrayType)args.head).elemtype);
344 buf.append("...");
345 } else {
346 buf.append(args.head);
347 }
348 return buf.toString();
349 }
351 /** Access methods.
352 */
353 public List<Type> getTypeArguments() { return List.nil(); }
354 public Type getEnclosingType() { return null; }
355 public List<Type> getParameterTypes() { return List.nil(); }
356 public Type getReturnType() { return null; }
357 public List<Type> getThrownTypes() { return List.nil(); }
358 public Type getUpperBound() { return null; }
359 public Type getLowerBound() { return null; }
361 /** Navigation methods, these will work for classes, type variables,
362 * foralls, but will return null for arrays and methods.
363 */
365 /** Return all parameters of this type and all its outer types in order
366 * outer (first) to inner (last).
367 */
368 public List<Type> allparams() { return List.nil(); }
370 /** Does this type contain "error" elements?
371 */
372 public boolean isErroneous() {
373 return false;
374 }
376 public static boolean isErroneous(List<Type> ts) {
377 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
378 if (l.head.isErroneous()) return true;
379 return false;
380 }
382 /** Is this type parameterized?
383 * A class type is parameterized if it has some parameters.
384 * An array type is parameterized if its element type is parameterized.
385 * All other types are not parameterized.
386 */
387 public boolean isParameterized() {
388 return false;
389 }
391 /** Is this type a raw type?
392 * A class type is a raw type if it misses some of its parameters.
393 * An array type is a raw type if its element type is raw.
394 * All other types are not raw.
395 * Type validation will ensure that the only raw types
396 * in a program are types that miss all their type variables.
397 */
398 public boolean isRaw() {
399 return false;
400 }
402 public boolean isCompound() {
403 return tsym.completer == null
404 // Compound types can't have a completer. Calling
405 // flags() will complete the symbol causing the
406 // compiler to load classes unnecessarily. This led
407 // to regression 6180021.
408 && (tsym.flags() & COMPOUND) != 0;
409 }
411 public boolean isInterface() {
412 return (tsym.flags() & INTERFACE) != 0;
413 }
415 public boolean isFinal() {
416 return (tsym.flags() & FINAL) != 0;
417 }
419 /**
420 * Does this type contain occurrences of type t?
421 */
422 public boolean contains(Type t) {
423 return t == this;
424 }
426 public static boolean contains(List<Type> ts, Type t) {
427 for (List<Type> l = ts;
428 l.tail != null /*inlined: l.nonEmpty()*/;
429 l = l.tail)
430 if (l.head.contains(t)) return true;
431 return false;
432 }
434 /** Does this type contain an occurrence of some type in 'ts'?
435 */
436 public boolean containsAny(List<Type> ts) {
437 for (Type t : ts)
438 if (this.contains(t)) return true;
439 return false;
440 }
442 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
443 for (Type t : ts1)
444 if (t.containsAny(ts2)) return true;
445 return false;
446 }
448 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
449 ListBuffer<Type> buf = ListBuffer.lb();
450 for (Type t : ts) {
451 if (tf.accepts(t)) {
452 buf.append(t);
453 }
454 }
455 return buf.toList();
456 }
458 public boolean isSuperBound() { return false; }
459 public boolean isExtendsBound() { return false; }
460 public boolean isUnbound() { return false; }
461 public Type withTypeVar(Type t) { return this; }
463 /** The underlying method type of this type.
464 */
465 public MethodType asMethodType() { throw new AssertionError(); }
467 /** Complete loading all classes in this type.
468 */
469 public void complete() {}
471 public TypeSymbol asElement() {
472 return tsym;
473 }
475 public TypeKind getKind() {
476 switch (tag) {
477 case BYTE: return TypeKind.BYTE;
478 case CHAR: return TypeKind.CHAR;
479 case SHORT: return TypeKind.SHORT;
480 case INT: return TypeKind.INT;
481 case LONG: return TypeKind.LONG;
482 case FLOAT: return TypeKind.FLOAT;
483 case DOUBLE: return TypeKind.DOUBLE;
484 case BOOLEAN: return TypeKind.BOOLEAN;
485 case VOID: return TypeKind.VOID;
486 case BOT: return TypeKind.NULL;
487 case NONE: return TypeKind.NONE;
488 default: return TypeKind.OTHER;
489 }
490 }
492 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
493 if (isPrimitive())
494 return v.visitPrimitive(this, p);
495 else
496 throw new AssertionError();
497 }
499 public static class WildcardType extends Type
500 implements javax.lang.model.type.WildcardType {
502 public Type type;
503 public BoundKind kind;
504 public TypeVar bound;
506 @Override
507 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
508 return v.visitWildcardType(this, s);
509 }
511 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
512 super(WILDCARD, tsym);
513 this.type = Assert.checkNonNull(type);
514 this.kind = kind;
515 }
516 public WildcardType(WildcardType t, TypeVar bound) {
517 this(t.type, t.kind, t.tsym, bound);
518 }
520 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
521 this(type, kind, tsym);
522 this.bound = bound;
523 }
525 public boolean contains(Type t) {
526 return kind != UNBOUND && type.contains(t);
527 }
529 public boolean isSuperBound() {
530 return kind == SUPER ||
531 kind == UNBOUND;
532 }
533 public boolean isExtendsBound() {
534 return kind == EXTENDS ||
535 kind == UNBOUND;
536 }
537 public boolean isUnbound() {
538 return kind == UNBOUND;
539 }
541 public Type withTypeVar(Type t) {
542 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
543 if (bound == t)
544 return this;
545 bound = (TypeVar)t;
546 return this;
547 }
549 boolean isPrintingBound = false;
550 public String toString() {
551 StringBuilder s = new StringBuilder();
552 s.append(kind.toString());
553 if (kind != UNBOUND)
554 s.append(type);
555 if (moreInfo && bound != null && !isPrintingBound)
556 try {
557 isPrintingBound = true;
558 s.append("{:").append(bound.bound).append(":}");
559 } finally {
560 isPrintingBound = false;
561 }
562 return s.toString();
563 }
565 public Type map(Mapping f) {
566 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
567 Type t = type;
568 if (t != null)
569 t = f.apply(t);
570 if (t == type)
571 return this;
572 else
573 return new WildcardType(t, kind, tsym, bound);
574 }
576 public Type getExtendsBound() {
577 if (kind == EXTENDS)
578 return type;
579 else
580 return null;
581 }
583 public Type getSuperBound() {
584 if (kind == SUPER)
585 return type;
586 else
587 return null;
588 }
590 public TypeKind getKind() {
591 return TypeKind.WILDCARD;
592 }
594 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
595 return v.visitWildcard(this, p);
596 }
597 }
599 public static class ClassType extends Type implements DeclaredType {
601 /** The enclosing type of this type. If this is the type of an inner
602 * class, outer_field refers to the type of its enclosing
603 * instance class, in all other cases it referes to noType.
604 */
605 private Type outer_field;
607 /** The type parameters of this type (to be set once class is loaded).
608 */
609 public List<Type> typarams_field;
611 /** A cache variable for the type parameters of this type,
612 * appended to all parameters of its enclosing class.
613 * @see #allparams
614 */
615 public List<Type> allparams_field;
617 /** The supertype of this class (to be set once class is loaded).
618 */
619 public Type supertype_field;
621 /** The interfaces of this class (to be set once class is loaded).
622 */
623 public List<Type> interfaces_field;
625 /** All the interfaces of this class, including missing ones.
626 */
627 public List<Type> all_interfaces_field;
629 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
630 super(CLASS, tsym);
631 this.outer_field = outer;
632 this.typarams_field = typarams;
633 this.allparams_field = null;
634 this.supertype_field = null;
635 this.interfaces_field = null;
636 /*
637 // this can happen during error recovery
638 assert
639 outer.isParameterized() ?
640 typarams.length() == tsym.type.typarams().length() :
641 outer.isRaw() ?
642 typarams.length() == 0 :
643 true;
644 */
645 }
647 @Override
648 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
649 return v.visitClassType(this, s);
650 }
652 public Type constType(Object constValue) {
653 final Object value = constValue;
654 return new ClassType(getEnclosingType(), typarams_field, tsym) {
655 @Override
656 public Object constValue() {
657 return value;
658 }
659 @Override
660 public Type baseType() {
661 return tsym.type;
662 }
663 };
664 }
666 /** The Java source which this type represents.
667 */
668 public String toString() {
669 StringBuilder buf = new StringBuilder();
670 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
671 buf.append(getEnclosingType().toString());
672 buf.append(".");
673 buf.append(className(tsym, false));
674 } else {
675 buf.append(className(tsym, true));
676 }
677 if (getTypeArguments().nonEmpty()) {
678 buf.append('<');
679 buf.append(getTypeArguments().toString());
680 buf.append(">");
681 }
682 return buf.toString();
683 }
684 //where
685 private String className(Symbol sym, boolean longform) {
686 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
687 StringBuilder s = new StringBuilder(supertype_field.toString());
688 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
689 s.append("&");
690 s.append(is.head.toString());
691 }
692 return s.toString();
693 } else if (sym.name.isEmpty()) {
694 String s;
695 ClassType norm = (ClassType) tsym.type;
696 if (norm == null) {
697 s = Log.getLocalizedString("anonymous.class", (Object)null);
698 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
699 s = Log.getLocalizedString("anonymous.class",
700 norm.interfaces_field.head);
701 } else {
702 s = Log.getLocalizedString("anonymous.class",
703 norm.supertype_field);
704 }
705 if (moreInfo)
706 s += String.valueOf(sym.hashCode());
707 return s;
708 } else if (longform) {
709 return sym.getQualifiedName().toString();
710 } else {
711 return sym.name.toString();
712 }
713 }
715 public List<Type> getTypeArguments() {
716 if (typarams_field == null) {
717 complete();
718 if (typarams_field == null)
719 typarams_field = List.nil();
720 }
721 return typarams_field;
722 }
724 public boolean hasErasedSupertypes() {
725 return isRaw();
726 }
728 public Type getEnclosingType() {
729 return outer_field;
730 }
732 public void setEnclosingType(Type outer) {
733 outer_field = outer;
734 }
736 public List<Type> allparams() {
737 if (allparams_field == null) {
738 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
739 }
740 return allparams_field;
741 }
743 public boolean isErroneous() {
744 return
745 getEnclosingType().isErroneous() ||
746 isErroneous(getTypeArguments()) ||
747 this != tsym.type && tsym.type.isErroneous();
748 }
750 public boolean isParameterized() {
751 return allparams().tail != null;
752 // optimization, was: allparams().nonEmpty();
753 }
755 /** A cache for the rank. */
756 int rank_field = -1;
758 /** A class type is raw if it misses some
759 * of its type parameter sections.
760 * After validation, this is equivalent to:
761 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
762 */
763 public boolean isRaw() {
764 return
765 this != tsym.type && // necessary, but not sufficient condition
766 tsym.type.allparams().nonEmpty() &&
767 allparams().isEmpty();
768 }
770 public Type map(Mapping f) {
771 Type outer = getEnclosingType();
772 Type outer1 = f.apply(outer);
773 List<Type> typarams = getTypeArguments();
774 List<Type> typarams1 = map(typarams, f);
775 if (outer1 == outer && typarams1 == typarams) return this;
776 else return new ClassType(outer1, typarams1, tsym);
777 }
779 public boolean contains(Type elem) {
780 return
781 elem == this
782 || (isParameterized()
783 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
784 || (isCompound()
785 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
786 }
788 public void complete() {
789 if (tsym.completer != null) tsym.complete();
790 }
792 public TypeKind getKind() {
793 return TypeKind.DECLARED;
794 }
796 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
797 return v.visitDeclared(this, p);
798 }
799 }
801 public static class ErasedClassType extends ClassType {
802 public ErasedClassType(Type outer, TypeSymbol tsym) {
803 super(outer, List.<Type>nil(), tsym);
804 }
806 @Override
807 public boolean hasErasedSupertypes() {
808 return true;
809 }
810 }
812 // a clone of a ClassType that knows about the alternatives of a union type.
813 public static class UnionClassType extends ClassType implements UnionType {
814 final List<? extends Type> alternatives_field;
816 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
817 super(ct.outer_field, ct.typarams_field, ct.tsym);
818 allparams_field = ct.allparams_field;
819 supertype_field = ct.supertype_field;
820 interfaces_field = ct.interfaces_field;
821 all_interfaces_field = ct.interfaces_field;
822 alternatives_field = alternatives;
823 }
825 public Type getLub() {
826 return tsym.type;
827 }
829 public java.util.List<? extends TypeMirror> getAlternatives() {
830 return Collections.unmodifiableList(alternatives_field);
831 }
833 @Override
834 public TypeKind getKind() {
835 return TypeKind.UNION;
836 }
838 @Override
839 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
840 return v.visitUnion(this, p);
841 }
842 }
844 // a clone of a ClassType that knows about the bounds of an intersection type.
845 public static class IntersectionClassType extends ClassType implements IntersectionType {
847 public boolean allInterfaces;
849 public enum IntersectionKind {
850 EXPLICIT,
851 IMPLICT;
852 }
854 public IntersectionKind intersectionKind;
856 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
857 super(Type.noType, List.<Type>nil(), csym);
858 this.allInterfaces = allInterfaces;
859 Assert.check((csym.flags() & COMPOUND) != 0);
860 supertype_field = bounds.head;
861 interfaces_field = bounds.tail;
862 Assert.check(supertype_field.tsym.completer != null ||
863 !supertype_field.isInterface(), supertype_field);
864 }
866 public java.util.List<? extends TypeMirror> getBounds() {
867 return Collections.unmodifiableList(getComponents());
868 }
870 public List<Type> getComponents() {
871 return interfaces_field.prepend(supertype_field);
872 }
874 @Override
875 public TypeKind getKind() {
876 return TypeKind.INTERSECTION;
877 }
879 @Override
880 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
881 return intersectionKind == IntersectionKind.EXPLICIT ?
882 v.visitIntersection(this, p) :
883 v.visitDeclared(this, p);
884 }
885 }
887 public static class ArrayType extends Type
888 implements javax.lang.model.type.ArrayType {
890 public Type elemtype;
892 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
893 super(ARRAY, arrayClass);
894 this.elemtype = elemtype;
895 }
897 @Override
898 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
899 return v.visitArrayType(this, s);
900 }
902 public String toString() {
903 return elemtype + "[]";
904 }
906 public boolean equals(Object obj) {
907 return
908 this == obj ||
909 (obj instanceof ArrayType &&
910 this.elemtype.equals(((ArrayType)obj).elemtype));
911 }
913 public int hashCode() {
914 return (ARRAY.ordinal() << 5) + elemtype.hashCode();
915 }
917 public boolean isVarargs() {
918 return false;
919 }
921 public List<Type> allparams() { return elemtype.allparams(); }
923 public boolean isErroneous() {
924 return elemtype.isErroneous();
925 }
927 public boolean isParameterized() {
928 return elemtype.isParameterized();
929 }
931 public boolean isRaw() {
932 return elemtype.isRaw();
933 }
935 public ArrayType makeVarargs() {
936 return new ArrayType(elemtype, tsym) {
937 @Override
938 public boolean isVarargs() {
939 return true;
940 }
941 };
942 }
944 public Type map(Mapping f) {
945 Type elemtype1 = f.apply(elemtype);
946 if (elemtype1 == elemtype) return this;
947 else return new ArrayType(elemtype1, tsym);
948 }
950 public boolean contains(Type elem) {
951 return elem == this || elemtype.contains(elem);
952 }
954 public void complete() {
955 elemtype.complete();
956 }
958 public Type getComponentType() {
959 return elemtype;
960 }
962 public TypeKind getKind() {
963 return TypeKind.ARRAY;
964 }
966 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
967 return v.visitArray(this, p);
968 }
969 }
971 public static class MethodType extends Type implements ExecutableType {
973 public List<Type> argtypes;
974 public Type restype;
975 public List<Type> thrown;
977 public MethodType(List<Type> argtypes,
978 Type restype,
979 List<Type> thrown,
980 TypeSymbol methodClass) {
981 super(METHOD, methodClass);
982 this.argtypes = argtypes;
983 this.restype = restype;
984 this.thrown = thrown;
985 }
987 @Override
988 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
989 return v.visitMethodType(this, s);
990 }
992 /** The Java source which this type represents.
993 *
994 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
995 * should be.
996 */
997 public String toString() {
998 return "(" + argtypes + ")" + restype;
999 }
1001 public List<Type> getParameterTypes() { return argtypes; }
1002 public Type getReturnType() { return restype; }
1003 public List<Type> getThrownTypes() { return thrown; }
1005 public boolean isErroneous() {
1006 return
1007 isErroneous(argtypes) ||
1008 restype != null && restype.isErroneous();
1009 }
1011 public Type map(Mapping f) {
1012 List<Type> argtypes1 = map(argtypes, f);
1013 Type restype1 = f.apply(restype);
1014 List<Type> thrown1 = map(thrown, f);
1015 if (argtypes1 == argtypes &&
1016 restype1 == restype &&
1017 thrown1 == thrown) return this;
1018 else return new MethodType(argtypes1, restype1, thrown1, tsym);
1019 }
1021 public boolean contains(Type elem) {
1022 return elem == this || contains(argtypes, elem) || restype.contains(elem);
1023 }
1025 public MethodType asMethodType() { return this; }
1027 public void complete() {
1028 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
1029 l.head.complete();
1030 restype.complete();
1031 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1032 l.head.complete();
1033 }
1035 public List<TypeVar> getTypeVariables() {
1036 return List.nil();
1037 }
1039 public TypeSymbol asElement() {
1040 return null;
1041 }
1043 public TypeKind getKind() {
1044 return TypeKind.EXECUTABLE;
1045 }
1047 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1048 return v.visitExecutable(this, p);
1049 }
1050 }
1052 public static class PackageType extends Type implements NoType {
1054 PackageType(TypeSymbol tsym) {
1055 super(PACKAGE, tsym);
1056 }
1058 @Override
1059 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1060 return v.visitPackageType(this, s);
1061 }
1063 public String toString() {
1064 return tsym.getQualifiedName().toString();
1065 }
1067 public TypeKind getKind() {
1068 return TypeKind.PACKAGE;
1069 }
1071 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1072 return v.visitNoType(this, p);
1073 }
1074 }
1076 public static class TypeVar extends Type implements TypeVariable {
1078 /** The upper bound of this type variable; set from outside.
1079 * Must be nonempty once it is set.
1080 * For a bound, `bound' is the bound type itself.
1081 * Multiple bounds are expressed as a single class type which has the
1082 * individual bounds as superclass, respectively interfaces.
1083 * The class type then has as `tsym' a compiler generated class `c',
1084 * which has a flag COMPOUND and whose owner is the type variable
1085 * itself. Furthermore, the erasure_field of the class
1086 * points to the first class or interface bound.
1087 */
1088 public Type bound = null;
1090 /** The lower bound of this type variable.
1091 * TypeVars don't normally have a lower bound, so it is normally set
1092 * to syms.botType.
1093 * Subtypes, such as CapturedType, may provide a different value.
1094 */
1095 public Type lower;
1097 public TypeVar(Name name, Symbol owner, Type lower) {
1098 super(TYPEVAR, null);
1099 tsym = new TypeSymbol(0, name, this, owner);
1100 this.lower = lower;
1101 }
1103 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
1104 super(TYPEVAR, tsym);
1105 this.bound = bound;
1106 this.lower = lower;
1107 }
1109 @Override
1110 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1111 return v.visitTypeVar(this, s);
1112 }
1114 @Override
1115 public Type getUpperBound() { return bound; }
1117 int rank_field = -1;
1119 @Override
1120 public Type getLowerBound() {
1121 return lower;
1122 }
1124 public TypeKind getKind() {
1125 return TypeKind.TYPEVAR;
1126 }
1128 public boolean isCaptured() {
1129 return false;
1130 }
1132 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1133 return v.visitTypeVariable(this, p);
1134 }
1135 }
1137 /** A captured type variable comes from wildcards which can have
1138 * both upper and lower bound. CapturedType extends TypeVar with
1139 * a lower bound.
1140 */
1141 public static class CapturedType extends TypeVar {
1143 public WildcardType wildcard;
1145 public CapturedType(Name name,
1146 Symbol owner,
1147 Type upper,
1148 Type lower,
1149 WildcardType wildcard) {
1150 super(name, owner, lower);
1151 this.lower = Assert.checkNonNull(lower);
1152 this.bound = upper;
1153 this.wildcard = wildcard;
1154 }
1156 @Override
1157 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1158 return v.visitCapturedType(this, s);
1159 }
1161 @Override
1162 public boolean isCaptured() {
1163 return true;
1164 }
1166 @Override
1167 public String toString() {
1168 return "capture#"
1169 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1170 + " of "
1171 + wildcard;
1172 }
1173 }
1175 public static abstract class DelegatedType extends Type {
1176 public Type qtype;
1177 public DelegatedType(TypeTag tag, Type qtype) {
1178 super(tag, qtype.tsym);
1179 this.qtype = qtype;
1180 }
1181 public String toString() { return qtype.toString(); }
1182 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1183 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1184 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1185 public Type getReturnType() { return qtype.getReturnType(); }
1186 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1187 public List<Type> allparams() { return qtype.allparams(); }
1188 public Type getUpperBound() { return qtype.getUpperBound(); }
1189 public boolean isErroneous() { return qtype.isErroneous(); }
1190 }
1192 /**
1193 * The type of a generic method type. It consists of a method type and
1194 * a list of method type-parameters that are used within the method
1195 * type.
1196 */
1197 public static class ForAll extends DelegatedType implements ExecutableType {
1198 public List<Type> tvars;
1200 public ForAll(List<Type> tvars, Type qtype) {
1201 super(FORALL, (MethodType)qtype);
1202 this.tvars = tvars;
1203 }
1205 @Override
1206 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1207 return v.visitForAll(this, s);
1208 }
1210 public String toString() {
1211 return "<" + tvars + ">" + qtype;
1212 }
1214 public List<Type> getTypeArguments() { return tvars; }
1216 public boolean isErroneous() {
1217 return qtype.isErroneous();
1218 }
1220 public Type map(Mapping f) {
1221 return f.apply(qtype);
1222 }
1224 public boolean contains(Type elem) {
1225 return qtype.contains(elem);
1226 }
1228 public MethodType asMethodType() {
1229 return (MethodType)qtype;
1230 }
1232 public void complete() {
1233 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1234 ((TypeVar)l.head).bound.complete();
1235 }
1236 qtype.complete();
1237 }
1239 public List<TypeVar> getTypeVariables() {
1240 return List.convert(TypeVar.class, getTypeArguments());
1241 }
1243 public TypeKind getKind() {
1244 return TypeKind.EXECUTABLE;
1245 }
1247 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1248 return v.visitExecutable(this, p);
1249 }
1250 }
1252 /** A class for inference variables, for use during method/diamond type
1253 * inference. An inference variable has upper/lower bounds and a set
1254 * of equality constraints. Such bounds are set during subtyping, type-containment,
1255 * type-equality checks, when the types being tested contain inference variables.
1256 * A change listener can be attached to an inference variable, to receive notifications
1257 * whenever the bounds of an inference variable change.
1258 */
1259 public static class UndetVar extends DelegatedType {
1261 /** Inference variable change listener. The listener method is called
1262 * whenever a change to the inference variable's bounds occurs
1263 */
1264 public interface UndetVarListener {
1265 /** called when some inference variable bounds (of given kinds ibs) change */
1266 void varChanged(UndetVar uv, Set<InferenceBound> ibs);
1267 }
1269 /**
1270 * Inference variable bound kinds
1271 */
1272 public enum InferenceBound {
1273 /** upper bounds */
1274 UPPER,
1275 /** lower bounds */
1276 LOWER,
1277 /** equality constraints */
1278 EQ;
1279 }
1281 /** inference variable bounds */
1282 private Map<InferenceBound, List<Type>> bounds;
1284 /** inference variable's inferred type (set from Infer.java) */
1285 public Type inst = null;
1287 /** inference variable's change listener */
1288 public UndetVarListener listener = null;
1290 @Override
1291 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1292 return v.visitUndetVar(this, s);
1293 }
1295 public UndetVar(TypeVar origin, Types types) {
1296 this(origin, types, true);
1297 }
1299 public UndetVar(TypeVar origin, Types types, boolean includeBounds) {
1300 super(UNDETVAR, origin);
1301 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
1302 bounds.put(InferenceBound.UPPER, includeBounds ? types.getBounds(origin) : List.<Type>nil());
1303 bounds.put(InferenceBound.LOWER, List.<Type>nil());
1304 bounds.put(InferenceBound.EQ, List.<Type>nil());
1305 }
1307 public String toString() {
1308 if (inst != null) return inst.toString();
1309 else return qtype + "?";
1310 }
1312 public Type baseType() {
1313 if (inst != null) return inst.baseType();
1314 else return this;
1315 }
1317 /** get all bounds of a given kind */
1318 public List<Type> getBounds(InferenceBound ib) {
1319 return bounds.get(ib);
1320 }
1322 /** add a bound of a given kind - this might trigger listener notification */
1323 public void addBound(InferenceBound ib, Type bound, Types types) {
1324 List<Type> prevBounds = bounds.get(ib);
1325 for (Type b : prevBounds) {
1326 if (types.isSameType(b, bound)) {
1327 return;
1328 }
1329 }
1330 bounds.put(ib, prevBounds.prepend(bound));
1331 notifyChange(EnumSet.of(ib));
1332 }
1334 /** replace types in all bounds - this might trigger listener notification */
1335 public void substBounds(List<Type> from, List<Type> to, Types types) {
1336 EnumSet<InferenceBound> changed = EnumSet.noneOf(InferenceBound.class);
1337 Map<InferenceBound, List<Type>> bounds2 = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
1338 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
1339 InferenceBound ib = _entry.getKey();
1340 List<Type> prevBounds = _entry.getValue();
1341 List<Type> newBounds = types.subst(prevBounds, from, to);
1342 bounds2.put(ib, newBounds);
1343 if (prevBounds != newBounds) {
1344 changed.add(ib);
1345 }
1346 }
1347 if (!changed.isEmpty()) {
1348 bounds = bounds2;
1349 notifyChange(changed);
1350 }
1351 }
1353 private void notifyChange(EnumSet<InferenceBound> ibs) {
1354 if (listener != null) {
1355 listener.varChanged(this, ibs);
1356 }
1357 }
1358 }
1360 /** Represents VOID or NONE.
1361 */
1362 static class JCNoType extends Type implements NoType {
1363 public JCNoType(TypeTag tag) {
1364 super(tag, null);
1365 }
1367 @Override
1368 public TypeKind getKind() {
1369 switch (tag) {
1370 case VOID: return TypeKind.VOID;
1371 case NONE: return TypeKind.NONE;
1372 default:
1373 throw new AssertionError("Unexpected tag: " + tag);
1374 }
1375 }
1377 @Override
1378 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1379 return v.visitNoType(this, p);
1380 }
1381 }
1383 static class BottomType extends Type implements NullType {
1384 public BottomType() {
1385 super(BOT, null);
1386 }
1388 @Override
1389 public TypeKind getKind() {
1390 return TypeKind.NULL;
1391 }
1393 @Override
1394 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1395 return v.visitNull(this, p);
1396 }
1398 @Override
1399 public Type constType(Object value) {
1400 return this;
1401 }
1403 @Override
1404 public String stringValue() {
1405 return "null";
1406 }
1407 }
1409 public static class ErrorType extends ClassType
1410 implements javax.lang.model.type.ErrorType {
1412 private Type originalType = null;
1414 public ErrorType(Type originalType, TypeSymbol tsym) {
1415 super(noType, List.<Type>nil(), null);
1416 tag = ERROR;
1417 this.tsym = tsym;
1418 this.originalType = (originalType == null ? noType : originalType);
1419 }
1421 public ErrorType(ClassSymbol c, Type originalType) {
1422 this(originalType, c);
1423 c.type = this;
1424 c.kind = ERR;
1425 c.members_field = new Scope.ErrorScope(c);
1426 }
1428 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1429 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1430 }
1432 @Override
1433 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1434 return v.visitErrorType(this, s);
1435 }
1437 public Type constType(Object constValue) { return this; }
1438 public Type getEnclosingType() { return this; }
1439 public Type getReturnType() { return this; }
1440 public Type asSub(Symbol sym) { return this; }
1441 public Type map(Mapping f) { return this; }
1443 public boolean isGenType(Type t) { return true; }
1444 public boolean isErroneous() { return true; }
1445 public boolean isCompound() { return false; }
1446 public boolean isInterface() { return false; }
1448 public List<Type> allparams() { return List.nil(); }
1449 public List<Type> getTypeArguments() { return List.nil(); }
1451 public TypeKind getKind() {
1452 return TypeKind.ERROR;
1453 }
1455 public Type getOriginalType() {
1456 return originalType;
1457 }
1459 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1460 return v.visitError(this, p);
1461 }
1462 }
1464 /**
1465 * A visitor for types. A visitor is used to implement operations
1466 * (or relations) on types. Most common operations on types are
1467 * binary relations and this interface is designed for binary
1468 * relations, that is, operations on the form
1469 * Type × S → R.
1470 * <!-- In plain text: Type x S -> R -->
1471 *
1472 * @param <R> the return type of the operation implemented by this
1473 * visitor; use Void if no return type is needed.
1474 * @param <S> the type of the second argument (the first being the
1475 * type itself) of the operation implemented by this visitor; use
1476 * Void if a second argument is not needed.
1477 */
1478 public interface Visitor<R,S> {
1479 R visitClassType(ClassType t, S s);
1480 R visitWildcardType(WildcardType t, S s);
1481 R visitArrayType(ArrayType t, S s);
1482 R visitMethodType(MethodType t, S s);
1483 R visitPackageType(PackageType t, S s);
1484 R visitTypeVar(TypeVar t, S s);
1485 R visitCapturedType(CapturedType t, S s);
1486 R visitForAll(ForAll t, S s);
1487 R visitUndetVar(UndetVar t, S s);
1488 R visitErrorType(ErrorType t, S s);
1489 R visitType(Type t, S s);
1490 }
1491 }