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