Removed lock level WRITING from LexicalWritableScope.

This commit is contained in:
Stanislav Erokhin
2016-02-24 21:40:30 +03:00
parent f85abed6e6
commit 310c68f633
7 changed files with 26 additions and 68 deletions
@@ -724,7 +724,7 @@ public class DescriptorResolver {
LexicalScopeKind.PROPERTY_HEADER);
typeParameterDescriptors = resolveTypeParametersForCallableDescriptor(
propertyDescriptor, writableScope, scope, typeParameters, trace);
writableScope.changeLockLevel(LexicalWritableScope.LockLevel.READING);
writableScope.freeze();
resolveGenericBounds(property, propertyDescriptor, writableScope, typeParameterDescriptors, trace);
scopeWithTypeParameters = writableScope;
}
@@ -143,7 +143,6 @@ class FunctionDescriptorResolver(
val typeParameterDescriptors = descriptorResolver.
resolveTypeParametersForCallableDescriptor(functionDescriptor, innerScope, scope, function.typeParameters, trace)
innerScope.changeLockLevel(LexicalWritableScope.LockLevel.BOTH)
descriptorResolver.resolveGenericBounds(function, functionDescriptor, innerScope, typeParameterDescriptors, trace)
val receiverTypeRef = function.receiverTypeReference
@@ -156,7 +155,7 @@ class FunctionDescriptorResolver(
val valueParameterDescriptors = createValueParameterDescriptors(function, functionDescriptor, innerScope, trace, expectedFunctionType)
innerScope.changeLockLevel(LexicalWritableScope.LockLevel.READING)
innerScope.freeze()
val returnType = function.typeReference?.let { typeResolver.resolveType(innerScope, it, trace, true) }
@@ -283,7 +282,6 @@ class FunctionDescriptorResolver(
TraceBasedLocalRedeclarationChecker(trace),
LexicalScopeKind.CONSTRUCTOR_HEADER
)
parameterScope.changeLockLevel(LexicalWritableScope.LockLevel.BOTH)
val constructor = constructorDescriptor.initialize(
resolveValueParameters(constructorDescriptor, parameterScope, valueParameters, trace, null),
resolveVisibilityFromModifiers(
@@ -17,8 +17,6 @@
package org.jetbrains.kotlin.resolve.scopes
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.Printer
class LexicalScopeImpl @JvmOverloads constructor(
@@ -35,12 +33,6 @@ class LexicalScopeImpl @JvmOverloads constructor(
InitializeHandler().initialize()
}
override fun getContributedClassifier(name: Name, location: LookupLocation) = getClassifier(name)
override fun getContributedVariables(name: Name, location: LookupLocation) = getVariables(name)
override fun getContributedFunctions(name: Name, location: LookupLocation) = getFunctions(name)
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = addedDescriptors
override fun toString(): String = kind.toString()
override fun printStructure(p: Printer) {
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.utils.takeSnapshot
import java.util.*
@@ -36,6 +37,14 @@ abstract class LexicalScopeStorage(
private var functionsByName: MutableMap<Name, IntList>? = null
private var variablesAndClassifiersByName: MutableMap<Name, IntList>? = null
override fun getContributedClassifier(name: Name, location: LookupLocation) = variableOrClassDescriptorByName(name) as? ClassifierDescriptor
override fun getContributedVariables(name: Name, location: LookupLocation) = listOfNotNull(variableOrClassDescriptorByName(name) as? VariableDescriptor)
override fun getContributedFunctions(name: Name, location: LookupLocation) = functionsByName(name)
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= addedDescriptors
protected fun addVariableOrClassDescriptor(descriptor: DeclarationDescriptor) {
val name = descriptor.name
val descriptorIndex = addDescriptor(descriptor)
@@ -72,8 +81,8 @@ abstract class LexicalScopeStorage(
return null
}
protected fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size): List<FunctionDescriptor>? {
if (descriptorLimit == 0) return null
protected fun functionsByName(name: Name, descriptorLimit: Int = addedDescriptors.size): List<FunctionDescriptor> {
if (descriptorLimit == 0) return emptyList()
var list = functionsByName?.get(name)
while (list != null) {
@@ -82,7 +91,7 @@ abstract class LexicalScopeStorage(
}
list = list.prev
}
return null
return emptyList()
}
private fun addDescriptor(descriptor: DeclarationDescriptor): Int {
@@ -106,14 +115,4 @@ abstract class LexicalScopeStorage(
} while (rest != null)
return result
}
protected fun getClassifier(name: Name, descriptorLimit: Int = addedDescriptors.size)
= variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor
protected fun getVariables(name: Name, descriptorLimit: Int = addedDescriptors.size): Collection<VariableDescriptor>
= listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor)
protected fun getFunctions(name: Name, descriptorLimit: Int = addedDescriptors.size): Collection<FunctionDescriptor>
= functionsByName(name, descriptorLimit) ?: emptyList()
}
@@ -29,24 +29,15 @@ class LexicalWritableScope(
redeclarationChecker: LocalRedeclarationChecker,
override val kind: LexicalScopeKind
) : LexicalScopeStorage(parent, redeclarationChecker) {
enum class LockLevel {
WRITING,
BOTH,
READING
}
private var lockLevel: LockLevel = LockLevel.WRITING
private var canWrite: Boolean = true
private var lastSnapshot: Snapshot? = null
fun changeLockLevel(lockLevel: LockLevel) {
if (lockLevel.ordinal < this.lockLevel.ordinal) {
throw IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel + " at " + toString())
}
this.lockLevel = lockLevel
fun freeze() {
canWrite = false
}
fun takeSnapshot(): LexicalScope {
checkMayRead()
if (lastSnapshot == null || lastSnapshot!!.descriptorLimit != addedDescriptors.size) {
lastSnapshot = Snapshot(addedDescriptors.size)
}
@@ -68,40 +59,21 @@ class LexicalWritableScope(
addVariableOrClassDescriptor(classifierDescriptor)
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= checkMayRead().addedDescriptors
override fun getContributedClassifier(name: Name, location: LookupLocation) = checkMayRead().getClassifier(name)
override fun getContributedVariables(name: Name, location: LookupLocation) = checkMayRead().getVariables(name)
override fun getContributedFunctions(name: Name, location: LookupLocation) = checkMayRead().getFunctions(name)
private fun checkMayRead(): LexicalWritableScope {
if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) {
throw IllegalStateException("cannot read with lock level " + lockLevel + " at " + toString())
}
return this
}
private fun checkMayWrite() {
if (lockLevel != LockLevel.WRITING && lockLevel != LockLevel.BOTH) {
throw IllegalStateException("cannot write with lock level " + lockLevel + " at " + toString())
if (!canWrite) {
throw IllegalStateException("Cannot write into freezed scope:" + toString())
}
}
private inner class Snapshot(val descriptorLimit: Int) : LexicalScope by this {
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= this@LexicalWritableScope.addedDescriptors.subList(0, descriptorLimit)
= addedDescriptors.subList(0, descriptorLimit)
override fun getContributedClassifier(name: Name, location: LookupLocation)
= this@LexicalWritableScope.getClassifier(name, descriptorLimit)
override fun getContributedClassifier(name: Name, location: LookupLocation) = variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor
override fun getContributedVariables(name: Name, location: LookupLocation) = listOfNotNull(variableOrClassDescriptorByName(name, descriptorLimit) as? VariableDescriptor)
override fun getContributedVariables(name: Name, location: LookupLocation)
= this@LexicalWritableScope.getVariables(name, descriptorLimit)
override fun getContributedFunctions(name: Name, location: LookupLocation) = functionsByName(name, descriptorLimit)
override fun getContributedFunctions(name: Name, location: LookupLocation)
= this@LexicalWritableScope.getFunctions(name, descriptorLimit)
override fun toString(): String = "Snapshot($descriptorLimit) for $kind"
@@ -165,7 +165,6 @@ public class ExpressionTypingServices {
DeclarationDescriptor containingDescriptor = context.scope.getOwnerDescriptor();
LexicalWritableScope scope = new LexicalWritableScope(context.scope, containingDescriptor, false, null,
new TraceBasedLocalRedeclarationChecker(context.trace), LexicalScopeKind.CODE_BLOCK);
scope.changeLockLevel(LexicalWritableScope.LockLevel.BOTH);
KotlinTypeInfo r;
if (block.isEmpty()) {
@@ -176,7 +175,7 @@ public class ExpressionTypingServices {
r = getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression,
context.replaceStatementFilter(statementFilter));
}
scope.changeLockLevel(LexicalWritableScope.LockLevel.READING);
scope.freeze();
if (containingDescriptor instanceof ScriptDescriptor) {
context.trace.record(BindingContext.SCRIPT_SCOPE, (ScriptDescriptor) containingDescriptor, scope);
@@ -81,10 +81,8 @@ public class ExpressionTypingUtils {
@NotNull
public static LexicalWritableScope newWritableScopeImpl(ExpressionTypingContext context, @NotNull LexicalScopeKind scopeKind) {
LexicalWritableScope scope = new LexicalWritableScope(context.scope, context.scope.getOwnerDescriptor(), false, null,
new TraceBasedLocalRedeclarationChecker(context.trace), scopeKind);
scope.changeLockLevel(LexicalWritableScope.LockLevel.BOTH);
return scope;
return new LexicalWritableScope(context.scope, context.scope.getOwnerDescriptor(), false, null,
new TraceBasedLocalRedeclarationChecker(context.trace), scopeKind);
}
public static KtExpression createFakeExpressionOfType(