Refactoring: don't register scopes explicitly, get them from scope provider on demand
This commit is contained in:
@@ -127,7 +127,8 @@ public class ReplInterpreter {
|
||||
scopeProvider
|
||||
);
|
||||
|
||||
this.topDownAnalysisContext = new TopDownAnalysisContext(TopDownAnalysisMode.LocalDeclarations, DataFlowInfo.EMPTY);
|
||||
this.topDownAnalysisContext = new TopDownAnalysisContext(TopDownAnalysisMode.LocalDeclarations, DataFlowInfo.EMPTY,
|
||||
container.getResolveSession().getScopeProvider());
|
||||
this.topDownAnalyzer = container.getLazyTopDownAnalyzerForTopLevel();
|
||||
this.resolveSession = container.getResolveSession();
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import org.jetbrains.annotations.Mutable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.ReadOnly;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
@@ -46,7 +46,8 @@ public interface BodiesResolveContext {
|
||||
@Mutable
|
||||
Map<JetNamedFunction, SimpleFunctionDescriptor> getFunctions();
|
||||
|
||||
Function<JetDeclaration, JetScope> getDeclaringScopes();
|
||||
@Nullable
|
||||
JetScope getDeclaringScope(@NotNull JetDeclaration declaration);
|
||||
|
||||
@NotNull
|
||||
DataFlowInfo getOuterDataFlowInfo();
|
||||
|
||||
@@ -144,7 +144,7 @@ public class BodyResolver {
|
||||
|
||||
private void resolveSecondaryConstructors(@NotNull BodiesResolveContext c) {
|
||||
for (Map.Entry<JetSecondaryConstructor, ConstructorDescriptor> entry : c.getSecondaryConstructors().entrySet()) {
|
||||
JetScope declaringScope = c.getDeclaringScopes().apply(entry.getKey());
|
||||
JetScope declaringScope = c.getDeclaringScope(entry.getKey());
|
||||
assert declaringScope != null : "Declaring scope should be registered before body resolve";
|
||||
resolveSecondaryConstructorBody(c.getOuterDataFlowInfo(), trace, entry.getKey(), entry.getValue(), declaringScope);
|
||||
}
|
||||
@@ -633,7 +633,7 @@ public class BodyResolver {
|
||||
}
|
||||
|
||||
private JetScope makeScopeForPropertyAccessor(@NotNull BodiesResolveContext c, @NotNull JetPropertyAccessor accessor, @NotNull PropertyDescriptor descriptor) {
|
||||
JetScope accessorDeclaringScope = c.getDeclaringScopes().apply(accessor);
|
||||
JetScope accessorDeclaringScope = c.getDeclaringScope(accessor);
|
||||
assert accessorDeclaringScope != null : "Scope for accessor " + accessor.getText() + " should exists";
|
||||
return JetScopeUtils.makeScopeForPropertyAccessor(descriptor, accessorDeclaringScope, trace);
|
||||
}
|
||||
@@ -737,7 +737,7 @@ public class BodyResolver {
|
||||
|
||||
@NotNull
|
||||
private static JetScope getScopeForProperty(@NotNull BodiesResolveContext c, @NotNull JetProperty property) {
|
||||
JetScope scope = c.getDeclaringScopes().apply(property);
|
||||
JetScope scope = c.getDeclaringScope(property);
|
||||
assert scope != null : "Scope for property " + property.getText() + " should exists";
|
||||
return scope;
|
||||
}
|
||||
@@ -749,7 +749,7 @@ public class BodyResolver {
|
||||
|
||||
computeDeferredType(descriptor.getReturnType());
|
||||
|
||||
JetScope declaringScope = c.getDeclaringScopes().apply(declaration);
|
||||
JetScope declaringScope = c.getDeclaringScope(declaration);
|
||||
assert declaringScope != null;
|
||||
|
||||
resolveFunctionBody(c.getOuterDataFlowInfo(), trace, declaration, descriptor, declaringScope);
|
||||
|
||||
@@ -103,7 +103,7 @@ public class LazyTopDownAnalyzer {
|
||||
}
|
||||
|
||||
public fun analyzeDeclarations(topDownAnalysisMode: TopDownAnalysisMode, declarations: Collection<PsiElement>, outerDataFlowInfo: DataFlowInfo): TopDownAnalysisContext {
|
||||
val c = TopDownAnalysisContext(topDownAnalysisMode, outerDataFlowInfo)
|
||||
val c = TopDownAnalysisContext(topDownAnalysisMode, outerDataFlowInfo, declarationScopeProvider!!)
|
||||
|
||||
val topLevelFqNames = HashMultimap.create<FqName, JetElement>()
|
||||
|
||||
@@ -199,7 +199,6 @@ public class LazyTopDownAnalyzer {
|
||||
|
||||
override fun visitSecondaryConstructor(constructor: JetSecondaryConstructor) {
|
||||
c.getSecondaryConstructors().put(constructor, lazyDeclarationResolver!!.resolveToDescriptor(constructor) as ConstructorDescriptor)
|
||||
registerScope(c, constructor)
|
||||
}
|
||||
|
||||
override fun visitEnumEntry(enumEntry: JetEnumEntry) {
|
||||
@@ -211,7 +210,6 @@ public class LazyTopDownAnalyzer {
|
||||
}
|
||||
|
||||
override fun visitAnonymousInitializer(initializer: JetClassInitializer) {
|
||||
registerScope(c, initializer)
|
||||
val classOrObject = PsiTreeUtil.getParentOfType<JetClassOrObject>(initializer, javaClass<JetClassOrObject>())
|
||||
c.getAnonymousInitializers().put(initializer, lazyDeclarationResolver!!.resolveToDescriptor(classOrObject) as ClassDescriptorWithResolutionScopes)
|
||||
}
|
||||
@@ -269,10 +267,6 @@ public class LazyTopDownAnalyzer {
|
||||
c.getProperties().put(property, descriptor)
|
||||
ForceResolveUtil.forceResolveAllContents(descriptor.getAnnotations())
|
||||
registerTopLevelFqName(topLevelFqNames, property, descriptor)
|
||||
|
||||
registerScope(c, property)
|
||||
registerScope(c, property.getGetter())
|
||||
registerScope(c, property.getSetter())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,15 +278,9 @@ public class LazyTopDownAnalyzer {
|
||||
for (parameterDescriptor in simpleFunctionDescriptor.getValueParameters()) {
|
||||
ForceResolveUtil.forceResolveAllContents(parameterDescriptor.getAnnotations())
|
||||
}
|
||||
registerScope(c, function)
|
||||
}
|
||||
}
|
||||
|
||||
private fun registerScope(c: TopDownAnalysisContext, declaration: JetDeclaration?) {
|
||||
if (declaration == null) return
|
||||
c.registerDeclaringScope(declaration, declarationScopeProvider!!.getResolutionScopeForDeclaration(declaration))
|
||||
}
|
||||
|
||||
private fun registerTopLevelFqName(topLevelFqNames: Multimap<FqName, JetElement>, declaration: JetNamedDeclaration, descriptor: DeclarationDescriptor) {
|
||||
if (DescriptorUtils.isTopLevelDeclaration(descriptor)) {
|
||||
val fqName = declaration.getFqName()
|
||||
|
||||
@@ -16,15 +16,15 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.collect.Maps;
|
||||
import kotlin.KotlinPackage;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
|
||||
import org.jetbrains.kotlin.resolve.lazy.DeclarationScopeProvider;
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope;
|
||||
|
||||
import java.io.PrintStream;
|
||||
@@ -42,7 +42,6 @@ public class TopDownAnalysisContext implements BodiesResolveContext {
|
||||
private final Set<JetFile> files = new LinkedHashSet<JetFile>();
|
||||
private final Map<JetSecondaryConstructor, ConstructorDescriptor> secondaryConstructors = Maps.newLinkedHashMap();
|
||||
|
||||
private final Map<JetDeclaration, JetScope> declaringScopes = Maps.newHashMap();
|
||||
private final Map<JetNamedFunction, SimpleFunctionDescriptor> functions = Maps.newLinkedHashMap();
|
||||
private final Map<JetProperty, PropertyDescriptor> properties = Maps.newLinkedHashMap();
|
||||
private final Map<JetParameter, PropertyDescriptor> primaryConstructorParameterProperties = Maps.newHashMap();
|
||||
@@ -51,12 +50,18 @@ public class TopDownAnalysisContext implements BodiesResolveContext {
|
||||
private final Map<JetScript, ScriptDescriptor> scripts = Maps.newLinkedHashMap();
|
||||
|
||||
private final TopDownAnalysisMode topDownAnalysisMode;
|
||||
private final DeclarationScopeProvider declarationScopeProvider;
|
||||
|
||||
private StringBuilder debugOutput;
|
||||
|
||||
public TopDownAnalysisContext(@NotNull TopDownAnalysisMode topDownAnalysisMode, @NotNull DataFlowInfo outerDataFlowInfo) {
|
||||
public TopDownAnalysisContext(
|
||||
@NotNull TopDownAnalysisMode topDownAnalysisMode,
|
||||
@NotNull DataFlowInfo outerDataFlowInfo,
|
||||
@NotNull DeclarationScopeProvider declarationScopeProvider
|
||||
) {
|
||||
this.topDownAnalysisMode = topDownAnalysisMode;
|
||||
this.outerDataFlowInfo = outerDataFlowInfo;
|
||||
this.declarationScopeProvider = declarationScopeProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,13 +128,10 @@ public class TopDownAnalysisContext implements BodiesResolveContext {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Function<JetDeclaration, JetScope> getDeclaringScopes() {
|
||||
return Functions.forMap(declaringScopes);
|
||||
}
|
||||
|
||||
public void registerDeclaringScope(@NotNull JetDeclaration declaration, @NotNull JetScope scope) {
|
||||
declaringScopes.put(declaration, scope);
|
||||
public JetScope getDeclaringScope(@NotNull JetDeclaration declaration) {
|
||||
return declarationScopeProvider.getResolutionScopeForDeclaration(declaration);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -319,11 +319,9 @@ public abstract class ElementResolver protected constructor(
|
||||
bodyResolver.resolvePropertyDelegate(DataFlowInfo.EMPTY, jetProperty, descriptor, propertyDelegate, propertyResolutionScope, propertyResolutionScope)
|
||||
}
|
||||
|
||||
val bodyResolveContext = BodyResolveContextForLazy(TopDownAnalysisMode.LocalDeclarations, object : Function<JetDeclaration, JetScope> {
|
||||
override fun apply(declaration: JetDeclaration?): JetScope? {
|
||||
assert(declaration!!.getParent() == jetProperty) { "Must be called only for property accessors, but called for " + declaration }
|
||||
return resolveSession.getScopeProvider().getResolutionScopeForDeclaration(declaration)
|
||||
}
|
||||
val bodyResolveContext = BodyResolveContextForLazy(TopDownAnalysisMode.LocalDeclarations, { declaration ->
|
||||
assert(declaration.getParent() == jetProperty) { "Must be called only for property accessors, but called for $declaration" }
|
||||
resolveSession.getScopeProvider().getResolutionScopeForDeclaration(declaration)
|
||||
})
|
||||
|
||||
bodyResolver.resolvePropertyAccessors(bodyResolveContext, jetProperty, descriptor)
|
||||
@@ -446,9 +444,8 @@ public abstract class ElementResolver protected constructor(
|
||||
|
||||
private class BodyResolveContextForLazy(
|
||||
private val topDownAnalysisMode: TopDownAnalysisMode,
|
||||
private val declaringScopes: Function<in JetDeclaration, JetScope>
|
||||
private val declaringScopes: Function1<JetDeclaration, JetScope?>
|
||||
) : BodiesResolveContext {
|
||||
|
||||
override fun getFiles(): Collection<JetFile> = setOf()
|
||||
|
||||
override fun getDeclaredClasses(): Map<JetClassOrObject, ClassDescriptorWithResolutionScopes> = mapOf()
|
||||
@@ -461,11 +458,11 @@ public abstract class ElementResolver protected constructor(
|
||||
|
||||
override fun getFunctions(): Map<JetNamedFunction, SimpleFunctionDescriptor> = mapOf()
|
||||
|
||||
override fun getDeclaringScopes() = declaringScopes as Function<JetDeclaration, JetScope>
|
||||
override fun getDeclaringScope(declaration: JetDeclaration): JetScope? = declaringScopes(declaration)
|
||||
|
||||
override fun getScripts(): Map<JetScript, ScriptDescriptor> = mapOf()
|
||||
|
||||
override fun getOuterDataFlowInfo() = DataFlowInfo.EMPTY
|
||||
override fun getOuterDataFlowInfo(): DataFlowInfo = DataFlowInfo.EMPTY
|
||||
|
||||
override fun getTopDownAnalysisMode() = topDownAnalysisMode
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user