From 74bdb2398efbedf8b6cee326513a3f1468db4d61 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Fri, 19 Feb 2021 07:20:06 +0000 Subject: [PATCH] FIR: Get rid of PSI dependency in the `:fir:resolve` module --- compiler/fir/resolve/build.gradle.kts | 3 +-- .../kotlin/fir/FirQualifiedNameResolver.kt | 11 +---------- .../tree/src/org/jetbrains/kotlin/fir/Utils.kt | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/compiler/fir/resolve/build.gradle.kts b/compiler/fir/resolve/build.gradle.kts index 7c4bf38f27a..16a0cb9f451 100644 --- a/compiler/fir/resolve/build.gradle.kts +++ b/compiler/fir/resolve/build.gradle.kts @@ -14,10 +14,9 @@ dependencies { api(project(":compiler:fir:tree")) api(kotlinxCollectionsImmutable()) implementation(project(":core:util.runtime")) - implementation(project(":compiler:psi")) compileOnly(project(":kotlin-reflect-api")) - compileOnly(intellijCoreDep()) { includeJars("intellij-core", "guava", rootProject = rootProject) } + compileOnly(intellijCoreDep()) { includeJars("guava", rootProject = rootProject) } } sourceSets { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirQualifiedNameResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirQualifiedNameResolver.kt index 9c9c0f7e46b..a59121afa78 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirQualifiedNameResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirQualifiedNameResolver.kt @@ -19,7 +19,6 @@ import org.jetbrains.kotlin.fir.resolve.typeForQualifier import org.jetbrains.kotlin.fir.types.FirTypeProjection import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf const val ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE = "_root_ide_package_" @@ -92,7 +91,7 @@ class FirQualifiedNameResolver(private val components: BodyResolveComponents) { } return buildResolvedQualifier { - this.source = getWholeQualifierSource(source, qualifierPartsToDrop) + this.source = source?.getWholeQualifierSourceIfPossible(qualifierPartsToDrop) packageFqName = resolved.packageFqName relativeClassFqName = resolved.relativeClassFqName symbol = resolved.classSymbol @@ -104,12 +103,4 @@ class FirQualifiedNameResolver(private val components: BodyResolveComponents) { return null } - - private fun getWholeQualifierSource(qualifierStartSource: FirSourceElement?, stepsToWholeQualifier: Int): FirSourceElement? { - if (qualifierStartSource !is FirRealPsiSourceElement<*>) return qualifierStartSource - - val qualifierStart = qualifierStartSource.psi - val wholeQualifier = qualifierStart.parentsWithSelf.drop(stepsToWholeQualifier).first() - return wholeQualifier.toFirPsiSourceElement() - } } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt index 13d2c3e7ba3..f3effe58dc1 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt @@ -56,3 +56,20 @@ fun R.copyWithNewSourceKind(newKind: FirFakeSourceElementKind): } as R } +/** + * Let's take `a.b.c.call()` expression as an example. + * + * This function allows to transform `SourceElement(psi = 'a')` to `SourceElement(psi = 'a.b.c')` + * ([stepsToWholeQualifier] should be = 2 for that). + * + * @receiver original source element + * @param stepsToWholeQualifier distance between the original psi and the whole qualifier psi + */ +fun FirSourceElement.getWholeQualifierSourceIfPossible(stepsToWholeQualifier: Int): FirSourceElement { + if (this !is FirRealPsiSourceElement<*>) return this + + val qualifiersChain = generateSequence(psi) { it.parent } + val wholeQualifier = qualifiersChain.drop(stepsToWholeQualifier).first() + + return wholeQualifier.toFirPsiSourceElement() as FirRealPsiSourceElement +}