DeclarationsChecker: converted to Kt + EffectiveVisibility related refactoring

This commit is contained in:
Mikhail Glukhikh
2015-11-23 11:12:59 +03:00
parent fe2fb7e5a5
commit 6e733f708e
3 changed files with 570 additions and 709 deletions
File diff suppressed because it is too large Load Diff
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.descriptors
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.descriptors.EffectiveVisibility.*
sealed class EffectiveVisibility(val name: String) { sealed class EffectiveVisibility(val name: String) {
@@ -169,29 +170,28 @@ sealed class EffectiveVisibility(val name: String) {
} }
} }
protected enum class Permissiveness { internal enum class Permissiveness {
LESS, LESS,
SAME, SAME,
MORE, MORE,
UNKNOWN UNKNOWN
} }
abstract protected fun relation(other: EffectiveVisibility): Permissiveness abstract internal fun relation(other: EffectiveVisibility): Permissiveness
fun sameOrMorePermissive(other: EffectiveVisibility) = when (relation(other)) { fun sameOrMorePermissive(other: EffectiveVisibility) = when (relation(other)) {
Permissiveness.SAME, Permissiveness.MORE -> true Permissiveness.SAME, Permissiveness.MORE -> true
Permissiveness.LESS, Permissiveness.UNKNOWN -> false Permissiveness.LESS, Permissiveness.UNKNOWN -> false
} }
open protected fun lowerBound(other: EffectiveVisibility) = when (relation(other)) { open internal fun lowerBound(other: EffectiveVisibility) = when (relation(other)) {
Permissiveness.SAME, Permissiveness.LESS -> this Permissiveness.SAME, Permissiveness.LESS -> this
Permissiveness.MORE -> other Permissiveness.MORE -> other
Permissiveness.UNKNOWN -> Private Permissiveness.UNKNOWN -> Private
} }
}
companion object { internal fun containerRelation(first: ClassDescriptor?, second: ClassDescriptor?): Permissiveness =
internal fun containerRelation(first: ClassDescriptor?, second: ClassDescriptor?): Permissiveness =
if (first == null || second == null) { if (first == null || second == null) {
Permissiveness.UNKNOWN Permissiveness.UNKNOWN
} }
@@ -208,47 +208,46 @@ sealed class EffectiveVisibility(val name: String) {
Permissiveness.UNKNOWN Permissiveness.UNKNOWN
} }
private fun lowerBound(first: EffectiveVisibility, second: EffectiveVisibility) = private fun lowerBound(first: EffectiveVisibility, second: EffectiveVisibility) =
first.lowerBound(second) first.lowerBound(second)
private fun lowerBound(first: EffectiveVisibility, args: List<EffectiveVisibility>) = private fun lowerBound(first: EffectiveVisibility, args: List<EffectiveVisibility>) =
args.fold(first, { x, y -> x.lowerBound(y) }) args.fold(first, { x, y -> x.lowerBound(y) })
private fun Visibility.forVisibility(descriptor: ClassDescriptor? = null): EffectiveVisibility = when (this) { private fun Visibility.forVisibility(descriptor: ClassDescriptor? = null): EffectiveVisibility = when (this) {
Visibilities.PRIVATE, Visibilities.PRIVATE_TO_THIS -> Private Visibilities.PRIVATE, Visibilities.PRIVATE_TO_THIS -> Private
Visibilities.PROTECTED -> Protected(descriptor) Visibilities.PROTECTED -> Protected(descriptor)
Visibilities.INTERNAL -> Internal Visibilities.INTERNAL -> Internal
Visibilities.PUBLIC -> Public Visibilities.PUBLIC -> Public
Visibilities.LOCAL -> Local Visibilities.LOCAL -> Local
else -> this.effectiveVisibility(descriptor) else -> this.effectiveVisibility(descriptor)
} }
fun effectiveVisibility(visibility: Visibility, descriptor: ClassDescriptor?) = visibility.forVisibility(descriptor) fun effectiveVisibility(visibility: Visibility, descriptor: ClassDescriptor?) = visibility.forVisibility(descriptor)
private fun ClassifierDescriptor.forClassifier(): EffectiveVisibility = private fun ClassifierDescriptor.effectiveVisibility(): EffectiveVisibility =
lowerBound(if (this is ClassDescriptor) this.forClass() else Public, lowerBound(if (this is ClassDescriptor) this.effectiveVisibility() else Public,
(this.containingDeclaration as? ClassifierDescriptor)?.forClassifier() ?: Public) (this.containingDeclaration as? ClassifierDescriptor)?.effectiveVisibility() ?: Public)
fun ClassDescriptor.forClass() = forClass(emptySet()) fun ClassDescriptor.effectiveVisibility() = effectiveVisibility(emptySet())
private fun ClassDescriptor.forClass(classes: Set<ClassDescriptor>): EffectiveVisibility = private fun ClassDescriptor.effectiveVisibility(classes: Set<ClassDescriptor>): EffectiveVisibility =
if (this in classes) Public if (this in classes) Public
else with(this.containingDeclaration as? ClassDescriptor) { else with(this.containingDeclaration as? ClassDescriptor) {
lowerBound(visibility.forVisibility(this), this?.forClass(classes + this@forClass) ?: Public) lowerBound(visibility.effectiveVisibility(this), this?.effectiveVisibility(classes + this@effectiveVisibility) ?: Public)
} }
fun KotlinType.forType() = forType(emptySet()) fun KotlinType.effectiveVisibility() = effectiveVisibility(emptySet())
private fun KotlinType.forType(types: Set<KotlinType>): EffectiveVisibility = private fun KotlinType.effectiveVisibility(types: Set<KotlinType>): EffectiveVisibility =
if (this in types) Public if (this in types) Public
else lowerBound(constructor.forTypeConstructor(), else lowerBound(constructor.effectiveVisibility(),
arguments.map { it.type.forType(types + this) } ) arguments.map { it.type.effectiveVisibility(types + this) })
private fun TypeConstructor.forTypeConstructor() = private fun TypeConstructor.effectiveVisibility() =
this.declarationDescriptor?.forClassifier() ?: Public this.declarationDescriptor?.effectiveVisibility() ?: Public
fun MemberDescriptor.effectiveVisibility() =
lowerBound(visibility.effectiveVisibility(this.containingDeclaration as? ClassDescriptor),
(this.containingDeclaration as? ClassDescriptor)?.effectiveVisibility() ?: Public)
fun MemberDescriptor.forMember() =
lowerBound(visibility.forVisibility(this.containingDeclaration as? ClassDescriptor),
(this.containingDeclaration as? ClassDescriptor)?.forClass() ?: Public)
}
}
@@ -51,5 +51,5 @@ public abstract class Visibility protected constructor(
public open fun normalize(): Visibility = this public open fun normalize(): Visibility = this
// Should be overloaded in Java visibilities // Should be overloaded in Java visibilities
public open fun effectiveVisibility(descriptor: ClassDescriptor?) = EffectiveVisibility.effectiveVisibility(normalize(), descriptor) public open fun effectiveVisibility(descriptor: ClassDescriptor?) = effectiveVisibility(normalize(), descriptor)
} }