org.jetbrains.kotlin.resolve.BindingContext Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.kotlin.cfg.TailRecursionKind;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemCompleter;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
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.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;
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 = Slices.createSimpleSlice();
WritableSlice TYPE = Slices.createSimpleSlice();
WritableSlice EXPRESSION_TYPE_INFO = new BasicWritableSlice(DO_NOTHING);
WritableSlice EXPECTED_EXPRESSION_TYPE = new BasicWritableSlice(DO_NOTHING);
WritableSlice EXPECTED_RETURN_TYPE = new BasicWritableSlice(DO_NOTHING);
WritableSlice DATAFLOW_INFO_AFTER_CONDITION = 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 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, TailRecursionKind> 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> DELEGATED_PROPERTY_RESOLVED_CALL = Slices.createSimpleSlice();
WritableSlice DELEGATED_PROPERTY_CALL = Slices.createSimpleSlice();
WritableSlice> DELEGATED_PROPERTY_PD_RESOLVED_CALL = Slices.createSimpleSlice();
WritableSlice> COMPONENT_RESOLVED_CALL = Slices.createSimpleSlice();
WritableSlice> INDEXED_LVALUE_GET = Slices.createSimpleSlice();
WritableSlice> INDEXED_LVALUE_SET = Slices.createSimpleSlice();
WritableSlice SMARTCAST = Slices.createSimpleSlice();
WritableSlice SMARTCAST_NULL = Slices.createSimpleSlice();
WritableSlice IMPLICIT_RECEIVER_SMARTCAST = Slices.createSimpleSlice();
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();
/**
* Has type of current expression has been already resolved
*/
WritableSlice PROCESSED = Slices.createSimpleSetSlice();
WritableSlice USED_AS_EXPRESSION = Slices.createSimpleSetSlice();
WritableSlice USED_AS_RESULT_OF_LAMBDA = Slices.createSimpleSetSlice();
WritableSlice UNREACHABLE_CODE = 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 Slices.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 && !setter.hasBody() && setter.getModality() != Modality.ABSTRACT) return true;
if (!getter.hasBody() && getter.getModality() != Modality.ABSTRACT) return true;
return backingFieldRequired;
}
};
WritableSlice IS_UNINITIALIZED = Slices.createSimpleSetSlice();
WritableSlice BLOCK = new Slices.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[] DECLARATIONS_TO_DESCRIPTORS = new WritableSlice[] {
CLASS, TYPE_PARAMETER, FUNCTION, CONSTRUCTOR, VARIABLE, VALUE_PARAMETER, PROPERTY_ACCESSOR,
PRIMARY_CONSTRUCTOR_PARAMETER, SCRIPT
};
@SuppressWarnings("unchecked")
ReadOnlySlice DECLARATION_TO_DESCRIPTOR =
Slices.sliceBuilder()
.setFurtherLookupSlices(DECLARATIONS_TO_DESCRIPTORS)
.build();
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 FILE_TO_PACKAGE_FRAGMENT = Slices.createSimpleSlice();
WritableSlice> PACKAGE_TO_FILES = 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);
}