Search in sources :

Example 1 with Annotations

use of org.jetbrains.kotlin.descriptors.annotations.Annotations in project kotlin by JetBrains.

the class DescriptorResolver method resolvePropertyGetterDescriptor.

@NotNull
private PropertyGetterDescriptorImpl resolvePropertyGetterDescriptor(@NotNull LexicalScope scopeForDeclarationResolution, @NotNull KtProperty property, @NotNull PropertyDescriptor propertyDescriptor, @NotNull AnnotationSplitter annotationSplitter, @NotNull BindingTrace trace, @Nullable KotlinType propertyTypeIfKnown) {
    PropertyGetterDescriptorImpl getterDescriptor;
    KtPropertyAccessor getter = property.getGetter();
    KotlinType getterType;
    if (getter != null) {
        Annotations getterAnnotations = new CompositeAnnotations(CollectionsKt.listOf(annotationSplitter.getAnnotationsForTarget(PROPERTY_GETTER), annotationResolver.resolveAnnotationsWithoutArguments(scopeForDeclarationResolution, getter.getModifierList(), trace)));
        getterDescriptor = new PropertyGetterDescriptorImpl(propertyDescriptor, getterAnnotations, resolveMemberModalityFromModifiers(getter, propertyDescriptor.getModality(), trace.getBindingContext(), propertyDescriptor.getContainingDeclaration()), resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()), /* isDefault = */
        false, getter.hasModifier(EXTERNAL_KEYWORD), property.hasModifier(KtTokens.INLINE_KEYWORD) || getter.hasModifier(KtTokens.INLINE_KEYWORD), CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt.toSourceElement(getter));
        getterType = determineGetterReturnType(scopeForDeclarationResolution, trace, getterDescriptor, getter, propertyTypeIfKnown);
        trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor);
    } else {
        Annotations getterAnnotations = annotationSplitter.getAnnotationsForTarget(PROPERTY_GETTER);
        getterDescriptor = DescriptorFactory.createGetter(propertyDescriptor, getterAnnotations, !property.hasDelegate(), /* isExternal = */
        false, property.hasModifier(KtTokens.INLINE_KEYWORD));
        getterType = propertyTypeIfKnown;
    }
    getterDescriptor.initialize(getterType != null ? getterType : VariableTypeAndInitializerResolver.STUB_FOR_PROPERTY_WITHOUT_TYPE);
    return getterDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Annotations

use of org.jetbrains.kotlin.descriptors.annotations.Annotations in project kotlin by JetBrains.

the class TypeSubstitutor method unsafeSubstitute.

@NotNull
private TypeProjection unsafeSubstitute(@NotNull TypeProjection originalProjection, int recursionDepth) throws SubstitutionException {
    assertRecursionDepth(recursionDepth, originalProjection, substitution);
    if (originalProjection.isStarProjection())
        return originalProjection;
    // The type is within the substitution range, i.e. T or T?
    KotlinType type = originalProjection.getType();
    if (DynamicTypesKt.isDynamic(type) || type.unwrap() instanceof RawType) {
        // todo investigate
        return originalProjection;
    }
    TypeProjection replacement = substitution.get(type);
    Variance originalProjectionKind = originalProjection.getProjectionKind();
    if (replacement == null && FlexibleTypesKt.isFlexible(type) && !TypeCapabilitiesKt.isCustomTypeVariable(type)) {
        FlexibleType flexibleType = FlexibleTypesKt.asFlexibleType(type);
        TypeProjection substitutedLower = unsafeSubstitute(new TypeProjectionImpl(originalProjectionKind, flexibleType.getLowerBound()), recursionDepth + 1);
        TypeProjection substitutedUpper = unsafeSubstitute(new TypeProjectionImpl(originalProjectionKind, flexibleType.getUpperBound()), recursionDepth + 1);
        Variance substitutedProjectionKind = substitutedLower.getProjectionKind();
        assert (substitutedProjectionKind == substitutedUpper.getProjectionKind()) && originalProjectionKind == Variance.INVARIANT || originalProjectionKind == substitutedProjectionKind : "Unexpected substituted projection kind: " + substitutedProjectionKind + "; original: " + originalProjectionKind;
        KotlinType substitutedFlexibleType = KotlinTypeFactory.flexibleType(TypeSubstitutionKt.asSimpleType(substitutedLower.getType()), TypeSubstitutionKt.asSimpleType(substitutedUpper.getType()));
        return new TypeProjectionImpl(substitutedProjectionKind, substitutedFlexibleType);
    }
    if (KotlinBuiltIns.isNothing(type) || type.isError())
        return originalProjection;
    if (replacement != null) {
        VarianceConflictType varianceConflict = conflictType(originalProjectionKind, replacement.getProjectionKind());
        // Captured type might be substituted in an opposite projection:
        // out 'Captured (in Int)' = out Int
        // in 'Captured (out Int)' = in Int
        boolean allowVarianceConflict = CapturedTypeConstructorKt.isCaptured(type);
        if (!allowVarianceConflict) {
            //noinspection EnumSwitchStatementWhichMissesCases
            switch(varianceConflict) {
                case OUT_IN_IN_POSITION:
                    throw new SubstitutionException("Out-projection in in-position");
                case IN_IN_OUT_POSITION:
                    // todo use the right type parameter variance and upper bound
                    return new TypeProjectionImpl(Variance.OUT_VARIANCE, type.getConstructor().getBuiltIns().getNullableAnyType());
            }
        }
        KotlinType substitutedType;
        CustomTypeVariable typeVariable = TypeCapabilitiesKt.getCustomTypeVariable(type);
        if (replacement.isStarProjection()) {
            return replacement;
        } else if (typeVariable != null) {
            substitutedType = typeVariable.substitutionResult(replacement.getType());
        } else {
            // this is a simple type T or T?: if it's T, we should just take replacement, if T? - we make replacement nullable
            substitutedType = TypeUtils.makeNullableIfNeeded(replacement.getType(), type.isMarkedNullable());
        }
        // substitutionType.annotations = replacement.annotations ++ type.annotations
        if (!type.getAnnotations().isEmpty()) {
            Annotations typeAnnotations = filterOutUnsafeVariance(substitution.filterAnnotations(type.getAnnotations()));
            substitutedType = TypeUtilsKt.replaceAnnotations(substitutedType, new CompositeAnnotations(substitutedType.getAnnotations(), typeAnnotations));
        }
        Variance resultingProjectionKind = varianceConflict == VarianceConflictType.NO_CONFLICT ? combine(originalProjectionKind, replacement.getProjectionKind()) : originalProjectionKind;
        return new TypeProjectionImpl(resultingProjectionKind, substitutedType);
    }
    // The type is not within the substitution range, i.e. Foo, Bar<T> etc.
    return substituteCompoundType(originalProjection, recursionDepth);
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) FilteredAnnotations(org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Annotations

use of org.jetbrains.kotlin.descriptors.annotations.Annotations in project kotlin by JetBrains.

the class DescriptorResolver method resolvePropertyDescriptor.

@NotNull
public PropertyDescriptor resolvePropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull LexicalScope scopeForDeclarationResolution, @NotNull LexicalScope scopeForInitializerResolution, @NotNull KtProperty property, @NotNull final BindingTrace trace, @NotNull DataFlowInfo dataFlowInfo) {
    KtModifierList modifierList = property.getModifierList();
    boolean isVar = property.isVar();
    boolean hasBody = hasBody(property);
    Visibility visibility = resolveVisibilityFromModifiers(property, getDefaultVisibility(property, containingDeclaration));
    Modality modality = containingDeclaration instanceof ClassDescriptor ? resolveMemberModalityFromModifiers(property, getDefaultModality(containingDeclaration, visibility, hasBody), trace.getBindingContext(), containingDeclaration) : Modality.FINAL;
    final AnnotationSplitter.PropertyWrapper wrapper = new AnnotationSplitter.PropertyWrapper(property);
    Annotations allAnnotations = annotationResolver.resolveAnnotationsWithoutArguments(scopeForDeclarationResolution, modifierList, trace);
    AnnotationSplitter annotationSplitter = new AnnotationSplitter(storageManager, allAnnotations, new Function0<Set<AnnotationUseSiteTarget>>() {

        @Override
        public Set<AnnotationUseSiteTarget> invoke() {
            return AnnotationSplitter.getTargetSet(false, trace.getBindingContext(), wrapper);
        }
    });
    Annotations propertyAnnotations = new CompositeAnnotations(CollectionsKt.listOf(annotationSplitter.getAnnotationsForTargets(PROPERTY, FIELD, PROPERTY_DELEGATE_FIELD), annotationSplitter.getOtherAnnotations()));
    PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(containingDeclaration, propertyAnnotations, modality, visibility, isVar, KtPsiUtil.safeName(property.getName()), CallableMemberDescriptor.Kind.DECLARATION, KotlinSourceElementKt.toSourceElement(property), modifierList != null && modifierList.hasModifier(KtTokens.LATEINIT_KEYWORD), modifierList != null && modifierList.hasModifier(KtTokens.CONST_KEYWORD), modifierList != null && modifierList.hasModifier(KtTokens.HEADER_KEYWORD) || containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).isHeader(), modifierList != null && modifierList.hasModifier(KtTokens.IMPL_KEYWORD), modifierList != null && modifierList.hasModifier(KtTokens.EXTERNAL_KEYWORD), property.hasDelegate());
    wrapper.setDescriptor(propertyDescriptor);
    List<TypeParameterDescriptorImpl> typeParameterDescriptors;
    LexicalScope scopeForDeclarationResolutionWithTypeParameters;
    LexicalScope scopeForInitializerResolutionWithTypeParameters;
    KotlinType receiverType = null;
    {
        List<KtTypeParameter> typeParameters = property.getTypeParameters();
        if (typeParameters.isEmpty()) {
            scopeForDeclarationResolutionWithTypeParameters = scopeForDeclarationResolution;
            scopeForInitializerResolutionWithTypeParameters = scopeForInitializerResolution;
            typeParameterDescriptors = Collections.emptyList();
        } else {
            LexicalWritableScope writableScopeForDeclarationResolution = new LexicalWritableScope(scopeForDeclarationResolution, containingDeclaration, false, new TraceBasedLocalRedeclarationChecker(trace, overloadChecker), LexicalScopeKind.PROPERTY_HEADER);
            LexicalWritableScope writableScopeForInitializerResolution = new LexicalWritableScope(scopeForInitializerResolution, containingDeclaration, false, LocalRedeclarationChecker.DO_NOTHING.INSTANCE, LexicalScopeKind.PROPERTY_HEADER);
            typeParameterDescriptors = resolveTypeParametersForDescriptor(propertyDescriptor, scopeForDeclarationResolution, typeParameters, trace);
            for (TypeParameterDescriptor descriptor : typeParameterDescriptors) {
                writableScopeForDeclarationResolution.addClassifierDescriptor(descriptor);
                writableScopeForInitializerResolution.addClassifierDescriptor(descriptor);
            }
            writableScopeForDeclarationResolution.freeze();
            writableScopeForInitializerResolution.freeze();
            resolveGenericBounds(property, propertyDescriptor, writableScopeForDeclarationResolution, typeParameterDescriptors, trace);
            scopeForDeclarationResolutionWithTypeParameters = writableScopeForDeclarationResolution;
            scopeForInitializerResolutionWithTypeParameters = writableScopeForInitializerResolution;
        }
        KtTypeReference receiverTypeRef = property.getReceiverTypeReference();
        if (receiverTypeRef != null) {
            receiverType = typeResolver.resolveType(scopeForDeclarationResolutionWithTypeParameters, receiverTypeRef, trace, true);
        }
    }
    ReceiverParameterDescriptor receiverDescriptor = DescriptorFactory.createExtensionReceiverParameterForCallable(propertyDescriptor, receiverType);
    LexicalScope scopeForInitializer = ScopeUtils.makeScopeForPropertyInitializer(scopeForInitializerResolutionWithTypeParameters, propertyDescriptor);
    KotlinType typeIfKnown = variableTypeAndInitializerResolver.resolveTypeNullable(propertyDescriptor, scopeForInitializer, property, dataFlowInfo, /* local = */
    trace, false);
    PropertyGetterDescriptorImpl getter = resolvePropertyGetterDescriptor(scopeForDeclarationResolutionWithTypeParameters, property, propertyDescriptor, annotationSplitter, trace, typeIfKnown);
    KotlinType type = typeIfKnown != null ? typeIfKnown : getter.getReturnType();
    assert type != null : "At least getter type must be initialized via resolvePropertyGetterDescriptor";
    variableTypeAndInitializerResolver.setConstantForVariableIfNeeded(propertyDescriptor, scopeForInitializer, property, dataFlowInfo, type, trace);
    propertyDescriptor.setType(type, typeParameterDescriptors, getDispatchReceiverParameterIfNeeded(containingDeclaration), receiverDescriptor);
    PropertySetterDescriptor setter = resolvePropertySetterDescriptor(scopeForDeclarationResolutionWithTypeParameters, property, propertyDescriptor, annotationSplitter, trace);
    propertyDescriptor.initialize(getter, setter);
    trace.record(BindingContext.VARIABLE, property, propertyDescriptor);
    return propertyDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) AnnotationSplitter(org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Annotations

use of org.jetbrains.kotlin.descriptors.annotations.Annotations in project kotlin by JetBrains.

the class DescriptorResolver method resolveTypeParameterForDescriptor.

private TypeParameterDescriptorImpl resolveTypeParameterForDescriptor(final DeclarationDescriptor containingDescriptor, LexicalScope scopeForAnnotationsResolve, final KtTypeParameter typeParameter, int index, final BindingTrace trace) {
    if (typeParameter.getVariance() != Variance.INVARIANT) {
        trace.report(VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED.on(typeParameter));
    }
    Annotations annotations = annotationResolver.resolveAnnotationsWithArguments(scopeForAnnotationsResolve, typeParameter.getModifierList(), trace);
    TypeParameterDescriptorImpl typeParameterDescriptor = TypeParameterDescriptorImpl.createForFurtherModification(containingDescriptor, annotations, typeParameter.hasModifier(KtTokens.REIFIED_KEYWORD), typeParameter.getVariance(), KtPsiUtil.safeName(typeParameter.getName()), index, KotlinSourceElementKt.toSourceElement(typeParameter), new Function1<KotlinType, Void>() {

        @Override
        public Void invoke(KotlinType type) {
            if (!(containingDescriptor instanceof TypeAliasDescriptor)) {
                trace.report(Errors.CYCLIC_GENERIC_UPPER_BOUND.on(typeParameter));
            }
            return null;
        }
    }, supertypeLoopsResolver);
    trace.record(BindingContext.TYPE_PARAMETER, typeParameter, typeParameterDescriptor);
    return typeParameterDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) LazyTypeAliasDescriptor(org.jetbrains.kotlin.resolve.lazy.descriptors.LazyTypeAliasDescriptor)

Example 5 with Annotations

use of org.jetbrains.kotlin.descriptors.annotations.Annotations in project kotlin by JetBrains.

the class DescriptorResolver method resolvePropertySetterDescriptor.

@Nullable
private PropertySetterDescriptor resolvePropertySetterDescriptor(@NotNull LexicalScope scopeWithTypeParameters, @NotNull KtProperty property, @NotNull PropertyDescriptor propertyDescriptor, @NotNull AnnotationSplitter annotationSplitter, @NotNull BindingTrace trace) {
    KtPropertyAccessor setter = property.getSetter();
    PropertySetterDescriptorImpl setterDescriptor = null;
    if (setter != null) {
        Annotations annotations = new CompositeAnnotations(CollectionsKt.listOf(annotationSplitter.getAnnotationsForTarget(PROPERTY_SETTER), annotationResolver.resolveAnnotationsWithoutArguments(scopeWithTypeParameters, setter.getModifierList(), trace)));
        KtParameter parameter = setter.getParameter();
        setterDescriptor = new PropertySetterDescriptorImpl(propertyDescriptor, annotations, resolveMemberModalityFromModifiers(setter, propertyDescriptor.getModality(), trace.getBindingContext(), propertyDescriptor.getContainingDeclaration()), resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()), /* isDefault = */
        false, setter.hasModifier(EXTERNAL_KEYWORD), property.hasModifier(KtTokens.INLINE_KEYWORD) || setter.hasModifier(KtTokens.INLINE_KEYWORD), CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt.toSourceElement(setter));
        KtTypeReference returnTypeReference = setter.getReturnTypeReference();
        if (returnTypeReference != null) {
            KotlinType returnType = typeResolver.resolveType(scopeWithTypeParameters, returnTypeReference, trace, true);
            if (!KotlinBuiltIns.isUnit(returnType)) {
                trace.report(WRONG_SETTER_RETURN_TYPE.on(returnTypeReference));
            }
        }
        if (parameter != null) {
            // This check is redundant: the parser does not allow a default value, but we'll keep it just in case
            if (parameter.hasDefaultValue()) {
                trace.report(SETTER_PARAMETER_WITH_DEFAULT_VALUE.on(parameter.getDefaultValue()));
            }
            KotlinType type;
            KtTypeReference typeReference = parameter.getTypeReference();
            if (typeReference == null) {
                // TODO : this maybe unknown at this point
                type = propertyDescriptor.getType();
            } else {
                type = typeResolver.resolveType(scopeWithTypeParameters, typeReference, trace, true);
                KotlinType inType = propertyDescriptor.getType();
                if (!TypeUtils.equalTypes(type, inType)) {
                    trace.report(WRONG_SETTER_PARAMETER_TYPE.on(typeReference, inType, type));
                }
            }
            ValueParameterDescriptorImpl valueParameterDescriptor = resolveValueParameterDescriptor(scopeWithTypeParameters, setterDescriptor, parameter, 0, type, trace);
            setterDescriptor.initialize(valueParameterDescriptor);
        } else {
            setterDescriptor.initializeDefault();
        }
        trace.record(BindingContext.PROPERTY_ACCESSOR, setter, setterDescriptor);
    } else if (property.isVar()) {
        Annotations setterAnnotations = annotationSplitter.getAnnotationsForTarget(PROPERTY_SETTER);
        setterDescriptor = DescriptorFactory.createSetter(propertyDescriptor, setterAnnotations, !property.hasDelegate(), /* isExternal = */
        false, property.hasModifier(KtTokens.INLINE_KEYWORD), propertyDescriptor.getSource());
    }
    if (!property.isVar()) {
        if (setter != null) {
            //                trace.getErrorHandler().genericError(setter.asElement().getNode(), "A 'val'-property cannot have a setter");
            trace.report(VAL_WITH_SETTER.on(setter));
        }
    }
    return setterDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Annotations (org.jetbrains.kotlin.descriptors.annotations.Annotations)12 CompositeAnnotations (org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations)8 NotNull (org.jetbrains.annotations.NotNull)6 AnnotationSplitter (org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter)4 FqName (org.jetbrains.kotlin.name.FqName)3 Name (org.jetbrains.kotlin.name.Name)3 Nullable (org.jetbrains.annotations.Nullable)2 LazyTypeAliasDescriptor (org.jetbrains.kotlin.resolve.lazy.descriptors.LazyTypeAliasDescriptor)2 PsiElement (com.intellij.psi.PsiElement)1 AnnotatedWithFakeAnnotations (org.jetbrains.kotlin.codegen.annotation.AnnotatedWithFakeAnnotations)1 AnnotationDescriptor (org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor)1 FilteredAnnotations (org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations)1 TransientReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 SmartSet (org.jetbrains.kotlin.utils.SmartSet)1 Method (org.jetbrains.org.objectweb.asm.commons.Method)1