summaryrefslogtreecommitdiff
path: root/sources/scala/runtime/types/ScalaClassType.java
blob: 861abd0c51fcabcc83f876146f8f2861caf30ab6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003, LAMP/EPFL                  **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$

package scala.runtime.types;

import scala.Type;
import scala.Array;
import scala.ScalaObject;
import scala.runtime.RunTime;
import scala.runtime.FNV_Hash;
import scala.runtime.PearsonHash;

public class ScalaClassType extends ClassType {
    public static final ScalaClassType[] EMPTY_ARRAY =
        new ScalaClassType[0];

    private static final ScalaClassType[][] EMPTY_ANCESTORS =
        new ScalaClassType[0][];

    private final TypeConstructor constr;
    private final Type[] inst;

    private ScalaClassType[] parents;
    private ScalaClassType[][] ancestors = null;

    private final int hashCode;

    public ScalaClassType(TypeConstructor constr,
                          Type[] inst,
                          ScalaClassType[] parents) {
        super(constr.clazz, constr.isTrivial);

        this.constr = constr;
        this.inst = inst;

        int hash = FNV_Hash.hashStep(FNV_Hash.INIT,
                                     PearsonHash.hash8(constr.hashCode()));
        for (int i = 0; i < inst.length; ++i) {
            hash = FNV_Hash.hashStep(hash,
                                     PearsonHash.hash8(inst[i].hashCode()));
        }
        this.hashCode = hash;
        this.parents = parents;
        this.ancestors = constr.isStronglyTrivial ? EMPTY_ANCESTORS : null;
    }

    public boolean isInstance(Object o) {
        return super.isInstance(o)
            && (isTrivial
                || ((ScalaObject)o).getScalaType().isNonTrivialSubClassType(this));
    }

    public boolean isNonTrivialInstance(Object o) {
        assert Statistics.incWeakInstanceOf();
        return ((ScalaObject)o).getScalaType().isNonTrivialSubClassType(this);
    }

    protected boolean isSubClassType(ClassType that) {
        return (this == that)
            || (super.isSubClassType(that)
                && (that.isTrivial
                    || isNonTrivialSubClassType((ScalaClassType)that)));
    }

    public boolean isNonTrivialSubClassType(ClassType that) {
        ScalaClassType thatCT = (ScalaClassType)that;
        ScalaClassType parentCT = myInstantiationFor(thatCT);

        // At this stage, if parentCT is null, it means that the
        // constructors had different prefixes, hence we return false.
        return (parentCT != null)
            && (parentCT == thatCT || parentCT.hasSubInstantiation(thatCT));
    }

    // Return true iff the instantiation of THIS is "smaller" than the
    // one of THAT.
    private boolean hasSubInstantiation(ScalaClassType that) {
        assert this.constr == that.constr;

        final Type[] thisInst = this.inst;
        final Type[] thatInst = that.inst;

        int i = 0;

        // invariant parameters
        final int firstM = this.constr.zCount;
        while (i < firstM) {
            Type thisTp = thisInst[i], thatTp = thatInst[i];
            if (!(thisTp == thatTp || thisTp.isSameType(thatTp)))
                return false;
            ++i;
        }
        // contravariant parameters
        final int firstP = firstM + this.constr.mCount;
        while (i < firstP) {
            Type thisTp = thisInst[i], thatTp = thatInst[i];
            if (!(thisTp == thatTp || thatTp.isSubType(thisTp)))
                return false;
            ++i;
        }
        // covariant parameters
        final int firstOutside = firstP + this.constr.pCount;
        while (i < firstOutside) {
            Type thisTp = thisInst[i], thatTp = thatInst[i];
            if (!(thisTp == thatTp || thisTp.isSubType(thatTp)))
                return false;
            ++i;
        }
        return true;
    }

    public boolean isSameType(Type that) {
        return this == that;
    }

    private ScalaClassType myInstantiationFor(ScalaClassType that) {
        // Find our instantiation for the other type, if any.
        ScalaClassType[] thisSlice = getAncestors()[that.constr.level];

        for (int i = 0; i < thisSlice.length; ++i) {
            if (thisSlice[i].constr == that.constr) {
                assert Statistics.addAncestorSearchIterations(i + 1);
                return thisSlice[i];
            }
        }
        assert Statistics.addAncestorSearchIterations(thisSlice.length);

        return null;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();

        int firstM = constr.zCount;
        int firstP = firstM + constr.mCount;
        buf.append(constr);
        if (inst.length > 0) {
            buf.append("[");
            for (int i = 0; i < inst.length; ++i) {
                if (i > 0) buf.append(", ");
                if (i >= firstP)
                    buf.append('+');
                else if (i >= firstM)
                    buf.append('-');
                buf.append(inst[i]);
            }
            buf.append("]");
        }
        return buf.toString();
    }

    public int hashCode() {
        return hashCode;
    }

    public ScalaClassType setParents(ScalaClassType[] parents) {
        assert this.parents == null || Type.isSameType(this.parents, parents);
        this.parents = parents;
        // TODO notifyAll?
        return this;
    }

    public ScalaClassType[] getParents() {
        int timeout = 1;
        while (parents == null) {
            try {
                wait(timeout);
            } catch (InterruptedException e) {
                throw new Error(e);
            }
            timeout *= 2;
            if (timeout >= 1000)
                throw new Error("computation of parents apparently stuck for "
                                + this);
        }
        return parents;
    }

    private ScalaClassType[][] getAncestors() {
        if (ancestors == null)
            computeAncestors();
        return ancestors;
    }

    private void computeAncestors() {
        final int level = constr.level;
        final int ancestorDepth = constr.ancestorCacheDepth;
        final int[] ancestorCode = constr.ancestorCode;
        ScalaClassType[] parents = getParents();

        ancestors = new ScalaClassType[ancestorDepth][];
        ScalaClassType[][] initialAncestors = parents.length > 0
            ? parents[0].getAncestors()
            : EMPTY_ANCESTORS;

        for (int l = 0, dci = 0; l < ancestorDepth; ++l) {
            int toAddParents = 0;
            if (dci < ancestorCode.length && ancestorCode[dci] == l) {
                dci++;
                toAddParents = ancestorCode[dci++];
            }
            int toAddSelf = (l == level) && (!constr.isTrivial) ? 1 : 0;
            int toAdd = toAddParents + toAddSelf;
            ScalaClassType[] initialRow;

            if (l < initialAncestors.length)
                initialRow = initialAncestors[l];
            else
                initialRow = ScalaClassType.EMPTY_ARRAY;

            if (toAdd == 0) {
                ancestors[l] = initialRow;
            } else {
                int initialLen = initialRow.length;
                ScalaClassType[] newRow =
                    new ScalaClassType[initialLen + toAdd];

                if (toAddSelf == 1)
                    newRow[0] = this;

                System.arraycopy(initialRow, 0, newRow, toAddSelf, initialLen);
                for (int i = 0; i < toAddParents; ++i) {
                    int p = ancestorCode[dci++];
                    int o = ancestorCode[dci++];
                    newRow[toAddSelf + initialLen + i] =
                        parents[p].getAncestors()[l][o];
                }
                ancestors[l] = newRow;
            }
        }
    }
}