Search in sources :

Example 1 with ImplicitReceiver

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

the class ImplementationBodyCodegen method lookupConstructorExpressionsInClosureIfPresent.

private void lookupConstructorExpressionsInClosureIfPresent() {
    if (!state.getClassBuilderMode().generateBodies || descriptor.getConstructors().isEmpty())
        return;
    KtVisitorVoid visitor = new KtVisitorVoid() {

        @Override
        public void visitKtElement(@NotNull KtElement e) {
            e.acceptChildren(this);
        }

        @Override
        public void visitSimpleNameExpression(@NotNull KtSimpleNameExpression expr) {
            DeclarationDescriptor descriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expr);
            if (isLocalFunction(descriptor)) {
                lookupInContext(descriptor);
            } else if (descriptor instanceof CallableMemberDescriptor) {
                ResolvedCall<? extends CallableDescriptor> call = CallUtilKt.getResolvedCall(expr, bindingContext);
                if (call != null) {
                    lookupReceivers(call);
                }
                if (call instanceof VariableAsFunctionResolvedCall) {
                    lookupReceivers(((VariableAsFunctionResolvedCall) call).getVariableCall());
                }
            } else if (descriptor instanceof VariableDescriptor) {
                DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
                if (containingDeclaration instanceof ConstructorDescriptor) {
                    ClassDescriptor classDescriptor = ((ConstructorDescriptor) containingDeclaration).getConstructedClass();
                    if (classDescriptor == ImplementationBodyCodegen.this.descriptor)
                        return;
                }
                lookupInContext(descriptor);
            }
        }

        private void lookupReceivers(@NotNull ResolvedCall<? extends CallableDescriptor> call) {
            lookupReceiver(call.getDispatchReceiver());
            lookupReceiver(call.getExtensionReceiver());
        }

        private void lookupReceiver(@Nullable ReceiverValue value) {
            if (value instanceof ImplicitReceiver) {
                if (value instanceof ExtensionReceiver) {
                    ReceiverParameterDescriptor parameter = ((ExtensionReceiver) value).getDeclarationDescriptor().getExtensionReceiverParameter();
                    assert parameter != null : "Extension receiver should exist: " + ((ExtensionReceiver) value).getDeclarationDescriptor();
                    lookupInContext(parameter);
                } else {
                    lookupInContext(((ImplicitReceiver) value).getDeclarationDescriptor());
                }
            }
        }

        private void lookupInContext(@NotNull DeclarationDescriptor toLookup) {
            ExpressionCodegen.lookupValuaAndLocalVariableMetadata(toLookup, StackValue.LOCAL_0, state, true, context, null);
        }

        @Override
        public void visitThisExpression(@NotNull KtThisExpression expression) {
            DeclarationDescriptor descriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expression.getInstanceReference());
            assert descriptor instanceof CallableDescriptor || descriptor instanceof ClassDescriptor : "'This' reference target should be class or callable descriptor but was " + descriptor;
            if (descriptor instanceof ClassDescriptor) {
                lookupInContext(descriptor);
            }
            if (descriptor instanceof CallableDescriptor) {
                ReceiverParameterDescriptor parameter = ((CallableDescriptor) descriptor).getExtensionReceiverParameter();
                if (parameter != null) {
                    lookupInContext(parameter);
                }
            }
        }

        @Override
        public void visitSuperExpression(@NotNull KtSuperExpression expression) {
            lookupInContext(ExpressionCodegen.getSuperCallLabelTarget(context, expression));
        }
    };
    for (KtDeclaration declaration : myClass.getDeclarations()) {
        if (declaration instanceof KtProperty) {
            KtProperty property = (KtProperty) declaration;
            KtExpression initializer = property.getDelegateExpressionOrInitializer();
            if (initializer != null) {
                initializer.accept(visitor);
            }
        } else if (declaration instanceof KtAnonymousInitializer) {
            KtAnonymousInitializer initializer = (KtAnonymousInitializer) declaration;
            initializer.accept(visitor);
        } else if (declaration instanceof KtSecondaryConstructor) {
            KtSecondaryConstructor constructor = (KtSecondaryConstructor) declaration;
            constructor.accept(visitor);
        }
    }
    for (KtSuperTypeListEntry specifier : myClass.getSuperTypeListEntries()) {
        if (specifier instanceof KtDelegatedSuperTypeEntry) {
            KtExpression delegateExpression = ((KtDelegatedSuperTypeEntry) specifier).getDelegateExpression();
            assert delegateExpression != null;
            delegateExpression.accept(visitor);
        } else if (specifier instanceof KtSuperTypeCallEntry) {
            specifier.accept(visitor);
        }
    }
}
Also used : JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) ExtensionReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull) ImplicitReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver) Nullable(org.jetbrains.annotations.Nullable) ReceiverValue(org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 BindingContextUtils.getNotNull (org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)1 ExtensionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver)1 ImplicitReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver)1 ReceiverValue (org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue)1