[IR] Add dumpKotlinLike for IrType and IrTypeArgument

This commit is contained in:
Zalim Bashorov
2020-12-25 21:17:25 +03:00
parent 77a9d14f93
commit c569ec1bad
@@ -29,9 +29,22 @@ import org.jetbrains.kotlin.utils.Printer
* * IMPLICIT_DYNAMIC_CAST -- expr /*~> dynamic */ * * IMPLICIT_DYNAMIC_CAST -- expr /*~> dynamic */
* * REINTERPRET_CAST -- expr /*=> Type */ * * REINTERPRET_CAST -- expr /*=> Type */
*/ */
fun IrElement.dumpKotlinLike(options: KotlinLikeDumpOptions = KotlinLikeDumpOptions()): String { fun IrElement.dumpKotlinLike(options: KotlinLikeDumpOptions = KotlinLikeDumpOptions()): String =
dumpKotlinLike(this, KotlinLikeDumper::printElement, options)
fun IrType.dumpKotlinLike(): String =
dumpKotlinLike(this, KotlinLikeDumper::printType)
fun IrTypeArgument.dumpKotlinLike(): String =
dumpKotlinLike(this, KotlinLikeDumper::printTypeArgument)
private inline fun <T> dumpKotlinLike(
target: T,
print: KotlinLikeDumper.(T) -> Unit,
options: KotlinLikeDumpOptions = KotlinLikeDumpOptions()
): String {
val sb = StringBuilder() val sb = StringBuilder()
accept(KotlinLikeDumper(Printer(sb, 1, " "), options), null) KotlinLikeDumper(Printer(sb, 1, " "), options).print(target)
return sb.toString() return sb.toString()
} }
@@ -98,6 +111,18 @@ enum class FakeOverridesStrategy {
*/ */
private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOptions) : IrElementVisitor<Unit, IrDeclaration?> { private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOptions) : IrElementVisitor<Unit, IrDeclaration?> {
fun printElement(element: IrElement) {
element.accept(this, null)
}
fun printType(type: IrType) {
type.printTypeWithNoIndent()
}
fun printTypeArgument(typeArg: IrTypeArgument) {
typeArg.printTypeArgumentWithNoIndent()
}
override fun visitElement(element: IrElement, data: IrDeclaration?) { override fun visitElement(element: IrElement, data: IrDeclaration?) {
val e = "/* ERROR: unsupported element type: " + element.javaClass.simpleName + " */" val e = "/* ERROR: unsupported element type: " + element.javaClass.simpleName + " */"
if (element is IrExpression) { if (element is IrExpression) {
@@ -397,14 +422,7 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
arguments.forEachIndexed { i, typeArg -> arguments.forEachIndexed { i, typeArg ->
p(i > 0, ",") p(i > 0, ",")
when (typeArg) { typeArg.printTypeArgumentWithNoIndent()
is IrStarProjection ->
p.printWithNoIndent("*")
is IrTypeProjection -> {
typeArg.variance.printVarianceWithNoIndent()
typeArg.type.printTypeWithNoIndent()
}
}
} }
p.printWithNoIndent(">") p.printWithNoIndent(">")
} }
@@ -420,6 +438,17 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
} }
} }
private fun IrTypeArgument.printTypeArgumentWithNoIndent() {
when (this) {
is IrStarProjection ->
p.printWithNoIndent("*")
is IrTypeProjection -> {
variance.printVarianceWithNoIndent()
type.printTypeWithNoIndent()
}
}
}
override fun visitTypeAlias(declaration: IrTypeAlias, data: IrDeclaration?) { override fun visitTypeAlias(declaration: IrTypeAlias, data: IrDeclaration?) {
declaration.printlnAnnotations() declaration.printlnAnnotations()
p.printIndent() p.printIndent()