Search in sources :

Example 1 with DeclarationDescriptor

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

the class KotlinOverridingDialog method formatElement.

private static String formatElement(PsiElement element) {
    element = KtPsiUtil.ascendIfPropertyAccessor(element);
    if (element instanceof KtNamedFunction || element instanceof KtProperty) {
        BindingContext bindingContext = ResolutionUtils.analyze((KtElement) element, BodyResolveMode.FULL);
        DeclarationDescriptor declarationDescriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, element);
        if (declarationDescriptor instanceof CallableMemberDescriptor) {
            DeclarationDescriptor containingDescriptor = declarationDescriptor.getContainingDeclaration();
            if (containingDescriptor instanceof ClassDescriptor) {
                return KotlinBundle.message("x.in.y", DescriptorRenderer.COMPACT.render(declarationDescriptor), IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(containingDescriptor));
            }
        }
    }
    assert element instanceof PsiMethod : "Method accepts only kotlin functions/properties and java methods, but '" + element.getText() + "' was found";
    return RenderingUtilsKt.formatPsiMethod((PsiMethod) element, true, false);
}
Also used : KtProperty(org.jetbrains.kotlin.psi.KtProperty) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) PsiMethod(com.intellij.psi.PsiMethod) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) KtNamedFunction(org.jetbrains.kotlin.psi.KtNamedFunction) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) CallableMemberDescriptor(org.jetbrains.kotlin.descriptors.CallableMemberDescriptor)

Example 2 with DeclarationDescriptor

use of org.jetbrains.kotlin.descriptors.DeclarationDescriptor 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 3 with DeclarationDescriptor

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

the class TracingStrategyImpl method bindReference.

@Override
public <D extends CallableDescriptor> void bindReference(@NotNull BindingTrace trace, @NotNull ResolvedCall<D> resolvedCall) {
    DeclarationDescriptor descriptor = resolvedCall.getCandidateDescriptor();
    if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
        descriptor = ((VariableAsFunctionResolvedCall) resolvedCall).getVariableCall().getCandidateDescriptor();
    }
    if (descriptor instanceof FakeCallableDescriptorForObject) {
        FakeCallableDescriptorForObject fakeCallableDescriptorForObject = (FakeCallableDescriptorForObject) descriptor;
        descriptor = fakeCallableDescriptorForObject.getReferencedDescriptor();
        if (fakeCallableDescriptorForObject.getClassDescriptor().getCompanionObjectDescriptor() != null) {
            trace.record(SHORT_REFERENCE_TO_COMPANION_OBJECT, reference, fakeCallableDescriptorForObject.getClassDescriptor());
        }
    }
    DeclarationDescriptor storedReference = trace.get(REFERENCE_TARGET, reference);
    if (storedReference == null || !ErrorUtils.isError(descriptor)) {
        trace.record(REFERENCE_TARGET, reference, descriptor);
    }
}
Also used : FakeCallableDescriptorForObject(org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject) VariableAsFunctionResolvedCall(org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor)

Example 4 with DeclarationDescriptor

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

the class ExpressionTypingServices method getBlockReturnedType.

@NotNull
public KotlinTypeInfo getBlockReturnedType(@NotNull KtBlockExpression expression, @NotNull CoercionStrategy coercionStrategyForLastExpression, @NotNull ExpressionTypingContext context) {
    List<KtExpression> block = StatementFilterKt.filterStatements(statementFilter, expression);
    DeclarationDescriptor containingDescriptor = context.scope.getOwnerDescriptor();
    TraceBasedLocalRedeclarationChecker redeclarationChecker = new TraceBasedLocalRedeclarationChecker(context.trace, expressionTypingComponents.overloadChecker);
    LexicalWritableScope scope = new LexicalWritableScope(context.scope, containingDescriptor, false, redeclarationChecker, LexicalScopeKind.CODE_BLOCK);
    KotlinTypeInfo r;
    if (block.isEmpty()) {
        r = expressionTypingComponents.dataFlowAnalyzer.createCheckedTypeInfo(expressionTypingComponents.builtIns.getUnitType(), context, expression);
    } else {
        r = getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context.replaceStatementFilter(statementFilter));
    }
    scope.freeze();
    if (containingDescriptor instanceof ScriptDescriptor) {
        context.trace.record(BindingContext.SCRIPT_SCOPE, (ScriptDescriptor) containingDescriptor, scope);
    }
    return r;
}
Also used : LexicalWritableScope(org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope) TraceBasedLocalRedeclarationChecker(org.jetbrains.kotlin.resolve.scopes.TraceBasedLocalRedeclarationChecker) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with DeclarationDescriptor

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

the class CompileKotlinAgainstCustomBinariesTest method testDuplicateObjectInBinaryAndSources.

public void testDuplicateObjectInBinaryAndSources() throws Exception {
    Collection<DeclarationDescriptor> allDescriptors = analyzeAndGetAllDescriptors(compileLibrary("library"));
    assertEquals(allDescriptors.toString(), 2, allDescriptors.size());
    for (DeclarationDescriptor descriptor : allDescriptors) {
        assertTrue("Wrong name: " + descriptor, descriptor.getName().asString().equals("Lol"));
        assertTrue("Should be an object: " + descriptor, isObject(descriptor));
    }
}
Also used : DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor)

Aggregations

DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)18 NotNull (org.jetbrains.annotations.NotNull)5 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)3 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)2 KtNamedFunction (org.jetbrains.kotlin.psi.KtNamedFunction)2 ResolvedCall (org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)2 VariableAsFunctionResolvedCall (org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 Trinity (com.intellij.openapi.util.Trinity)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 IElementType (com.intellij.psi.tree.IElementType)1 File (java.io.File)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 JarFile (java.util.jar.JarFile)1