[FIR] Introduce FirAnalyzerFacade which hides implementation details of running FIR compiler
This commit is contained in:
+8
-42
@@ -31,8 +31,6 @@ import org.jetbrains.kotlin.asJava.finder.JavaElementFinder
|
|||||||
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection
|
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection
|
||||||
import org.jetbrains.kotlin.backend.common.output.SimpleOutputFileCollection
|
import org.jetbrains.kotlin.backend.common.output.SimpleOutputFileCollection
|
||||||
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
|
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
|
||||||
import org.jetbrains.kotlin.backend.common.serialization.signature.IdSignatureDescriptor
|
|
||||||
import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions
|
|
||||||
import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory
|
import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory
|
||||||
import org.jetbrains.kotlin.backend.jvm.jvmPhases
|
import org.jetbrains.kotlin.backend.jvm.jvmPhases
|
||||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||||
@@ -56,24 +54,15 @@ import org.jetbrains.kotlin.diagnostics.*
|
|||||||
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
|
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
|
||||||
import org.jetbrains.kotlin.fir.FirPsiSourceElement
|
import org.jetbrains.kotlin.fir.FirPsiSourceElement
|
||||||
import org.jetbrains.kotlin.fir.FirSession
|
import org.jetbrains.kotlin.fir.FirSession
|
||||||
import org.jetbrains.kotlin.fir.analysis.collectors.FirDiagnosticsCollector
|
import org.jetbrains.kotlin.fir.analysis.FirAnalyzerFacade
|
||||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.*
|
import org.jetbrains.kotlin.fir.analysis.diagnostics.*
|
||||||
import org.jetbrains.kotlin.fir.backend.Fir2IrConverter
|
|
||||||
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendClassResolver
|
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendClassResolver
|
||||||
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmClassCodegen
|
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmClassCodegen
|
||||||
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmKotlinMangler
|
|
||||||
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmVisibilityConverter
|
|
||||||
import org.jetbrains.kotlin.fir.builder.RawFirBuilder
|
|
||||||
import org.jetbrains.kotlin.fir.checkers.registerExtendedCommonCheckers
|
import org.jetbrains.kotlin.fir.checkers.registerExtendedCommonCheckers
|
||||||
import org.jetbrains.kotlin.fir.java.FirProjectSessionProvider
|
import org.jetbrains.kotlin.fir.java.FirProjectSessionProvider
|
||||||
import org.jetbrains.kotlin.fir.resolve.firProvider
|
|
||||||
import org.jetbrains.kotlin.fir.resolve.providers.impl.FirProviderImpl
|
|
||||||
import org.jetbrains.kotlin.fir.resolve.transformers.FirTotalResolveProcessor
|
|
||||||
import org.jetbrains.kotlin.fir.session.FirSessionFactory
|
import org.jetbrains.kotlin.fir.session.FirSessionFactory
|
||||||
import org.jetbrains.kotlin.idea.MainFunctionDetector
|
import org.jetbrains.kotlin.idea.MainFunctionDetector
|
||||||
import org.jetbrains.kotlin.ir.backend.jvm.jvmResolveLibraries
|
import org.jetbrains.kotlin.ir.backend.jvm.jvmResolveLibraries
|
||||||
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmManglerDesc
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
|
|
||||||
import org.jetbrains.kotlin.javac.JavacWrapper
|
import org.jetbrains.kotlin.javac.JavacWrapper
|
||||||
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
|
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
|
||||||
import org.jetbrains.kotlin.modules.Module
|
import org.jetbrains.kotlin.modules.Module
|
||||||
@@ -314,9 +303,7 @@ object KotlinToJVMBytecodeCompiler {
|
|||||||
val project = environment.project
|
val project = environment.project
|
||||||
val performanceManager = environment.configuration.get(CLIConfigurationKeys.PERF_MANAGER)
|
val performanceManager = environment.configuration.get(CLIConfigurationKeys.PERF_MANAGER)
|
||||||
|
|
||||||
Extensions.getArea(project)
|
PsiElementFinder.EP.getPoint(project).unregisterExtension(JavaElementFinder::class.java)
|
||||||
.getExtensionPoint(PsiElementFinder.EP_NAME)
|
|
||||||
.unregisterExtension(JavaElementFinder::class.java)
|
|
||||||
|
|
||||||
val projectConfiguration = environment.configuration
|
val projectConfiguration = environment.configuration
|
||||||
val localFileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL)
|
val localFileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL)
|
||||||
@@ -363,25 +350,11 @@ object KotlinToJVMBytecodeCompiler {
|
|||||||
project, environment.createPackagePartProvider(librariesScope)
|
project, environment.createPackagePartProvider(librariesScope)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
val firProvider = (session.firProvider as FirProviderImpl)
|
|
||||||
val builder = RawFirBuilder(session, firProvider.kotlinScopeProvider, stubMode = false)
|
val firAnalyzerFacade = FirAnalyzerFacade(session, moduleConfiguration.languageVersionSettings, ktFiles)
|
||||||
val resolveTransformer = FirTotalResolveProcessor(session)
|
|
||||||
val collector = FirDiagnosticsCollector.create(session)
|
firAnalyzerFacade.runResolution()
|
||||||
val firDiagnostics = mutableListOf<FirDiagnostic<*>>()
|
val firDiagnostics = firAnalyzerFacade.runCheckers()
|
||||||
val firFiles = ktFiles.map {
|
|
||||||
val firFile = builder.buildFirFile(it)
|
|
||||||
firProvider.recordFile(firFile)
|
|
||||||
firFile
|
|
||||||
}.also { firFiles ->
|
|
||||||
try {
|
|
||||||
resolveTransformer.process(firFiles)
|
|
||||||
firFiles.forEach {
|
|
||||||
firDiagnostics += collector.collectDiagnostics(it)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AnalyzerWithCompilerReport.reportDiagnostics(
|
AnalyzerWithCompilerReport.reportDiagnostics(
|
||||||
SimpleDiagnostics(
|
SimpleDiagnostics(
|
||||||
firDiagnostics.map { it.toRegularDiagnostic() }
|
firDiagnostics.map { it.toRegularDiagnostic() }
|
||||||
@@ -395,16 +368,9 @@ object KotlinToJVMBytecodeCompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
performanceManager?.notifyGenerationStarted()
|
performanceManager?.notifyGenerationStarted()
|
||||||
val signaturer = IdSignatureDescriptor(JvmManglerDesc())
|
|
||||||
|
|
||||||
performanceManager?.notifyIRTranslationStarted()
|
performanceManager?.notifyIRTranslationStarted()
|
||||||
val (moduleFragment, symbolTable, sourceManager, components) =
|
val (moduleFragment, symbolTable, sourceManager, components) = firAnalyzerFacade.convertToIr()
|
||||||
Fir2IrConverter.createModuleFragment(
|
|
||||||
session, resolveTransformer.scopeSession, firFiles,
|
|
||||||
moduleConfiguration.languageVersionSettings, signaturer,
|
|
||||||
JvmGeneratorExtensions(), FirJvmKotlinMangler(session), IrFactoryImpl,
|
|
||||||
FirJvmVisibilityConverter
|
|
||||||
)
|
|
||||||
|
|
||||||
performanceManager?.notifyIRTranslationFinished()
|
performanceManager?.notifyIRTranslationFinished()
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,16 @@ dependencies {
|
|||||||
api(project(":core:descriptors.jvm"))
|
api(project(":core:descriptors.jvm"))
|
||||||
api(project(":compiler:frontend.java"))
|
api(project(":compiler:frontend.java"))
|
||||||
api(project(":compiler:fir:java"))
|
api(project(":compiler:fir:java"))
|
||||||
|
api(project(":compiler:fir:raw-fir:psi2fir"))
|
||||||
|
api(project(":compiler:fir:fir2ir"))
|
||||||
|
api(project(":compiler:fir:checkers"))
|
||||||
|
|
||||||
implementation(project(":compiler:fir:resolve"))
|
implementation(project(":compiler:fir:resolve"))
|
||||||
implementation(project(":compiler:fir:checkers"))
|
implementation(project(":compiler:fir:fir2ir:jvm-backend"))
|
||||||
|
implementation(project(":compiler:backend.jvm"))
|
||||||
|
implementation(project(":compiler:ir.serialization.common"))
|
||||||
|
implementation(project(":compiler:ir.serialization.jvm"))
|
||||||
|
implementation(project(":compiler:ir.tree.impl"))
|
||||||
|
|
||||||
compileOnly(project(":kotlin-reflect-api"))
|
compileOnly(project(":kotlin-reflect-api"))
|
||||||
compileOnly(intellijCoreDep()) { includeJars("intellij-core", "guava", rootProject = rootProject) }
|
compileOnly(intellijCoreDep()) { includeJars("intellij-core", "guava", rootProject = rootProject) }
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 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.fir.analysis
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.backend.common.serialization.signature.IdSignatureDescriptor
|
||||||
|
import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions
|
||||||
|
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||||
|
import org.jetbrains.kotlin.fir.FirSession
|
||||||
|
import org.jetbrains.kotlin.fir.analysis.collectors.FirDiagnosticsCollector
|
||||||
|
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnostic
|
||||||
|
import org.jetbrains.kotlin.fir.backend.Fir2IrConverter
|
||||||
|
import org.jetbrains.kotlin.fir.backend.Fir2IrResult
|
||||||
|
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmKotlinMangler
|
||||||
|
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmVisibilityConverter
|
||||||
|
import org.jetbrains.kotlin.fir.builder.RawFirBuilder
|
||||||
|
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.ScopeSession
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.firProvider
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.providers.impl.FirProviderImpl
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.transformers.FirTotalResolveProcessor
|
||||||
|
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmManglerDesc
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
|
||||||
|
class FirAnalyzerFacade(val session: FirSession, val languageVersionSettings: LanguageVersionSettings, val ktFiles: List<KtFile>) {
|
||||||
|
private var firFiles: List<FirFile>? = null
|
||||||
|
private var scopeSession: ScopeSession? = null
|
||||||
|
private var collectedDiagnostics: List<FirDiagnostic<*>>? = null
|
||||||
|
|
||||||
|
private fun buildRawFir() {
|
||||||
|
if (firFiles != null) return
|
||||||
|
val firProvider = (session.firProvider as FirProviderImpl)
|
||||||
|
val builder = RawFirBuilder(session, firProvider.kotlinScopeProvider, stubMode = false)
|
||||||
|
firFiles = ktFiles.map {
|
||||||
|
val firFile = builder.buildFirFile(it)
|
||||||
|
firProvider.recordFile(firFile)
|
||||||
|
firFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun runResolution(): List<FirFile> {
|
||||||
|
if (firFiles == null) buildRawFir()
|
||||||
|
if (scopeSession != null) return firFiles!!
|
||||||
|
val resolveProcessor = FirTotalResolveProcessor(session)
|
||||||
|
resolveProcessor.process(firFiles!!)
|
||||||
|
scopeSession = resolveProcessor.scopeSession
|
||||||
|
return firFiles!!
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalStdlibApi::class)
|
||||||
|
fun runCheckers(): List<FirDiagnostic<*>> {
|
||||||
|
if (scopeSession == null) runResolution()
|
||||||
|
if (collectedDiagnostics != null) return collectedDiagnostics!!
|
||||||
|
val collector = FirDiagnosticsCollector.create(session)
|
||||||
|
collectedDiagnostics = buildList {
|
||||||
|
for (file in firFiles!!) {
|
||||||
|
addAll(collector.collectDiagnostics(file))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return collectedDiagnostics!!
|
||||||
|
}
|
||||||
|
|
||||||
|
fun convertToIr(): Fir2IrResult {
|
||||||
|
if (scopeSession == null) runResolution()
|
||||||
|
val signaturer = IdSignatureDescriptor(JvmManglerDesc())
|
||||||
|
|
||||||
|
return Fir2IrConverter.createModuleFragment(
|
||||||
|
session, scopeSession!!, firFiles!!,
|
||||||
|
languageVersionSettings, signaturer,
|
||||||
|
JvmGeneratorExtensions(), FirJvmKotlinMangler(session), IrFactoryImpl,
|
||||||
|
FirJvmVisibilityConverter
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user