From 14f83566d0b256fae3b29f87f14775e757396ec5 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 13 Jun 2023 16:54:50 +0900 Subject: [PATCH] [fir2ir] Support stub-based FIR declarations In the IDE, FIR declarations for libraries are built on top of binary stubs. This helps to improve memory usage significantly, as Protobuf metadata isn't loaded all the time. However, the containing file type for stubs is different. --- .../kotlin/fir/backend/ConversionUtils.kt | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index b367cc84430..c45f4ec5a43 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.backend import com.intellij.psi.PsiCompiledElement +import com.intellij.psi.PsiElement import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.* import org.jetbrains.kotlin.backend.common.actualizer.IrActualizedResult @@ -55,6 +56,7 @@ import org.jetbrains.kotlin.ir.util.functions import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.StandardClassIds +import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments import org.jetbrains.kotlin.types.ConstantValueKind import org.jetbrains.kotlin.types.Variance @@ -76,7 +78,7 @@ internal inline fun KtSourceElement?.convertWithOffsets(f: (star val startOffset: Int val endOffset: Int - if (psi is PsiCompiledElement) { + if (isCompiledElement(psi)) { startOffset = UNDEFINED_OFFSET endOffset = UNDEFINED_OFFSET } else { @@ -101,7 +103,7 @@ internal inline fun FirStatement.convertWithOffsets( ): T { val startOffset: Int val endOffset: Int - if (psi is PsiCompiledElement) { + if (isCompiledElement(psi)) { startOffset = UNDEFINED_OFFSET endOffset = UNDEFINED_OFFSET } else { @@ -111,6 +113,19 @@ internal inline fun FirStatement.convertWithOffsets( return f(startOffset, endOffset) } +private fun isCompiledElement(element: PsiElement?): Boolean { + if (element == null) { + return false + } + + if (element is PsiCompiledElement) { + return true + } + + val containingFile = element.containingFile + return containingFile !is KtFile || containingFile.isCompiled +} + internal fun createErrorType(): IrErrorType = IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT) enum class ConversionTypeOrigin(val forSetter: Boolean) {