Skip to content

Commit

Permalink
(fixup) Deduplicate InterfaceAbstractClassSolver
Browse files Browse the repository at this point in the history
  • Loading branch information
udalov committed Feb 16, 2022
1 parent 0a7d62f commit a0b9a9c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 226 deletions.

This file was deleted.

Expand Up @@ -5,8 +5,8 @@

package org.jetbrains.kotlin.ir.generator.model

import org.jetbrains.kotlin.ir.generator.elementBaseType
import org.jetbrains.kotlin.ir.generator.config.*
import org.jetbrains.kotlin.ir.generator.elementBaseType
import org.jetbrains.kotlin.ir.generator.util.*
import org.jetbrains.kotlin.utils.addToStdlib.castAll
import org.jetbrains.kotlin.utils.addToStdlib.partitionIsInstance
Expand Down Expand Up @@ -65,7 +65,7 @@ fun config2model(config: Config): Model {
}

val rootElement = replaceElementRefs(config, ec2el)
setTypeKinds(elements)
configureInterfacesAndAbstractClasses(elements)
addAbstractElement(elements)
markLeaves(elements)
configureDescriptorApiAnnotation(elements)
Expand Down Expand Up @@ -144,33 +144,6 @@ private fun markLeaves(elements: List<Element>) {
}
}


private fun setTypeKinds(elements: List<Element>) {
val nodeMap = elements.associateWith {
object : Node {
val element = it
override var kind = it.targetKind
override var parentNodes = emptyList<Node>()
}
}
val nodes = nodeMap.values.toList()
for (node in nodes) {
node.parentNodes = node.element.elementParents.map { nodeMap.getValue(it.element) }
}

solveGraphForClassVsInterface(nodes)
for (node in nodes) {
node.element.targetKind?.let { requested ->
val actual = node.kind!!
check(actual == requested) { "Could not meet type kind requirement for element ${node.element} - requested $requested, was $actual" }
}
node.element.kind = when (node.kind!!) {
TypeKind.Interface -> Element.Kind.Interface
TypeKind.Class -> Element.Kind.AbstractClass
}
}
}

private fun addAbstractElement(elements: List<Element>) {
for (el in elements) {
if (el.kind!!.typeKind == TypeKind.Class && el.elementParents.none { it.element.kind!!.typeKind == TypeKind.Class }) {
Expand Down
@@ -0,0 +1,47 @@
/*
* 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.ir.generator.model

import org.jetbrains.kotlin.generators.util.Node
import org.jetbrains.kotlin.generators.util.solveGraphForClassVsInterface
import org.jetbrains.kotlin.ir.generator.util.TypeKind

private class NodeImpl(val element: Element) : Node {
override val parents: List<Node>
get() = element.elementParents.map { NodeImpl(it.element) }

override val origin: Node
get() = this

override fun equals(other: Any?): Boolean =
other is NodeImpl && element == other.element

override fun hashCode(): Int =
element.hashCode()
}

fun configureInterfacesAndAbstractClasses(elements: List<Element>) {
val nodes = elements.map(::NodeImpl)
val solution = solveGraphForClassVsInterface(
nodes,
nodes.filter { it.element.targetKind == TypeKind.Interface },
nodes.filter { it.element.targetKind == TypeKind.Class },
)
updateKinds(nodes, solution)
}

private fun updateKinds(nodes: List<NodeImpl>, solution: List<Boolean>) {
for (index in solution.indices) {
val isClass = solution[index]
val element = nodes[index].element
if (isClass) {
check(element.targetKind != TypeKind.Interface) { element }
element.kind = Element.Kind.AbstractClass
} else {
element.kind = Element.Kind.Interface
}
}
}

0 comments on commit a0b9a9c

Please sign in to comment.