Search in sources :

Example 1 with ClassifierDescriptor

use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.

the class AbstractClassTypeConstructor method hashCode.

@Override
public final int hashCode() {
    int currentHashCode = hashCode;
    if (currentHashCode != 0)
        return currentHashCode;
    ClassifierDescriptor descriptor = getDeclarationDescriptor();
    if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
        currentHashCode = DescriptorUtils.getFqName(descriptor).hashCode();
    } else {
        currentHashCode = System.identityHashCode(this);
    }
    hashCode = currentHashCode;
    return currentHashCode;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor)

Example 2 with ClassifierDescriptor

use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.

the class AbstractClassTypeConstructor method equals.

@Override
public boolean equals(Object other) {
    if (!(other instanceof TypeConstructor))
        return false;
    // performance optimization: getFqName is slow method
    if (other.hashCode() != hashCode())
        return false;
    // To avoid problems in type checker we suppose that it is different type constructors.
    if (((TypeConstructor) other).getParameters().size() != getParameters().size())
        return false;
    ClassifierDescriptor myDescriptor = getDeclarationDescriptor();
    ClassifierDescriptor otherDescriptor = ((TypeConstructor) other).getDeclarationDescriptor();
    // descriptor for type is created once per module
    if (myDescriptor == otherDescriptor)
        return true;
    // All error types have the same descriptor
    if (!hasMeaningfulFqName(myDescriptor) || otherDescriptor != null && !hasMeaningfulFqName(otherDescriptor)) {
        return this == other;
    }
    if (myDescriptor instanceof ClassDescriptor && otherDescriptor instanceof ClassDescriptor) {
        FqNameUnsafe otherFqName = DescriptorUtils.getFqName(otherDescriptor);
        FqNameUnsafe myFqName = DescriptorUtils.getFqName(myDescriptor);
        return myFqName.equals(otherFqName);
    }
    return false;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) FqNameUnsafe(org.jetbrains.kotlin.name.FqNameUnsafe) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor)

Example 3 with ClassifierDescriptor

use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.

the class CommonSupertypes method computeSupertypeProjections.

// constructor - type constructor of a supertype to be instantiated
// types - instantiations of constructor occurring as supertypes of classes we are trying to intersect
@NotNull
private static SimpleType computeSupertypeProjections(@NotNull TypeConstructor constructor, @NotNull Set<SimpleType> types, int recursionDepth, int maxDepth) {
    assert !types.isEmpty();
    if (types.size() == 1) {
        return types.iterator().next();
    }
    List<TypeParameterDescriptor> parameters = constructor.getParameters();
    List<TypeProjection> newProjections = new ArrayList<TypeProjection>(parameters.size());
    for (TypeParameterDescriptor parameterDescriptor : parameters) {
        Set<TypeProjection> typeProjections = new HashSet<TypeProjection>();
        for (KotlinType type : types) {
            typeProjections.add(type.getArguments().get(parameterDescriptor.getIndex()));
        }
        newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth));
    }
    boolean nullable = false;
    for (KotlinType type : types) {
        nullable |= type.isMarkedNullable();
    }
    ClassifierDescriptor classifier = constructor.getDeclarationDescriptor();
    MemberScope newScope;
    if (classifier instanceof ClassDescriptor) {
        newScope = ((ClassDescriptor) classifier).getMemberScope(newProjections);
    } else if (classifier instanceof TypeParameterDescriptor) {
        newScope = classifier.getDefaultType().getMemberScope();
    } else {
        newScope = ErrorUtils.createErrorScope("A scope for common supertype which is not a normal classifier", true);
    }
    return KotlinTypeFactory.simpleType(Annotations.Companion.getEMPTY(), constructor, newProjections, nullable, newScope);
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ClassifierDescriptor

use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.

the class TypeSubstitutorTest method stringsToSubstitutionMap.

private Map<TypeConstructor, TypeProjection> stringsToSubstitutionMap(Pair<String, String>[] substitutionStrs) {
    Map<TypeConstructor, TypeProjection> map = Maps.newHashMap();
    for (Pair<String, String> pair : substitutionStrs) {
        String typeParameterName = pair.first;
        String replacementProjectionString = pair.second;
        ClassifierDescriptor classifier = ScopeUtilsKt.findClassifier(scope, Name.identifier(typeParameterName), NoLookupLocation.FROM_TEST);
        assertNotNull("No type parameter named " + typeParameterName, classifier);
        assertTrue(typeParameterName + " is not a type parameter: " + classifier, classifier instanceof TypeParameterDescriptor);
        String typeStr = "C<" + replacementProjectionString + ">";
        KotlinType typeWithArgument = resolveType(typeStr);
        assert !typeWithArgument.getArguments().isEmpty() : "No arguments: " + typeWithArgument + " from " + typeStr;
        map.put(classifier.getTypeConstructor(), typeWithArgument.getArguments().get(0));
    }
    return map;
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor)

Example 5 with ClassifierDescriptor

use of org.jetbrains.kotlin.descriptors.ClassifierDescriptor in project kotlin by JetBrains.

the class TypeSubstitutorTest method getContextScope.

private LexicalScope getContextScope() throws IOException {
    // todo comments
    String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
    KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
    AnalysisResult analysisResult = JvmResolveUtil.analyze(ktFile, getEnvironment());
    ModuleDescriptor module = analysisResult.getModuleDescriptor();
    LexicalScope topLevelScope = analysisResult.getBindingContext().get(BindingContext.LEXICAL_SCOPE, ktFile);
    final ClassifierDescriptor contextClass = ScopeUtilsKt.findClassifier(topLevelScope, Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
    assert contextClass instanceof ClassDescriptor;
    LocalRedeclarationChecker redeclarationChecker = new ThrowingLocalRedeclarationChecker(new OverloadChecker(TypeSpecificityComparator.NONE.INSTANCE));
    LexicalScope typeParameters = new LexicalScopeImpl(topLevelScope, module, false, null, LexicalScopeKind.SYNTHETIC, redeclarationChecker, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {

        @Override
        public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
            for (TypeParameterDescriptor parameterDescriptor : contextClass.getTypeConstructor().getParameters()) {
                handler.addClassifierDescriptor(parameterDescriptor);
            }
            return Unit.INSTANCE;
        }
    });
    return new LexicalChainedScope(typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC, Arrays.asList(contextClass.getDefaultType().getMemberScope(), module.getBuiltIns().getBuiltInsPackageScope()));
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) Unit(kotlin.Unit) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Aggregations

ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)9 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)5 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)4 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 PsiElement (com.intellij.psi.PsiElement)1 File (java.io.File)1 Map (java.util.Map)1 Unit (kotlin.Unit)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)1 FqName (org.jetbrains.kotlin.name.FqName)1 FqNameUnsafe (org.jetbrains.kotlin.name.FqNameUnsafe)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 KtTypeProjection (org.jetbrains.kotlin.psi.KtTypeProjection)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1