FIR2IR/MPP: use single SymbolTable during MPP compilation

This commit is contained in:
Mikhail Glukhikh
2023-01-11 14:13:58 +01:00
committed by Space Team
parent 33daca6809
commit 890e3d1c10
9 changed files with 58 additions and 23 deletions
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.backend.Fir2IrResult
import org.jetbrains.kotlin.fir.declarations.FirFile import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.psi import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.model.TestFile import org.jetbrains.kotlin.test.model.TestFile
@@ -37,7 +38,11 @@ class LowLevelFirAnalyzerFacade(
} }
override fun runResolution(): List<FirFile> = shouldNotBeCalled() override fun runResolution(): List<FirFile> = shouldNotBeCalled()
override fun convertToIr(fir2IrExtensions: Fir2IrExtensions, dependentComponents: List<Fir2IrComponents>): Fir2IrResult = shouldNotBeCalled() override fun convertToIr(
fir2IrExtensions: Fir2IrExtensions,
dependentComponents: List<Fir2IrComponents>,
symbolTable: SymbolTable?
): Fir2IrResult = shouldNotBeCalled()
} }
private fun shouldNotBeCalled(): Nothing = error("Should not be called for LL test") private fun shouldNotBeCalled(): Nothing = error("Should not be called for LL test")
@@ -597,7 +597,8 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
IrGenerationExtension.getInstances(environmentForJS.project), IrGenerationExtension.getInstances(environmentForJS.project),
generateSignatures = false, generateSignatures = false,
kotlinBuiltIns = builtInsModule ?: DefaultBuiltIns.Instance, // TODO: consider passing externally kotlinBuiltIns = builtInsModule ?: DefaultBuiltIns.Instance, // TODO: consider passing externally
dependentComponents = emptyList() dependentComponents = emptyList(),
currentSymbolTable = null
).also { ).also {
(it.irModuleFragment.descriptor as? FirModuleDescriptor)?.let { it.allDependencyModules = librariesDescriptors } (it.irModuleFragment.descriptor as? FirModuleDescriptor)?.let { it.allDependencyModules = librariesDescriptors }
} }
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmDescriptorMangler import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmDescriptorMangler
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
data class FirResult( data class FirResult(
val platformOutput: ModuleCompilerAnalyzedOutput, val platformOutput: ModuleCompilerAnalyzedOutput,
@@ -47,13 +48,15 @@ fun FirResult.convertToIrAndActualize(
fir2IrExtensions, fir2IrExtensions,
irGeneratorExtensions, irGeneratorExtensions,
linkViaSignatures = linkViaSignatures, linkViaSignatures = linkViaSignatures,
dependentComponents = emptyList() dependentComponents = emptyList(),
currentSymbolTable = null
) )
result = platformOutput.convertToIr( result = platformOutput.convertToIr(
fir2IrExtensions, fir2IrExtensions,
irGeneratorExtensions, irGeneratorExtensions,
linkViaSignatures = linkViaSignatures, linkViaSignatures = linkViaSignatures,
dependentComponents = listOf(commonIrOutput.components) dependentComponents = listOf(commonIrOutput.components),
currentSymbolTable = commonIrOutput.components.symbolTable
) )
IrActualizer.actualize( IrActualizer.actualize(
result.irModuleFragment, result.irModuleFragment,
@@ -64,7 +67,8 @@ fun FirResult.convertToIrAndActualize(
fir2IrExtensions, fir2IrExtensions,
irGeneratorExtensions, irGeneratorExtensions,
linkViaSignatures = linkViaSignatures, linkViaSignatures = linkViaSignatures,
dependentComponents = emptyList() dependentComponents = emptyList(),
currentSymbolTable = null
) )
} }
@@ -75,7 +79,8 @@ private fun ModuleCompilerAnalyzedOutput.convertToIr(
fir2IrExtensions: Fir2IrExtensions, fir2IrExtensions: Fir2IrExtensions,
irGeneratorExtensions: Collection<IrGenerationExtension>, irGeneratorExtensions: Collection<IrGenerationExtension>,
linkViaSignatures: Boolean, linkViaSignatures: Boolean,
dependentComponents: List<Fir2IrComponents> dependentComponents: List<Fir2IrComponents>,
currentSymbolTable: SymbolTable?
): Fir2IrResult { ): Fir2IrResult {
if (linkViaSignatures) { if (linkViaSignatures) {
val signaturer = JvmIdSignatureDescriptor(mangler = JvmDescriptorMangler(mainDetector = null)) val signaturer = JvmIdSignatureDescriptor(mangler = JvmDescriptorMangler(mainDetector = null))
@@ -88,7 +93,8 @@ private fun ModuleCompilerAnalyzedOutput.convertToIr(
irGeneratorExtensions, irGeneratorExtensions,
kotlinBuiltIns = DefaultBuiltIns.Instance, // TODO: consider passing externally kotlinBuiltIns = DefaultBuiltIns.Instance, // TODO: consider passing externally
generateSignatures = true, generateSignatures = true,
dependentComponents = dependentComponents dependentComponents = dependentComponents,
currentSymbolTable = currentSymbolTable
) )
} else { } else {
return Fir2IrConverter.createModuleFragmentWithoutSignatures( return Fir2IrConverter.createModuleFragmentWithoutSignatures(
@@ -99,7 +105,8 @@ private fun ModuleCompilerAnalyzedOutput.convertToIr(
Fir2IrJvmSpecialAnnotationSymbolProvider(), Fir2IrJvmSpecialAnnotationSymbolProvider(),
irGeneratorExtensions, irGeneratorExtensions,
kotlinBuiltIns = DefaultBuiltIns.Instance, // TODO: consider passing externally, kotlinBuiltIns = DefaultBuiltIns.Instance, // TODO: consider passing externally,
dependentComponents = dependentComponents dependentComponents = dependentComponents,
currentSymbolTable = currentSymbolTable
) )
} }
} }
@@ -439,21 +439,22 @@ class Fir2IrConverter(
irGenerationExtensions: Collection<IrGenerationExtension>, irGenerationExtensions: Collection<IrGenerationExtension>,
generateSignatures: Boolean, generateSignatures: Boolean,
kotlinBuiltIns: KotlinBuiltIns, kotlinBuiltIns: KotlinBuiltIns,
dependentComponents: List<Fir2IrComponents> dependentComponents: List<Fir2IrComponents>,
currentSymbolTable: SymbolTable?
): Fir2IrResult { ): Fir2IrResult {
if (!generateSignatures) { if (!generateSignatures) {
return createModuleFragmentWithoutSignatures( return createModuleFragmentWithoutSignatures(
session, scopeSession, firFiles, languageVersionSettings, session, scopeSession, firFiles, languageVersionSettings,
fir2IrExtensions, mangler, irMangler, irFactory, fir2IrExtensions, mangler, irMangler, irFactory,
visibilityConverter, specialSymbolProvider, irGenerationExtensions, visibilityConverter, specialSymbolProvider, irGenerationExtensions,
kotlinBuiltIns, dependentComponents kotlinBuiltIns, dependentComponents, currentSymbolTable
) )
} }
val signatureComposer = FirBasedSignatureComposer( val signatureComposer = FirBasedSignatureComposer(
mangler, mangler,
dependentComposers = dependentComponents.map { it.signatureComposer as FirBasedSignatureComposer } dependentComposers = dependentComponents.map { it.signatureComposer as FirBasedSignatureComposer }
) )
val symbolTable = createSymbolTable(signaturer, signatureComposer, irFactory, dependentComponents) val symbolTable = createSymbolTable(signaturer, signatureComposer, irFactory, currentSymbolTable)
return createModuleFragmentWithSymbolTable( return createModuleFragmentWithSymbolTable(
session, scopeSession, firFiles, languageVersionSettings, session, scopeSession, firFiles, languageVersionSettings,
fir2IrExtensions, irMangler, irFactory, visibilityConverter, fir2IrExtensions, irMangler, irFactory, visibilityConverter,
@@ -475,11 +476,12 @@ class Fir2IrConverter(
specialSymbolProvider: Fir2IrSpecialSymbolProvider, specialSymbolProvider: Fir2IrSpecialSymbolProvider,
irGenerationExtensions: Collection<IrGenerationExtension>, irGenerationExtensions: Collection<IrGenerationExtension>,
kotlinBuiltIns: KotlinBuiltIns, kotlinBuiltIns: KotlinBuiltIns,
dependentComponents: List<Fir2IrComponents> dependentComponents: List<Fir2IrComponents>,
currentSymbolTable: SymbolTable?
): Fir2IrResult { ): Fir2IrResult {
val signatureComposer = FirBasedSignatureComposer(mangler, dependentComposers = dependentComponents.map { it.signatureComposer as FirBasedSignatureComposer }) val signatureComposer = FirBasedSignatureComposer(mangler, dependentComposers = dependentComponents.map { it.signatureComposer as FirBasedSignatureComposer })
val signaturer = DescriptorSignatureComposerStub() val signaturer = DescriptorSignatureComposerStub()
val symbolTable = createSymbolTable(signaturer, signatureComposer, irFactory, dependentComponents) val symbolTable = createSymbolTable(signaturer, signatureComposer, irFactory, currentSymbolTable)
return createModuleFragmentWithSymbolTable( return createModuleFragmentWithSymbolTable(
session, scopeSession, firFiles, languageVersionSettings, session, scopeSession, firFiles, languageVersionSettings,
fir2IrExtensions, irMangler, irFactory, visibilityConverter, fir2IrExtensions, irMangler, irFactory, visibilityConverter,
@@ -492,9 +494,9 @@ class Fir2IrConverter(
signaturer: IdSignatureComposer, signaturer: IdSignatureComposer,
signatureComposer: FirBasedSignatureComposer, signatureComposer: FirBasedSignatureComposer,
irFactory: IrFactory, irFactory: IrFactory,
dependentComponents: List<Fir2IrComponents> currentSymbolTable: SymbolTable?
): SymbolTable = ): SymbolTable =
dependentComponents.lastOrNull()?.symbolTable ?: SymbolTable( currentSymbolTable ?: SymbolTable(
signaturer = WrappedDescriptorSignatureComposer(signaturer, signatureComposer), signaturer = WrappedDescriptorSignatureComposer(signaturer, signatureComposer),
irFactory = irFactory irFactory = irFactory
) )
@@ -127,7 +127,8 @@ fun AbstractFirAnalyzerFacade.convertToJsIr(
irGeneratorExtensions, irGeneratorExtensions,
generateSignatures = false, generateSignatures = false,
kotlinBuiltIns = builtIns ?: DefaultBuiltIns.Instance, // TODO: consider passing externally, kotlinBuiltIns = builtIns ?: DefaultBuiltIns.Instance, // TODO: consider passing externally,
dependentComponents = emptyList() dependentComponents = emptyList(),
currentSymbolTable = null
).also { ).also {
(it.irModuleFragment.descriptor as? FirModuleDescriptor)?.let { it.allDependencyModules = dependencies } (it.irModuleFragment.descriptor as? FirModuleDescriptor)?.let { it.allDependencyModules = dependencies }
} }
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendExtension
import org.jetbrains.kotlin.fir.backend.jvm.JvmFir2IrExtensions import org.jetbrains.kotlin.fir.backend.jvm.JvmFir2IrExtensions
import org.jetbrains.kotlin.fir.psi import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.CompilerEnvironment import org.jetbrains.kotlin.resolve.CompilerEnvironment
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
@@ -67,6 +68,7 @@ class Fir2IrResultsConverter(
val dependentIrParts = mutableListOf<JvmIrCodegenFactory.JvmIrBackendInput>() val dependentIrParts = mutableListOf<JvmIrCodegenFactory.JvmIrBackendInput>()
lateinit var mainIrPart: JvmIrCodegenFactory.JvmIrBackendInput lateinit var mainIrPart: JvmIrCodegenFactory.JvmIrBackendInput
var currentSymbolTable: SymbolTable? = null
for ((index, firOutputPart) in inputArtifact.partsForDependsOnModules.withIndex()) { for ((index, firOutputPart) in inputArtifact.partsForDependsOnModules.withIndex()) {
val dependentComponents = mutableListOf<Fir2IrComponents>() val dependentComponents = mutableListOf<Fir2IrComponents>()
if (isMppSupported) { if (isMppSupported) {
@@ -75,7 +77,10 @@ class Fir2IrResultsConverter(
} }
} }
val (irModuleFragment, components, pluginContext) = firOutputPart.firAnalyzerFacade.convertToIr(fir2IrExtensions, dependentComponents) val (irModuleFragment, components, pluginContext) = firOutputPart.firAnalyzerFacade.convertToIr(
fir2IrExtensions, dependentComponents, currentSymbolTable
)
currentSymbolTable = components.symbolTable
componentsMap[firOutputPart.module.name] = components componentsMap[firOutputPart.module.name] = components
val irPart = JvmIrCodegenFactory.JvmIrBackendInput( val irPart = JvmIrCodegenFactory.JvmIrBackendInput(
@@ -123,7 +123,11 @@ object GenerationUtils {
generateSignatures = false generateSignatures = false
) )
val fir2IrExtensions = JvmFir2IrExtensions(configuration, JvmIrDeserializerImpl(), JvmIrMangler) val fir2IrExtensions = JvmFir2IrExtensions(configuration, JvmIrDeserializerImpl(), JvmIrMangler)
val (moduleFragment, components, pluginContext) = firAnalyzerFacade.convertToIr(fir2IrExtensions, dependentComponents = emptyList()) val (moduleFragment, components, pluginContext) = firAnalyzerFacade.convertToIr(
fir2IrExtensions,
dependentComponents = emptyList(),
symbolTable = null
)
val dummyBindingContext = NoScopeRecordCliBindingTrace().bindingContext val dummyBindingContext = NoScopeRecordCliBindingTrace().bindingContext
val codegenFactory = JvmIrCodegenFactory( val codegenFactory = JvmIrCodegenFactory(
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.fir.resolve.transformers.FirTotalResolveProcessor
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmDescriptorMangler import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmDescriptorMangler
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.sourceFiles.LightTreeFile import org.jetbrains.kotlin.sourceFiles.LightTreeFile
@@ -38,7 +39,11 @@ abstract class AbstractFirAnalyzerFacade {
abstract fun runResolution(): List<FirFile> abstract fun runResolution(): List<FirFile>
abstract fun convertToIr(fir2IrExtensions: Fir2IrExtensions, dependentComponents: List<Fir2IrComponents>): Fir2IrResult abstract fun convertToIr(
fir2IrExtensions: Fir2IrExtensions,
dependentComponents: List<Fir2IrComponents>,
symbolTable: SymbolTable?
): Fir2IrResult
} }
class FirAnalyzerFacade( class FirAnalyzerFacade(
@@ -87,7 +92,6 @@ class FirAnalyzerFacade(
return firFiles!! return firFiles!!
} }
@OptIn(ExperimentalStdlibApi::class)
override fun runCheckers(): Map<FirFile, List<KtDiagnostic>> { override fun runCheckers(): Map<FirFile, List<KtDiagnostic>> {
if (_scopeSession == null) runResolution() if (_scopeSession == null) runResolution()
if (collectedDiagnostics != null) return collectedDiagnostics!! if (collectedDiagnostics != null) return collectedDiagnostics!!
@@ -104,7 +108,11 @@ class FirAnalyzerFacade(
return collectedDiagnostics!! return collectedDiagnostics!!
} }
override fun convertToIr(fir2IrExtensions: Fir2IrExtensions, dependentComponents: List<Fir2IrComponents>): Fir2IrResult { override fun convertToIr(
fir2IrExtensions: Fir2IrExtensions,
dependentComponents: List<Fir2IrComponents>,
symbolTable: SymbolTable?
): Fir2IrResult {
if (_scopeSession == null) runResolution() if (_scopeSession == null) runResolution()
val mangler = JvmDescriptorMangler(null) val mangler = JvmDescriptorMangler(null)
val signaturer = JvmIdSignatureDescriptor(mangler) val signaturer = JvmIdSignatureDescriptor(mangler)
@@ -119,7 +127,8 @@ class FirAnalyzerFacade(
irGeneratorExtensions, irGeneratorExtensions,
generateSignatures, generateSignatures,
kotlinBuiltIns = DefaultBuiltIns.Instance, // TODO: consider passing externally, kotlinBuiltIns = DefaultBuiltIns.Instance, // TODO: consider passing externally,
dependentComponents = dependentComponents dependentComponents = dependentComponents,
currentSymbolTable = symbolTable
) )
} }
} }
@@ -79,7 +79,8 @@ internal fun PhaseContext.fir2Ir(
IrGenerationExtension.getInstances(config.project), IrGenerationExtension.getInstances(config.project),
generateSignatures = false, generateSignatures = false,
kotlinBuiltIns = builtInsModule ?: DefaultBuiltIns.Instance, // TODO: consider passing externally kotlinBuiltIns = builtInsModule ?: DefaultBuiltIns.Instance, // TODO: consider passing externally
dependentComponents = emptyList() dependentComponents = emptyList(),
currentSymbolTable = null
).also { ).also {
(it.irModuleFragment.descriptor as? FirModuleDescriptor)?.let { it.allDependencyModules = librariesDescriptors } (it.irModuleFragment.descriptor as? FirModuleDescriptor)?.let { it.allDependencyModules = librariesDescriptors }
} }