[FE] Add context receivers to scope

This commit is contained in:
Anastasiya Shadrina
2021-02-16 23:47:56 +07:00
committed by TeamCityServer
parent c5687e080d
commit d923c95671
27 changed files with 166 additions and 59 deletions
@@ -162,7 +162,7 @@ public class BodyResolver {
descriptor, localContext != null ? localContext.inferenceSession : null
),
scope -> new LexicalScopeImpl(
scope, descriptor, scope.isOwnerDescriptorAccessibleByLabel(), scope.getImplicitReceiver(),
scope, descriptor, scope.isOwnerDescriptorAccessibleByLabel(), scope.getImplicitReceivers(),
LexicalScopeKind.CONSTRUCTOR_HEADER
),
localContext
@@ -439,7 +439,7 @@ public class BodyResolver {
) {
// Initializing a scope will report errors if any.
new LexicalScopeImpl(
scopeForConstructorResolution, descriptor, true, null, LexicalScopeKind.CLASS_HEADER,
scopeForConstructorResolution, descriptor, true, Collections.emptyList(), LexicalScopeKind.CLASS_HEADER,
new TraceBasedLocalRedeclarationChecker(trace, overloadChecker),
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
@@ -780,7 +780,7 @@ public class BodyResolver {
LexicalScope originalScope,
ConstructorDescriptor unsubstitutedPrimaryConstructor
) {
return new LexicalScopeImpl(originalScope, unsubstitutedPrimaryConstructor, false, null,
return new LexicalScopeImpl(originalScope, unsubstitutedPrimaryConstructor, false, Collections.emptyList(),
LexicalScopeKind.DEFAULT_VALUE, LocalRedeclarationChecker.DO_NOTHING.INSTANCE,
handler -> {
for (ValueParameterDescriptor valueParameter : unsubstitutedPrimaryConstructor.getValueParameters()) {
@@ -863,7 +863,12 @@ public class BodyResolver {
LexicalScope accessorDeclaringScope = c.getDeclaringScope(accessor);
assert accessorDeclaringScope != null : "Scope for accessor " + accessor.getText() + " should exists";
LexicalScope headerScope = ScopeUtils.makeScopeForPropertyHeader(accessorDeclaringScope, descriptor);
return new LexicalScopeImpl(headerScope, descriptor, true, descriptor.getExtensionReceiverParameter(),
List<ReceiverParameterDescriptor> implicitReceivers = new ArrayList<>();
ReceiverParameterDescriptor extensionReceiverParameter = descriptor.getExtensionReceiverParameter();
if (extensionReceiverParameter != null) {
implicitReceivers.add(extensionReceiverParameter);
}
return new LexicalScopeImpl(headerScope, descriptor, true, implicitReceivers,
LexicalScopeKind.PROPERTY_ACCESSOR_BODY);
}
@@ -1023,7 +1028,7 @@ public class BodyResolver {
KtProperty property = (KtProperty) function.getParent();
SourceElement propertySourceElement = KotlinSourceElementKt.toSourceElement(property);
SyntheticFieldDescriptor fieldDescriptor = new SyntheticFieldDescriptor(accessorDescriptor, propertySourceElement);
innerScope = new LexicalScopeImpl(innerScope, functionDescriptor, true, null,
innerScope = new LexicalScopeImpl(innerScope, functionDescriptor, true, Collections.emptyList(),
LexicalScopeKind.PROPERTY_ACCESSOR_BODY,
LocalRedeclarationChecker.DO_NOTHING.INSTANCE, handler -> {
handler.addVariableDescriptor(fieldDescriptor);
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.resolve.scopes.*;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import java.util.ArrayList;
import java.util.List;
public class FunctionDescriptorUtil {
@@ -65,8 +66,17 @@ public class FunctionDescriptorUtil {
@NotNull FunctionDescriptor descriptor,
@NotNull LocalRedeclarationChecker redeclarationChecker
) {
List<ReceiverParameterDescriptor> implicitReceivers = new ArrayList<>();
ReceiverParameterDescriptor extensionReceiverParameter = descriptor.getExtensionReceiverParameter();
if (descriptor.getExtensionReceiverParameter() != null) {
implicitReceivers.add(extensionReceiverParameter);
}
List<ReceiverParameterDescriptor> contextReceiverParameters = descriptor.getContextReceiverParameters();
if (!contextReceiverParameters.isEmpty()) {
implicitReceivers.addAll(contextReceiverParameters);
}
return new LexicalScopeImpl(
outerScope, descriptor, true, descriptor.getExtensionReceiverParameter(),
outerScope, descriptor, true, implicitReceivers,
LexicalScopeKind.FUNCTION_INNER_SCOPE, redeclarationChecker,
handler -> {
for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) {
@@ -49,7 +49,8 @@ object DslScopeViolationCallChecker : CallChecker {
) {
val receiversUntilOneFromTheCall =
context.scope.parentsWithSelf
.mapNotNull { (it as? LexicalScope)?.implicitReceiver?.value }
.flatMap { (it as? LexicalScope)?.implicitReceivers ?: emptyList() }
.map { it.value }
.takeWhile { it != callImplicitReceiver }.toList()
if (receiversUntilOneFromTheCall.isEmpty()) return
@@ -366,10 +366,8 @@ class NewResolutionOldInference(
) : ImplicitScopeTower {
private val cache = HashMap<ReceiverValue, ReceiverValueWithSmartCastInfo>()
override fun getImplicitReceiver(scope: LexicalScope): ReceiverValueWithSmartCastInfo? =
scope.implicitReceiver?.value?.let {
cache.getOrPut(it) { resolutionContext.transformToReceiverWithSmartCastInfo(it) }
}
override fun getImplicitReceivers(scope: LexicalScope): List<ReceiverValueWithSmartCastInfo> =
scope.implicitReceivers.map { cache.getOrPut(it.value) { resolutionContext.transformToReceiverWithSmartCastInfo(it.value) } }
override fun getNameForGivenImportAlias(name: Name): Name? =
(resolutionContext.call.callElement.containingFile as? KtFile)?.getNameForGivenImportAlias(name)
@@ -400,13 +400,8 @@ class PSICallResolver(
override val implicitsResolutionFilter: ImplicitsExtensionsResolutionFilter get() = this@PSICallResolver.implicitsResolutionFilter
private val cache = HashMap<ReceiverParameterDescriptor, ReceiverValueWithSmartCastInfo>()
override fun getImplicitReceiver(scope: LexicalScope): ReceiverValueWithSmartCastInfo? {
val implicitReceiver = scope.implicitReceiver ?: return null
return cache.getOrPut(implicitReceiver) {
context.transformToReceiverWithSmartCastInfo(implicitReceiver.value)
}
}
override fun getImplicitReceivers(scope: LexicalScope): List<ReceiverValueWithSmartCastInfo> =
scope.implicitReceivers.map { cache.getOrPut(it) { context.transformToReceiverWithSmartCastInfo(it.value) } }
override fun getNameForGivenImportAlias(name: Name): Name? =
(context.call.callElement.containingFile as? KtFile)?.getNameForGivenImportAlias(name)
@@ -35,7 +35,7 @@ class ClassResolutionScopesSupport(
private val getOuterScope: () -> LexicalScope
) {
private fun scopeWithGenerics(parent: LexicalScope): LexicalScopeImpl {
return LexicalScopeImpl(parent, classDescriptor, false, null, LexicalScopeKind.CLASS_HEADER) {
return LexicalScopeImpl(parent, classDescriptor, false, emptyList(), LexicalScopeKind.CLASS_HEADER) {
classDescriptor.declaredTypeParameters.forEach { addClassifierDescriptor(it) }
}
}
@@ -69,7 +69,7 @@ class ClassResolutionScopesSupport(
scopeWithGenerics,
classDescriptor,
true,
classDescriptor.thisAsReceiverParameter,
listOf(classDescriptor.thisAsReceiverParameter),
LexicalScopeKind.CLASS_MEMBER_SCOPE
)
}
@@ -96,7 +96,7 @@ class ClassResolutionScopesSupport(
val lexicalChainedScope = LexicalChainedScope.create(
parentForNewScope, ownerDescriptor,
isOwnerDescriptorAccessibleByLabel = false,
implicitReceiver = companionObjectDescriptor?.thisAsReceiverParameter,
implicitReceivers = listOfNotNull(companionObjectDescriptor?.thisAsReceiverParameter),
kind = LexicalScopeKind.CLASS_INHERITANCE,
classDescriptor.staticScope,
classDescriptor.unsubstitutedInnerClassesScope,
@@ -146,7 +146,7 @@ fun scopeForInitializerResolution(
classDescriptor.scopeForMemberDeclarationResolution,
parentDescriptor,
false,
null,
emptyList(),
LexicalScopeKind.CLASS_INITIALIZER
) {
if (primaryConstructorParameters.isNotEmpty()) {
@@ -50,7 +50,7 @@ class ValueParameterResolver(
inferenceSession: InferenceSession?
) {
val scopeForDefaultValue =
LexicalScopeImpl(declaringScope, declaringScope.ownerDescriptor, false, null, LexicalScopeKind.DEFAULT_VALUE)
LexicalScopeImpl(declaringScope, declaringScope.ownerDescriptor, false, listOf(), LexicalScopeKind.DEFAULT_VALUE)
val contextForDefaultValue = ExpressionTypingContext.newContext(
trace, scopeForDefaultValue, dataFlowInfo, TypeUtils.NO_EXPECTED_TYPE,