IR: remove unused AbstractClosureAnnotator

This commit is contained in:
Georgy Bronnikov
2020-09-14 15:07:51 +03:00
parent 5e02a4efd7
commit 38ee182f51
2 changed files with 0 additions and 187 deletions
@@ -1,111 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.backend.common
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrLocalDelegatedProperty
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.resolve.DescriptorUtils
import java.util.*
class Closure(val capturedValues: List<ValueDescriptor>)
@OptIn(ObsoleteDescriptorBasedAPI::class)
abstract class AbstractClosureAnnotator : IrElementVisitorVoid {
protected abstract fun recordFunctionClosure(functionDescriptor: FunctionDescriptor, closure: Closure)
protected abstract fun recordClassClosure(classDescriptor: ClassDescriptor, closure: Closure)
private class ClosureBuilder(val owner: DeclarationDescriptor) {
val capturedValues = mutableSetOf<ValueDescriptor>()
fun buildClosure() = Closure(capturedValues.toList())
fun addNested(closure: Closure) {
fillInNestedClosure(capturedValues, closure.capturedValues)
}
private fun <T : CallableDescriptor> fillInNestedClosure(destination: MutableSet<T>, nested: List<T>) {
nested.filterTo(destination) {
it.containingDeclaration != owner
}
}
}
private val closuresStack = ArrayDeque<ClosureBuilder>()
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitClass(declaration: IrClass) {
val classDescriptor = declaration.descriptor
val closureBuilder = ClosureBuilder(classDescriptor)
closuresStack.push(closureBuilder)
declaration.acceptChildrenVoid(this)
closuresStack.pop()
val closure = closureBuilder.buildClosure()
if (DescriptorUtils.isLocal(classDescriptor)) {
recordClassClosure(classDescriptor, closure)
}
closuresStack.peek()?.addNested(closure)
}
override fun visitFunction(declaration: IrFunction) {
val functionDescriptor = declaration.descriptor
val closureBuilder = ClosureBuilder(functionDescriptor)
closuresStack.push(closureBuilder)
declaration.acceptChildrenVoid(this)
closuresStack.pop()
val closure = closureBuilder.buildClosure()
if (DescriptorUtils.isLocal(functionDescriptor)) {
recordFunctionClosure(functionDescriptor, closure)
}
closuresStack.peek()?.addNested(closure)
}
override fun visitLocalDelegatedProperty(declaration: IrLocalDelegatedProperty) {
// Getter and setter of local delegated properties are special generated functions and don't have closure.
declaration.delegate.initializer?.acceptVoid(this)
}
override fun visitVariableAccess(expression: IrValueAccessExpression) {
val closureBuilder = closuresStack.peek() ?: return
val variableDescriptor = expression.symbol.descriptor
if (variableDescriptor.containingDeclaration != closureBuilder.owner) {
closureBuilder.capturedValues.add(variableDescriptor)
}
expression.acceptChildrenVoid(this)
}
}
@@ -1,76 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.ir
import org.jetbrains.kotlin.backend.common.AbstractClosureAnnotator
import org.jetbrains.kotlin.backend.common.Closure
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
import java.io.PrintWriter
import java.io.StringWriter
abstract class AbstractClosureAnnotatorTestCase : AbstractIrGeneratorTestCase() {
override fun doTest(wholeFile: File, testFiles: List<TestFile>) {
val dir = wholeFile.parentFile
val ignoreErrors = shouldIgnoreErrors(wholeFile)
for ((testFile, irFile) in generateIrFilesAsSingleModule(testFiles, ignoreErrors)) {
doTestIrFileAgainstExpectations(dir, testFile, irFile)
}
}
private fun doTestIrFileAgainstExpectations(dir: File, testFile: TestFile, irFile: IrFile) {
val expectedFile = File(dir, testFile.name.replace(".kt", ".closure"))
val actualClosures = renderClosures(irFile)
KotlinTestUtils.assertEqualsToFile(expectedFile, actualClosures)
}
private fun renderClosures(irFile: IrFile): String {
val actualStringWriter = StringWriter()
val actualOut = PrintWriter(actualStringWriter)
irFile.acceptChildrenVoid(object : AbstractClosureAnnotator() {
override fun recordClassClosure(classDescriptor: ClassDescriptor, closure: Closure) {
actualOut.println("Closure for class ${classDescriptor.name}:")
printClosure(closure)
actualOut.println()
}
override fun recordFunctionClosure(functionDescriptor: FunctionDescriptor, closure: Closure) {
if (functionDescriptor is ConstructorDescriptor) {
actualOut.println("Closure for constructor ${functionDescriptor.containingDeclaration.name}:")
} else {
actualOut.println("Closure for function ${functionDescriptor.name}:")
}
printClosure(closure)
actualOut.println()
}
private fun printClosure(closure: Closure) {
closure.capturedValues.forEach {
actualOut.println(" variable ${it.name}")
}
}
})
return actualStringWriter.toString()
}
}