Skip to content

Commit

Permalink
Refactoring: don't register scopes explicitly, get them from scope pr…
Browse files Browse the repository at this point in the history
…ovider on demand
  • Loading branch information
goodwinnk committed Jun 30, 2015
1 parent b81d8eb commit 980a0d1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public List<JetScope> scopes(@NotNull JetFile file) {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void resolveBehaviorDeclarationBodies(@NotNull BodiesResolveContext c) {

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);
}
Expand Down Expand Up @@ -633,7 +633,7 @@ private void resolvePropertyDeclarationBodies(@NotNull BodiesResolveContext c) {
}

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);
}
Expand Down Expand Up @@ -737,7 +737,7 @@ public void resolvePropertyInitializer(

@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;
}
Expand All @@ -749,7 +749,7 @@ private void resolveFunctionBodies(@NotNull BodiesResolveContext c) {

computeDeferredType(descriptor.getReturnType());

JetScope declaringScope = c.getDeclaringScopes().apply(declaration);
JetScope declaringScope = c.getDeclaringScope(declaration);
assert declaringScope != null;

resolveFunctionBody(c.getOuterDataFlowInfo(), trace, declaration, descriptor, declaringScope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>()

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -123,13 +128,10 @@ public Map<JetProperty, PropertyDescriptor> getProperties() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
Expand Down

0 comments on commit 980a0d1

Please sign in to comment.