All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.resolve.BindingContext Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.resolve;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiElement;
import kotlin.Pair;
import kotlin.annotations.jvm.ReadOnly;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.kotlin.cfg.LeakingThisDescriptor;
import org.jetbrains.kotlin.cfg.TailRecursionKind;
import org.jetbrains.kotlin.contracts.description.EventOccurrencesRange;
import org.jetbrains.kotlin.contracts.model.Computation;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext;
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemCompleter;
import org.jetbrains.kotlin.resolve.calls.model.PartialCallContainer;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue;
import org.jetbrains.kotlin.resolve.calls.smartcasts.ExplicitSmartCasts;
import org.jetbrains.kotlin.resolve.calls.smartcasts.ImplicitSmartCasts;
import org.jetbrains.kotlin.resolve.calls.tower.KotlinResolutionCallbacksImpl;
import org.jetbrains.kotlin.resolve.checkers.PrimitiveNumericComparisonInfo;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics;
import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
import org.jetbrains.kotlin.resolve.scopes.receivers.Qualifier;
import org.jetbrains.kotlin.types.DeferredType;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.expressions.CaptureKind;
import org.jetbrains.kotlin.types.expressions.DoubleColonLHS;
import org.jetbrains.kotlin.types.expressions.KotlinTypeInfo;
import org.jetbrains.kotlin.types.expressions.PreliminaryDeclarationVisitor;
import org.jetbrains.kotlin.util.Box;
import org.jetbrains.kotlin.util.slicedMap.*;

import java.util.Collection;
import java.util.Collections;

import static org.jetbrains.kotlin.util.slicedMap.RewritePolicy.DO_NOTHING;
import static org.jetbrains.kotlin.util.slicedMap.Slices.COMPILE_TIME_VALUE_REWRITE_POLICY;
import static org.jetbrains.kotlin.util.slicedMap.Slices.CONSERVATIVE_SET_INCLUSION_SEMANTICS;

public interface BindingContext {
    BindingContext EMPTY = new BindingContext() {
        @NotNull
        @Override
        public Diagnostics getDiagnostics() {
            return Diagnostics.Companion.getEMPTY();
        }

        @Override
        public  V get(ReadOnlySlice slice, K key) {
            return null;
        }

        @NotNull
        @Override
        public  Collection getKeys(WritableSlice slice) {
            return Collections.emptyList();
        }

        @NotNull
        @TestOnly
        @Override
        public  ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice) {
            return ImmutableMap.of();
        }

        @Nullable
        @Override
        public KotlinType getType(@NotNull KtExpression expression) {
            return null;
        }

        @Override
        public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) {
            // Do nothing
        }
    };

    WritableSlice ANNOTATION = Slices.createSimpleSlice();

    WritableSlice> COMPILE_TIME_VALUE = new BasicWritableSlice<>(COMPILE_TIME_VALUE_REWRITE_POLICY);

    WritableSlice TYPE = Slices.createSimpleSlice();
    WritableSlice ABBREVIATED_TYPE = Slices.createSimpleSlice();
    WritableSlice EXPRESSION_TYPE_INFO = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice DATA_FLOW_INFO_BEFORE = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice EXPECTED_EXPRESSION_TYPE = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice EXPRESSION_EFFECTS = Slices.createSimpleSlice();
    WritableSlice CONTRACT_NOT_ALLOWED = Slices.createSimpleSlice();
    WritableSlice IS_CONTRACT_DECLARATION_BLOCK = Slices.createSimpleSlice();
    WritableSlice EXPECTED_RETURN_TYPE = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice DATAFLOW_INFO_AFTER_CONDITION = Slices.createSimpleSlice();
    WritableSlice BOUND_INITIALIZER_VALUE = Slices.createSimpleSlice();
    WritableSlice LEAKING_THIS = Slices.createSimpleSlice();
    WritableSlice UNUSED_MAIN_PARAMETER = Slices.createSimpleSlice();

    WritableSlice UNUSED_DELEGATED_PROPERTY_OPERATOR_PARAMETER = Slices.createSimpleSlice();

    /**
     * A qualifier corresponds to a receiver expression (if any). For 'A.B' qualifier is recorded for 'A'.
     */
    WritableSlice QUALIFIER = new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice DOUBLE_COLON_LHS = new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice THIS_TYPE_FOR_SUPER_EXPRESSION = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice SUPER_EXPRESSION_FROM_ANY_MIGRATION = Slices.createSimpleSlice();

    WritableSlice REFERENCE_TARGET = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice THIS_REFERENCE_TARGET = new BasicWritableSlice<>(DO_NOTHING);
    // if 'A' really means 'A.Companion' then this slice stores class descriptor for A, REFERENCE_TARGET stores descriptor Companion in this case
    WritableSlice SHORT_REFERENCE_TO_COMPANION_OBJECT =
            new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice> RESOLVED_CALL = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice ONLY_RESOLVED_CALL = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice PARTIAL_CALL_RESOLUTION_CONTEXT = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice DELEGATE_EXPRESSION_TO_PROVIDE_DELEGATE_CALL = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice TAIL_RECURSION_CALL = Slices.createSimpleSlice();
    WritableSlice CONSTRAINT_SYSTEM_COMPLETER = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice CALL = new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice> AMBIGUOUS_REFERENCE_TARGET =
            new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice> LOOP_RANGE_ITERATOR_RESOLVED_CALL = Slices.createSimpleSlice();

    WritableSlice> LOOP_RANGE_HAS_NEXT_RESOLVED_CALL = Slices.createSimpleSlice();
    WritableSlice> LOOP_RANGE_NEXT_RESOLVED_CALL = Slices.createSimpleSlice();

    WritableSlice ENCLOSING_SUSPEND_FUNCTION_FOR_SUSPEND_FUNCTION_CALL = Slices.createSimpleSlice();

    WritableSlice> DELEGATED_PROPERTY_RESOLVED_CALL = Slices.createSimpleSlice();

    // Please consider using DELEGATED_PROPERTY_RESOLVED_CALL as it contains fully substituted types
    WritableSlice DELEGATED_PROPERTY_CALL = Slices.createSimpleSlice();
    WritableSlice> PROVIDE_DELEGATE_RESOLVED_CALL = Slices.createSimpleSlice();
    WritableSlice PROVIDE_DELEGATE_CALL = Slices.createSimpleSlice();

    WritableSlice> COMPONENT_RESOLVED_CALL = Slices.createSimpleSlice();

    WritableSlice> INDEXED_LVALUE_GET = Slices.createSimpleSlice();
    WritableSlice> INDEXED_LVALUE_SET = Slices.createSimpleSlice();

    WritableSlice> COLLECTION_LITERAL_CALL = Slices.createSimpleSlice();

    WritableSlice SMARTCAST = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice UNSTABLE_SMARTCAST = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice SMARTCAST_NULL = Slices.createSimpleSlice();
    WritableSlice IMPLICIT_RECEIVER_SMARTCAST = new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice EXHAUSTIVE_WHEN = Slices.createSimpleSlice();
    WritableSlice IMPLICIT_EXHAUSTIVE_WHEN = Slices.createSimpleSlice();

    WritableSlice LEXICAL_SCOPE = Slices.createSimpleSlice();

    WritableSlice SCRIPT_SCOPE = Slices.createSimpleSlice();

    WritableSlice VARIABLE_REASSIGNMENT = Slices.createSimpleSetSlice();
    WritableSlice AUTO_CREATED_IT = Slices.createSimpleSetSlice();

    WritableSlice, Boolean> SUSPEND_LAMBDA_PARAMETER_USED = Slices.createSimpleSlice();

    WritableSlice MARKED_EQUALIY_CALL_PROPER_IN_BUILDER_INFERENCE = Slices.createSimpleSlice();

    /**
     * Has type of current expression has been already resolved
     */
    WritableSlice PROCESSED = Slices.createSimpleSlice();
    // Please do not use this slice (USED_AS_EXPRESSION) directly,
    // use extension element.isUsedAsExpression() instead
    WritableSlice USED_AS_EXPRESSION = new BasicWritableSlice<>(CONSERVATIVE_SET_INCLUSION_SEMANTICS);
    WritableSlice USED_AS_RESULT_OF_LAMBDA = Slices.createSimpleSetSlice();

    WritableSlice CAPTURED_IN_CLOSURE = new BasicWritableSlice<>(DO_NOTHING);
    WritableSlice PRELIMINARY_VISITOR = new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice, Boolean> DEFERRED_TYPE = Slices.createCollectiveSetSlice();

    WritableSlice BACKING_FIELD_REQUIRED = new SetSlice(DO_NOTHING) {
        @Override
        public Boolean computeValue(
                SlicedMap map,
                PropertyDescriptor propertyDescriptor,
                Boolean backingFieldRequired,
                boolean valueNotFound
        ) {
            if (propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.DECLARATION) {
                return false;
            }
            backingFieldRequired = valueNotFound ? false : backingFieldRequired;
            PsiElement declarationPsiElement = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor);
            if (declarationPsiElement instanceof KtParameter) {
                KtParameter jetParameter = (KtParameter) declarationPsiElement;
                return jetParameter.hasValOrVar() ||
                       backingFieldRequired; // this part is unused because we do not allow access to constructor parameters in member bodies
            }
            if (propertyDescriptor.getModality() == Modality.ABSTRACT) return false;
            if (declarationPsiElement instanceof KtProperty &&
                ((KtProperty) declarationPsiElement).hasDelegate()) return false;
            PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
            PropertySetterDescriptor setter = propertyDescriptor.getSetter();

            if (getter == null) return true;
            if (propertyDescriptor.isVar() && setter == null) return true;
            if (setter != null && !DescriptorPsiUtilsKt.hasBody(setter) && setter.getModality() != Modality.ABSTRACT) return true;
            if (!DescriptorPsiUtilsKt.hasBody(getter) && getter.getModality() != Modality.ABSTRACT) return true;

            return backingFieldRequired;
        }
    };
    WritableSlice IS_UNINITIALIZED = Slices.createSimpleSetSlice();
    WritableSlice MUST_BE_LATEINIT = Slices.createSimpleSetSlice();

    WritableSlice LAMBDA_INVOCATIONS = Slices.createSimpleSlice();

    WritableSlice BLOCK = new SetSlice(DO_NOTHING) {
        @Override
        public Boolean computeValue(SlicedMap map, KtLambdaExpression expression, Boolean isBlock, boolean valueNotFound) {
            isBlock = valueNotFound ? false : isBlock;
            return isBlock && !expression.getFunctionLiteral().hasParameterSpecification();
        }
    };

    WritableSlice CLASS = Slices.createSimpleSlice();
    WritableSlice SCRIPT = Slices.createSimpleSlice();
    WritableSlice TYPE_PARAMETER = Slices.createSimpleSlice();
    /**
     * @see BindingContextUtils#recordFunctionDeclarationToDescriptor(BindingTrace, PsiElement, SimpleFunctionDescriptor)}
     */
    WritableSlice FUNCTION = Slices.createSimpleSlice();
    WritableSlice CONSTRUCTOR = Slices.createSimpleSlice();
    WritableSlice> CONSTRUCTOR_RESOLVED_DELEGATION_CALL =
            Slices.createSimpleSlice();
    WritableSlice VARIABLE = Slices.createSimpleSlice();
    WritableSlice VALUE_PARAMETER = Slices.createSimpleSlice();
    WritableSlice PROPERTY_ACCESSOR = Slices.createSimpleSlice();
    WritableSlice PRIMARY_CONSTRUCTOR_PARAMETER = Slices.createSimpleSlice();
    WritableSlice TYPE_ALIAS = Slices.createSimpleSlice();

    WritableSlice DEPRECATED_SHORT_NAME_ACCESS = Slices.createSimpleSlice();

    WritableSlice[] DECLARATIONS_TO_DESCRIPTORS = new WritableSlice[] {
            CLASS, TYPE_PARAMETER, FUNCTION, CONSTRUCTOR, VARIABLE, VALUE_PARAMETER, PROPERTY_ACCESSOR,
            PRIMARY_CONSTRUCTOR_PARAMETER, SCRIPT, TYPE_ALIAS
    };

    @SuppressWarnings("unchecked")
    ReadOnlySlice DECLARATION_TO_DESCRIPTOR =
            Slices.sliceBuilder()
                    .setFurtherLookupSlices(DECLARATIONS_TO_DESCRIPTORS)
                    .build();

    WritableSlice> DESCRIPTOR_TO_CONTEXT_RECEIVER_MAP = Slices.createSimpleSlice();
    WritableSlice LABEL_TARGET = Slices.createSimpleSlice();
    WritableSlice> AMBIGUOUS_LABEL_TARGET = Slices.createSimpleSlice();
    WritableSlice VALUE_PARAMETER_AS_PROPERTY = Slices.createSimpleSlice();

    WritableSlice DATA_CLASS_COMPONENT_FUNCTION = Slices.createSimpleSlice();
    WritableSlice DATA_CLASS_COPY_FUNCTION = Slices.createSimpleSlice();

    WritableSlice FQNAME_TO_CLASS_DESCRIPTOR = new BasicWritableSlice<>(DO_NOTHING, true);
    WritableSlice> PACKAGE_TO_FILES = Slices.createSimpleSlice();
    WritableSlice CAST_TYPE_USED_AS_EXPECTED_TYPE = Slices.createSimpleSlice();

    WritableSlice NEW_INFERENCE_LAMBDA_INFO = new BasicWritableSlice<>(DO_NOTHING);

    WritableSlice PRIMITIVE_NUMERIC_COMPARISON_INFO = Slices.createSimpleSlice();

    WritableSlice> NEW_INFERENCE_CATCH_EXCEPTION_PARAMETER = Slices.createSimpleSlice();
    WritableSlice NEW_INFERENCE_IS_LAMBDA_FOR_OVERLOAD_RESOLUTION_INLINE = Slices.createSimpleSlice();

    @SuppressWarnings("UnusedDeclaration")
    @Deprecated // This field is needed only for the side effects of its initializer
            Void _static_initializer = BasicWritableSlice.initSliceDebugNames(BindingContext.class);

    @NotNull
    Diagnostics getDiagnostics();

    @Nullable
     V get(ReadOnlySlice slice, K key);

    // slice.isCollective() must be true
    @NotNull
    @ReadOnly
     Collection getKeys(WritableSlice slice);

    /** This method should be used only for debug and testing */
    @TestOnly
    @NotNull
     ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice);

    @Nullable
    KotlinType getType(@NotNull KtExpression expression);

    void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy