Search in sources :

Example 1 with PropertyDescriptor

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

the class ControlFlowAnalyzer method process.

public void process(@NotNull BodiesResolveContext c) {
    for (KtFile file : c.getFiles()) {
        checkDeclarationContainer(c, file);
    }
    for (KtClassOrObject aClass : c.getDeclaredClasses().keySet()) {
        checkDeclarationContainer(c, aClass);
    }
    for (KtScript script : c.getScripts().keySet()) {
        checkDeclarationContainer(c, script);
    }
    for (KtSecondaryConstructor constructor : c.getSecondaryConstructors().keySet()) {
        checkSecondaryConstructor(constructor);
    }
    for (Map.Entry<KtNamedFunction, SimpleFunctionDescriptor> entry : c.getFunctions().entrySet()) {
        KtNamedFunction function = entry.getKey();
        SimpleFunctionDescriptor functionDescriptor = entry.getValue();
        KotlinType expectedReturnType = !function.hasBlockBody() && !function.hasDeclaredReturnType() ? NO_EXPECTED_TYPE : functionDescriptor.getReturnType();
        checkFunction(c, function, expectedReturnType);
    }
    for (Map.Entry<KtProperty, PropertyDescriptor> entry : c.getProperties().entrySet()) {
        KtProperty property = entry.getKey();
        PropertyDescriptor propertyDescriptor = entry.getValue();
        checkProperty(c, property, propertyDescriptor);
    }
}
Also used : PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) SimpleFunctionDescriptor(org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor) Map(java.util.Map)

Example 2 with PropertyDescriptor

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

the class FieldOwnerContext method getFieldName.

@NotNull
public String getFieldName(@NotNull PropertyDescriptor possiblySubstitutedDescriptor, boolean isDelegated) {
    if (possiblySubstitutedDescriptor instanceof AccessorForPropertyDescriptor) {
        possiblySubstitutedDescriptor = ((AccessorForPropertyDescriptor) possiblySubstitutedDescriptor).getCalleeDescriptor();
    }
    PropertyDescriptor descriptor = possiblySubstitutedDescriptor.getOriginal();
    assert descriptor.getKind().isReal() : "Only declared properties can have backing fields: " + descriptor;
    String defaultPropertyName = KotlinTypeMapper.mapDefaultFieldName(descriptor, isDelegated);
    Map<PropertyDescriptor, String> descriptor2Name = fieldNames.get(defaultPropertyName);
    if (descriptor2Name == null) {
        descriptor2Name = new HashMap<PropertyDescriptor, String>();
        fieldNames.put(defaultPropertyName, descriptor2Name);
    }
    String actualName = descriptor2Name.get(descriptor);
    if (actualName != null)
        return actualName;
    String newName = descriptor2Name.isEmpty() || AnnotationUtilKt.hasJvmFieldAnnotation(descriptor) ? defaultPropertyName : defaultPropertyName + "$" + descriptor2Name.size();
    descriptor2Name.put(descriptor, newName);
    return newName;
}
Also used : AccessorForPropertyDescriptor(org.jetbrains.kotlin.codegen.AccessorForPropertyDescriptor) PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) AccessorForPropertyDescriptor(org.jetbrains.kotlin.codegen.AccessorForPropertyDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PropertyDescriptor

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

the class DebugInfoUtil method markDebugAnnotations.

public static void markDebugAnnotations(@NotNull PsiElement root, @NotNull final BindingContext bindingContext, @NotNull final DebugInfoReporter debugInfoReporter) {
    final Map<KtReferenceExpression, DiagnosticFactory<?>> markedWithErrorElements = Maps.newHashMap();
    for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
        DiagnosticFactory<?> factory = diagnostic.getFactory();
        if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) {
            markedWithErrorElements.put((KtReferenceExpression) diagnostic.getPsiElement(), factory);
        } else if (factory == Errors.SUPER_IS_NOT_AN_EXPRESSION || factory == Errors.SUPER_NOT_AVAILABLE) {
            KtSuperExpression superExpression = (KtSuperExpression) diagnostic.getPsiElement();
            markedWithErrorElements.put(superExpression.getInstanceReference(), factory);
        } else if (factory == Errors.EXPRESSION_EXPECTED_PACKAGE_FOUND) {
            markedWithErrorElements.put((KtSimpleNameExpression) diagnostic.getPsiElement(), factory);
        } else if (factory == Errors.UNSUPPORTED) {
            for (KtReferenceExpression reference : PsiTreeUtil.findChildrenOfType(diagnostic.getPsiElement(), KtReferenceExpression.class)) {
                markedWithErrorElements.put(reference, factory);
            }
        }
    }
    root.acceptChildren(new KtTreeVisitorVoid() {

        @Override
        public void visitForExpression(@NotNull KtForExpression expression) {
            KtExpression range = expression.getLoopRange();
            reportIfDynamicCall(range, range, LOOP_RANGE_ITERATOR_RESOLVED_CALL);
            reportIfDynamicCall(range, range, LOOP_RANGE_HAS_NEXT_RESOLVED_CALL);
            reportIfDynamicCall(range, range, LOOP_RANGE_NEXT_RESOLVED_CALL);
            super.visitForExpression(expression);
        }

        @Override
        public void visitDestructuringDeclaration(@NotNull KtDestructuringDeclaration destructuringDeclaration) {
            for (KtDestructuringDeclarationEntry entry : destructuringDeclaration.getEntries()) {
                reportIfDynamicCall(entry, entry, COMPONENT_RESOLVED_CALL);
            }
            super.visitDestructuringDeclaration(destructuringDeclaration);
        }

        @Override
        public void visitProperty(@NotNull KtProperty property) {
            VariableDescriptor descriptor = bindingContext.get(VARIABLE, property);
            if (descriptor instanceof PropertyDescriptor && property.getDelegate() != null) {
                PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
                reportIfDynamicCall(property.getDelegate(), propertyDescriptor, PROVIDE_DELEGATE_RESOLVED_CALL);
                reportIfDynamicCall(property.getDelegate(), propertyDescriptor.getGetter(), DELEGATED_PROPERTY_RESOLVED_CALL);
                reportIfDynamicCall(property.getDelegate(), propertyDescriptor.getSetter(), DELEGATED_PROPERTY_RESOLVED_CALL);
            }
            super.visitProperty(property);
        }

        @Override
        public void visitThisExpression(@NotNull KtThisExpression expression) {
            ResolvedCall<? extends CallableDescriptor> resolvedCall = CallUtilKt.getResolvedCall(expression, bindingContext);
            if (resolvedCall != null) {
                reportIfDynamic(expression, resolvedCall.getResultingDescriptor(), debugInfoReporter);
            }
            super.visitThisExpression(expression);
        }

        @Override
        public void visitReferenceExpression(@NotNull KtReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            if (!BindingContextUtils.isExpressionWithValidReference(expression, bindingContext)) {
                return;
            }
            IElementType referencedNameElementType = null;
            if (expression instanceof KtSimpleNameExpression) {
                KtSimpleNameExpression nameExpression = (KtSimpleNameExpression) expression;
                IElementType elementType = expression.getNode().getElementType();
                if (elementType == KtNodeTypes.OPERATION_REFERENCE) {
                    referencedNameElementType = nameExpression.getReferencedNameElementType();
                    if (EXCLUDED.contains(referencedNameElementType)) {
                        return;
                    }
                }
                if (elementType == KtNodeTypes.LABEL || nameExpression.getReferencedNameElementType() == KtTokens.THIS_KEYWORD) {
                    return;
                }
            }
            debugInfoReporter.preProcessReference(expression);
            String target = null;
            DeclarationDescriptor declarationDescriptor = bindingContext.get(REFERENCE_TARGET, expression);
            if (declarationDescriptor != null) {
                target = declarationDescriptor.toString();
                reportIfDynamic(expression, declarationDescriptor, debugInfoReporter);
            }
            if (target == null) {
                PsiElement labelTarget = bindingContext.get(LABEL_TARGET, expression);
                if (labelTarget != null) {
                    target = labelTarget.getText();
                }
            }
            if (target == null) {
                Collection<? extends DeclarationDescriptor> declarationDescriptors = bindingContext.get(AMBIGUOUS_REFERENCE_TARGET, expression);
                if (declarationDescriptors != null) {
                    target = "[" + declarationDescriptors.size() + " descriptors]";
                }
            }
            if (target == null) {
                Collection<? extends PsiElement> labelTargets = bindingContext.get(AMBIGUOUS_LABEL_TARGET, expression);
                if (labelTargets != null) {
                    target = "[" + labelTargets.size() + " elements]";
                }
            }
            if (MAY_BE_UNRESOLVED.contains(referencedNameElementType)) {
                return;
            }
            boolean resolved = target != null;
            boolean markedWithError = markedWithErrorElements.containsKey(expression);
            if (expression instanceof KtArrayAccessExpression && markedWithErrorElements.containsKey(((KtArrayAccessExpression) expression).getArrayExpression())) {
                // if 'foo' in 'foo[i]' is unresolved it means 'foo[i]' is unresolved (otherwise 'foo[i]' is marked as 'missing unresolved')
                markedWithError = true;
            }
            KotlinType expressionType = bindingContext.getType(expression);
            DiagnosticFactory<?> factory = markedWithErrorElements.get(expression);
            if (declarationDescriptor != null && (ErrorUtils.isError(declarationDescriptor) || ErrorUtils.containsErrorType(expressionType))) {
                if (factory != Errors.EXPRESSION_EXPECTED_PACKAGE_FOUND) {
                    debugInfoReporter.reportElementWithErrorType(expression);
                }
            }
            if (resolved && markedWithError) {
                if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(factory)) {
                    debugInfoReporter.reportUnresolvedWithTarget(expression, target);
                }
            } else if (!resolved && !markedWithError) {
                debugInfoReporter.reportMissingUnresolved(expression);
            }
        }

        private <E extends KtElement, K, D extends CallableDescriptor> boolean reportIfDynamicCall(E element, K key, WritableSlice<K, ResolvedCall<D>> slice) {
            ResolvedCall<D> resolvedCall = bindingContext.get(slice, key);
            if (resolvedCall != null) {
                return reportIfDynamic(element, resolvedCall.getResultingDescriptor(), debugInfoReporter);
            }
            return false;
        }
    });
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor) CallableDescriptor(org.jetbrains.kotlin.descriptors.CallableDescriptor) PsiElement(com.intellij.psi.PsiElement) PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) DiagnosticFactory(org.jetbrains.kotlin.diagnostics.DiagnosticFactory) IElementType(com.intellij.psi.tree.IElementType) ResolvedCall(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) Collection(java.util.Collection)

Example 4 with PropertyDescriptor

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

the class InitializerVisitor method visitProperty.

@Override
public final Void visitProperty(@NotNull KtProperty property, @NotNull TranslationContext context) {
    PropertyDescriptor descriptor = BindingUtils.getPropertyDescriptor(context.bindingContext(), property);
    JsExpression value = PropertyTranslatorKt.translateDelegateOrInitializerExpression(context, property);
    JsStatement statement = null;
    KtExpression initializer = property.getInitializer();
    KtExpression delegate = property.getDelegateExpression();
    if (initializer != null) {
        assert value != null;
        statement = generateInitializerForProperty(context, descriptor, value);
    } else if (delegate != null) {
        assert value != null;
        statement = generateInitializerForDelegate(descriptor, value);
    } else if (Boolean.TRUE.equals(context.bindingContext().get(BindingContext.BACKING_FIELD_REQUIRED, descriptor))) {
        JsNameRef backingFieldReference = TranslationUtils.backingFieldReference(context, descriptor);
        JsExpression defaultValue = generateDefaultValue(descriptor, context, backingFieldReference);
        statement = TranslationUtils.assignmentToBackingField(context, descriptor, defaultValue).makeStmt();
    } else if (JsDescriptorUtils.isSimpleFinalProperty(descriptor)) {
        JsNameRef propRef = new JsNameRef(context.getNameForDescriptor(descriptor), JsLiteral.THIS);
        JsExpression defaultValue = generateDefaultValue(descriptor, context, propRef);
        statement = JsAstUtils.assignment(propRef, defaultValue).makeStmt();
    }
    if (statement != null && !JsAstUtils.isEmptyStatement(statement)) {
        context.addStatementsToCurrentBlock(JsAstUtils.flattenStatement(statement));
    }
    return null;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) JsStatement(org.jetbrains.kotlin.js.backend.ast.JsStatement) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef)

Example 5 with PropertyDescriptor

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

the class AssignmentTranslator method isReferenceToBackingFieldFromConstructor.

private static boolean isReferenceToBackingFieldFromConstructor(@Nullable DeclarationDescriptor descriptor, @NotNull TranslationContext context) {
    if (!(descriptor instanceof PropertyDescriptor))
        return false;
    PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
    if (!(context.getDeclarationDescriptor() instanceof ClassDescriptor))
        return false;
    ClassDescriptor classDescriptor = (ClassDescriptor) context.getDeclarationDescriptor();
    if (classDescriptor != propertyDescriptor.getContainingDeclaration())
        return false;
    return !propertyDescriptor.isVar();
}
Also used : PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor)

Aggregations

PropertyDescriptor (org.jetbrains.kotlin.descriptors.PropertyDescriptor)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 PsiElement (com.intellij.psi.PsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 Collection (java.util.Collection)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1 AccessorForPropertyDescriptor (org.jetbrains.kotlin.codegen.AccessorForPropertyDescriptor)1 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)1 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 SimpleFunctionDescriptor (org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1 Diagnostic (org.jetbrains.kotlin.diagnostics.Diagnostic)1 DiagnosticFactory (org.jetbrains.kotlin.diagnostics.DiagnosticFactory)1 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)1 JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)1 JsStatement (org.jetbrains.kotlin.js.backend.ast.JsStatement)1 ResolvedCall (org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)1