IR: move PsiSourceManager, PsiErrorBuilder to ir.backend.common

This commit is contained in:
Alexander Udalov
2021-02-22 12:04:44 +01:00
parent bec4d1903b
commit 5e0860ca14
6 changed files with 9 additions and 20 deletions
@@ -0,0 +1,72 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.common.psi
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.*
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.util.render
import kotlin.reflect.KClass
class PsiErrorBuilder(private val diagnosticSink: DiagnosticSink) {
fun <E : PsiElement> at(irDeclaration: IrDeclaration, psiElementClass: KClass<E>): Location<E> =
Location(
PsiSourceManager.findPsiElement(irDeclaration, psiElementClass)
?: throw AssertionError("No ${psiElementClass.simpleName} found for '${irDeclaration.render()}'")
)
fun at(irDeclaration: IrDeclaration): Location<PsiElement> =
Location(
PsiSourceManager.findPsiElement(irDeclaration)
?: throw AssertionError("No PsiElement found for '${irDeclaration.render()}'")
)
fun <E : PsiElement> at(psiElement: E) = Location(psiElement)
fun <E : PsiElement> at(irElement: IrElement, irDeclaration: IrDeclaration, psiElementClass: KClass<E>): Location<E> =
Location(
PsiSourceManager.findPsiElement(irElement, irDeclaration, psiElementClass)
?: throw AssertionError("No ${psiElementClass.simpleName} found for '${irElement.render()}'")
)
fun at(irElement: IrElement, irDeclaration: IrDeclaration): Location<PsiElement> =
Location(
PsiSourceManager.findPsiElement(irElement, irDeclaration)
?: throw AssertionError("No PsiElement found for '${irElement.render()}'")
)
fun <E : PsiElement> at(irElement: IrElement, irFile: IrFile, psiElementClass: KClass<E>): Location<E> =
Location(
PsiSourceManager.findPsiElement(irElement, irFile, psiElementClass)
?: throw AssertionError("No ${psiElementClass.simpleName} found for '${irElement.render()}'")
)
fun at(irElement: IrElement, irFile: IrFile): Location<PsiElement> =
Location(
PsiSourceManager.findPsiElement(irElement, irFile)
?: throw AssertionError("No PsiElement found for '${irElement.render()}'")
)
inner class Location<E : PsiElement>(private val psiElement: E) {
fun report(diagnosticFactory: DiagnosticFactory0<E>) {
diagnosticSink.report(diagnosticFactory.on(psiElement))
}
fun <A : Any> report(diagnosticFactory: DiagnosticFactory1<E, A>, a: A) {
diagnosticSink.report(diagnosticFactory.on(psiElement, a))
}
fun <A : Any, B : Any> report(diagnosticFactory: DiagnosticFactory2<E, A, B>, a: A, b: B) {
diagnosticSink.report(diagnosticFactory.on(psiElement, a, b))
}
fun <A : Any, B : Any, C : Any> report(diagnosticFactory: DiagnosticFactory3<E, A, B, C>, a: A, b: B, c: C) {
diagnosticSink.report(diagnosticFactory.on(psiElement, a, b, c))
}
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.common.psi
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.PsiIrFileEntry
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.util.fileOrNull
import kotlin.reflect.KClass
object PsiSourceManager {
fun <E : PsiElement> findPsiElement(irElement: IrElement, irFile: IrFile, psiElementClass: KClass<E>): E? {
val psiFileEntry = irFile.fileEntry as? PsiIrFileEntry ?: return null
return psiFileEntry.findPsiElement(irElement, psiElementClass)
}
fun findPsiElement(irElement: IrElement, irFile: IrFile): PsiElement? {
val psiFileEntry = irFile.fileEntry as? PsiIrFileEntry ?: return null
return psiFileEntry.findPsiElement(irElement)
}
fun <E : PsiElement> findPsiElement(irElement: IrElement, irDeclaration: IrDeclaration, psiElementClass: KClass<E>): E? {
val irFile = irDeclaration.fileOrNull ?: return null
return findPsiElement(irElement, irFile, psiElementClass)
}
fun findPsiElement(irElement: IrElement, irDeclaration: IrDeclaration): PsiElement? {
val irFile = irDeclaration.fileOrNull ?: return null
return findPsiElement(irElement, irFile)
}
fun <E : PsiElement> findPsiElement(irDeclaration: IrDeclaration, psiElementClass: KClass<E>): E? =
findPsiElement(irDeclaration, irDeclaration, psiElementClass)
fun findPsiElement(irDeclaration: IrDeclaration): PsiElement? =
findPsiElement(irDeclaration, irDeclaration)
}