[FIR] Support some approximation of effective visibility

This commit is contained in:
Mikhail Glukhikh
2020-03-18 18:04:00 +03:00
parent cb3a31af6e
commit 50a9313a5e
28 changed files with 480 additions and 81 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.declarations
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirEffectiveVisibility
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.visitors.*
@@ -19,6 +20,7 @@ import org.jetbrains.kotlin.fir.visitors.*
interface FirDeclarationStatus : FirElement {
override val source: FirSourceElement?
val visibility: Visibility
val effectiveVisibility: FirEffectiveVisibility
val modality: Modality?
val isExpect: Boolean
val isActual: Boolean
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.declarations
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirEffectiveVisibility
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.visitors.*
@@ -18,6 +19,7 @@ import org.jetbrains.kotlin.fir.visitors.*
interface FirResolvedDeclarationStatus : FirDeclarationStatus {
override val source: FirSourceElement?
override val visibility: Visibility
override val effectiveVisibility: FirEffectiveVisibility
override val modality: Modality?
override val isExpect: Boolean
override val isActual: Boolean
@@ -0,0 +1,222 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirEffectiveVisibility.*
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
sealed class FirEffectiveVisibility(val name: String, val publicApi: Boolean = false, val privateApi: Boolean = false) {
override fun toString() = name
// Public
// /--/ | \-------------\
// Protected(Base) | \
// | Protected(Other) Internal = PackagePrivate
// Protected(Derived) | / \
// | | / InternalProtected(Base)
// ProtectedBound / \
// \ / /InternalProtected(Derived)
// \InternalProtectedBound/
// |
// Private = Local
object Private : FirEffectiveVisibility("private", privateApi = true) {
override fun relation(other: FirEffectiveVisibility) =
if (this == other || Local == other) Permissiveness.SAME else Permissiveness.LESS
override fun toVisibility() = Visibilities.PRIVATE
}
// Effectively same as Private
object Local : FirEffectiveVisibility("local") {
override fun relation(other: FirEffectiveVisibility) =
if (this == other || Private == other) Permissiveness.SAME else Permissiveness.LESS
override fun toVisibility() = Visibilities.LOCAL
}
object Public : FirEffectiveVisibility("public", publicApi = true) {
override fun relation(other: FirEffectiveVisibility) =
if (this == other) Permissiveness.SAME else Permissiveness.MORE
override fun toVisibility() = Visibilities.PUBLIC
}
abstract class InternalOrPackage protected constructor(internal: Boolean) : FirEffectiveVisibility(
if (internal) "internal" else "public/*package*/"
) {
override fun relation(other: FirEffectiveVisibility) = when (other) {
Public -> Permissiveness.LESS
Private, Local, InternalProtectedBound, is InternalProtected -> Permissiveness.MORE
is InternalOrPackage -> Permissiveness.SAME
ProtectedBound, is Protected -> Permissiveness.UNKNOWN
}
override fun lowerBound(other: FirEffectiveVisibility) = when (other) {
Public -> this
Private, Local, InternalProtectedBound, is InternalOrPackage, is InternalProtected -> other
is Protected -> InternalProtected(other.container)
ProtectedBound -> InternalProtectedBound
}
}
object Internal : InternalOrPackage(true) {
override fun toVisibility() = Visibilities.INTERNAL
}
object PackagePrivate : InternalOrPackage(false) {
override fun toVisibility() = Visibilities.PRIVATE
}
class Protected(val container: FirRegularClass?) : FirEffectiveVisibility("protected", publicApi = true) {
override fun equals(other: Any?) = (other is Protected && container == other.container)
override fun hashCode() = container?.hashCode() ?: 0
override fun toString() = "${super.toString()} (in ${container?.name ?: '?'})"
override fun relation(other: FirEffectiveVisibility) = when (other) {
Public -> Permissiveness.LESS
Private, Local, ProtectedBound, InternalProtectedBound -> Permissiveness.MORE
is Protected -> containerRelation(container, other.container)
is InternalProtected -> when (containerRelation(container, other.container)) {
// Protected never can be less permissive than internal & protected
Permissiveness.SAME, Permissiveness.MORE -> Permissiveness.MORE
Permissiveness.UNKNOWN, Permissiveness.LESS -> Permissiveness.UNKNOWN
}
is InternalOrPackage -> Permissiveness.UNKNOWN
}
override fun lowerBound(other: FirEffectiveVisibility) = when (other) {
Public -> this
Private, Local, ProtectedBound, InternalProtectedBound -> other
is Protected -> when (relation(other)) {
Permissiveness.SAME, Permissiveness.MORE -> this
Permissiveness.LESS -> other
Permissiveness.UNKNOWN -> ProtectedBound
}
is InternalProtected -> when (relation(other)) {
Permissiveness.LESS -> other
else -> InternalProtectedBound
}
is InternalOrPackage -> InternalProtected(container)
}
override fun toVisibility() = Visibilities.PROTECTED
}
// Lower bound for all protected visibilities
object ProtectedBound : FirEffectiveVisibility("protected (in different classes)", publicApi = true) {
override fun relation(other: FirEffectiveVisibility) = when (other) {
Public, is Protected -> Permissiveness.LESS
Private, Local, InternalProtectedBound -> Permissiveness.MORE
ProtectedBound -> Permissiveness.SAME
is InternalOrPackage, is InternalProtected -> Permissiveness.UNKNOWN
}
override fun lowerBound(other: FirEffectiveVisibility) = when (other) {
Public, is Protected -> this
Private, Local, ProtectedBound, InternalProtectedBound -> other
is InternalOrPackage, is InternalProtected -> InternalProtectedBound
}
override fun toVisibility() = Visibilities.PROTECTED
}
// Lower bound for internal and protected(C)
class InternalProtected(val container: FirRegularClass?) : FirEffectiveVisibility("internal & protected") {
override fun equals(other: Any?) = (other is InternalProtected && container == other.container)
override fun hashCode() = container?.hashCode() ?: 0
override fun toString() = "${super.toString()} (in ${container?.name ?: '?'})"
override fun relation(other: FirEffectiveVisibility) = when (other) {
Public, is InternalOrPackage -> Permissiveness.LESS
Private, Local, InternalProtectedBound -> Permissiveness.MORE
is InternalProtected -> containerRelation(container, other.container)
is Protected -> when (containerRelation(container, other.container)) {
// Internal & protected never can be more permissive than just protected
Permissiveness.SAME, Permissiveness.LESS -> Permissiveness.LESS
Permissiveness.UNKNOWN, Permissiveness.MORE -> Permissiveness.UNKNOWN
}
ProtectedBound -> Permissiveness.UNKNOWN
}
override fun lowerBound(other: FirEffectiveVisibility) = when (other) {
Public, is InternalOrPackage -> this
Private, Local, InternalProtectedBound -> other
is Protected, is InternalProtected -> when (relation(other)) {
Permissiveness.SAME, Permissiveness.MORE -> this
Permissiveness.LESS -> other
Permissiveness.UNKNOWN -> InternalProtectedBound
}
ProtectedBound -> InternalProtectedBound
}
override fun toVisibility() = Visibilities.PRIVATE
}
// Lower bound for internal and protected lower bound
object InternalProtectedBound : FirEffectiveVisibility("internal & protected (in different classes)") {
override fun relation(other: FirEffectiveVisibility) = when (other) {
Public, is Protected, is InternalProtected, ProtectedBound, is InternalOrPackage -> Permissiveness.LESS
Private, Local -> Permissiveness.MORE
InternalProtectedBound -> Permissiveness.SAME
}
override fun toVisibility() = Visibilities.PRIVATE
}
enum class Permissiveness {
LESS,
SAME,
MORE,
UNKNOWN
}
abstract fun relation(other: FirEffectiveVisibility): Permissiveness
abstract fun toVisibility(): Visibility
open fun lowerBound(other: FirEffectiveVisibility) = when (relation(other)) {
Permissiveness.SAME, Permissiveness.LESS -> this
Permissiveness.MORE -> other
Permissiveness.UNKNOWN -> Private
}
}
internal fun containerRelation(first: FirRegularClass?, second: FirRegularClass?): Permissiveness =
if (first == null || second == null) {
Permissiveness.UNKNOWN
} else if (first == second) {
Permissiveness.SAME
// TODO
// } else if (DescriptorUtils.isSubclass(first, second)) {
// Permissiveness.LESS
// } else if (DescriptorUtils.isSubclass(second, first)) {
// Permissiveness.MORE
} else {
Permissiveness.UNKNOWN
}
internal fun Visibility.firEffectiveVisibilityApproximation(): FirEffectiveVisibility =
when (this) {
Visibilities.PUBLIC -> Public
Visibilities.PRIVATE -> Private
Visibilities.PRIVATE_TO_THIS -> Private
Visibilities.INTERNAL -> Internal
Visibilities.LOCAL -> Local
Visibilities.PROTECTED -> ProtectedBound
else -> Public
}
@@ -41,6 +41,13 @@ fun FirElement.render(mode: FirRenderer.RenderMode = FirRenderer.RenderMode.Norm
buildString { this@render.accept(FirRenderer(this, mode)) }
class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderMode.Normal) : FirVisitorVoid() {
companion object {
private val visibilitiesToRenderEffectiveSet = setOf(
Visibilities.PRIVATE, Visibilities.PRIVATE_TO_THIS, Visibilities.INTERNAL,
Visibilities.PROTECTED, Visibilities.PUBLIC, Visibilities.LOCAL
)
}
abstract class RenderMode(val renderLambdaBodies: Boolean, val renderCallArguments: Boolean) {
object Normal : RenderMode(renderLambdaBodies = true, renderCallArguments = true)
}
@@ -152,11 +159,18 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
callableDeclaration.returnTypeRef.accept(this)
}
private fun Visibility.asString() =
when (this) {
Visibilities.UNKNOWN -> "public?"
private fun Visibility.asString(effectiveVisibility: FirEffectiveVisibility? = null): String {
val itself = when (this) {
Visibilities.UNKNOWN -> return "public?"
else -> toString()
}
if (effectiveVisibility == null) return itself
val effectiveAsVisibility = effectiveVisibility.toVisibility()
if (effectiveAsVisibility == this) return itself
if (effectiveAsVisibility == Visibilities.PRIVATE && this == Visibilities.PRIVATE_TO_THIS) return itself
if (this !in visibilitiesToRenderEffectiveSet) return itself
return itself + "[${effectiveVisibility.name}]"
}
private fun FirMemberDeclaration.modalityAsString(): String {
return modality?.name?.toLowerCase() ?: run {
@@ -187,7 +201,8 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
override fun visitMemberDeclaration(memberDeclaration: FirMemberDeclaration) {
memberDeclaration.annotations.renderAnnotations()
if (memberDeclaration !is FirProperty || !memberDeclaration.isLocal) {
print(memberDeclaration.visibility.asString() + " " + memberDeclaration.modalityAsString() + " ")
print(memberDeclaration.visibility.asString(memberDeclaration.effectiveVisibility) + " ")
print(memberDeclaration.modalityAsString() + " ")
}
if (memberDeclaration.isExpect) {
print("expect ")
@@ -377,7 +392,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
override fun visitConstructor(constructor: FirConstructor) {
constructor.annotations.renderAnnotations()
print(constructor.visibility.asString() + " constructor")
print(constructor.visibility.asString(constructor.effectiveVisibility) + " constructor")
constructor.typeParameters.renderTypeParameters()
constructor.valueParameters.renderParameters()
print(": ")
@@ -26,6 +26,7 @@ inline val FirRegularClass.isData get() = status.isData
inline val FirRegularClass.isInline get() = status.isInline
inline val FirMemberDeclaration.modality get() = status.modality
inline val FirMemberDeclaration.visibility get() = status.visibility
inline val FirMemberDeclaration.effectiveVisibility get() = status.effectiveVisibility
inline val FirMemberDeclaration.isActual get() = status.isActual
inline val FirMemberDeclaration.isExpect get() = status.isExpect
inline val FirMemberDeclaration.isInner get() = status.isInner
@@ -7,10 +7,12 @@ package org.jetbrains.kotlin.fir.declarations.impl
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirEffectiveVisibility
import org.jetbrains.kotlin.fir.FirPureAbstractElement
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.declarations.FirDeclarationStatus
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl.Modifier.*
import org.jetbrains.kotlin.fir.firEffectiveVisibilityApproximation
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.FirVisitor
@@ -19,6 +21,8 @@ open class FirDeclarationStatusImpl(
override val modality: Modality?
) : FirPureAbstractElement(), FirDeclarationStatus {
override val source: FirSourceElement? get() = null
override val effectiveVisibility: FirEffectiveVisibility
get() = visibility.firEffectiveVisibilityApproximation()
protected var flags: Int = 0
private operator fun get(modifier: Modifier): Boolean = (flags and modifier.mask) != 0
@@ -159,7 +163,7 @@ open class FirDeclarationStatusImpl(
return this
}
fun resolved(visibility: Visibility, modality: Modality): FirDeclarationStatus {
return FirResolvedDeclarationStatusImpl(visibility, modality, flags)
fun resolved(visibility: Visibility, effectiveVisibility: FirEffectiveVisibility, modality: Modality): FirDeclarationStatus {
return FirResolvedDeclarationStatusImpl(visibility, effectiveVisibility, modality, flags)
}
}
@@ -7,18 +7,21 @@ package org.jetbrains.kotlin.fir.declarations.impl
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirEffectiveVisibility
import org.jetbrains.kotlin.fir.declarations.FirResolvedDeclarationStatus
class FirResolvedDeclarationStatusImpl(
visibility: Visibility,
override var effectiveVisibility: FirEffectiveVisibility,
modality: Modality
) : FirDeclarationStatusImpl(visibility, modality), FirResolvedDeclarationStatus {
internal constructor(
visibility: Visibility,
effectiveVisibility: FirEffectiveVisibility,
modality: Modality,
flags: Int
) : this(visibility, modality) {
) : this(visibility, effectiveVisibility, modality) {
this.flags = flags
}
@@ -75,6 +75,8 @@ object FieldSets {
val visibility = field(visibilityType)
val effectiveVisibility = field("effectiveVisibility", effectiveVisibilityType)
val modality = field(modalityType, nullable = true)
val scopeProvider = field("scopeProvider", firScopeProviderType)
@@ -5,12 +5,14 @@
package org.jetbrains.kotlin.fir.tree.generator
import org.jetbrains.kotlin.descriptors.effectiveVisibility
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.annotations
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.arguments
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.body
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.classKind
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.controlFlowGraphReferenceField
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.declarations
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.effectiveVisibility
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.initializer
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.modality
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.name
@@ -303,6 +305,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
declarationStatus.configure {
+visibility
+effectiveVisibility
+modality
generateBooleanFields(
"expect", "actual", "override", "operator", "infix", "inline", "tailRec",
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.fir.tree.generator
import org.jetbrains.kotlin.contracts.description.InvocationKind
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.tree.generator.context.generatedType
import org.jetbrains.kotlin.fir.tree.generator.context.type
@@ -29,6 +26,7 @@ val invocationKindType = type(InvocationKind::class)
val varianceType = type(Variance::class)
val nameType = type(Name::class)
val visibilityType = type(Visibility::class)
val effectiveVisibilityType = type("fir", "FirEffectiveVisibility")
val visibilitiesType = type(Visibilities::class)
val modalityType = type(Modality::class)
val fqNameType = type(FqName::class)