Move LexicalScope to frontend

This commit is contained in:
Alexander Udalov
2015-10-29 18:26:29 +03:00
parent 09a36acee4
commit 4d909d9012
5 changed files with 18 additions and 24 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.ScriptDescriptor;
import org.jetbrains.kotlin.psi.KtScript;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil;
import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
import org.jetbrains.kotlin.types.ErrorUtils;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.expressions.CoercionStrategy;
@@ -32,7 +33,6 @@ import java.util.Map;
import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE;
// SCRIPT: resolve symbols in scripts
public class ScriptBodyResolver {
@@ -54,18 +54,17 @@ public class ScriptBodyResolver {
@NotNull
public KotlinType resolveScriptReturnType(
@NotNull KtScript script,
@NotNull ScriptDescriptor scriptDescriptor,
@NotNull LexicalScope scopeForBodyResolution,
@NotNull BindingTrace trace
) {
// Resolve all contents of the script
ExpressionTypingContext context = ExpressionTypingContext.newContext(
trace,
scriptDescriptor.getScopeForBodyResolution(),
DataFlowInfo.EMPTY,
NO_EXPECTED_TYPE
trace, scopeForBodyResolution, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE
);
PreliminaryDeclarationVisitor.Companion.createForDeclaration(script, trace);
KotlinType returnType = expressionTypingServices.getBlockReturnedType(script.getBlockExpression(), CoercionStrategy.NO_COERCION, context).getType();
KotlinType returnType = expressionTypingServices.getBlockReturnedType(
script.getBlockExpression(), CoercionStrategy.NO_COERCION, context
).getType();
if (returnType == null) {
returnType = ErrorUtils.createErrorType("getBlockReturnedType returned null");
}
@@ -32,7 +32,6 @@ import org.jetbrains.kotlin.resolve.ScriptBodyResolver
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
import org.jetbrains.kotlin.resolve.lazy.LazyEntity
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl
import org.jetbrains.kotlin.resolve.scopes.receivers.ScriptReceiver
import org.jetbrains.kotlin.resolve.source.toSourceElement
@@ -86,22 +85,22 @@ public class LazyScriptDescriptor(
)
},
DeferredType.create(resolveSession.storageManager, resolveSession.trace) {
scriptBodyResolver.resolveScriptReturnType(jetScript, this, resolveSession.trace)
val scope = LexicalScopeImpl(
resolveSession.fileScopeProvider.getFileResolutionScope(jetScript.getContainingJetFile()),
this, false, implicitReceiver, "Scope for body resolution for $this"
) {
for (valueParameterDescriptor in result.valueParameters) {
addVariableDescriptor(valueParameterDescriptor)
}
}
scriptBodyResolver.resolveScriptReturnType(jetScript, scope, resolveSession.trace)
}
)
result
}
override fun getScriptCodeDescriptor() = scriptCodeDescriptor()
override fun getScopeForBodyResolution(): LexicalScope {
return LexicalScopeImpl(resolveSession.fileScopeProvider.getFileResolutionScope(jetScript.getContainingJetFile()),
this, false, implicitReceiver, "Scope for body resolution for " + this) {
for (valueParameterDescriptor in getScriptCodeDescriptor().valueParameters) {
addVariableDescriptor(valueParameterDescriptor)
}
}
}
override fun getScriptCodeDescriptor(): ScriptCodeDescriptor = scriptCodeDescriptor()
override fun forceResolveAllContents() {
ForceResolveUtil.forceResolveAllContents(getClassDescriptor())
@@ -0,0 +1,118 @@
/*
* 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.scopes
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.Printer
// see utils/ScopeUtils.kt
interface LexicalScope {
val parent: LexicalScope?
val ownerDescriptor: DeclarationDescriptor
val isOwnerDescriptorAccessibleByLabel: Boolean
val implicitReceiver: ReceiverParameterDescriptor?
/**
* All visible descriptors from current scope possibly filtered by the given name and kind filters
* (that means that the implementation is not obliged to use the filters but may do so when it gives any performance advantage).
*/
fun getContributedDescriptors(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = KtScope.ALL_NAME_FILTER
): Collection<DeclarationDescriptor>
fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor?
fun getContributedVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor>
fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
fun printStructure(p: Printer)
}
// TODO: common base interface instead direct inheritance
interface ImportingScope : LexicalScope {
override val parent: ImportingScope?
override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override val implicitReceiver: ReceiverParameterDescriptor?
get() = null
// methods getDeclaredSmth for this scope will be delegated to importScope
fun getContributedPackage(name: Name): PackageViewDescriptor?
fun getContributedSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor>
fun getContributedSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor>
fun getContributedSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>): Collection<PropertyDescriptor>
fun getContributedSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>): Collection<FunctionDescriptor>
object Empty : BaseImportingScope(null) {
override fun printStructure(p: Printer) {
p.println("ImportingScope.Empty")
}
}
}
abstract class BaseLexicalScope(override val parent: LexicalScope?) : LexicalScope {
override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override val implicitReceiver: ReceiverParameterDescriptor?
get() = null
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = emptyList()
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor> = emptyList()
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> = emptyList()
}
abstract class BaseImportingScope(parent: ImportingScope?) : BaseLexicalScope(parent), ImportingScope {
override val parent: ImportingScope?
get() = super.parent as ImportingScope?
override val ownerDescriptor: DeclarationDescriptor
get() = throw UnsupportedOperationException()
override final val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override final val implicitReceiver: ReceiverParameterDescriptor?
get() = null
override fun getContributedPackage(name: Name): PackageViewDescriptor? = null
override fun getContributedSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor> = emptyList()
override fun getContributedSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor> = emptyList()
override fun getContributedSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>): Collection<PropertyDescriptor> = emptyList()
override fun getContributedSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>): Collection<FunctionDescriptor> = emptyList()
}