ErrorUtils.java 8.1 KB
Newer Older
1 2
package org.jetbrains.jet.lang.types;

A
Andrey Breslav 已提交
3
import org.jetbrains.annotations.NotNull;
A
Andrey Breslav 已提交
4
import org.jetbrains.jet.lang.descriptors.*;
A
Andrey Breslav 已提交
5
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
A
Andrey Breslav 已提交
6
import org.jetbrains.jet.lang.resolve.JetScope;
7

8
import java.util.*;
9 10 11 12

/**
 * @author abreslav
 */
A
rename  
Andrey Breslav 已提交
13
public class ErrorUtils {
A
Andrey Breslav 已提交
14 15

    private static final ModuleDescriptor ERROR_MODULE = new ModuleDescriptor("<ERROR MODULE>");
A
Andrey Breslav 已提交
16
    private static final JetScope ERROR_SCOPE = new JetScope() {
A
Andrey Breslav 已提交
17

A
Andrey Breslav 已提交
18
        @Override
A
Andrey Breslav 已提交
19
        public ClassifierDescriptor getClassifier(@NotNull String name) {
A
Andrey Breslav 已提交
20
            return ERROR_CLASS;
21 22 23
        }

        @Override
24
        public VariableDescriptor getVariable(@NotNull String name) {
A
Andrey Breslav 已提交
25
            return ERROR_PROPERTY;
26 27 28
        }

        @Override
29
        public NamespaceDescriptor getNamespace(@NotNull String name) {
A
Andrey Breslav 已提交
30
            return null; // TODO : review
A
Andrey Breslav 已提交
31 32 33 34
        }

        @NotNull
        @Override
A
Andrey Breslav 已提交
35
        public JetType getThisType() {
A
Andrey Breslav 已提交
36
            return createErrorType("<ERROR TYPE>");
A
Andrey Breslav 已提交
37
        }
38 39 40

        @NotNull
        @Override
41
        public FunctionGroup getFunctionGroup(@NotNull String name) {
A
Andrey Breslav 已提交
42
            return ERROR_FUNCTION_GROUP;
43
        }
44

45 46 47
        @NotNull
        @Override
        public DeclarationDescriptor getContainingDeclaration() {
A
Andrey Breslav 已提交
48
            return ERROR_MODULE;
49 50
        }

A
Andrey Breslav 已提交
51 52 53 54 55 56
        @NotNull
        @Override
        public Collection<DeclarationDescriptor> getDeclarationsByLabel(String labelName) {
            return Collections.emptyList();
        }

A
Andrey Breslav 已提交
57 58 59 60 61
        @Override
        public PropertyDescriptor getPropertyByFieldReference(@NotNull String fieldName) {
            return null; // TODO : review
        }

A
Andrey Breslav 已提交
62 63 64 65 66
        @Override
        public DeclarationDescriptor getDeclarationDescriptorForUnqualifiedThis() {
            return ERROR_CLASS; // TODO : review
        }

A
Andrey Breslav 已提交
67 68 69 70 71
        @Override
        public Collection<DeclarationDescriptor> getAllDescriptors() {
            return Collections.emptyList();
        }

A
Andrey Breslav 已提交
72
    };
73

A
Andrey Breslav 已提交
74 75 76 77 78 79 80 81 82 83 84
    private static final FunctionGroup ERROR_FUNCTION_GROUP = new FunctionGroup() {
        @NotNull
        @Override
        public String getName() {
            return "<ERROR FUNCTION>";
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
85 86 87 88 89 90

        @NotNull
        @Override
        public Set<FunctionDescriptor> getFunctionDescriptors() {
            return Collections.singleton(createErrorFunction(0, Collections.<JetType>emptyList()));
        }
A
Andrey Breslav 已提交
91 92
    };

A
Andrey Breslav 已提交
93
    private static final ClassDescriptorImpl ERROR_CLASS = new ClassDescriptorImpl(ERROR_MODULE, Collections.<AnnotationDescriptor>emptyList(), "<ERROR CLASS>") {
94 95
        @NotNull
        @Override
A
Andrey Breslav 已提交
96
        public FunctionGroup getConstructors() {
97 98 99
            return ERROR_FUNCTION_GROUP;
        }
    };
A
Andrey Breslav 已提交
100
    private static final ConstructorDescriptor ERROR_CONSTRUCTOR = new ConstructorDescriptorImpl(ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), true);
101 102 103 104
    static {
        ERROR_CLASS.initialize(
            true, Collections.<TypeParameterDescriptor>emptyList(), Collections.<JetType>emptyList(), getErrorScope(), ERROR_FUNCTION_GROUP, ERROR_CONSTRUCTOR);
    }
105 106 107 108 109

    private static JetScope getErrorScope() {
        return ERROR_SCOPE;
    }

A
Andrey Breslav 已提交
110
    private static final JetType ERROR_PROPERTY_TYPE = createErrorType("<ERROR PROPERTY TYPE>");
111
    private static final VariableDescriptor ERROR_PROPERTY = new PropertyDescriptor(
112
            ERROR_CLASS,
A
Andrey Breslav 已提交
113
            Collections.<AnnotationDescriptor>emptyList(),
114 115 116 117 118
            new MemberModifiers(false, false, false),
            true,
            null,
            "<ERROR PROPERTY>",
            ERROR_PROPERTY_TYPE, ERROR_PROPERTY_TYPE);
119

120
    private static FunctionDescriptor createErrorFunction(List<TypeParameterDescriptor> typeParameters, List<JetType> positionedValueArgumentTypes) {
A
Andrey Breslav 已提交
121
        FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), "<ERROR FUNCTION>");
122
        return functionDescriptor.initialize(
123
                null,
124 125
                typeParameters,
                getValueParameters(functionDescriptor, positionedValueArgumentTypes),
126 127
                createErrorType("<ERROR FUNCTION RETURN>"),
                MemberModifiers.DEFAULT_MODIFIERS
128
        );
129 130
    }

131
    public static FunctionDescriptor createErrorFunction(int typeParameterCount, List<JetType> positionedValueParameterTypes) {
A
Andrey Breslav 已提交
132
        return new FunctionDescriptorImpl(ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), "<ERROR FUNCTION>").initialize(
133
                null,
134 135
                Collections.<TypeParameterDescriptor>emptyList(), // TODO
                Collections.<ValueParameterDescriptor>emptyList(), // TODO
136 137
                createErrorType("<ERROR FUNCTION RETURN TYPE>"),
                MemberModifiers.DEFAULT_MODIFIERS
138 139 140
        );
    }

141
    private static final JetType ERROR_PARAMETER_TYPE = createErrorType("<ERROR VALUE_PARAMETER TYPE>");
142 143 144 145 146 147
    private static List<ValueParameterDescriptor> getValueParameters(FunctionDescriptor functionDescriptor, List<JetType> argumentTypes) {
        List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
        for (int i = 0, argumentTypesSize = argumentTypes.size(); i < argumentTypesSize; i++) {
            result.add(new ValueParameterDescriptorImpl(
                    functionDescriptor,
                    i,
A
Andrey Breslav 已提交
148
                    Collections.<AnnotationDescriptor>emptyList(),
149
                    "<ERROR VALUE_PARAMETER>",
150 151
                    ERROR_PARAMETER_TYPE,
                    ERROR_PARAMETER_TYPE,
152 153 154 155 156
                    false,
                    false));
        }
        return result;
    }
A
Andrey Breslav 已提交
157

A
Andrey Breslav 已提交
158
    @NotNull
A
Andrey Breslav 已提交
159
    public static JetType createErrorType(String debugMessage) {
A
Andrey Breslav 已提交
160
        return createErrorType(debugMessage, ERROR_SCOPE);
161 162
    }

A
Andrey Breslav 已提交
163
    private static JetType createErrorType(String debugMessage, JetScope memberScope) {
A
Andrey Breslav 已提交
164
        return new ErrorTypeImpl(new TypeConstructorImpl(ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), false, "[ERROR : " + debugMessage + "]", Collections.<TypeParameterDescriptor>emptyList(), Collections.singleton(JetStandardClasses.getAnyType())), memberScope);
165 166
    }

A
Andrey Breslav 已提交
167
    public static JetType createWrongVarianceErrorType(TypeProjection value) {
A
Andrey Breslav 已提交
168
        return createErrorType(value + " is not allowed here]", value.getType().getMemberScope());
169 170
    }

171 172 173 174
    public static ClassifierDescriptor getErrorClass() {
        return ERROR_CLASS;
    }

A
Andrey Breslav 已提交
175 176 177 178
    public static boolean isError(@NotNull TypeConstructor typeConstructor) {
        return typeConstructor == ERROR_CLASS.getTypeConstructor();
    }

179
    public static boolean isErrorType(@NotNull JetType type) {
A
Andrey Breslav 已提交
180 181 182 183
        return type != JetTypeInferrer.NO_EXPECTED_TYPE &&(
               (type instanceof DeferredType && ((DeferredType) type).getActualType() == null) ||
               type instanceof ErrorTypeImpl ||
               isError(type.getConstructor()));
184 185
    }

A
Andrey Breslav 已提交
186
    public static boolean isError(@NotNull DeclarationDescriptor candidate) {
187 188 189
        return candidate.getContainingDeclaration() == getErrorClass();
    }

A
Andrey Breslav 已提交
190
    private static class ErrorTypeImpl implements JetType {
191 192 193

        private final TypeConstructor constructor;

194
        private final JetScope memberScope;
A
Andrey Breslav 已提交
195
        private ErrorTypeImpl(TypeConstructor constructor, JetScope memberScope) {
196
            this.constructor = constructor;
A
Andrey Breslav 已提交
197
            this.memberScope = memberScope;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        }

        @NotNull
        @Override
        public TypeConstructor getConstructor() {
            return constructor;
        }

        @NotNull
        @Override
        public List<TypeProjection> getArguments() {
            return Collections.emptyList();
        }

        @Override
        public boolean isNullable() {
            return false;
        }

        @NotNull
        @Override
A
Andrey Breslav 已提交
219 220
        public JetScope getMemberScope() {
            return memberScope;
221 222 223
        }

        @Override
A
Andrey Breslav 已提交
224
        public List<AnnotationDescriptor> getAnnotations() {
225 226
            return Collections.emptyList();
        }
227

228 229 230 231
        @Override
        public String toString() {
            return constructor.toString();
        }
232
    }
233

A
rename  
Andrey Breslav 已提交
234
    private ErrorUtils() {}
235
}