JetStandardClasses.java 11.7 KB
Newer Older
A
Andrey Breslav 已提交
1 2
package org.jetbrains.jet.lang.types;

A
Andrey Breslav 已提交
3
import org.jetbrains.annotations.NotNull;
A
Andrey Breslav 已提交
4
import org.jetbrains.annotations.Nullable;
5
import org.jetbrains.jet.lang.ErrorHandler;
6
import org.jetbrains.jet.lang.resolve.JetScope;
7
import org.jetbrains.jet.lang.resolve.WritableScope;
A
Andrey Breslav 已提交
8

9 10
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
A
Andrey Breslav 已提交
11
import java.util.*;
A
Andrey Breslav 已提交
12 13 14 15 16

/**
 * @author abreslav
 */
public class JetStandardClasses {
A
Andrey Breslav 已提交
17

18 19 20 21 22
    private JetStandardClasses() {
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

23 24 25 26
    private static NamespaceDescriptor STANDARD_CLASSES_NAMESPACE = new NamespaceDescriptor(null, Collections.<Attribute>emptyList(), "jet");

    private static final ClassDescriptor NOTHING_CLASS = new ClassDescriptorImpl(
            STANDARD_CLASSES_NAMESPACE,
A
Andrey Breslav 已提交
27
            Collections.<Attribute>emptyList(),
28
            "Nothing").initialize(
29
            true,
A
Andrey Breslav 已提交
30
            Collections.<TypeParameterDescriptor>emptyList(),
A
Andrey Breslav 已提交
31
            new AbstractCollection<JetType>() {
A
Andrey Breslav 已提交
32 33
                @Override
                public boolean contains(Object o) {
A
Andrey Breslav 已提交
34
                    return o instanceof JetType;
A
Andrey Breslav 已提交
35 36 37
                }

                @Override
A
Andrey Breslav 已提交
38
                public Iterator<JetType> iterator() {
A
Andrey Breslav 已提交
39 40 41 42 43 44 45
                    throw new UnsupportedOperationException();
                }

                @Override
                public int size() {
                    throw new UnsupportedOperationException();
                }
A
Andrey Breslav 已提交
46
            }, JetScope.EMPTY
A
Andrey Breslav 已提交
47
    );
A
Andrey Breslav 已提交
48
    private static final JetType NOTHING_TYPE = new JetTypeImpl(getNothing());
49

A
Andrey Breslav 已提交
50
    private static final JetType NULLABLE_NOTHING_TYPE = new JetTypeImpl(
51 52 53 54 55 56 57 58
            Collections.<Attribute>emptyList(),
            getNothing().getTypeConstructor(),
            true,
            Collections.<TypeProjection>emptyList(),
            JetScope.EMPTY);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

59
    private static final ClassDescriptor ANY = new ClassDescriptorImpl(
60
            STANDARD_CLASSES_NAMESPACE,
61
            Collections.<Attribute>emptyList(),
62
            "Any").initialize(
63
            false,
A
Andrey Breslav 已提交
64
            Collections.<TypeParameterDescriptor>emptyList(),
A
Andrey Breslav 已提交
65
            Collections.<JetType>emptySet(),
A
Andrey Breslav 已提交
66
            JetScope.EMPTY
A
Andrey Breslav 已提交
67
    );
A
Andrey Breslav 已提交
68
    private static final JetType ANY_TYPE = new JetTypeImpl(ANY.getTypeConstructor(), JetScope.EMPTY);
69

A
Andrey Breslav 已提交
70
    private static final JetType NULLABLE_ANY_TYPE = TypeUtils.makeNullable(ANY_TYPE);
71

72
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73

74
    public static final JetScope STUB = JetScope.EMPTY;
75

76
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
A
Andrey Breslav 已提交
77 78 79

    public static final int TUPLE_COUNT = 22;
    private static final ClassDescriptor[] TUPLE = new ClassDescriptor[TUPLE_COUNT];
80

A
Andrey Breslav 已提交
81 82 83
    static {
        for (int i = 0; i < TUPLE_COUNT; i++) {
            List<TypeParameterDescriptor> parameters = new ArrayList<TypeParameterDescriptor>();
84 85 86 87
            ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl(
                    STANDARD_CLASSES_NAMESPACE,
                    Collections.<Attribute>emptyList(),
                    "Tuple" + i);
A
Andrey Breslav 已提交
88 89
            for (int j = 0; j < i; j++) {
                parameters.add(new TypeParameterDescriptor(
90
                        classDescriptor,
91
                        Collections.<Attribute>emptyList(),
A
Andrey Breslav 已提交
92
                        Variance.OUT_VARIANCE, "T" + j,
A
Andrey Breslav 已提交
93
                        Collections.singleton(getNullableAnyType())));
A
Andrey Breslav 已提交
94
            }
95
            TUPLE[i] = classDescriptor.initialize(
96
                    true,
A
Andrey Breslav 已提交
97
                    parameters,
98
                    Collections.singleton(getAnyType()), STUB);
A
Andrey Breslav 已提交
99 100
        }
    }
101 102 103

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

A
Andrey Breslav 已提交
104 105
    public static final int FUNCTION_COUNT = 22;
    private static final ClassDescriptor[] FUNCTION = new ClassDescriptor[FUNCTION_COUNT];
106

A
Andrey Breslav 已提交
107
    private static final ClassDescriptor[] RECEIVER_FUNCTION = new ClassDescriptor[FUNCTION_COUNT];
108

A
Andrey Breslav 已提交
109 110
    static {
        for (int i = 0; i < FUNCTION_COUNT; i++) {
111 112
            ClassDescriptorImpl function = new ClassDescriptorImpl(
                    STANDARD_CLASSES_NAMESPACE,
A
Andrey Breslav 已提交
113
                    Collections.<Attribute>emptyList(),
114 115
                    "Function" + i);
            FUNCTION[i] = function.initialize(
A
Andrey Breslav 已提交
116
                    false,
117
                    createTypeParameters(i, function),
118
                    Collections.singleton(getAnyType()), STUB);
119 120 121

            ClassDescriptorImpl receiverFunction = new ClassDescriptorImpl(
                    STANDARD_CLASSES_NAMESPACE,
122
                    Collections.<Attribute>emptyList(),
123 124 125 126
                    "ReceiverFunction" + i);
            List<TypeParameterDescriptor> parameters = createTypeParameters(i, receiverFunction);
            parameters.add(0, new TypeParameterDescriptor(
                    receiverFunction,
A
Andrey Breslav 已提交
127
                    Collections.<Attribute>emptyList(),
128 129 130
                    Variance.IN_VARIANCE, "T",
                    Collections.singleton(getNullableAnyType())));
            RECEIVER_FUNCTION[i] = receiverFunction.initialize(
A
Andrey Breslav 已提交
131 132
                    false,
                    parameters,
133
                    Collections.singleton(getAnyType()), STUB);
A
Andrey Breslav 已提交
134 135 136
        }
    }

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    private static List<TypeParameterDescriptor> createTypeParameters(int parameterCount, ClassDescriptorImpl function) {
        List<TypeParameterDescriptor> parameters = new ArrayList<TypeParameterDescriptor>();
        for (int j = 0; j < parameterCount; j++) {
            parameters.add(new TypeParameterDescriptor(
                    function,
                    Collections.<Attribute>emptyList(),
                    Variance.IN_VARIANCE, "P" + j,
                    Collections.singleton(getNullableAnyType())));
        }
        parameters.add(new TypeParameterDescriptor(
                function,
                Collections.<Attribute>emptyList(),
                Variance.OUT_VARIANCE, "R",
                Collections.singleton(getNullableAnyType())));
        return parameters;
    }

154 155
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

A
Andrey Breslav 已提交
156
    private static final JetType UNIT_TYPE = new JetTypeImpl(getTuple(0));
A
Andrey Breslav 已提交
157

158
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
A
Andrey Breslav 已提交
159

160
    @NotNull
161
    /*package*/ static final JetScope STANDARD_CLASSES;
162

163
    static {
164
        WritableScope writableScope = new WritableScope(JetScope.EMPTY, STANDARD_CLASSES_NAMESPACE, ErrorHandler.DO_NOTHING);
165
        STANDARD_CLASSES = writableScope;
166 167
        writableScope.addClassAlias("Unit", getTuple(0));

168 169 170 171 172 173 174 175 176
        Field[] declaredFields = JetStandardClasses.class.getDeclaredFields();
        for (Field field : declaredFields) {
            if ((field.getModifiers() & Modifier.STATIC) == 0) {
                continue;
            }
            Class<?> type = field.getType();
            if (type == ClassDescriptor.class) {
                try {
                    ClassDescriptor descriptor = (ClassDescriptor) field.get(null);
177
                    writableScope.addClassDescriptor(descriptor);
178 179 180 181 182 183 184
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            } else if (type.isArray() && type.getComponentType() == ClassDescriptor.class) {
                try {
                    ClassDescriptor[] array = (ClassDescriptor[]) field.get(null);
                    for (ClassDescriptor descriptor : array) {
185
                        writableScope.addClassDescriptor(descriptor);
186 187 188 189 190 191 192 193
                    }
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        }
    }

194 195
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

A
Andrey Breslav 已提交
196
    @NotNull
A
Andrey Breslav 已提交
197 198 199 200
    public static ClassDescriptor getAny() {
        return ANY;
    }

A
Andrey Breslav 已提交
201
    @NotNull
A
Andrey Breslav 已提交
202
    public static JetType getAnyType() {
203 204 205
        return ANY_TYPE;
    }

A
Andrey Breslav 已提交
206
    public static JetType getNullableAnyType() {
207 208 209
        return NULLABLE_ANY_TYPE;
    }

A
Andrey Breslav 已提交
210 211 212 213 214
    @NotNull
    public static ClassDescriptor getNothing() {
        return NOTHING_CLASS;
    }

A
Andrey Breslav 已提交
215 216 217 218 219
    @NotNull
    public static ClassDescriptor getTuple(int size) {
        return TUPLE[size];
    }

220 221 222 223 224 225 226 227 228 229
    @NotNull
    public static ClassDescriptor getFunction(int parameterCount) {
        return FUNCTION[parameterCount];
    }

    @NotNull
    public static ClassDescriptor getReceiverFunction(int parameterCount) {
        return RECEIVER_FUNCTION[parameterCount];
    }

A
Andrey Breslav 已提交
230
    public static JetType getUnitType() {
A
Andrey Breslav 已提交
231 232 233
        return UNIT_TYPE;
    }

A
Andrey Breslav 已提交
234
    public static JetType getNothingType() {
A
Andrey Breslav 已提交
235 236 237
        return NOTHING_TYPE;
    }

A
Andrey Breslav 已提交
238
    public static JetType getNullableNothingType() {
A
Andrey Breslav 已提交
239 240 241
        return NULLABLE_NOTHING_TYPE;
    }

A
Andrey Breslav 已提交
242
    public static boolean isNothing(JetType type) {
243 244 245
        return type.getConstructor() == NOTHING_CLASS.getTypeConstructor();
    }

A
Andrey Breslav 已提交
246
    public static JetType getTupleType(List<Attribute> attributes, List<JetType> arguments) {
A
Andrey Breslav 已提交
247 248 249
        if (attributes.isEmpty() && arguments.isEmpty()) {
            return getUnitType();
        }
A
Andrey Breslav 已提交
250
        return new JetTypeImpl(attributes, getTuple(arguments.size()).getTypeConstructor(), false, toProjections(arguments), STUB);
A
Andrey Breslav 已提交
251 252
    }

A
Andrey Breslav 已提交
253
    public static JetType getTupleType(List<JetType> arguments) {
A
Andrey Breslav 已提交
254 255 256
        return getTupleType(Collections.<Attribute>emptyList(), arguments);
    }

A
Andrey Breslav 已提交
257
    public static JetType getTupleType(JetType... arguments) {
A
Andrey Breslav 已提交
258 259 260
        return getTupleType(Collections.<Attribute>emptyList(), Arrays.asList(arguments));
    }

A
Andrey Breslav 已提交
261
    public static JetType getLabeledTupleType(List<Attribute> attributes, List<ValueParameterDescriptor> arguments) {
A
Andrey Breslav 已提交
262 263 264 265
        // TODO
        return getTupleType(attributes, toTypes(arguments));
    }

A
Andrey Breslav 已提交
266
    public static JetType getLabeledTupleType(List<ValueParameterDescriptor> arguments) {
A
Andrey Breslav 已提交
267
        // TODO
A
Andrey Breslav 已提交
268 269 270
        return getLabeledTupleType(Collections.<Attribute>emptyList(), arguments);
    }

A
Andrey Breslav 已提交
271
    private static List<TypeProjection> toProjections(List<JetType> arguments) {
A
Andrey Breslav 已提交
272
        List<TypeProjection> result = new ArrayList<TypeProjection>();
A
Andrey Breslav 已提交
273
        for (JetType argument : arguments) {
274
            result.add(new TypeProjection(Variance.OUT_VARIANCE, argument));
A
Andrey Breslav 已提交
275 276 277 278
        }
        return result;
    }

A
Andrey Breslav 已提交
279 280
    private static List<JetType> toTypes(List<ValueParameterDescriptor> labeledEntries) {
        List<JetType> result = new ArrayList<JetType>();
281
        for (ValueParameterDescriptor entry : labeledEntries) {
A
Andrey Breslav 已提交
282 283 284
            result.add(entry.getType());
        }
        return result;
A
Andrey Breslav 已提交
285
    }
A
Andrey Breslav 已提交
286 287

    // TODO : labeled version?
A
Andrey Breslav 已提交
288
    public static JetType getFunctionType(List<Attribute> attributes, @Nullable JetType receiverType, @NotNull List<JetType> parameterTypes, @NotNull JetType returnType) {
A
Andrey Breslav 已提交
289 290 291 292
        List<TypeProjection> arguments = new ArrayList<TypeProjection>();
        if (receiverType != null) {
            arguments.add(defaultProjection(receiverType));
        }
A
Andrey Breslav 已提交
293
        for (JetType parameterType : parameterTypes) {
A
Andrey Breslav 已提交
294 295 296 297 298
            arguments.add(defaultProjection(parameterType));
        }
        arguments.add(defaultProjection(returnType));
        int size = parameterTypes.size();
        TypeConstructor constructor = receiverType == null ? FUNCTION[size].getTypeConstructor() : RECEIVER_FUNCTION[size].getTypeConstructor();
A
Andrey Breslav 已提交
299
        return new JetTypeImpl(attributes, constructor, false, arguments, STUB);
A
Andrey Breslav 已提交
300 301
    }

A
Andrey Breslav 已提交
302
    private static TypeProjection defaultProjection(JetType returnType) {
A
Andrey Breslav 已提交
303 304
        return new TypeProjection(Variance.INVARIANT, returnType);
    }
A
Andrey Breslav 已提交
305
}