mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-10 08:31:29 +00:00
[Commonizer] Remove now unnecessary ArtificialCirDeclaration
This marker interface is not necessary anymore, since no new actuals will be generated by the Commonizer. It was used to filter declarations during the serializ
This commit is contained in:
committed by
Space
parent
e041532dd2
commit
0c32abed02
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this delegate code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.commonizer.cir
|
||||
|
||||
/**
|
||||
* Marker for classifiers that are made up to support commonization.
|
||||
* E.g. TypeAliases might create artificial classes for the types they are pointing to.
|
||||
*/
|
||||
internal interface ArtificialCirDeclaration : CirDeclaration
|
||||
|
||||
internal class ArtificialCirModule(val delegate: CirModule) : CirModule by delegate, ArtificialCirDeclaration
|
||||
|
||||
internal fun CirModule.markedArtificial() = ArtificialCirModule(this)
|
||||
|
||||
internal class ArtificialCirClass(val delegate: CirClass) : CirClass by delegate, ArtificialCirDeclaration
|
||||
|
||||
internal fun CirClass.markedArtificial(): ArtificialCirClass = ArtificialCirClass(this)
|
||||
|
||||
internal class ArtificialCirClassConstructor(val delegate: CirClassConstructor) : CirClassConstructor by delegate, ArtificialCirDeclaration
|
||||
|
||||
internal fun CirClassConstructor.markedArtificial() = ArtificialCirClassConstructor(this)
|
||||
|
||||
internal class ArtificialCirFunction(val delegate: CirFunction) : CirFunction by delegate, ArtificialCirDeclaration
|
||||
|
||||
internal fun CirFunction.markedArtificial() = ArtificialCirFunction(this)
|
||||
|
||||
internal class ArtificialCirProperty(val delegate: CirProperty) : CirProperty by delegate, ArtificialCirDeclaration
|
||||
|
||||
internal fun CirProperty.markedArtificial() = ArtificialCirProperty(this)
|
||||
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.commonizer.mergedtree.CirKnownClassifiers
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirNode.Companion.indexOfCommon
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirRootNode
|
||||
import org.jetbrains.kotlin.commonizer.metadata.CirTreeSerializer
|
||||
import org.jetbrains.kotlin.commonizer.transformer.Checked.Companion.invoke
|
||||
import org.jetbrains.kotlin.commonizer.transformer.InlineTypeAliasCirNodeTransformer
|
||||
import org.jetbrains.kotlin.commonizer.tree.CirTreeRoot
|
||||
import org.jetbrains.kotlin.commonizer.tree.defaultCirTreeRootDeserializer
|
||||
|
||||
@@ -404,8 +404,7 @@ internal data class CirTreeSerializationContext(
|
||||
}
|
||||
|
||||
inline fun <reified T : CirDeclaration> get(node: CirNode<*, *>): T? {
|
||||
return ((if (isCommon) node.commonDeclaration() else node.targetDeclarations[targetIndex]) as T?)
|
||||
.takeIf { it !is ArtificialCirDeclaration }
|
||||
return (if (isCommon) node.commonDeclaration() else node.targetDeclarations[targetIndex]) as T?
|
||||
}
|
||||
|
||||
inline fun <reified T : CirDeclaration> get(node: CirNodeWithLiftingUp<*, *>): T? {
|
||||
@@ -413,7 +412,7 @@ internal data class CirTreeSerializationContext(
|
||||
isCommon -> node.commonDeclaration() as T?
|
||||
node.isLiftedUp -> null
|
||||
else -> node.targetDeclarations[targetIndex] as T?
|
||||
}.takeIf { it !is ArtificialCirDeclaration }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright 2010-2021 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.commonizer.transformer
|
||||
|
||||
import org.jetbrains.kotlin.commonizer.cir.ArtificialCirDeclaration
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.*
|
||||
|
||||
internal data class CirDeclarationCount(
|
||||
val artificialNodeCount: Int,
|
||||
val nonArtificialNodeCount: Int
|
||||
)
|
||||
|
||||
internal data class MutableCirDeclarationCount(
|
||||
var artificialNodeCount: Int = 0,
|
||||
var nonArtificialNodeCount: Int = 0
|
||||
) {
|
||||
fun toCirDeclarationCount() = CirDeclarationCount(
|
||||
artificialNodeCount = artificialNodeCount,
|
||||
nonArtificialNodeCount = nonArtificialNodeCount
|
||||
)
|
||||
}
|
||||
|
||||
internal fun CirNode<*, *>.countCirDeclarations(): CirDeclarationCount {
|
||||
val counter = MutableCirDeclarationCount()
|
||||
accept(CirNodeCounterVisitor, counter)
|
||||
return counter.toCirDeclarationCount()
|
||||
}
|
||||
|
||||
private object CirNodeCounterVisitor : CirNodeVisitor<MutableCirDeclarationCount, Unit> {
|
||||
|
||||
private operator fun MutableCirDeclarationCount.plusAssign(node: CirNode<*, *>) {
|
||||
node.targetDeclarations.forEach { declaration ->
|
||||
if (declaration == null) return@forEach
|
||||
if (declaration is ArtificialCirDeclaration) artificialNodeCount++ else nonArtificialNodeCount++
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitRootNode(node: CirRootNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
node.modules.forEach { (_, module) -> module.accept(this, data) }
|
||||
}
|
||||
|
||||
override fun visitModuleNode(node: CirModuleNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
node.packages.forEach { (_, pkg) -> pkg.accept(this, data) }
|
||||
}
|
||||
|
||||
override fun visitPackageNode(node: CirPackageNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
node.typeAliases.forEach { (_, typeAlias) -> typeAlias.accept(this, data) }
|
||||
node.properties.forEach { (_, property) -> property.accept(this, data) }
|
||||
node.functions.forEach { (_, function) -> function.accept(this, data) }
|
||||
node.classes.forEach { (_, clazz) -> clazz.accept(this, data) }
|
||||
}
|
||||
|
||||
override fun visitPropertyNode(node: CirPropertyNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
}
|
||||
|
||||
override fun visitFunctionNode(node: CirFunctionNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
}
|
||||
|
||||
override fun visitClassNode(node: CirClassNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
node.properties.forEach { (_, property) -> property.accept(this, data) }
|
||||
node.functions.forEach { (_, function) -> function.accept(this, data) }
|
||||
node.constructors.forEach { (_, constructor) -> constructor.accept(this, data) }
|
||||
node.classes.forEach { (_, clazz) -> clazz.accept(this, data) }
|
||||
}
|
||||
|
||||
override fun visitClassConstructorNode(node: CirClassConstructorNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
}
|
||||
|
||||
override fun visitTypeAliasNode(node: CirTypeAliasNode, data: MutableCirDeclarationCount) {
|
||||
data += node
|
||||
}
|
||||
}
|
||||
@@ -8,36 +8,6 @@ package org.jetbrains.kotlin.commonizer.transformer
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirRootNode
|
||||
|
||||
internal fun interface CirNodeTransformer {
|
||||
|
||||
sealed class Context {
|
||||
object Unchecked : Context()
|
||||
}
|
||||
|
||||
operator fun Context.invoke(root: CirRootNode)
|
||||
operator fun invoke(root: CirRootNode)
|
||||
}
|
||||
|
||||
internal class Checked private constructor() : CirNodeTransformer.Context() {
|
||||
companion object {
|
||||
private val areAssertionsEnabled = this::class.java.desiredAssertionStatus()
|
||||
|
||||
operator fun CirNodeTransformer.invoke(cirRootNode: CirRootNode) {
|
||||
val context = if (areAssertionsEnabled) Checked() else Unchecked
|
||||
val declarationCountBefore = if (areAssertionsEnabled) cirRootNode.countCirDeclarations() else null
|
||||
|
||||
with(this) {
|
||||
context.invoke(cirRootNode)
|
||||
}
|
||||
|
||||
if (declarationCountBefore != null) {
|
||||
val declarationCountAfter = cirRootNode.countCirDeclarations()
|
||||
if (declarationCountAfter.nonArtificialNodeCount > declarationCountBefore.nonArtificialNodeCount) {
|
||||
throw AssertionError(
|
||||
"$this attached declarations not marked as artificial\n" +
|
||||
"Declaration Count before: $declarationCountBefore\n" +
|
||||
"Declaration Count after: $declarationCountAfter"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.commonizer.mergedtree.*
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirNodeRelationship.Composite.Companion.plus
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirNodeRelationship.ParentNode
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirNodeRelationship.PreferredNode
|
||||
import org.jetbrains.kotlin.commonizer.transformer.CirNodeTransformer.Context
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
@@ -19,7 +18,7 @@ internal class InlineTypeAliasCirNodeTransformer(
|
||||
private val storageManager: StorageManager,
|
||||
private val classifiers: CirKnownClassifiers
|
||||
) : CirNodeTransformer {
|
||||
override fun Context.invoke(root: CirRootNode) {
|
||||
override fun invoke(root: CirRootNode) {
|
||||
root.modules.values.forEach(::invoke)
|
||||
}
|
||||
|
||||
@@ -88,21 +87,21 @@ internal class InlineTypeAliasCirNodeTransformer(
|
||||
val aliasedConstructor = aliasedConstructorNode.targetDeclarations[targetIndex] ?: return@forEach
|
||||
intoClassNode.constructors.getOrPut(key) {
|
||||
buildClassConstructorNode(storageManager, targetSize, classifiers, ParentNode(intoClassNode))
|
||||
}.targetDeclarations[targetIndex] = aliasedConstructor.withContainingClass(intoClass).markedArtificial()
|
||||
}.targetDeclarations[targetIndex] = aliasedConstructor.withContainingClass(intoClass)
|
||||
}
|
||||
|
||||
fromAliasedClassNode.functions.forEach { (key, aliasedFunctionNode) ->
|
||||
val aliasedFunction = aliasedFunctionNode.targetDeclarations[targetIndex] ?: return@forEach
|
||||
intoClassNode.functions.getOrPut(key) {
|
||||
buildFunctionNode(storageManager, targetSize, classifiers, ParentNode(intoClassNode))
|
||||
}.targetDeclarations[targetIndex] = aliasedFunction.withContainingClass(intoClass).markedArtificial()
|
||||
}.targetDeclarations[targetIndex] = aliasedFunction.withContainingClass(intoClass)
|
||||
}
|
||||
|
||||
fromAliasedClassNode.properties.forEach { (key, aliasedPropertyNode) ->
|
||||
val aliasedProperty = aliasedPropertyNode.targetDeclarations[targetIndex] ?: return@forEach
|
||||
intoClassNode.properties.getOrPut(key) {
|
||||
buildPropertyNode(storageManager, targetSize, classifiers, ParentNode(intoClassNode))
|
||||
}.targetDeclarations[targetIndex] = aliasedProperty.withContainingClass(intoClass).markedArtificial()
|
||||
}.targetDeclarations[targetIndex] = aliasedProperty.withContainingClass(intoClass)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +131,7 @@ private fun ClassNodeIndex(module: CirModuleNode): ClassNodeIndex = module.packa
|
||||
private data class ArtificialAliasedCirClass(
|
||||
val pointingTypeAlias: CirTypeAlias,
|
||||
val pointedClass: CirClass
|
||||
) : CirClass by pointedClass, ArtificialCirDeclaration {
|
||||
) : CirClass by pointedClass {
|
||||
override val name: CirName = pointingTypeAlias.name
|
||||
override var companion: CirName?
|
||||
get() = null
|
||||
@@ -143,4 +142,4 @@ private fun CirTypeAlias.toArtificialCirClass(): CirClass = CirClass.create(
|
||||
annotations = emptyList(), name = name, typeParameters = typeParameters,
|
||||
visibility = this.visibility, modality = Modality.FINAL, kind = ClassKind.CLASS,
|
||||
companion = null, isCompanion = false, isData = false, isValue = false, isInner = false, isExternal = false
|
||||
).also { it.supertypes = emptyList() }.markedArtificial()
|
||||
).also { it.supertypes = emptyList() }
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.commonizer.transformer
|
||||
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirModule
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirName
|
||||
import org.jetbrains.kotlin.commonizer.cir.markedArtificial
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.buildModuleNode
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.buildRootNode
|
||||
import org.jetbrains.kotlin.commonizer.transformer.Checked.Companion.invoke
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertIs
|
||||
|
||||
class RunTransformationTest {
|
||||
|
||||
private val storageManager = LockBasedStorageManager("test")
|
||||
|
||||
@Test
|
||||
fun `fails when attaching non-artificial declarations`() {
|
||||
val badTransformer = CirNodeTransformer { root ->
|
||||
root.modules[CirName.create("bad-module")] = buildModuleNode(storageManager, 1).also { moduleNode ->
|
||||
moduleNode.targetDeclarations[0] = CirModule.create(CirName.create("bad-module"))
|
||||
}
|
||||
}
|
||||
|
||||
val node = buildRootNode(storageManager, 0)
|
||||
assertFailsWith<AssertionError> { badTransformer(node) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when attaching non-artificial declarations`() {
|
||||
val goodTransformer = CirNodeTransformer { root ->
|
||||
assertIs<Checked>(this, "Expected 'Checked' context")
|
||||
|
||||
root.modules[CirName.create("artificial-module")] = buildModuleNode(storageManager, 1).also { moduleNode ->
|
||||
moduleNode.targetDeclarations[0] = CirModule.create(CirName.create("artificial-module")).markedArtificial()
|
||||
}
|
||||
}
|
||||
|
||||
val node = buildRootNode(storageManager, 0)
|
||||
goodTransformer(node)
|
||||
|
||||
assertEquals(CirName.create("artificial-module"), node.modules.values.single().targetDeclarations.single()?.name)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user