Search in sources :

Example 6 with Annotations

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

the class DescriptorResolver method resolvePrimaryConstructorParameterToAProperty.

@NotNull
public PropertyDescriptor resolvePrimaryConstructorParameterToAProperty(@NotNull ClassDescriptor classDescriptor, @NotNull ValueParameterDescriptor valueParameter, @NotNull LexicalScope scope, @NotNull KtParameter parameter, final BindingTrace trace) {
    KotlinType type = resolveParameterType(scope, parameter, trace);
    Name name = parameter.getNameAsSafeName();
    boolean isMutable = parameter.isMutable();
    KtModifierList modifierList = parameter.getModifierList();
    if (modifierList != null) {
        if (modifierList.hasModifier(KtTokens.ABSTRACT_KEYWORD)) {
            trace.report(ABSTRACT_PROPERTY_IN_PRIMARY_CONSTRUCTOR_PARAMETERS.on(parameter));
        }
    }
    final AnnotationSplitter.PropertyWrapper propertyWrapper = new AnnotationSplitter.PropertyWrapper(parameter);
    Annotations allAnnotations = annotationResolver.resolveAnnotationsWithoutArguments(scope, parameter.getModifierList(), trace);
    AnnotationSplitter annotationSplitter = new AnnotationSplitter(storageManager, allAnnotations, new Function0<Set<AnnotationUseSiteTarget>>() {

        @Override
        public Set<AnnotationUseSiteTarget> invoke() {
            return AnnotationSplitter.getTargetSet(true, trace.getBindingContext(), propertyWrapper);
        }
    });
    Annotations propertyAnnotations = new CompositeAnnotations(annotationSplitter.getAnnotationsForTargets(PROPERTY, FIELD), annotationSplitter.getOtherAnnotations());
    PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(classDescriptor, propertyAnnotations, resolveMemberModalityFromModifiers(parameter, Modality.FINAL, trace.getBindingContext(), classDescriptor), resolveVisibilityFromModifiers(parameter, getDefaultVisibility(parameter, classDescriptor)), isMutable, name, CallableMemberDescriptor.Kind.DECLARATION, KotlinSourceElementKt.toSourceElement(parameter), false, false, classDescriptor.isHeader(), modifierList != null && modifierList.hasModifier(KtTokens.IMPL_KEYWORD), false, false);
    propertyWrapper.setDescriptor(propertyDescriptor);
    propertyDescriptor.setType(type, Collections.<TypeParameterDescriptor>emptyList(), getDispatchReceiverParameterIfNeeded(classDescriptor), (ReceiverParameterDescriptor) null);
    Annotations setterAnnotations = annotationSplitter.getAnnotationsForTarget(PROPERTY_SETTER);
    Annotations getterAnnotations = new CompositeAnnotations(CollectionsKt.listOf(annotationSplitter.getAnnotationsForTarget(PROPERTY_GETTER)));
    PropertyGetterDescriptorImpl getter = DescriptorFactory.createDefaultGetter(propertyDescriptor, getterAnnotations);
    PropertySetterDescriptor setter = propertyDescriptor.isVar() ? DescriptorFactory.createDefaultSetter(propertyDescriptor, setterAnnotations) : null;
    propertyDescriptor.initialize(getter, setter);
    getter.initialize(propertyDescriptor.getType());
    trace.record(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter, propertyDescriptor);
    trace.record(BindingContext.VALUE_PARAMETER_AS_PROPERTY, valueParameter, propertyDescriptor);
    return propertyDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) 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 7 with Annotations

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

the class DescriptorResolver method resolveTypeAliasDescriptor.

@NotNull
public TypeAliasDescriptor resolveTypeAliasDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull LexicalScope scope, @NotNull KtTypeAlias typeAlias, @NotNull final BindingTrace trace) {
    if (!(containingDeclaration instanceof PackageFragmentDescriptor)) {
        trace.report(TOPLEVEL_TYPEALIASES_ONLY.on(typeAlias));
    }
    KtModifierList modifierList = typeAlias.getModifierList();
    Visibility visibility = resolveVisibilityFromModifiers(typeAlias, getDefaultVisibility(typeAlias, containingDeclaration));
    Annotations allAnnotations = annotationResolver.resolveAnnotationsWithArguments(scope, modifierList, trace);
    final Name name = KtPsiUtil.safeName(typeAlias.getName());
    SourceElement sourceElement = KotlinSourceElementKt.toSourceElement(typeAlias);
    final LazyTypeAliasDescriptor typeAliasDescriptor = LazyTypeAliasDescriptor.create(storageManager, trace, containingDeclaration, allAnnotations, name, sourceElement, visibility);
    List<TypeParameterDescriptorImpl> typeParameterDescriptors;
    final LexicalScope scopeWithTypeParameters;
    {
        List<KtTypeParameter> typeParameters = typeAlias.getTypeParameters();
        if (typeParameters.isEmpty()) {
            scopeWithTypeParameters = scope;
            typeParameterDescriptors = Collections.emptyList();
        } else {
            LexicalWritableScope writableScope = new LexicalWritableScope(scope, containingDeclaration, false, new TraceBasedLocalRedeclarationChecker(trace, overloadChecker), LexicalScopeKind.TYPE_ALIAS_HEADER);
            typeParameterDescriptors = resolveTypeParametersForDescriptor(typeAliasDescriptor, writableScope, scope, typeParameters, trace);
            writableScope.freeze();
            checkNoGenericBoundsOnTypeAliasParameters(typeAlias, trace);
            resolveGenericBounds(typeAlias, typeAliasDescriptor, writableScope, typeParameterDescriptors, trace);
            scopeWithTypeParameters = writableScope;
        }
    }
    final KtTypeReference typeReference = typeAlias.getTypeReference();
    if (typeReference == null) {
        typeAliasDescriptor.initialize(typeParameterDescriptors, ErrorUtils.createErrorType(name.asString()), ErrorUtils.createErrorType(name.asString()));
    } else if (!languageVersionSettings.supportsFeature(LanguageFeature.TypeAliases)) {
        typeResolver.resolveAbbreviatedType(scopeWithTypeParameters, typeReference, trace);
        PsiElement typeAliasKeyword = typeAlias.getTypeAliasKeyword();
        trace.report(UNSUPPORTED_FEATURE.on(typeAliasKeyword != null ? typeAliasKeyword : typeAlias, TuplesKt.to(LanguageFeature.TypeAliases, languageVersionSettings)));
        typeAliasDescriptor.initialize(typeParameterDescriptors, ErrorUtils.createErrorType(name.asString()), ErrorUtils.createErrorType(name.asString()));
    } else {
        typeAliasDescriptor.initialize(typeParameterDescriptors, storageManager.createRecursionTolerantLazyValue(new Function0<SimpleType>() {

            @Override
            public SimpleType invoke() {
                return typeResolver.resolveAbbreviatedType(scopeWithTypeParameters, typeReference, trace);
            }
        }, ErrorUtils.createErrorType("Recursive type alias expansion for " + typeAliasDescriptor.getName().asString())), storageManager.createRecursionTolerantLazyValue(new Function0<SimpleType>() {

            @Override
            public SimpleType invoke() {
                return typeResolver.resolveExpandedTypeForTypeAlias(typeAliasDescriptor);
            }
        }, ErrorUtils.createErrorType("Recursive type alias expansion for " + typeAliasDescriptor.getName().asString())));
    }
    trace.record(TYPE_ALIAS, typeAlias, typeAliasDescriptor);
    return typeAliasDescriptor;
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) LazyTypeAliasDescriptor(org.jetbrains.kotlin.resolve.lazy.descriptors.LazyTypeAliasDescriptor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Annotations

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

the class DescriptorResolver method resolveValueParameterDescriptor.

@NotNull
public ValueParameterDescriptorImpl resolveValueParameterDescriptor(@NotNull final LexicalScope scope, @NotNull final FunctionDescriptor owner, @NotNull KtParameter valueParameter, int index, @NotNull final KotlinType type, @NotNull final BindingTrace trace) {
    KotlinType varargElementType = null;
    KotlinType variableType = type;
    if (valueParameter.hasModifier(VARARG_KEYWORD)) {
        varargElementType = type;
        variableType = getVarargParameterType(type);
    }
    KtModifierList modifierList = valueParameter.getModifierList();
    Annotations allAnnotations = annotationResolver.resolveAnnotationsWithoutArguments(scope, valueParameter.getModifierList(), trace);
    Annotations valueParameterAnnotations = Annotations.Companion.getEMPTY();
    if (modifierList != null) {
        if (valueParameter.hasValOrVar()) {
            AnnotationSplitter annotationSplitter = AnnotationSplitter.create(storageManager, allAnnotations, SetsKt.setOf(CONSTRUCTOR_PARAMETER));
            valueParameterAnnotations = annotationSplitter.getAnnotationsForTarget(CONSTRUCTOR_PARAMETER);
        } else {
            valueParameterAnnotations = allAnnotations;
        }
    }
    final KtDestructuringDeclaration destructuringDeclaration = valueParameter.getDestructuringDeclaration();
    Function0<List<VariableDescriptor>> destructuringVariables;
    if (destructuringDeclaration != null) {
        if (!languageVersionSettings.supportsFeature(LanguageFeature.DestructuringLambdaParameters)) {
            trace.report(Errors.UNSUPPORTED_FEATURE.on(valueParameter, TuplesKt.to(LanguageFeature.DestructuringLambdaParameters, languageVersionSettings)));
        }
        destructuringVariables = new Function0<List<VariableDescriptor>>() {

            @Override
            public List<VariableDescriptor> invoke() {
                assert owner.getDispatchReceiverParameter() == null : "Destructuring declarations are only be parsed for lambdas, and they must not have a dispatch receiver";
                LexicalScope scopeForDestructuring = ScopeUtilsKt.createScopeForDestructuring(scope, owner.getExtensionReceiverParameter());
                List<VariableDescriptor> result = destructuringDeclarationResolver.resolveLocalVariablesFromDestructuringDeclaration(scope, destructuringDeclaration, new TransientReceiver(type), /* initializer = */
                null, ExpressionTypingContext.newContext(trace, scopeForDestructuring, DataFlowInfoFactory.EMPTY, TypeUtils.NO_EXPECTED_TYPE));
                modifiersChecker.withTrace(trace).checkModifiersForDestructuringDeclaration(destructuringDeclaration);
                return result;
            }
        };
    } else {
        destructuringVariables = null;
    }
    Name parameterName;
    if (destructuringDeclaration == null) {
        // NB: val/var for parameter is only allowed in primary constructors where single underscore names are still prohibited.
        // The problem with val/var is that when lazy resolve try to find their descriptor, it searches through the member scope
        // of containing class where, it can not find a descriptor with special name.
        // Thus, to preserve behavior, we don't use a special name for val/var.
        parameterName = !valueParameter.hasValOrVar() && UnderscoreUtilKt.isSingleUnderscore(valueParameter) ? Name.special("<anonymous parameter " + index + ">") : KtPsiUtil.safeName(valueParameter.getName());
    } else {
        parameterName = Name.special("<name for destructuring parameter " + index + ">");
    }
    ValueParameterDescriptorImpl valueParameterDescriptor = ValueParameterDescriptorImpl.createWithDestructuringDeclarations(owner, null, index, valueParameterAnnotations, parameterName, variableType, valueParameter.hasDefaultValue(), valueParameter.hasModifier(CROSSINLINE_KEYWORD), valueParameter.hasModifier(NOINLINE_KEYWORD), varargElementType, KotlinSourceElementKt.toSourceElement(valueParameter), destructuringVariables);
    trace.record(BindingContext.VALUE_PARAMETER, valueParameter, valueParameterDescriptor);
    return valueParameterDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) TransientReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver) AnnotationSplitter(org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter) FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with Annotations

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

the class JetTestFunctionDetector method isTest.

private static boolean isTest(@NotNull FunctionDescriptor functionDescriptor) {
    Annotations annotations = functionDescriptor.getAnnotations();
    for (AnnotationDescriptor annotation : annotations) {
        // TODO ideally we should find the fully qualified name here...
        KotlinType type = annotation.getType();
        String name = type.toString();
        if (name.equals("Test")) {
            return true;
        }
    }
    /*
        if (function.getName().startsWith("test")) {
            List<JetParameter> parameters = function.getValueParameters();
            return parameters.size() == 0;
        }
        */
    return false;
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 10 with Annotations

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

the class PropertyCodegen method genBackingFieldAndAnnotations.

private void genBackingFieldAndAnnotations(@Nullable KtNamedDeclaration declaration, @NotNull PropertyDescriptor descriptor, boolean isParameter) {
    boolean hasBackingField = hasBackingField(descriptor);
    boolean hasDelegate = declaration instanceof KtProperty && ((KtProperty) declaration).hasDelegate();
    AnnotationSplitter annotationSplitter = AnnotationSplitter.create(LockBasedStorageManager.NO_LOCKS, descriptor.getAnnotations(), AnnotationSplitter.getTargetSet(isParameter, descriptor.isVar(), hasBackingField, hasDelegate));
    Annotations propertyAnnotations = annotationSplitter.getAnnotationsForTarget(AnnotationUseSiteTarget.PROPERTY);
    // Fields and '$annotations' methods for non-private const properties are generated in the multi-file facade
    boolean isBackingFieldOwner = descriptor.isConst() && !Visibilities.isPrivate(descriptor.getVisibility()) ? !(context instanceof MultifileClassPartContext) : CodegenContextUtil.isImplClassOwner(context);
    if (isBackingFieldOwner) {
        Annotations fieldAnnotations = annotationSplitter.getAnnotationsForTarget(AnnotationUseSiteTarget.FIELD);
        Annotations delegateAnnotations = annotationSplitter.getAnnotationsForTarget(AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD);
        assert declaration != null : "Declaration is null: " + descriptor + " (context=" + context + ")";
        generateBackingField(declaration, descriptor, fieldAnnotations, delegateAnnotations);
        generateSyntheticMethodIfNeeded(descriptor, propertyAnnotations);
    }
    if (!propertyAnnotations.getAllAnnotations().isEmpty() && kind != OwnerKind.DEFAULT_IMPLS && CodegenContextUtil.isImplClassOwner(context)) {
        v.getSerializationBindings().put(SYNTHETIC_METHOD_FOR_PROPERTY, descriptor, getSyntheticMethodSignature(descriptor));
    }
}
Also used : AnnotatedWithFakeAnnotations(org.jetbrains.kotlin.codegen.annotation.AnnotatedWithFakeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) AnnotationSplitter(org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter)

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