[IR] KotlinLikeDumper: various changes for value and type params

* support annotations on value parameters
* support new value parameter flags -- hidden, assignable
* extract and reuse logic for value and type parameters
This commit is contained in:
Zalim Bashorov
2020-11-11 14:22:54 +03:00
committed by teamcityserver
parent 5cb2572c60
commit 182cb52bdb
@@ -185,25 +185,35 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
first = false first = false
} }
printParameterModifiersWithNoIndent( it.printAValueParameterWithNoIndent(this)
isVararg = it.varargElementType != null,
it.isCrossinline,
it.isNoinline,
)
p.printWithNoIndent(it.name.asString())
p.printWithNoIndent(": ")
(it.varargElementType ?: it.type).printTypeWithNoIndent()
// TODO print it.type too for varargs?
it.defaultValue?.let { v ->
p.printWithNoIndent(" = ")
v.accept(this@KotlinLikeDumper, this)
}
} }
p.printWithNoIndent(")") p.printWithNoIndent(")")
} }
private fun IrValueParameter.printAValueParameterWithNoIndent(data: IrDeclaration?) {
printAnnotationsWithNoIndent()
printParameterModifiersWithNoIndent(
isVararg = varargElementType != null,
isCrossinline,
isNoinline,
// TODO no test
isHidden,
// TODO no test
isAssignable
)
p.printWithNoIndent(name.asString())
p.printWithNoIndent(": ")
(varargElementType ?: type).printTypeWithNoIndent()
// TODO print it.type too for varargs?
defaultValue?.let { v ->
p.printWithNoIndent(" = ")
v.accept(this@KotlinLikeDumper, data)
}
}
private fun IrTypeParametersContainer.printWhereClauseIfNeededWithNoIndent() { private fun IrTypeParametersContainer.printWhereClauseIfNeededWithNoIndent() {
if (typeParameters.none { it.superTypes.size > 1 }) return if (typeParameters.none { it.superTypes.size > 1 }) return
@@ -212,21 +222,29 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
var first = true var first = true
typeParameters.forEach { typeParameters.forEach {
if (it.superTypes.size > 1) { if (it.superTypes.size > 1) {
it.superTypes.forEach { superType -> // TODO no test with more than one generic parameter with more supertypes
if (!first) { first = it.printWhereClauseTypesWithNoIndent(first)
p.printWithNoIndent(", ")
} else {
first = false
}
p.printWithNoIndent(it.name.asString())
p.printWithNoIndent(" : ")
superType.printTypeWithNoIndent()
}
} }
} }
} }
private fun IrTypeParameter.printWhereClauseTypesWithNoIndent(first: Boolean): Boolean {
var myFirst = first
superTypes.forEachIndexed { i, superType ->
if (!myFirst) {
p.printWithNoIndent(", ")
} else {
myFirst = false
}
p.printWithNoIndent(name.asString())
p.printWithNoIndent(" : ")
superType.printTypeWithNoIndent()
}
return myFirst
}
private fun IrTypeParametersContainer.printTypeParametersWithNoIndent(postfix: String = "") { private fun IrTypeParametersContainer.printTypeParametersWithNoIndent(postfix: String = "") {
if (typeParameters.isEmpty()) return if (typeParameters.isEmpty()) return
@@ -240,22 +258,26 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
first = false first = false
} }
it.variance.printVarianceWithNoIndent() it.printATypeParameterWithNoIndent()
if (it.isReified) p.printWithNoIndent("reified ")
it.printAnnotationsWithNoIndent()
p.printWithNoIndent(it.name.asString())
if (it.superTypes.size == 1) {
p.printWithNoIndent(" : ")
it.superTypes.single().printTypeWithNoIndent()
}
} }
p.printWithNoIndent(">") p.printWithNoIndent(">")
p.printWithNoIndent(postfix) p.printWithNoIndent(postfix)
} }
private fun IrTypeParameter.printATypeParameterWithNoIndent() {
variance.printVarianceWithNoIndent()
if (isReified) p.printWithNoIndent("reified ")
printAnnotationsWithNoIndent()
p.printWithNoIndent(name.asString())
if (superTypes.size == 1) {
p.printWithNoIndent(" : ")
superTypes.single().printTypeWithNoIndent()
}
}
private fun Variance.printVarianceWithNoIndent() { private fun Variance.printVarianceWithNoIndent() {
if (this != Variance.INVARIANT) { if (this != Variance.INVARIANT) {
p.printWithNoIndent("$label ") p.printWithNoIndent("$label ")
@@ -393,7 +415,9 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
printParameterModifiersWithNoIndent( printParameterModifiersWithNoIndent(
isVararg, isVararg,
isCrossinline = INAPPLICABLE, isCrossinline = INAPPLICABLE,
isNoinline = INAPPLICABLE isNoinline = INAPPLICABLE,
isHidden = INAPPLICABLE,
isAssignable = INAPPLICABLE
) )
p(isSuspend, "suspend") p(isSuspend, "suspend")
p(isInner, "inner") p(isInner, "inner")
@@ -415,10 +439,14 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
isVararg: Boolean, isVararg: Boolean,
isCrossinline: Boolean, isCrossinline: Boolean,
isNoinline: Boolean, isNoinline: Boolean,
isHidden: Boolean,
isAssignable: Boolean,
) { ) {
p(isVararg, "vararg") p(isVararg, "vararg")
p(isCrossinline, "crossinline") p(isCrossinline, "crossinline")
p(isNoinline, "noinline") p(isNoinline, "noinline")
p(isHidden, "hidden")
p(isAssignable, "var")
} }
private fun IrValueParameter.printExtensionReceiverParameter() { private fun IrValueParameter.printExtensionReceiverParameter() {
@@ -551,6 +579,16 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
p.printlnWithNoIndent() p.printlnWithNoIndent()
} }
override fun visitTypeParameter(declaration: IrTypeParameter, data: IrDeclaration?) {
declaration.printATypeParameterWithNoIndent()
if (declaration.superTypes.size > 1) declaration.printWhereClauseTypesWithNoIndent(true)
}
override fun visitValueParameter(declaration: IrValueParameter, data: IrDeclaration?) {
// TODO index?
declaration.printAValueParameterWithNoIndent(data)
}
override fun visitProperty(declaration: IrProperty, data: IrDeclaration?) { override fun visitProperty(declaration: IrProperty, data: IrDeclaration?) {
if (options.printFakeOverridesStrategy == FakeOverridesStrategy.NONE && declaration.isFakeOverride) return if (options.printFakeOverridesStrategy == FakeOverridesStrategy.NONE && declaration.isFakeOverride) return
@@ -937,7 +975,14 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
p.printWithNoIndent(">") p.printWithNoIndent(">")
} }
if (omitBracesIfNoArguments && valueArgumentsCount == 0 && typeArgumentsCount == 0) return fun allValueArgumentsAreNull(): Boolean {
for (i in 0 until valueArgumentsCount) {
if (getValueArgument(i) != null) return false
}
return true
}
if (omitBracesIfNoArguments && typeArgumentsCount == 0 && (valueArgumentsCount == 0 || allValueArgumentsAreNull())) return
p.printWithNoIndent("(") p.printWithNoIndent("(")
@@ -950,6 +995,7 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
// } // }
repeat(valueArgumentsCount) { i -> repeat(valueArgumentsCount) { i ->
// TODO should we print something for omitted arguments (== null)?
getValueArgument(i)?.let { getValueArgument(i)?.let {
if (i > 0) p.printWithNoIndent(", ") if (i > 0) p.printWithNoIndent(", ")
// TODO flag to print param name // TODO flag to print param name
@@ -1292,26 +1338,22 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
} }
override fun visitExternalPackageFragment(declaration: IrExternalPackageFragment, data: IrDeclaration?) { override fun visitExternalPackageFragment(declaration: IrExternalPackageFragment, data: IrDeclaration?) {
// TODO support
super.visitExternalPackageFragment(declaration, data) super.visitExternalPackageFragment(declaration, data)
} }
override fun visitScript(declaration: IrScript, data: IrDeclaration?) { override fun visitScript(declaration: IrScript, data: IrDeclaration?) {
// TODO support
super.visitScript(declaration, data) super.visitScript(declaration, data)
} }
override fun visitTypeParameter(declaration: IrTypeParameter, data: IrDeclaration?) {
super.visitTypeParameter(declaration, data)
}
override fun visitValueParameter(declaration: IrValueParameter, data: IrDeclaration?) {
super.visitValueParameter(declaration, data)
}
override fun visitSuspendableExpression(expression: IrSuspendableExpression, data: IrDeclaration?) { override fun visitSuspendableExpression(expression: IrSuspendableExpression, data: IrDeclaration?) {
// TODO support
super.visitSuspendableExpression(expression, data) super.visitSuspendableExpression(expression, data)
} }
override fun visitSuspensionPoint(expression: IrSuspensionPoint, data: IrDeclaration?) { override fun visitSuspensionPoint(expression: IrSuspensionPoint, data: IrDeclaration?) {
// TODO support
super.visitSuspensionPoint(expression, data) super.visitSuspensionPoint(expression, data)
} }