FIR: allow nullable types in diagnostic parameters

This commit is contained in:
Tianyu Geng
2021-04-29 16:19:21 -07:00
committed by Ilya Kirillov
parent 594fbbb4ef
commit cceb7197a5
14 changed files with 98 additions and 78 deletions
@@ -89,6 +89,9 @@ object ErrorListDiagnosticListRenderer : DiagnosticListRenderer() {
}
print(">")
}
if (type.isMarkedNullable) {
print("?")
}
}
private fun SmartPrinter.printTypeArgument(typeArgument: KTypeProjection) {
@@ -33,25 +33,25 @@ sealed class FirSimpleDiagnostic<out E : FirSourceElement> : FirDiagnostic<E>()
abstract override val factory: FirDiagnosticFactory0<*>
}
sealed class FirDiagnosticWithParameters1<out E : FirSourceElement, A : Any> : FirDiagnostic<E>() {
sealed class FirDiagnosticWithParameters1<out E : FirSourceElement, A> : FirDiagnostic<E>() {
abstract val a: A
abstract override val factory: FirDiagnosticFactory1<*, A>
}
sealed class FirDiagnosticWithParameters2<out E : FirSourceElement, A : Any, B : Any> : FirDiagnostic<E>() {
sealed class FirDiagnosticWithParameters2<out E : FirSourceElement, A, B> : FirDiagnostic<E>() {
abstract val a: A
abstract val b: B
abstract override val factory: FirDiagnosticFactory2<*, A, B>
}
sealed class FirDiagnosticWithParameters3<out E : FirSourceElement, A : Any, B : Any, C : Any> : FirDiagnostic<E>() {
sealed class FirDiagnosticWithParameters3<out E : FirSourceElement, A, B, C> : FirDiagnostic<E>() {
abstract val a: A
abstract val b: B
abstract val c: C
abstract override val factory: FirDiagnosticFactory3<*, A, B, C>
}
sealed class FirDiagnosticWithParameters4<out E : FirSourceElement, A : Any, B : Any, C : Any, D : Any> : FirDiagnostic<E>() {
sealed class FirDiagnosticWithParameters4<out E : FirSourceElement, A, B, C, D> : FirDiagnostic<E>() {
abstract val a: A
abstract val b: B
abstract val c: C
@@ -77,14 +77,14 @@ data class FirPsiSimpleDiagnostic<P : PsiElement>(
override val factory: FirDiagnosticFactory0<P>
) : FirSimpleDiagnostic<FirPsiSourceElement<P>>(), FirPsiDiagnostic<P>
data class FirPsiDiagnosticWithParameters1<P : PsiElement, A : Any>(
data class FirPsiDiagnosticWithParameters1<P : PsiElement, A>(
override val element: FirPsiSourceElement<P>,
override val a: A,
override val severity: Severity,
override val factory: FirDiagnosticFactory1<P, A>
) : FirDiagnosticWithParameters1<FirPsiSourceElement<P>, A>(), FirPsiDiagnostic<P>
data class FirPsiDiagnosticWithParameters2<P : PsiElement, A : Any, B : Any>(
data class FirPsiDiagnosticWithParameters2<P : PsiElement, A, B>(
override val element: FirPsiSourceElement<P>,
override val a: A,
override val b: B,
@@ -92,7 +92,7 @@ data class FirPsiDiagnosticWithParameters2<P : PsiElement, A : Any, B : Any>(
override val factory: FirDiagnosticFactory2<P, A, B>
) : FirDiagnosticWithParameters2<FirPsiSourceElement<P>, A, B>(), FirPsiDiagnostic<P>
data class FirPsiDiagnosticWithParameters3<P : PsiElement, A : Any, B : Any, C : Any>(
data class FirPsiDiagnosticWithParameters3<P : PsiElement, A, B, C>(
override val element: FirPsiSourceElement<P>,
override val a: A,
override val b: B,
@@ -101,7 +101,7 @@ data class FirPsiDiagnosticWithParameters3<P : PsiElement, A : Any, B : Any, C :
override val factory: FirDiagnosticFactory3<P, A, B, C>
) : FirDiagnosticWithParameters3<FirPsiSourceElement<P>, A, B, C>(), FirPsiDiagnostic<P>
data class FirPsiDiagnosticWithParameters4<P : PsiElement, A : Any, B : Any, C : Any, D : Any>(
data class FirPsiDiagnosticWithParameters4<P : PsiElement, A, B, C, D>(
override val element: FirPsiSourceElement<P>,
override val a: A,
override val b: B,
@@ -123,14 +123,14 @@ data class FirLightSimpleDiagnostic(
override val factory: FirDiagnosticFactory0<*>
) : FirSimpleDiagnostic<FirLightSourceElement>(), FirLightDiagnostic
data class FirLightDiagnosticWithParameters1<A : Any>(
data class FirLightDiagnosticWithParameters1<A>(
override val element: FirLightSourceElement,
override val a: A,
override val severity: Severity,
override val factory: FirDiagnosticFactory1<*, A>
) : FirDiagnosticWithParameters1<FirLightSourceElement, A>(), FirLightDiagnostic
data class FirLightDiagnosticWithParameters2<A : Any, B : Any>(
data class FirLightDiagnosticWithParameters2<A, B>(
override val element: FirLightSourceElement,
override val a: A,
override val b: B,
@@ -138,7 +138,7 @@ data class FirLightDiagnosticWithParameters2<A : Any, B : Any>(
override val factory: FirDiagnosticFactory2<*, A, B>
) : FirDiagnosticWithParameters2<FirLightSourceElement, A, B>(), FirLightDiagnostic
data class FirLightDiagnosticWithParameters3<A : Any, B : Any, C : Any>(
data class FirLightDiagnosticWithParameters3<A, B, C>(
override val element: FirLightSourceElement,
override val a: A,
override val b: B,
@@ -147,7 +147,7 @@ data class FirLightDiagnosticWithParameters3<A : Any, B : Any, C : Any>(
override val factory: FirDiagnosticFactory3<*, A, B, C>
) : FirDiagnosticWithParameters3<FirLightSourceElement, A, B, C>(), FirLightDiagnostic
data class FirLightDiagnosticWithParameters4<A : Any, B : Any, C : Any, D : Any>(
data class FirLightDiagnosticWithParameters4<A, B, C, D>(
override val element: FirLightSourceElement,
override val a: A,
override val b: B,
@@ -55,7 +55,7 @@ class FirDiagnosticFactory0<P : PsiElement>(
}
}
class FirDiagnosticFactory1<P : PsiElement, A : Any>(
class FirDiagnosticFactory1<P : PsiElement, A>(
name: String,
severity: Severity,
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT,
@@ -76,7 +76,7 @@ class FirDiagnosticFactory1<P : PsiElement, A : Any>(
}
}
class FirDiagnosticFactory2<P : PsiElement, A : Any, B : Any>(
class FirDiagnosticFactory2<P : PsiElement, A, B>(
name: String,
severity: Severity,
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT,
@@ -98,7 +98,7 @@ class FirDiagnosticFactory2<P : PsiElement, A : Any, B : Any>(
}
}
class FirDiagnosticFactory3<P : PsiElement, A : Any, B : Any, C : Any>(
class FirDiagnosticFactory3<P : PsiElement, A, B, C>(
name: String,
severity: Severity,
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT,
@@ -121,7 +121,7 @@ class FirDiagnosticFactory3<P : PsiElement, A : Any, B : Any, C : Any>(
}
}
class FirDiagnosticFactory4<P : PsiElement, A : Any, B : Any, C : Any, D : Any>(
class FirDiagnosticFactory4<P : PsiElement, A, B, C, D>(
name: String,
severity: Severity,
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT,
@@ -152,13 +152,13 @@ fun <P : PsiElement> FirDiagnosticFactory0<P>.on(element: FirSourceElement?): Fi
return element?.let { on(it) }
}
fun <P : PsiElement, A : Any> FirDiagnosticFactory1<P, A>.on(
fun <P : PsiElement, A> FirDiagnosticFactory1<P, A>.on(
element: FirSourceElement?, a: A
): FirDiagnosticWithParameters1<*, A>? {
return element?.let { on(it, a) }
}
fun <P : PsiElement, A : Any, B : Any> FirDiagnosticFactory2<P, A, B>.on(
fun <P : PsiElement, A, B> FirDiagnosticFactory2<P, A, B>.on(
element: FirSourceElement?,
a: A,
b: B
@@ -166,7 +166,7 @@ fun <P : PsiElement, A : Any, B : Any> FirDiagnosticFactory2<P, A, B>.on(
return element?.let { on(it, a, b) }
}
fun <P : PsiElement, A : Any, B : Any, C : Any> FirDiagnosticFactory3<P, A, B, C>.on(
fun <P : PsiElement, A, B, C> FirDiagnosticFactory3<P, A, B, C>.on(
element: FirSourceElement?,
a: A,
b: B,
@@ -16,19 +16,19 @@ fun <P : PsiElement> warning0(
return DiagnosticFactory0DelegateProvider(Severity.WARNING, positioningStrategy)
}
fun <P : PsiElement, A : Any> warning1(
fun <P : PsiElement, A> warning1(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory1DelegateProvider<P, A> {
return DiagnosticFactory1DelegateProvider(Severity.WARNING, positioningStrategy)
}
fun <P : PsiElement, A : Any, B : Any> warning2(
fun <P : PsiElement, A, B> warning2(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory2DelegateProvider<P, A, B> {
return DiagnosticFactory2DelegateProvider(Severity.WARNING, positioningStrategy)
}
fun <P : PsiElement, A : Any, B : Any, C : Any> warning3(
fun <P : PsiElement, A, B, C> warning3(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory3DelegateProvider<P, A, B, C> {
return DiagnosticFactory3DelegateProvider(Severity.WARNING, positioningStrategy)
@@ -40,25 +40,25 @@ fun <P : PsiElement> error0(
return DiagnosticFactory0DelegateProvider(Severity.ERROR, positioningStrategy)
}
fun <P : PsiElement, A : Any> error1(
fun <P : PsiElement, A> error1(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory1DelegateProvider<P, A> {
return DiagnosticFactory1DelegateProvider(Severity.ERROR, positioningStrategy)
}
fun <P : PsiElement, A : Any, B : Any> error2(
fun <P : PsiElement, A, B> error2(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory2DelegateProvider<P, A, B> {
return DiagnosticFactory2DelegateProvider(Severity.ERROR, positioningStrategy)
}
fun <P : PsiElement, A : Any, B : Any, C : Any> error3(
fun <P : PsiElement, A, B, C> error3(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory3DelegateProvider<P, A, B, C> {
return DiagnosticFactory3DelegateProvider(Severity.ERROR, positioningStrategy)
}
fun <P : PsiElement, A : Any, B : Any, C : Any, D : Any> error4(
fun <P : PsiElement, A, B, C, D> error4(
positioningStrategy: SourceElementPositioningStrategy<P> = SourceElementPositioningStrategy.DEFAULT
): DiagnosticFactory4DelegateProvider<P, A, B, C, D> {
return DiagnosticFactory4DelegateProvider(Severity.ERROR, positioningStrategy)
@@ -75,7 +75,7 @@ class DiagnosticFactory0DelegateProvider<P : PsiElement>(
}
}
class DiagnosticFactory1DelegateProvider<P : PsiElement, A : Any>(
class DiagnosticFactory1DelegateProvider<P : PsiElement, A>(
private val severity: Severity,
private val positioningStrategy: SourceElementPositioningStrategy<P>
) {
@@ -84,7 +84,7 @@ class DiagnosticFactory1DelegateProvider<P : PsiElement, A : Any>(
}
}
class DiagnosticFactory2DelegateProvider<P : PsiElement, A : Any, B : Any>(
class DiagnosticFactory2DelegateProvider<P : PsiElement, A, B>(
private val severity: Severity,
private val positioningStrategy: SourceElementPositioningStrategy<P>
) {
@@ -93,7 +93,7 @@ class DiagnosticFactory2DelegateProvider<P : PsiElement, A : Any, B : Any>(
}
}
class DiagnosticFactory3DelegateProvider<P : PsiElement, A : Any, B : Any, C : Any>(
class DiagnosticFactory3DelegateProvider<P : PsiElement, A, B, C>(
private val severity: Severity,
private val positioningStrategy: SourceElementPositioningStrategy<P>
) {
@@ -102,7 +102,7 @@ class DiagnosticFactory3DelegateProvider<P : PsiElement, A : Any, B : Any, C : A
}
}
class DiagnosticFactory4DelegateProvider<P : PsiElement, A : Any, B : Any, C : Any, D : Any>(
class DiagnosticFactory4DelegateProvider<P : PsiElement, A, B, C, D>(
private val severity: Severity,
private val positioningStrategy: SourceElementPositioningStrategy<P>
) {
@@ -18,7 +18,7 @@ class FirDiagnosticFactoryToRendererMap(val name: String) {
put(factory, SimpleFirDiagnosticRenderer(message))
}
fun <A : Any> put(
fun <A> put(
factory: FirDiagnosticFactory1<*, A>,
message: String,
rendererA: DiagnosticParameterRenderer<A>?
@@ -26,7 +26,7 @@ class FirDiagnosticFactoryToRendererMap(val name: String) {
put(factory, FirDiagnosticWithParameters1Renderer(message, rendererA))
}
fun <A : Any, B : Any> put(
fun <A, B> put(
factory: FirDiagnosticFactory2<*, A, B>,
message: String,
rendererA: DiagnosticParameterRenderer<A>?,
@@ -35,7 +35,7 @@ class FirDiagnosticFactoryToRendererMap(val name: String) {
put(factory, FirDiagnosticWithParameters2Renderer(message, rendererA, rendererB))
}
fun <A : Any, B : Any, C : Any> put(
fun <A, B, C> put(
factory: FirDiagnosticFactory3<*, A, B, C>,
message: String,
rendererA: DiagnosticParameterRenderer<A>?,
@@ -45,7 +45,7 @@ class FirDiagnosticFactoryToRendererMap(val name: String) {
put(factory, FirDiagnosticWithParameters3Renderer(message, rendererA, rendererB, rendererC))
}
fun <A : Any, B : Any, C : Any, D : Any> put(
fun <A, B, C, D> put(
factory: FirDiagnosticFactory4<*, A, B, C, D>,
message: String,
rendererA: DiagnosticParameterRenderer<A>?,
@@ -21,22 +21,22 @@ sealed class AbstractFirDiagnosticWithParametersRenderer<D : FirDiagnostic<*>>(
protected val message: String
) : FirDiagnosticRenderer<D>, AbstractDiagnosticWithParametersRenderer<D>(message)
class FirDiagnosticWithParameters1Renderer<A : Any>(
class FirDiagnosticWithParameters1Renderer<A>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?,
) : AbstractFirDiagnosticWithParametersRenderer<FirDiagnosticWithParameters1<*, A>>(message) {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters1<*, A>): Array<out Any> {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters1<*, A>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a)
return arrayOf(renderParameter(diagnostic.a, rendererForA, context))
}
}
class FirDiagnosticWithParameters2Renderer<A : Any, B : Any>(
class FirDiagnosticWithParameters2Renderer<A, B>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?,
private val rendererForB: DiagnosticParameterRenderer<B>?,
) : AbstractFirDiagnosticWithParametersRenderer<FirDiagnosticWithParameters2<*, A, B>>(message) {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters2<*, A, B>): Array<out Any> {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters2<*, A, B>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a, diagnostic.b)
return arrayOf(
renderParameter(diagnostic.a, rendererForA, context),
@@ -45,13 +45,13 @@ class FirDiagnosticWithParameters2Renderer<A : Any, B : Any>(
}
}
class FirDiagnosticWithParameters3Renderer<A : Any, B : Any, C : Any>(
class FirDiagnosticWithParameters3Renderer<A, B, C>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?,
private val rendererForB: DiagnosticParameterRenderer<B>?,
private val rendererForC: DiagnosticParameterRenderer<C>?,
) : AbstractFirDiagnosticWithParametersRenderer<FirDiagnosticWithParameters3<*, A, B, C>>(message) {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters3<*, A, B, C>): Array<out Any> {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters3<*, A, B, C>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a, diagnostic.b, diagnostic.c)
return arrayOf(
renderParameter(diagnostic.a, rendererForA, context),
@@ -61,14 +61,14 @@ class FirDiagnosticWithParameters3Renderer<A : Any, B : Any, C : Any>(
}
}
class FirDiagnosticWithParameters4Renderer<A : Any, B : Any, C : Any, D : Any>(
class FirDiagnosticWithParameters4Renderer<A, B, C, D>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?,
private val rendererForB: DiagnosticParameterRenderer<B>?,
private val rendererForC: DiagnosticParameterRenderer<C>?,
private val rendererForD: DiagnosticParameterRenderer<D>?,
) : AbstractFirDiagnosticWithParametersRenderer<FirDiagnosticWithParameters4<*, A, B, C, D>>(message) {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters4<*, A, B, C, D>): Array<out Any> {
override fun renderParameters(diagnostic: FirDiagnosticWithParameters4<*, A, B, C, D>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a, diagnostic.b, diagnostic.c)
return arrayOf(
renderParameter(diagnostic.a, rendererForA, context),
@@ -38,7 +38,7 @@ object FirDiagnosticRenderers {
}
}
val TO_STRING = Renderer { element: Any ->
val TO_STRING = Renderer { element: Any? ->
element.toString()
}
@@ -109,7 +109,7 @@ object FirDiagnosticRenderers {
}
}
val NOT_RENDERED = Renderer<Any> {
val NOT_RENDERED = Renderer<Any?> {
""
}
}
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.renderer.DescriptorRenderer
fun <P : Any> renderParameter(parameter: P, renderer: DiagnosticParameterRenderer<P>?, context: RenderingContext): Any =
fun <P> renderParameter(parameter: P, renderer: DiagnosticParameterRenderer<P>?, context: RenderingContext): Any? =
renderer?.render(parameter, context) ?: parameter
fun ClassifierDescriptorWithTypeParameters.renderKindWithName(): String =
@@ -27,29 +27,29 @@ abstract class AbstractDiagnosticWithParametersRenderer<in D : UnboundDiagnostic
return messageFormat.format(renderParameters(diagnostic))
}
abstract fun renderParameters(diagnostic: D): Array<out Any>
abstract fun renderParameters(diagnostic: D): Array<out Any?>
}
class DiagnosticWithParameters1Renderer<A : Any>(
class DiagnosticWithParameters1Renderer<A>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?
) : AbstractDiagnosticWithParametersRenderer<DiagnosticWithParameters1<*, A>>(message) {
override fun renderParameters(diagnostic: DiagnosticWithParameters1<*, A>): Array<out Any> {
override fun renderParameters(diagnostic: DiagnosticWithParameters1<*, A>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a)
return arrayOf(renderParameter(diagnostic.a, rendererForA, context))
}
}
class DiagnosticWithParameters2Renderer<A : Any, B : Any>(
class DiagnosticWithParameters2Renderer<A, B>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?,
private val rendererForB: DiagnosticParameterRenderer<B>?
) : AbstractDiagnosticWithParametersRenderer<DiagnosticWithParameters2<*, A, B>>(message) {
override fun renderParameters(diagnostic: DiagnosticWithParameters2<*, A, B>): Array<out Any> {
override fun renderParameters(diagnostic: DiagnosticWithParameters2<*, A, B>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a, diagnostic.b)
return arrayOf(
renderParameter(diagnostic.a, rendererForA, context),
@@ -58,14 +58,14 @@ class DiagnosticWithParameters2Renderer<A : Any, B : Any>(
}
}
class DiagnosticWithParameters3Renderer<A : Any, B : Any, C : Any>(
class DiagnosticWithParameters3Renderer<A, B, C>(
message: String,
private val rendererForA: DiagnosticParameterRenderer<A>?,
private val rendererForB: DiagnosticParameterRenderer<B>?,
private val rendererForC: DiagnosticParameterRenderer<C>?
) : AbstractDiagnosticWithParametersRenderer<DiagnosticWithParameters3<*, A, B, C>>(message) {
override fun renderParameters(diagnostic: DiagnosticWithParameters3<*, A, B, C>): Array<out Any> {
override fun renderParameters(diagnostic: DiagnosticWithParameters3<*, A, B, C>): Array<out Any?> {
val context = RenderingContext.of(diagnostic.a, diagnostic.b, diagnostic.c)
return arrayOf(
renderParameter(diagnostic.a, rendererForA, context),
@@ -38,7 +38,7 @@ open class DiagnosticCodeMetaInfoRenderConfiguration(
else -> DefaultErrorMessages.getRendererForDiagnostic(codeMetaInfo.diagnostic)
}
if (renderer is AbstractDiagnosticWithParametersRenderer) {
renderer.renderParameters(codeMetaInfo.diagnostic).mapTo(params, Any::toString)
renderer.renderParameters(codeMetaInfo.diagnostic).mapTo(params, Any?::toString)
}
if (renderSeverity)
params.add("severity='${codeMetaInfo.diagnostic.severity}'")
@@ -41,28 +41,28 @@ private fun FirLightDiagnostic.toPsiDiagnostic(): FirPsiDiagnostic<*> {
psiSourceElement,
a,
severity,
factory as FirDiagnosticFactory1<PsiElement, Any>
factory as FirDiagnosticFactory1<PsiElement, Any?>
)
is FirLightDiagnosticWithParameters2<*, *> -> FirPsiDiagnosticWithParameters2(
psiSourceElement,
a, b,
severity,
factory as FirDiagnosticFactory2<PsiElement, Any, Any>
factory as FirDiagnosticFactory2<PsiElement, Any?, Any?>
)
is FirLightDiagnosticWithParameters3<*, *, *> -> FirPsiDiagnosticWithParameters3(
psiSourceElement,
a, b, c,
severity,
factory as FirDiagnosticFactory3<PsiElement, Any, Any, Any>
factory as FirDiagnosticFactory3<PsiElement, Any?, Any?, Any?>
)
is FirLightDiagnosticWithParameters4<*, *, *, *> -> FirPsiDiagnosticWithParameters4(
psiSourceElement,
a, b, c, d,
severity,
factory as FirDiagnosticFactory4<PsiElement, Any, Any, Any, Any>
factory as FirDiagnosticFactory4<PsiElement, Any?, Any?, Any?, Any?>
)
else -> error("Unknown diagnostic type ${this::class.simpleName}")
}
@@ -73,16 +73,21 @@ object HLDiagnosticConverter {
private object FirToKtConversionCreator {
fun createConversion(type: KType): HLParameterConversion {
val nullable = type.isMarkedNullable
val kClass = type.classifier as KClass<*>
return tryMapAllowedType(kClass)
?: tryMapPsiElementType(type, kClass)
?: tryMapFirTypeToKtType(kClass)
?: tryMapFirTypeToKtType(kClass, nullable)
?: tryMapPlatformType(type, kClass)
?: error("Unsupported type $type, consider add corresponding mapping")
}
private fun tryMapFirTypeToKtType(kClass: KClass<*>): HLParameterConversion? {
return typeMapping[kClass]
private fun tryMapFirTypeToKtType(kClass: KClass<*>, nullable: Boolean): HLParameterConversion? {
return if (nullable) {
nullableTypeMapping[kClass] ?: typeMapping[kClass]
} else {
typeMapping[kClass]
}
}
private fun tryMapAllowedType(kClass: KClass<*>): HLParameterConversion? {
@@ -116,6 +121,17 @@ private object FirToKtConversionCreator {
return null
}
private val nullableTypeMapping: Map<KClass<*>, HLFunctionCallConversion> = mapOf(
FirExpression::class to HLFunctionCallConversion(
"{0}?.source?.psi as? KtExpression",
KtExpression::class.createType(nullable = true),
importsToAdd = listOf(
"org.jetbrains.kotlin.psi.KtExpression",
"org.jetbrains.kotlin.fir.psi"
)
),
)
private val typeMapping: Map<KClass<*>, HLFunctionCallConversion> = mapOf(
AbstractFirBasedSymbol::class to HLFunctionCallConversion(
"firSymbolBuilder.buildSymbol({0}.fir as FirDeclaration)",
@@ -11,13 +11,14 @@ import kotlin.reflect.KType
internal fun SmartPrinter.printTypeWithShortNames(type: KType) {
fun typeConversion(type: KType): String {
val nullableSuffix = if (type.isMarkedNullable) "?" else ""
val simpleName = (type.classifier as KClass<*>).simpleName!!
return if (type.arguments.isEmpty()) simpleName
return if (type.arguments.isEmpty()) simpleName + nullableSuffix
else simpleName + type.arguments.joinToString(separator = ", ", prefix = "<", postfix = ">") {
when (val typeArgument = it.type) {
null -> "*"
else -> typeConversion(typeArgument)
}
} + nullableSuffix
}
}
print(typeConversion(type))
@@ -15,19 +15,19 @@ internal fun interface KtFirDiagnostic0Creator : KtFirDiagnosticCreator {
fun KtFirAnalysisSession.create(diagnostic: FirSimpleDiagnostic<*>): KtFirDiagnostic<*>
}
internal fun interface KtFirDiagnostic1Creator<A : Any> : KtFirDiagnosticCreator {
internal fun interface KtFirDiagnostic1Creator<A> : KtFirDiagnosticCreator {
fun KtFirAnalysisSession.create(diagnostic: FirDiagnosticWithParameters1<*, A>): KtFirDiagnostic<*>
}
internal fun interface KtFirDiagnostic2Creator<A : Any, B : Any> : KtFirDiagnosticCreator {
internal fun interface KtFirDiagnostic2Creator<A, B> : KtFirDiagnosticCreator {
fun KtFirAnalysisSession.create(diagnostic: FirDiagnosticWithParameters2<*, A, B>): KtFirDiagnostic<*>
}
internal fun interface KtFirDiagnostic3Creator<A : Any, B : Any, C : Any> : KtFirDiagnosticCreator {
internal fun interface KtFirDiagnostic3Creator<A, B, C> : KtFirDiagnosticCreator {
fun KtFirAnalysisSession.create(diagnostic: FirDiagnosticWithParameters3<*, A, B, C>): KtFirDiagnostic<*>
}
internal fun interface KtFirDiagnostic4Creator<A : Any, B : Any, C : Any, D : Any> : KtFirDiagnosticCreator {
internal fun interface KtFirDiagnostic4Creator<A, B, C, D> : KtFirDiagnosticCreator {
fun KtFirAnalysisSession.create(diagnostic: FirDiagnosticWithParameters4<*, A, B, C, D>): KtFirDiagnostic<*>
}
@@ -42,17 +42,17 @@ internal class KtDiagnosticConverter(private val conversions: Map<AbstractFirDia
is KtFirDiagnostic0Creator -> with(creator) {
create(diagnostic as FirSimpleDiagnostic<*>)
}
is KtFirDiagnostic1Creator<*> -> with(creator as KtFirDiagnostic1Creator<Any>) {
create(diagnostic as FirDiagnosticWithParameters1<FirSourceElement, Any>)
is KtFirDiagnostic1Creator<*> -> with(creator as KtFirDiagnostic1Creator<Any?>) {
create(diagnostic as FirDiagnosticWithParameters1<FirSourceElement, Any?>)
}
is KtFirDiagnostic2Creator<*, *> -> with(creator as KtFirDiagnostic2Creator<Any, Any>) {
create(diagnostic as FirDiagnosticWithParameters2<FirSourceElement, Any, Any>)
is KtFirDiagnostic2Creator<*, *> -> with(creator as KtFirDiagnostic2Creator<Any?, Any?>) {
create(diagnostic as FirDiagnosticWithParameters2<FirSourceElement, Any?, Any?>)
}
is KtFirDiagnostic3Creator<*, *, *> -> with(creator as KtFirDiagnostic3Creator<Any, Any, Any>) {
create(diagnostic as FirDiagnosticWithParameters3<FirSourceElement, Any, Any, Any>)
is KtFirDiagnostic3Creator<*, *, *> -> with(creator as KtFirDiagnostic3Creator<Any?, Any?, Any?>) {
create(diagnostic as FirDiagnosticWithParameters3<FirSourceElement, Any?, Any?, Any?>)
}
is KtFirDiagnostic4Creator<*, *, *, *> -> with(creator as KtFirDiagnostic4Creator<Any, Any, Any, Any>) {
create(diagnostic as FirDiagnosticWithParameters4<FirSourceElement, Any, Any, Any, Any>)
is KtFirDiagnostic4Creator<*, *, *, *> -> with(creator as KtFirDiagnostic4Creator<Any?, Any?, Any?, Any?>) {
create(diagnostic as FirDiagnosticWithParameters4<FirSourceElement, Any?, Any?, Any?, Any?>)
}
else -> error("Invalid KtFirDiagnosticCreator ${creator::class.simpleName}")
}
@@ -67,19 +67,19 @@ internal class KtDiagnosticConverterBuilder private constructor() {
conversions[diagnostic] = creator
}
fun <A : Any> add(diagnostic: FirDiagnosticFactory1<*, A>, creator: KtFirDiagnostic1Creator<A>) {
fun <A> add(diagnostic: FirDiagnosticFactory1<*, A>, creator: KtFirDiagnostic1Creator<A>) {
conversions[diagnostic] = creator
}
fun <A : Any, B : Any> add(diagnostic: FirDiagnosticFactory2<*, A, B>, creator: KtFirDiagnostic2Creator<A, B>) {
fun <A, B> add(diagnostic: FirDiagnosticFactory2<*, A, B>, creator: KtFirDiagnostic2Creator<A, B>) {
conversions[diagnostic] = creator
}
fun <A : Any, B : Any, C : Any> add(diagnostic: FirDiagnosticFactory3<*, A, B, C>, creator: KtFirDiagnostic3Creator<A, B, C>) {
fun <A, B, C> add(diagnostic: FirDiagnosticFactory3<*, A, B, C>, creator: KtFirDiagnostic3Creator<A, B, C>) {
conversions[diagnostic] = creator
}
fun <A : Any, B : Any, C : Any, D : Any> add(diagnostic: FirDiagnosticFactory4<*, A, B, C, D>, creator: KtFirDiagnostic4Creator<A, B, C, D>) {
fun <A, B, C, D> add(diagnostic: FirDiagnosticFactory4<*, A, B, C, D>, creator: KtFirDiagnostic4Creator<A, B, C, D>) {
conversions[diagnostic] = creator
}