Introduce 'SupertypeLoopsResolver' and inject it's impls

This commit is contained in:
Denis Zharkov
2015-10-29 09:01:14 +03:00
parent 0bc5043c75
commit 18097c0ab7
10 changed files with 92 additions and 20 deletions
@@ -47,6 +47,12 @@ public fun StorageComponentContainer.configureModule(
for (extension in StorageComponentContainerContributor.getInstances(moduleContext.project)) {
extension.addDeclarations(this, platform)
}
configurePlatformIndependentComponents()
}
private fun StorageComponentContainer.configurePlatformIndependentComponents() {
useImpl<SupertypeLoopCheckerImpl>()
}
public fun StorageComponentContainer.configureModule(
@@ -78,6 +78,7 @@ public class DescriptorResolver {
@NotNull private final StorageManager storageManager;
@NotNull private final KotlinBuiltIns builtIns;
@NotNull private final ConstantExpressionEvaluator constantExpressionEvaluator;
@NotNull private final SupertypeLoopChecker supertypeLoopsResolver;
public DescriptorResolver(
@NotNull AnnotationResolver annotationResolver,
@@ -86,7 +87,8 @@ public class DescriptorResolver {
@NotNull ExpressionTypingServices expressionTypingServices,
@NotNull StorageManager storageManager,
@NotNull TypeResolver typeResolver,
@NotNull ConstantExpressionEvaluator constantExpressionEvaluator
@NotNull ConstantExpressionEvaluator constantExpressionEvaluator,
@NotNull SupertypeLoopChecker supertypeLoopsResolver
) {
this.annotationResolver = annotationResolver;
this.builtIns = builtIns;
@@ -95,6 +97,7 @@ public class DescriptorResolver {
this.storageManager = storageManager;
this.typeResolver = typeResolver;
this.constantExpressionEvaluator = constantExpressionEvaluator;
this.supertypeLoopsResolver = supertypeLoopsResolver;
}
public List<KotlinType> resolveSupertypes(
@@ -17,25 +17,28 @@
@file:JvmName("FindLoopsInSupertypes")
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.descriptors.SupertypeLoopChecker
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.utils.DFS
fun findLoopsInSupertypesAndDisconnect(
currentTypeConstructor: TypeConstructor,
superTypes: MutableCollection<KotlinType>,
neighbors: (TypeConstructor) -> Iterable<KotlinType>,
reportLoopAt: (KotlinType) -> Unit
) {
class SupertypeLoopCheckerImpl : SupertypeLoopChecker {
override fun findLoopsInSupertypesAndDisconnect(
currentTypeConstructor: TypeConstructor,
superTypes: MutableCollection<KotlinType>,
neighbors: (TypeConstructor) -> Iterable<KotlinType>,
reportLoop: (KotlinType) -> Unit
) {
val graph = DFS.Neighbors<TypeConstructor> { node -> neighbors(node).map { it.constructor } }
val graph = DFS.Neighbors<TypeConstructor> { node -> neighbors(node).map { it.constructor } }
val iterator = superTypes.iterator()
while (iterator.hasNext()) {
val item = iterator.next()
if (isReachable(item.constructor, currentTypeConstructor, graph)) {
iterator.remove()
reportLoopAt(item)
val iterator = superTypes.iterator()
while (iterator.hasNext()) {
val item = iterator.next()
if (isReachable(item.constructor, currentTypeConstructor, graph)) {
iterator.remove()
reportLoop(item)
}
}
}
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.resolve.lazy
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.SupertypeLoopChecker
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
@@ -34,4 +35,5 @@ public interface LazyClassContext {
val declarationProviderFactory: DeclarationProviderFactory
val annotationResolver: AnnotationResolver
val lookupTracker: LookupTracker
val supertypeLoopChecker: SupertypeLoopChecker
}
@@ -79,6 +79,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
private DeclarationScopeProvider declarationScopeProvider;
private LookupTracker lookupTracker;
private LocalDescriptorResolver localDescriptorResolver;
private SupertypeLoopChecker supertypeLoopsResolver;
@Inject
public void setJetImportFactory(KtImportsFactory jetImportFactory) {
@@ -411,6 +412,17 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
return lookupTracker;
}
@NotNull
@Override
public SupertypeLoopChecker getSupertypeLoopChecker() {
return supertypeLoopsResolver;
}
@Inject
public void setSupertypeLoopsResolver(@NotNull SupertypeLoopChecker supertypeLoopsResolver) {
this.supertypeLoopsResolver = supertypeLoopsResolver;
}
@Inject
public void setLocalDescriptorResolver(@NotNull LocalDescriptorResolver localDescriptorResolver) {
this.localDescriptorResolver = localDescriptorResolver;
@@ -582,7 +582,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
}
private void findAndDisconnectLoopsInTypeHierarchy(@Mutable Collection<KotlinType> supertypes) {
FindLoopsInSupertypes.findLoopsInSupertypesAndDisconnect(
c.getSupertypeLoopChecker().findLoopsInSupertypesAndDisconnect(
typeConstructor, supertypes,
new Function1<TypeConstructor, Iterable<? extends KotlinType>>() {
@Override
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.context.withProject
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.SupertypeLoopChecker
import org.jetbrains.kotlin.frontend.di.createContainerForLazyLocalClassifierAnalyzer
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.incremental.components.LookupTracker
@@ -54,7 +55,8 @@ public class LocalClassifierAnalyzer(
private val typeResolver: TypeResolver,
private val annotationResolver: AnnotationResolver,
private val platform: TargetPlatform,
private val dynamicTypesSettings: DynamicTypesSettings
private val dynamicTypesSettings: DynamicTypesSettings,
private val supertypeLoopChecker: SupertypeLoopChecker
) {
fun processClassOrObject(
scope: LexicalWritableScope?,
@@ -78,7 +80,8 @@ public class LocalClassifierAnalyzer(
descriptorResolver,
funcionDescriptorResolver,
typeResolver,
annotationResolver
annotationResolver,
supertypeLoopChecker
)
)
@@ -100,7 +103,8 @@ class LocalClassDescriptorHolder(
val descriptorResolver: DescriptorResolver,
val functionDescriptorResolver: FunctionDescriptorResolver,
val typeResolver: TypeResolver,
val annotationResolver: AnnotationResolver
val annotationResolver: AnnotationResolver,
val supertypeLoopChecker: SupertypeLoopChecker
) {
// We do not need to synchronize here, because this code is used strictly from one thread
private var classDescriptor: ClassDescriptor? = null
@@ -132,6 +136,7 @@ class LocalClassDescriptorHolder(
}
override val annotationResolver = this@LocalClassDescriptorHolder.annotationResolver
override val lookupTracker: LookupTracker = LookupTracker.DO_NOTHING
override val supertypeLoopChecker = this@LocalClassDescriptorHolder.supertypeLoopChecker
}
,
containingDeclaration,