JavaDescriptorResolver.java 27.4 KB
Newer Older
1 2
package org.jetbrains.jet.lang.resolve.java;

3
import com.google.common.collect.Lists;
A
Andrey Breslav 已提交
4
import com.google.common.collect.Maps;
A
Andrey Breslav 已提交
5
import com.google.common.collect.Sets;
6
import com.intellij.openapi.project.Project;
7
import com.intellij.openapi.vfs.VirtualFile;
8
import com.intellij.psi.*;
9
import com.intellij.psi.search.DelegatingGlobalSearchScope;
10 11 12
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
A
Andrey Breslav 已提交
13
import org.jetbrains.jet.lang.descriptors.*;
A
Andrey Breslav 已提交
14
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
15
import org.jetbrains.jet.lang.resolve.BindingContext;
16
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
S
Stepan Koltsov 已提交
17
import org.jetbrains.jet.lang.resolve.StdlibNames;
18
import org.jetbrains.jet.lang.types.*;
19
import org.jetbrains.jet.plugin.JetFileType;
20 21 22 23 24 25 26

import java.util.*;

/**
 * @author abreslav
 */
public class JavaDescriptorResolver {
27 28
    
    public static String JAVA_ROOT = "<java_root>";
29

30 31
    /*package*/ static final DeclarationDescriptor JAVA_METHOD_TYPE_PARAMETER_PARENT = new DeclarationDescriptorImpl(null, Collections.<AnnotationDescriptor>emptyList(), "<java_generic_method>") {

32 33
        @Override
        public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
34
            throw new UnsupportedOperationException();
35 36
        }

37 38
        @Override
        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
39
            return visitor.visitDeclarationDescriptor(this, data);
40 41 42
        }
    };

A
Andrey Breslav 已提交
43
    /*package*/ static final DeclarationDescriptor JAVA_CLASS_OBJECT = new DeclarationDescriptorImpl(null, Collections.<AnnotationDescriptor>emptyList(), "<java_class_object_emulation>") {
A
Andrey Breslav 已提交
44 45 46
        @NotNull
        @Override
        public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
47
            throw new UnsupportedOperationException();
A
Andrey Breslav 已提交
48 49 50 51 52 53 54 55
        }

        @Override
        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
            return visitor.visitDeclarationDescriptor(this, data);
        }
    };

56
    protected final Map<String, ClassDescriptor> classDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
57
    protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
58
    protected final Map<PsiMethod, FunctionDescriptor> methodDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
59
    protected final Map<PsiField, VariableDescriptor> fieldDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
60
    protected final Map<PsiElement, NamespaceDescriptor> namespaceDescriptorCache = Maps.newHashMap();
61 62 63 64 65 66
    protected final JavaPsiFacade javaFacade;
    protected final GlobalSearchScope javaSearchScope;
    protected final JavaSemanticServices semanticServices;

    public JavaDescriptorResolver(Project project, JavaSemanticServices semanticServices) {
        this.javaFacade = JavaPsiFacade.getInstance(project);
67 68 69 70 71 72
        this.javaSearchScope = new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(project)) {
            @Override
            public boolean contains(VirtualFile file) {
                return myBaseScope.contains(file) && file.getFileType() != JetFileType.INSTANCE;
            }
        };
73 74 75 76 77 78
        this.semanticServices = semanticServices;
    }

    @NotNull
    public ClassDescriptor resolveClass(@NotNull PsiClass psiClass) {
        String qualifiedName = psiClass.getQualifiedName();
79 80 81 82 83 84

        ClassDescriptor kotlinClassDescriptor = semanticServices.getKotlinClassDescriptor(qualifiedName);
        if (kotlinClassDescriptor != null) {
            return kotlinClassDescriptor;
        }

85 86 87 88 89 90 91 92 93 94
        ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
        if (classDescriptor == null) {
            classDescriptor = createJavaClassDescriptor(psiClass);
            classDescriptorCache.put(qualifiedName, classDescriptor);
        }
        return classDescriptor;
    }

    @Nullable
    public ClassDescriptor resolveClass(@NotNull String qualifiedName) {
95 96 97 98 99
        ClassDescriptor kotlinClassDescriptor = semanticServices.getKotlinClassDescriptor(qualifiedName);
        if (kotlinClassDescriptor != null) {
            return kotlinClassDescriptor;
        }

100 101
        ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
        if (classDescriptor == null) {
102
            PsiClass psiClass = findClass(qualifiedName);
103 104 105 106 107 108 109 110
            if (psiClass == null) {
                return null;
            }
            classDescriptor = createJavaClassDescriptor(psiClass);
        }
        return classDescriptor;
    }

111
    private ClassDescriptor createJavaClassDescriptor(@NotNull final PsiClass psiClass) {
A
Andrey Breslav 已提交
112 113 114
        assert !classDescriptorCache.containsKey(psiClass.getQualifiedName()) : psiClass.getQualifiedName();
        classDescriptorCache.put(psiClass.getQualifiedName(), null); // TODO

115
        String name = psiClass.getName();
116
        JavaClassDescriptor classDescriptor = new JavaClassDescriptor(
117
                resolveParentDescriptor(psiClass), psiClass.isInterface() ? ClassKind.TRAIT : ClassKind.CLASS
118
        );
A
Andrey Breslav 已提交
119
        classDescriptor.setName(name);
A
Andrey Breslav 已提交
120

A
Andrey Breslav 已提交
121
        List<JetType> supertypes = new ArrayList<JetType>();
122
        List<TypeParameterDescriptor> typeParameters = makeUninitializedTypeParameters(classDescriptor, psiClass.getTypeParameters());
A
Andrey Breslav 已提交
123 124
        classDescriptor.setTypeConstructor(new TypeConstructorImpl(
                classDescriptor,
A
Andrey Breslav 已提交
125
                Collections.<AnnotationDescriptor>emptyList(), // TODO
126
                // TODO
127
                psiClass.hasModifierProperty(PsiModifier.FINAL),
A
Andrey Breslav 已提交
128
                name,
A
Andrey Breslav 已提交
129
                typeParameters,
A
Andrey Breslav 已提交
130 131
                supertypes
        ));
132 133 134
        classDescriptor.setModality(Modality.convertFromFlags(
                psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
                !psiClass.hasModifierProperty(PsiModifier.FINAL))
A
Andrey Breslav 已提交
135
        );
136
        classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
A
Andrey Breslav 已提交
137
        classDescriptorCache.put(psiClass.getQualifiedName(), classDescriptor);
138
        classDescriptor.setUnsubstitutedMemberScope(new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, false));
139

A
Andrey Breslav 已提交
140
        // UGLY HACK
141 142
        initializeTypeParameters(psiClass);

A
Andrey Breslav 已提交
143
        supertypes.addAll(getSupertypes(psiClass));
144 145 146 147 148 149 150 151 152 153 154
        if (psiClass.isInterface()) {
            classDescriptor.setSuperclassType(JetStandardClasses.getAnyType()); // TODO : Make it java.lang.Object
        }
        else {
            PsiClassType[] extendsListTypes = psiClass.getExtendsListTypes();
            assert extendsListTypes.length == 0 || extendsListTypes.length == 1;
            JetType superclassType = extendsListTypes.length == 0
                                            ? JetStandardClasses.getAnyType()
                                            : semanticServices.getTypeTransformer().transformToType(extendsListTypes[0]);
            classDescriptor.setSuperclassType(superclassType);
        }
A
Andrey Breslav 已提交
155 156

        PsiMethod[] psiConstructors = psiClass.getConstructors();
157 158 159 160 161

        if (psiConstructors.length == 0) {
            if (!psiClass.hasModifierProperty(PsiModifier.ABSTRACT) && !psiClass.isInterface()) {
                ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
                        classDescriptor,
A
Andrey Breslav 已提交
162
                        Collections.<AnnotationDescriptor>emptyList(),
163
                        false);
164
                constructorDescriptor.initialize(typeParameters, Collections.<ValueParameterDescriptor>emptyList(), Modality.FINAL, classDescriptor.getVisibility());
165
                constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
166
                classDescriptor.addConstructor(constructorDescriptor);
167
                semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
168 169 170 171 172 173
            }
        }
        else {
            for (PsiMethod constructor : psiConstructors) {
                ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
                        classDescriptor,
A
Andrey Breslav 已提交
174
                        Collections.<AnnotationDescriptor>emptyList(), // TODO
175
                        false);
176
                constructorDescriptor.initialize(typeParameters, resolveParameterDescriptors(constructorDescriptor, constructor.getParameterList().getParameters()), Modality.FINAL,
177
                                                 resolveVisibilityFromPsiModifiers(constructor));
178
                constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
179
                classDescriptor.addConstructor(constructorDescriptor);
180
                semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, constructor, constructorDescriptor);
181
            }
A
Andrey Breslav 已提交
182
        }
183

184
        semanticServices.getTrace().record(BindingContext.CLASS, psiClass, classDescriptor);
185

186 187 188
        return classDescriptor;
    }

189 190 191
    private DeclarationDescriptor resolveParentDescriptor(PsiClass psiClass) {
        PsiClass containingClass = psiClass.getContainingClass();
        if (containingClass != null) {
192
            return resolveClass(containingClass);
193 194 195 196 197 198 199
        }
        
        PsiJavaFile containingFile = (PsiJavaFile) psiClass.getContainingFile();
        String packageName = containingFile.getPackageName();
        return resolveNamespace(packageName);
    }

200
    private List<TypeParameterDescriptor> makeUninitializedTypeParameters(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter[] typeParameters) {
201 202
        List<TypeParameterDescriptor> result = Lists.newArrayList();
        for (PsiTypeParameter typeParameter : typeParameters) {
203
            TypeParameterDescriptor typeParameterDescriptor = makeUninitializedTypeParameter(containingDeclaration, typeParameter);
A
Andrey Breslav 已提交
204
            result.add(typeParameterDescriptor);
205 206 207 208
        }
        return result;
    }

209 210 211
    @NotNull
    private TypeParameterDescriptor makeUninitializedTypeParameter(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter psiTypeParameter) {
        assert typeParameterDescriptorCache.get(psiTypeParameter) == null : psiTypeParameter.getText();
212
        TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification(
213
                containingDeclaration,
A
Andrey Breslav 已提交
214
                Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
215
                false,
216
                Variance.INVARIANT,
217 218
                psiTypeParameter.getName(),
                psiTypeParameter.getIndex()
219
        );
220 221 222 223 224
        typeParameterDescriptorCache.put(psiTypeParameter, typeParameterDescriptor);
        return typeParameterDescriptor;
    }

    private void initializeTypeParameter(PsiTypeParameter typeParameter, TypeParameterDescriptor typeParameterDescriptor) {
A
Andrey Breslav 已提交
225 226
        PsiClassType[] referencedTypes = typeParameter.getExtendsList().getReferencedTypes();
        if (referencedTypes.length == 0){
227
            typeParameterDescriptor.addUpperBound(JetStandardClasses.getNullableAnyType());
A
Andrey Breslav 已提交
228 229
        }
        else if (referencedTypes.length == 1) {
230
            typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedTypes[0]));
A
Andrey Breslav 已提交
231 232 233
        }
        else {
            for (PsiClassType referencedType : referencedTypes) {
234
                typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedType));
A
Andrey Breslav 已提交
235 236
            }
        }
237 238 239 240 241 242
    }

    private void initializeTypeParameters(PsiTypeParameterListOwner typeParameterListOwner) {
        for (PsiTypeParameter psiTypeParameter : typeParameterListOwner.getTypeParameters()) {
            initializeTypeParameter(psiTypeParameter, resolveTypeParameter(psiTypeParameter));
        }
A
Andrey Breslav 已提交
243 244 245
    }

    @NotNull
246
    private TypeParameterDescriptor resolveTypeParameter(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter psiTypeParameter) {
A
Andrey Breslav 已提交
247
        TypeParameterDescriptor typeParameterDescriptor = typeParameterDescriptorCache.get(psiTypeParameter);
248
        assert typeParameterDescriptor != null : psiTypeParameter.getText();
A
Andrey Breslav 已提交
249 250 251
        return typeParameterDescriptor;
    }

A
Andrey Breslav 已提交
252 253
    private Collection<? extends JetType> getSupertypes(PsiClass psiClass) {
        List<JetType> result = new ArrayList<JetType>();
254 255 256 257 258 259
        result.add(JetStandardClasses.getAnyType());
        transformSupertypeList(result, psiClass.getExtendsListTypes());
        transformSupertypeList(result, psiClass.getImplementsListTypes());
        return result;
    }

A
Andrey Breslav 已提交
260
    private void transformSupertypeList(List<JetType> result, PsiClassType[] extendsListTypes) {
261
        for (PsiClassType type : extendsListTypes) {
A
Andrey Breslav 已提交
262
            JetType transform = semanticServices.getTypeTransformer().transformToType(type);
263 264 265 266 267

            result.add(TypeUtils.makeNotNullable(transform));
        }
    }

268
    public NamespaceDescriptor resolveNamespace(String qualifiedName) {
269
        PsiPackage psiPackage = findPackage(qualifiedName);
270
        if (psiPackage == null) {
271
            PsiClass psiClass = findClass(qualifiedName);
A
Andrey Breslav 已提交
272 273
            if (psiClass == null) return null;
            return resolveNamespace(psiClass);
274 275 276 277
        }
        return resolveNamespace(psiPackage);
    }

278 279 280 281
    private PsiClass findClass(String qualifiedName) {
        return javaFacade.findClass(qualifiedName, javaSearchScope);
    }

282
    /*package*/ PsiPackage findPackage(String qualifiedName) {
283 284 285
        return javaFacade.findPackage(qualifiedName);
    }

286 287
    private NamespaceDescriptor resolveNamespace(@NotNull PsiPackage psiPackage) {
        NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(psiPackage);
288
        if (namespaceDescriptor == null) {
A
Andrey Breslav 已提交
289
            namespaceDescriptor = createJavaNamespaceDescriptor(psiPackage);
290
            namespaceDescriptorCache.put(psiPackage, namespaceDescriptor);
291 292 293 294
        }
        return namespaceDescriptor;
    }

A
Andrey Breslav 已提交
295 296 297 298 299 300 301 302 303
    private NamespaceDescriptor resolveNamespace(@NotNull PsiClass psiClass) {
        NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(psiClass);
        if (namespaceDescriptor == null) {
            namespaceDescriptor = createJavaNamespaceDescriptor(psiClass);
            namespaceDescriptorCache.put(psiClass, namespaceDescriptor);
        }
        return namespaceDescriptor;
    }

304 305
    private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull PsiPackage psiPackage) {
        String name = psiPackage.getName();
306
        JavaNamespaceDescriptor namespaceDescriptor = new JavaNamespaceDescriptor(
307
                resolveParentDescriptor(psiPackage),
A
Andrey Breslav 已提交
308
                Collections.<AnnotationDescriptor>emptyList(), // TODO
309
                name == null ? JAVA_ROOT : name
310
        );
311

312
        namespaceDescriptor.setMemberScope(new JavaPackageScope(psiPackage.getQualifiedName(), namespaceDescriptor, semanticServices));
313
        semanticServices.getTrace().record(BindingContext.NAMESPACE, psiPackage, namespaceDescriptor);
314 315 316
        return namespaceDescriptor;
    }

317 318 319 320 321 322 323 324
    private DeclarationDescriptor resolveParentDescriptor(@NotNull PsiPackage psiPackage) {
        PsiPackage parentPackage = psiPackage.getParentPackage();
        if (parentPackage == null) {
            return null;
        }
        return resolveNamespace(parentPackage);
    }

325
    private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull final PsiClass psiClass) {
326
        JavaNamespaceDescriptor namespaceDescriptor = new JavaNamespaceDescriptor(
327
                resolveParentDescriptor(psiClass),
A
Andrey Breslav 已提交
328
                Collections.<AnnotationDescriptor>emptyList(), // TODO
329
                psiClass.getName()
330
        );
331
        namespaceDescriptor.setMemberScope(new JavaClassMembersScope(namespaceDescriptor, psiClass, semanticServices, true));
332
        semanticServices.getTrace().record(BindingContext.NAMESPACE, psiClass, namespaceDescriptor);
333 334 335
        return namespaceDescriptor;
    }

336
    public List<ValueParameterDescriptor> resolveParameterDescriptors(DeclarationDescriptor containingDeclaration, PsiParameter[] parameters) {
337 338 339
        List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
        for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
            PsiParameter parameter = parameters[i];
340 341
            ValueParameterDescriptor valueParameterDescriptor = resolveParameterDescriptor(containingDeclaration, i, parameter);
            result.add(valueParameterDescriptor);
342 343 344
        }
        return result;
    }
A
Andrey Breslav 已提交
345

346 347 348 349 350 351 352 353 354 355 356
    private ValueParameterDescriptor resolveParameterDescriptor(DeclarationDescriptor containingDeclaration, int i, PsiParameter parameter) {
        PsiType psiType = parameter.getType();

        JetType varargElementType;
        if (psiType instanceof PsiEllipsisType) {
            PsiEllipsisType psiEllipsisType = (PsiEllipsisType) psiType;
            varargElementType = semanticServices.getTypeTransformer().transformToType(psiEllipsisType.getComponentType());
        }
        else {
            varargElementType = null;
        }
357 358 359 360 361 362 363 364 365 366 367 368

        boolean changeNullable = false;
        boolean nullable = true;
        
        // TODO: must be very slow, make it lazy?
        String name = parameter.getName() != null ? parameter.getName() : "p" + i;
        for (PsiAnnotation annotation : parameter.getModifierList().getAnnotations()) {
            // TODO: softcode annotation name

            PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
            attributes.toString();

S
Stepan Koltsov 已提交
369 370
            if (annotation.getQualifiedName().equals(StdlibNames.JET_PARAMETER_CLASS)) {
                PsiLiteralExpression nameExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_PARAMETER_NAME_FIELD);
371 372 373 374
                if (nameExpression != null) {
                    name = (String) nameExpression.getValue();
                }
                
S
Stepan Koltsov 已提交
375
                PsiLiteralExpression nullableExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_PARAMETER_NULLABLE_FIELD);
376 377 378 379 380 381 382 383 384 385
                if (nullableExpression != null) {
                    nullable = (Boolean) nullableExpression.getValue();
                } else {
                    // default value of parameter
                    nullable = false;
                    changeNullable = true;
                }
            }
        }
        
386 387 388 389 390
        JetType outType = semanticServices.getTypeTransformer().transformToType(psiType);
        return new ValueParameterDescriptorImpl(
                containingDeclaration,
                i,
                Collections.<AnnotationDescriptor>emptyList(), // TODO
391
                name,
392
                null, // TODO : review
393
                changeNullable ? TypeUtils.makeNullableAsSpecified(outType, nullable) : outType,
394 395 396 397 398
                false,
                varargElementType
        );
    }

A
Andrey Breslav 已提交
399 400 401 402 403 404 405 406 407
    public VariableDescriptor resolveFieldToVariableDescriptor(DeclarationDescriptor containingDeclaration, PsiField field) {
        VariableDescriptor variableDescriptor = fieldDescriptorCache.get(field);
        if (variableDescriptor != null) {
            return variableDescriptor;
        }
        JetType type = semanticServices.getTypeTransformer().transformToType(field.getType());
        boolean isFinal = field.hasModifierProperty(PsiModifier.FINAL);
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
                containingDeclaration,
A
Andrey Breslav 已提交
408
                Collections.<AnnotationDescriptor>emptyList(),
409
                Modality.FINAL,
410
                resolveVisibilityFromPsiModifiers(field),
A
Andrey Breslav 已提交
411 412
                !isFinal,
                null,
413
                DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration),
A
Andrey Breslav 已提交
414 415 416
                field.getName(),
                isFinal ? null : type,
                type);
417
        semanticServices.getTrace().record(BindingContext.VARIABLE, field, propertyDescriptor);
A
Andrey Breslav 已提交
418 419 420 421
        fieldDescriptorCache.put(field, propertyDescriptor);
        return propertyDescriptor;
    }

A
Andrey Breslav 已提交
422
    @NotNull
A
Andrey Breslav 已提交
423 424
    public Set<FunctionDescriptor> resolveFunctionGroup(@NotNull DeclarationDescriptor owner, @NotNull PsiClass psiClass, @Nullable ClassDescriptor classDescriptor, @NotNull String methodName, boolean staticMembers) {
        Set<FunctionDescriptor> writableFunctionGroup = Sets.newLinkedHashSet();
425
        final Collection<HierarchicalMethodSignature> signatures = psiClass.getVisibleSignatures();
A
Andrey Breslav 已提交
426
        TypeSubstitutor typeSubstitutor = createSubstitutorForGenericSupertypes(classDescriptor);
427
        for (HierarchicalMethodSignature signature: signatures) {
428
            if (!methodName.equals(signature.getName())) {
A
Andrey Breslav 已提交
429 430 431
                 continue;
            }

432
            FunctionDescriptor substitutedFunctionDescriptor = resolveHierarchicalSignatureToFunction(owner, psiClass, staticMembers, typeSubstitutor, signature);
433
            if (substitutedFunctionDescriptor != null) {
A
Andrey Breslav 已提交
434
                writableFunctionGroup.add(substitutedFunctionDescriptor);
435
            }
A
Andrey Breslav 已提交
436 437 438
        }
        return writableFunctionGroup;
    }
A
Andrey Breslav 已提交
439

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    @Nullable
    private FunctionDescriptor resolveHierarchicalSignatureToFunction(DeclarationDescriptor owner, PsiClass psiClass, boolean staticMembers, TypeSubstitutor typeSubstitutor, HierarchicalMethodSignature signature) {
        PsiMethod method = signature.getMethod();
        if (method.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
                return null;
        }
        FunctionDescriptor functionDescriptor = resolveMethodToFunctionDescriptor(owner, psiClass, typeSubstitutor, method);
//        if (functionDescriptor != null && !staticMembers) {
//            for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
//                ((FunctionDescriptorImpl) functionDescriptor).addOverriddenFunction(resolveHierarchicalSignatureToFunction(owner, superSignature.getMethod().getContainingClass(), false, typeSubstitutor, superSignature));
//            }
//        }
        return functionDescriptor;
    }

A
Andrey Breslav 已提交
455 456 457 458 459 460 461 462 463 464 465 466
    public TypeSubstitutor createSubstitutorForGenericSupertypes(ClassDescriptor classDescriptor) {
        TypeSubstitutor typeSubstitutor;
        if (classDescriptor != null) {
            typeSubstitutor = TypeUtils.buildDeepSubstitutor(classDescriptor.getDefaultType());
        }
        else {
            typeSubstitutor = TypeSubstitutor.EMPTY;
        }
        return typeSubstitutor;
    }

    @Nullable
A
Andrey Breslav 已提交
467
    public FunctionDescriptor resolveMethodToFunctionDescriptor(DeclarationDescriptor owner, PsiClass psiClass, TypeSubstitutor typeSubstitutorForGenericSuperclasses, PsiMethod method) {
A
Andrey Breslav 已提交
468 469 470 471 472 473 474 475 476 477 478
        PsiType returnType = method.getReturnType();
        if (returnType == null) {
            return null;
        }
        FunctionDescriptor functionDescriptor = methodDescriptorCache.get(method);
        if (functionDescriptor != null) {
            if (method.getContainingClass() != psiClass) {
                functionDescriptor = functionDescriptor.substitute(typeSubstitutorForGenericSuperclasses);
            }
            return functionDescriptor;
        }
A
Andrey Breslav 已提交
479
        DeclarationDescriptor classDescriptor = method.hasModifierProperty(PsiModifier.STATIC) ? resolveNamespace(method.getContainingClass()) : resolveClass(method.getContainingClass());
A
Andrey Breslav 已提交
480 481
        PsiParameter[] parameters = method.getParameterList().getParameters();
        FunctionDescriptorImpl functionDescriptorImpl = new FunctionDescriptorImpl(
A
Andrey Breslav 已提交
482 483
                owner,
                Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
484 485 486
                method.getName()
        );
        methodDescriptorCache.put(method, functionDescriptorImpl);
487 488
        List<TypeParameterDescriptor> typeParameters = makeUninitializedTypeParameters(functionDescriptorImpl, method.getTypeParameters());
        initializeTypeParameters(method);
A
Andrey Breslav 已提交
489 490
        functionDescriptorImpl.initialize(
                null,
A
Andrey Breslav 已提交
491
                DescriptorUtils.getExpectedThisObjectIfNeeded(classDescriptor),
492
                typeParameters,
A
Andrey Breslav 已提交
493
                semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters),
494
                semanticServices.getDescriptorResolver().makeReturnType(returnType, method),
495
                Modality.convertFromFlags(method.hasModifierProperty(PsiModifier.ABSTRACT), !method.hasModifierProperty(PsiModifier.FINAL)),
496
                resolveVisibilityFromPsiModifiers(method)
A
Andrey Breslav 已提交
497
        );
498
        semanticServices.getTrace().record(BindingContext.FUNCTION, method, functionDescriptorImpl);
A
Andrey Breslav 已提交
499 500 501 502 503 504
        FunctionDescriptor substitutedFunctionDescriptor = functionDescriptorImpl;
        if (method.getContainingClass() != psiClass) {
            substitutedFunctionDescriptor = functionDescriptorImpl.substitute(typeSubstitutorForGenericSuperclasses);
        }
        return substitutedFunctionDescriptor;
    }
505

506 507 508 509 510
    private JetType makeReturnType(PsiType returnType, PsiMethod method) {
        boolean changeNullable = false;
        boolean nullable = true;

        for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) {
S
Stepan Koltsov 已提交
511 512
            if (annotation.getQualifiedName().equals(StdlibNames.JET_METHOD_CLASS)) {
                PsiLiteralExpression nullableExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_METHOD_NULLABLE_RETURN_TYPE_FIELD);
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
                if (nullableExpression != null) {
                    nullable = (Boolean) nullableExpression.getValue();
                } else {
                    // default value of parameter
                    nullable = false;
                    changeNullable = true;
                }
            }
        }
        JetType transformedType = semanticServices.getTypeTransformer().transformToType(returnType);
        if (changeNullable) {
            return TypeUtils.makeNullableAsSpecified(transformedType, nullable);
        } else {
            return transformedType;
        }
    }

530
    private static Visibility resolveVisibilityFromPsiModifiers(PsiModifierListOwner modifierListOwner) {
531 532 533 534 535 536
        //TODO report error
        return modifierListOwner.hasModifierProperty(PsiModifier.PUBLIC) ? Visibility.PUBLIC :
                                        (modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE) ? Visibility.PRIVATE :
                                        (modifierListOwner.hasModifierProperty(PsiModifier.PROTECTED) ? Visibility.PROTECTED : Visibility.INTERNAL));
    }

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    public TypeParameterDescriptor resolveTypeParameter(PsiTypeParameter typeParameter) {
        PsiTypeParameterListOwner owner = typeParameter.getOwner();
        if (owner instanceof PsiClass) {
            PsiClass psiClass = (PsiClass) owner;
            return resolveTypeParameter(resolveClass(psiClass), typeParameter);
        }
        if (owner instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) owner;
            PsiClass containingClass = psiMethod.getContainingClass();
            DeclarationDescriptor ownerOwner;
            TypeSubstitutor substitutorForGenericSupertypes;
            if (psiMethod.hasModifierProperty(PsiModifier.STATIC)) {
                substitutorForGenericSupertypes = TypeSubstitutor.EMPTY;
                return resolveTypeParameter(JAVA_METHOD_TYPE_PARAMETER_PARENT, typeParameter);
            }
            else {
                ClassDescriptor classDescriptor = resolveClass(containingClass);
                ownerOwner = classDescriptor;
                substitutorForGenericSupertypes = semanticServices.getDescriptorResolver().createSubstitutorForGenericSupertypes(classDescriptor);
            }
            FunctionDescriptor functionDescriptor = resolveMethodToFunctionDescriptor(ownerOwner, containingClass, substitutorForGenericSupertypes, psiMethod);
            return resolveTypeParameter(functionDescriptor, typeParameter);
        }
        throw new IllegalStateException("Unknown parent type: " + owner);
    }
562
}