Skip to content
Permalink
1.4.20
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
/*
* 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.ir.util
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.SourceManager
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.utils.Printer
fun IrElement.dump(normalizeNames: Boolean = false): String =
try {
StringBuilder().also { sb ->
accept(DumpIrTreeVisitor(sb, normalizeNames), "")
}.toString()
} catch (e: Exception) {
"(Full dump is not available: ${e.message})\n" + render()
}
fun IrFile.dumpTreesFromLineNumber(lineNumber: Int, normalizeNames: Boolean = false): String {
val sb = StringBuilder()
accept(DumpTreeFromSourceLineVisitor(fileEntry, lineNumber, sb, normalizeNames), null)
return sb.toString()
}
class DumpIrTreeVisitor(
out: Appendable,
normalizeNames: Boolean = false
) : IrElementVisitor<Unit, String> {
private val printer = Printer(out, " ")
private val elementRenderer = RenderIrElementVisitor(normalizeNames)
private fun IrType.render() = elementRenderer.renderType(this)
override fun visitElement(element: IrElement, data: String) {
element.dumpLabeledElementWith(data) {
if (element is IrAnnotationContainer) {
dumpAnnotations(element)
}
element.acceptChildren(this@DumpIrTreeVisitor, "")
}
}
override fun visitModuleFragment(declaration: IrModuleFragment, data: String) {
declaration.dumpLabeledElementWith(data) {
declaration.files.dumpElements()
}
}
override fun visitFile(declaration: IrFile, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.declarations.dumpElements()
}
}
override fun visitClass(declaration: IrClass, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.thisReceiver?.accept(this, "\$this")
declaration.typeParameters.dumpElements()
declaration.declarations.dumpElements()
}
}
override fun visitTypeAlias(declaration: IrTypeAlias, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.typeParameters.dumpElements()
}
}
override fun visitTypeParameter(declaration: IrTypeParameter, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
}
}
override fun visitSimpleFunction(declaration: IrSimpleFunction, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.correspondingPropertySymbol?.dumpInternal("correspondingProperty")
declaration.overriddenSymbols.dumpItems<IrSymbol>("overridden") {
it.dump()
}
declaration.typeParameters.dumpElements()
declaration.dispatchReceiverParameter?.accept(this, "\$this")
declaration.extensionReceiverParameter?.accept(this, "\$receiver")
declaration.valueParameters.dumpElements()
declaration.body?.accept(this, "")
}
}
private fun dumpAnnotations(element: IrAnnotationContainer) {
element.annotations.dumpItems("annotations") { irAnnotation: IrConstructorCall ->
printer.println(elementRenderer.renderAsAnnotation(irAnnotation))
}
}
private fun IrSymbol.dump(label: String? = null) =
printer.println(
elementRenderer.renderSymbolReference(this).let {
if (label != null) "$label: $it" else it
}
)
override fun visitConstructor(declaration: IrConstructor, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.typeParameters.dumpElements()
declaration.dispatchReceiverParameter?.accept(this, "\$outer")
declaration.valueParameters.dumpElements()
declaration.body?.accept(this, "")
}
}
override fun visitProperty(declaration: IrProperty, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.backingField?.accept(this, "")
declaration.getter?.accept(this, "")
declaration.setter?.accept(this, "")
}
}
override fun visitField(declaration: IrField, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.initializer?.accept(this, "")
}
}
private fun List<IrElement>.dumpElements() {
forEach { it.accept(this@DumpIrTreeVisitor, "") }
}
override fun visitErrorCallExpression(expression: IrErrorCallExpression, data: String) {
expression.dumpLabeledElementWith(data) {
expression.explicitReceiver?.accept(this, "receiver")
expression.arguments.dumpElements()
}
}
override fun visitEnumEntry(declaration: IrEnumEntry, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.initializerExpression?.accept(this, "init")
declaration.correspondingClass?.accept(this, "class")
}
}
override fun visitMemberAccess(expression: IrMemberAccessExpression<*>, data: String) {
expression.dumpLabeledElementWith(data) {
dumpTypeArguments(expression)
expression.dispatchReceiver?.accept(this, "\$this")
expression.extensionReceiver?.accept(this, "\$receiver")
val valueParameterNames = expression.getValueParameterNamesForDebug()
for (index in 0 until expression.valueArgumentsCount) {
expression.getValueArgument(index)?.accept(this, valueParameterNames[index])
}
}
}
override fun visitConstructorCall(expression: IrConstructorCall, data: String) {
expression.dumpLabeledElementWith(data) {
dumpTypeArguments(expression)
expression.outerClassReceiver?.accept(this, "\$outer")
dumpConstructorValueArguments(expression)
}
}
private fun dumpConstructorValueArguments(expression: IrConstructorCall) {
val valueParameterNames = expression.getValueParameterNamesForDebug()
for (index in 0 until expression.valueArgumentsCount) {
expression.getValueArgument(index)?.accept(this, valueParameterNames[index])
}
}
private fun dumpTypeArguments(expression: IrMemberAccessExpression<*>) {
val typeParameterNames = expression.getTypeParameterNames(expression.typeArgumentsCount)
for (index in 0 until expression.typeArgumentsCount) {
printer.println("<${typeParameterNames[index]}>: ${expression.renderTypeArgument(index)}")
}
}
private fun dumpTypeArguments(expression: IrConstructorCall) {
val typeParameterNames = expression.getTypeParameterNames(expression.typeArgumentsCount)
for (index in 0 until expression.typeArgumentsCount) {
val typeParameterName = typeParameterNames[index]
val parameterLabel =
if (index < expression.classTypeArgumentsCount)
"class: $typeParameterName"
else
typeParameterName
printer.println("<$parameterLabel>: ${expression.renderTypeArgument(index)}")
}
}
private fun IrMemberAccessExpression<*>.getTypeParameterNames(expectedCount: Int): List<String> =
if (symbol.isBound)
symbol.owner.getTypeParameterNames(expectedCount)
else
getPlaceholderParameterNames(expectedCount)
private fun IrSymbolOwner.getTypeParameterNames(expectedCount: Int): List<String> =
if (this is IrTypeParametersContainer) {
val typeParameters = if (this is IrConstructor) getFullTypeParametersList() else this.typeParameters
(0 until expectedCount).map {
if (it < typeParameters.size)
typeParameters[it].name.asString()
else
"${it + 1}"
}
} else {
getPlaceholderParameterNames(expectedCount)
}
private fun IrConstructor.getFullTypeParametersList(): List<IrTypeParameter> {
val parentClass = try {
parent as? IrClass ?: return typeParameters
} catch (e: Exception) {
return typeParameters
}
return parentClass.typeParameters + typeParameters
}
private fun IrMemberAccessExpression<*>.renderTypeArgument(index: Int): String =
getTypeArgument(index)?.render() ?: "<none>"
override fun visitGetField(expression: IrGetField, data: String) {
expression.dumpLabeledElementWith(data) {
expression.receiver?.accept(this, "receiver")
}
}
override fun visitSetField(expression: IrSetField, data: String) {
expression.dumpLabeledElementWith(data) {
expression.receiver?.accept(this, "receiver")
expression.value.accept(this, "value")
}
}
override fun visitWhen(expression: IrWhen, data: String) {
expression.dumpLabeledElementWith(data) {
expression.branches.dumpElements()
}
}
override fun visitBranch(branch: IrBranch, data: String) {
branch.dumpLabeledElementWith(data) {
branch.condition.accept(this, "if")
branch.result.accept(this, "then")
}
}
override fun visitWhileLoop(loop: IrWhileLoop, data: String) {
loop.dumpLabeledElementWith(data) {
loop.condition.accept(this, "condition")
loop.body?.accept(this, "body")
}
}
override fun visitDoWhileLoop(loop: IrDoWhileLoop, data: String) {
loop.dumpLabeledElementWith(data) {
loop.body?.accept(this, "body")
loop.condition.accept(this, "condition")
}
}
override fun visitTry(aTry: IrTry, data: String) {
aTry.dumpLabeledElementWith(data) {
aTry.tryResult.accept(this, "try")
aTry.catches.dumpElements()
aTry.finallyExpression?.accept(this, "finally")
}
}
override fun visitTypeOperator(expression: IrTypeOperatorCall, data: String) {
expression.dumpLabeledElementWith(data) {
expression.acceptChildren(this, "")
}
}
override fun visitDynamicOperatorExpression(expression: IrDynamicOperatorExpression, data: String) {
expression.dumpLabeledElementWith(data) {
expression.receiver.accept(this, "receiver")
for ((i, arg) in expression.arguments.withIndex()) {
arg.accept(this, i.toString())
}
}
}
private inline fun IrElement.dumpLabeledElementWith(label: String, body: () -> Unit) {
printer.println(accept(elementRenderer, null).withLabel(label))
indented(body)
}
private inline fun <T> Collection<T>.dumpItems(caption: String, renderElement: (T) -> Unit) {
if (isEmpty()) return
indented(caption) {
forEach {
renderElement(it)
}
}
}
private fun IrSymbol.dumpInternal(label: String? = null) {
if (isBound)
owner.dumpInternal(label)
else
printer.println("$label: UNBOUND ${javaClass.simpleName}")
}
private fun IrElement.dumpInternal(label: String? = null) {
if (label != null) {
printer.println("$label: ", accept(elementRenderer, null))
} else {
printer.println(accept(elementRenderer, null))
}
}
private inline fun indented(label: String, body: () -> Unit) {
printer.println("$label:")
indented(body)
}
private inline fun indented(body: () -> Unit) {
printer.pushIndent()
body()
printer.popIndent()
}
private fun String.withLabel(label: String) =
if (label.isEmpty()) this else "$label: $this"
}
class DumpTreeFromSourceLineVisitor(
val fileEntry: SourceManager.FileEntry,
private val lineNumber: Int,
out: Appendable,
normalizeNames: Boolean = false
) : IrElementVisitorVoid {
private val dumper = DumpIrTreeVisitor(out, normalizeNames)
override fun visitElement(element: IrElement) {
if (fileEntry.getLineNumber(element.startOffset) == lineNumber) {
element.accept(dumper, "")
return
}
element.acceptChildrenVoid(this)
}
}
internal fun IrMemberAccessExpression<*>.getValueParameterNamesForDebug(): List<String> {
val expectedCount = valueArgumentsCount
if (symbol.isBound) {
val owner = symbol.owner
if (owner is IrFunction) {
return (0 until expectedCount).map {
if (it < owner.valueParameters.size)
owner.valueParameters[it].name.asString()
else
"${it + 1}"
}
}
}
return getPlaceholderParameterNames(expectedCount)
}
internal fun getPlaceholderParameterNames(expectedCount: Int) =
(1..expectedCount).map { "$it" }