Search in sources :

Example 1 with MemberScope

use of org.jetbrains.kotlin.resolve.scopes.MemberScope in project kotlin by JetBrains.

the class ClosureCodegen method getErasedInvokeFunction.

@NotNull
public static FunctionDescriptor getErasedInvokeFunction(@NotNull FunctionDescriptor function) {
    ClassDescriptor functionClass = DescriptorUtilsKt.getBuiltIns(function).getFunction(function.getValueParameters().size() + (function.getExtensionReceiverParameter() != null ? 1 : 0));
    MemberScope scope = functionClass.getDefaultType().getMemberScope();
    return scope.getContributedFunctions(OperatorNameConventions.INVOKE, NoLookupLocation.FROM_BACKEND).iterator().next();
}
Also used : MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with MemberScope

use of org.jetbrains.kotlin.resolve.scopes.MemberScope in project kotlin by JetBrains.

the class AbstractAnnotationDescriptorResolveTest method getPropertyDescriptor.

@NotNull
private static PropertyDescriptor getPropertyDescriptor(@NotNull ClassDescriptor classDescriptor, @NotNull String name) {
    Name propertyName = Name.identifier(name);
    MemberScope memberScope = classDescriptor.getMemberScope(Collections.<TypeProjection>emptyList());
    Collection<PropertyDescriptor> properties = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
    assert properties.size() == 1 : "Failed to find property " + propertyName + " in class " + classDescriptor.getName();
    return properties.iterator().next();
}
Also used : MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MemberScope

use of org.jetbrains.kotlin.resolve.scopes.MemberScope in project kotlin by JetBrains.

the class RecursiveDescriptorComparator method appendDeclarationRecursively.

private void appendDeclarationRecursively(@NotNull DeclarationDescriptor descriptor, @NotNull ModuleDescriptor module, @NotNull Printer printer, boolean topLevel) {
    if (!isFromModule(descriptor, module))
        return;
    boolean isEnumEntry = isEnumEntry(descriptor);
    boolean isClassOrPackage = (descriptor instanceof ClassOrPackageFragmentDescriptor || descriptor instanceof PackageViewDescriptor) && !isEnumEntry;
    if (isClassOrPackage && !topLevel) {
        printer.println();
    }
    boolean isPrimaryConstructor = descriptor instanceof ConstructorDescriptor && ((ConstructorDescriptor) descriptor).isPrimary();
    printer.print(isPrimaryConstructor && conf.checkPrimaryConstructors ? "/*primary*/ " : "", conf.renderer.render(descriptor));
    if (isClassOrPackage) {
        if (!topLevel) {
            printer.printlnWithNoIndent(" {").pushIndent();
        } else {
            printer.println();
            printer.println();
        }
        if (descriptor instanceof ClassDescriptor) {
            ClassDescriptor klass = (ClassDescriptor) descriptor;
            appendSubDescriptors(descriptor, module, klass.getDefaultType().getMemberScope(), klass.getConstructors(), printer);
            MemberScope staticScope = klass.getStaticScope();
            if (!DescriptorUtils.getAllDescriptors(staticScope).isEmpty()) {
                printer.println();
                printer.println("// Static members");
                appendSubDescriptors(descriptor, module, staticScope, Collections.<DeclarationDescriptor>emptyList(), printer);
            }
        } else if (descriptor instanceof PackageFragmentDescriptor) {
            appendSubDescriptors(descriptor, module, ((PackageFragmentDescriptor) descriptor).getMemberScope(), Collections.<DeclarationDescriptor>emptyList(), printer);
        } else if (descriptor instanceof PackageViewDescriptor) {
            appendSubDescriptors(descriptor, module, getPackageScopeInModule((PackageViewDescriptor) descriptor, module), Collections.<DeclarationDescriptor>emptyList(), printer);
        }
        if (!topLevel) {
            printer.popIndent().println("}");
        }
    } else if (conf.checkPropertyAccessors && descriptor instanceof PropertyDescriptor) {
        printer.printlnWithNoIndent();
        printer.pushIndent();
        PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
        PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
        if (getter != null) {
            printer.println(conf.renderer.render(getter));
        }
        PropertySetterDescriptor setter = propertyDescriptor.getSetter();
        if (setter != null) {
            printer.println(conf.renderer.render(setter));
        }
        printer.popIndent();
    } else {
        printer.printlnWithNoIndent();
    }
    if (isEnumEntry) {
        printer.println();
    }
}
Also used : MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) ChainedMemberScope(org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope)

Example 4 with MemberScope

use of org.jetbrains.kotlin.resolve.scopes.MemberScope in project kotlin by JetBrains.

the class LazyDeclarationResolver method findClassDescriptor.

@NotNull
private ClassDescriptor findClassDescriptor(@NotNull KtNamedDeclaration classObjectOrScript, @NotNull LookupLocation location) {
    MemberScope scope = getMemberScopeDeclaredIn(classObjectOrScript, location);
    // Why not use the result here. Because it may be that there is a redeclaration:
    //     class A {} class A { fun foo(): A<completion here>}
    // and if we find the class by name only, we may b-not get the right one.
    // This call is only needed to make sure the classes are written to trace
    ClassifierDescriptor scopeDescriptor = scope.getContributedClassifier(classObjectOrScript.getNameAsSafeName(), location);
    DeclarationDescriptor descriptor = getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, classObjectOrScript);
    if (descriptor == null) {
        throw new IllegalArgumentException(String.format("Could not find a classifier for %s.\n" + "Found descriptor: %s (%s).\n", PsiUtilsKt.getElementTextWithContext(classObjectOrScript), scopeDescriptor != null ? DescriptorRenderer.DEBUG_TEXT.render(scopeDescriptor) : "null", scopeDescriptor != null ? (scopeDescriptor.getContainingDeclaration().getClass()) : null));
    }
    return (ClassDescriptor) descriptor;
}
Also used : MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MemberScope

use of org.jetbrains.kotlin.resolve.scopes.MemberScope 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)

Aggregations

MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)12 NotNull (org.jetbrains.annotations.NotNull)9 FqName (org.jetbrains.kotlin.name.FqName)5 Name (org.jetbrains.kotlin.name.Name)5 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)3 Nullable (org.jetbrains.annotations.Nullable)2 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)2 ChainedMemberScope (org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope)2 ArrayList (java.util.ArrayList)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1 SubpackagesScope (org.jetbrains.kotlin.descriptors.impl.SubpackagesScope)1 KtClass (org.jetbrains.kotlin.psi.KtClass)1 KtDeclaration (org.jetbrains.kotlin.psi.KtDeclaration)1 TypeProjection (org.jetbrains.kotlin.types.TypeProjection)1