Refactor GenerationState creation
This commit is contained in:
@@ -48,25 +48,69 @@ import org.jetbrains.kotlin.serialization.deserialization.DeserializationConfigu
|
|||||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class GenerationState @JvmOverloads constructor(
|
class GenerationState private constructor(
|
||||||
val project: Project,
|
val project: Project,
|
||||||
builderFactory: ClassBuilderFactory,
|
builderFactory: ClassBuilderFactory,
|
||||||
val module: ModuleDescriptor,
|
val module: ModuleDescriptor,
|
||||||
bindingContext: BindingContext,
|
bindingContext: BindingContext,
|
||||||
val files: List<KtFile>,
|
val files: List<KtFile>,
|
||||||
val configuration: CompilerConfiguration,
|
val configuration: CompilerConfiguration,
|
||||||
val generateDeclaredClassFilter: GenerateClassFilter = GenerationState.GenerateClassFilter.GENERATE_ALL,
|
val generateDeclaredClassFilter: GenerateClassFilter,
|
||||||
val codegenFactory: CodegenFactory = DefaultCodegenFactory,
|
val codegenFactory: CodegenFactory,
|
||||||
// For incremental compilation
|
val targetId: TargetId?,
|
||||||
val targetId: TargetId? = null,
|
moduleName: String?,
|
||||||
moduleName: String? = configuration.get(CommonConfigurationKeys.MODULE_NAME),
|
val outDirectory: File?,
|
||||||
|
private val onIndependentPartCompilationEnd: GenerationStateEventCallback,
|
||||||
|
wantsDiagnostics: Boolean
|
||||||
|
) {
|
||||||
|
|
||||||
|
class Builder(
|
||||||
|
private val project: Project,
|
||||||
|
private val builderFactory: ClassBuilderFactory,
|
||||||
|
private val module: ModuleDescriptor,
|
||||||
|
private val bindingContext: BindingContext,
|
||||||
|
private val files: List<KtFile>,
|
||||||
|
private val configuration: CompilerConfiguration
|
||||||
|
) {
|
||||||
|
private var generateDeclaredClassFilter: GenerateClassFilter = GenerateClassFilter.GENERATE_ALL
|
||||||
|
fun generateDeclaredClassFilter(v: GenerateClassFilter) =
|
||||||
|
apply { generateDeclaredClassFilter = v }
|
||||||
|
|
||||||
|
private var codegenFactory: CodegenFactory = DefaultCodegenFactory
|
||||||
|
fun codegenFactory(v: CodegenFactory) =
|
||||||
|
apply { codegenFactory = v }
|
||||||
|
|
||||||
|
private var targetId: TargetId? = null
|
||||||
|
fun targetId(v: TargetId?) =
|
||||||
|
apply { targetId = v }
|
||||||
|
|
||||||
|
private var moduleName: String? = configuration[CommonConfigurationKeys.MODULE_NAME]
|
||||||
|
fun moduleName(v: String?) =
|
||||||
|
apply { moduleName = v }
|
||||||
|
|
||||||
// 'outDirectory' is a hack to correctly determine if a compiled class is from the same module as the callee during
|
// 'outDirectory' is a hack to correctly determine if a compiled class is from the same module as the callee during
|
||||||
// partial compilation. Module chunks are treated as a single module.
|
// partial compilation. Module chunks are treated as a single module.
|
||||||
// TODO: get rid of it with the proper module infrastructure
|
// TODO: get rid of it with the proper module infrastructure
|
||||||
val outDirectory: File? = null,
|
private var outDirectory: File? = null
|
||||||
private val onIndependentPartCompilationEnd: GenerationStateEventCallback = GenerationStateEventCallback.DO_NOTHING,
|
fun outDirectory(v: File?) =
|
||||||
wantsDiagnostics: Boolean = true
|
apply { outDirectory = v }
|
||||||
) {
|
|
||||||
|
private var onIndependentPartCompilationEnd: GenerationStateEventCallback = GenerationStateEventCallback.DO_NOTHING
|
||||||
|
fun onIndependentPartCompilationEnd(v: GenerationStateEventCallback) =
|
||||||
|
apply { onIndependentPartCompilationEnd = v }
|
||||||
|
|
||||||
|
private var wantsDiagnostics: Boolean = true
|
||||||
|
fun wantsDiagnostics(v: Boolean) =
|
||||||
|
apply { wantsDiagnostics = v }
|
||||||
|
|
||||||
|
fun build() =
|
||||||
|
GenerationState(
|
||||||
|
project, builderFactory, module, bindingContext, files, configuration,
|
||||||
|
generateDeclaredClassFilter, codegenFactory, targetId,
|
||||||
|
moduleName, outDirectory, onIndependentPartCompilationEnd, wantsDiagnostics
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
abstract class GenerateClassFilter {
|
abstract class GenerateClassFilter {
|
||||||
abstract fun shouldAnnotateClass(processingClassOrObject: KtClassOrObject): Boolean
|
abstract fun shouldAnnotateClass(processingClassOrObject: KtClassOrObject): Boolean
|
||||||
abstract fun shouldGenerateClass(processingClassOrObject: KtClassOrObject): Boolean
|
abstract fun shouldGenerateClass(processingClassOrObject: KtClassOrObject): Boolean
|
||||||
|
|||||||
+14
-8
@@ -421,6 +421,13 @@ object KotlinToJVMBytecodeCompiler {
|
|||||||
override fun toString() = "All files under: $directories"
|
override fun toString() = "All files under: $directories"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun GenerationState.Builder.withModule(module: Module?) =
|
||||||
|
apply {
|
||||||
|
targetId(module?.let { TargetId(it) })
|
||||||
|
moduleName(module?.getModuleName())
|
||||||
|
outDirectory(module?.let { File(it.getOutputDirectory()) })
|
||||||
|
}
|
||||||
|
|
||||||
private fun generate(
|
private fun generate(
|
||||||
environment: KotlinCoreEnvironment,
|
environment: KotlinCoreEnvironment,
|
||||||
configuration: CompilerConfiguration,
|
configuration: CompilerConfiguration,
|
||||||
@@ -429,20 +436,19 @@ object KotlinToJVMBytecodeCompiler {
|
|||||||
module: Module?
|
module: Module?
|
||||||
): GenerationState {
|
): GenerationState {
|
||||||
val isKapt2Enabled = environment.project.getUserData(IS_KAPT2_ENABLED_KEY) ?: false
|
val isKapt2Enabled = environment.project.getUserData(IS_KAPT2_ENABLED_KEY) ?: false
|
||||||
val generationState = GenerationState(
|
val generationState = GenerationState.Builder(
|
||||||
environment.project,
|
environment.project,
|
||||||
ClassBuilderFactories.binaries(isKapt2Enabled),
|
ClassBuilderFactories.binaries(isKapt2Enabled),
|
||||||
result.moduleDescriptor,
|
result.moduleDescriptor,
|
||||||
result.bindingContext,
|
result.bindingContext,
|
||||||
sourceFiles,
|
sourceFiles,
|
||||||
configuration,
|
configuration
|
||||||
GenerationState.GenerateClassFilter.GENERATE_ALL,
|
|
||||||
if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory else DefaultCodegenFactory,
|
|
||||||
module?.let(::TargetId),
|
|
||||||
module?.let(Module::getModuleName),
|
|
||||||
module?.let { File(it.getOutputDirectory()) },
|
|
||||||
createOutputFilesFlushingCallbackIfPossible(configuration)
|
|
||||||
)
|
)
|
||||||
|
.codegenFactory(if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory else DefaultCodegenFactory)
|
||||||
|
.withModule(module)
|
||||||
|
.onIndependentPartCompilationEnd(createOutputFilesFlushingCallbackIfPossible(configuration))
|
||||||
|
.build()
|
||||||
|
|
||||||
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()
|
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()
|
||||||
|
|
||||||
val generationStart = PerformanceCounter.currentTime()
|
val generationStart = PerformanceCounter.currentTime()
|
||||||
|
|||||||
@@ -83,14 +83,14 @@ open class GenericReplCompiler(disposable: Disposable,
|
|||||||
else -> error("Unexpected result ${analysisResult::class.java}")
|
else -> error("Unexpected result ${analysisResult::class.java}")
|
||||||
}
|
}
|
||||||
|
|
||||||
val generationState = GenerationState(
|
val generationState = GenerationState.Builder(
|
||||||
psiFile.project,
|
psiFile.project,
|
||||||
ClassBuilderFactories.binaries(false),
|
ClassBuilderFactories.binaries(false),
|
||||||
compilerState.analyzerEngine.module,
|
compilerState.analyzerEngine.module,
|
||||||
compilerState.analyzerEngine.trace.bindingContext,
|
compilerState.analyzerEngine.trace.bindingContext,
|
||||||
listOf(psiFile),
|
listOf(psiFile),
|
||||||
compilerConfiguration
|
compilerConfiguration
|
||||||
)
|
).build()
|
||||||
generationState.replSpecific.scriptResultFieldName = SCRIPT_RESULT_FIELD_NAME
|
generationState.replSpecific.scriptResultFieldName = SCRIPT_RESULT_FIELD_NAME
|
||||||
generationState.replSpecific.earlierScriptsForReplInterpreter = compilerState.history.map { it.item }
|
generationState.replSpecific.earlierScriptsForReplInterpreter = compilerState.history.map { it.item }
|
||||||
generationState.beforeCompile()
|
generationState.beforeCompile()
|
||||||
|
|||||||
@@ -47,16 +47,14 @@ fun buildLightClass(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
val classBuilderFactory = KotlinLightClassBuilderFactory(createJavaFileStub(project, packageFqName, files))
|
val classBuilderFactory = KotlinLightClassBuilderFactory(createJavaFileStub(project, packageFqName, files))
|
||||||
val state = GenerationState(
|
val state = GenerationState.Builder(
|
||||||
project,
|
project,
|
||||||
classBuilderFactory,
|
classBuilderFactory,
|
||||||
context.module,
|
context.module,
|
||||||
context.bindingContext,
|
context.bindingContext,
|
||||||
files.toList(),
|
files.toList(),
|
||||||
CompilerConfiguration.EMPTY,
|
CompilerConfiguration.EMPTY
|
||||||
generateClassFilter,
|
).generateDeclaredClassFilter(generateClassFilter).wantsDiagnostics(false).build()
|
||||||
wantsDiagnostics = false
|
|
||||||
)
|
|
||||||
state.beforeCompile()
|
state.beforeCompile()
|
||||||
|
|
||||||
generate(state, files)
|
generate(state, files)
|
||||||
|
|||||||
@@ -59,11 +59,12 @@ object GenerationUtils {
|
|||||||
val analysisResult = JvmResolveUtil.analyzeAndCheckForErrors(files.first().project, files, configuration, packagePartProvider)
|
val analysisResult = JvmResolveUtil.analyzeAndCheckForErrors(files.first().project, files, configuration, packagePartProvider)
|
||||||
analysisResult.throwIfError()
|
analysisResult.throwIfError()
|
||||||
|
|
||||||
val state = GenerationState(
|
val state = GenerationState.Builder(
|
||||||
files.first().project, classBuilderFactory, analysisResult.moduleDescriptor, analysisResult.bindingContext,
|
files.first().project, classBuilderFactory, analysisResult.moduleDescriptor, analysisResult.bindingContext,
|
||||||
files, configuration,
|
files, configuration
|
||||||
codegenFactory = if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory else DefaultCodegenFactory
|
).codegenFactory(
|
||||||
)
|
if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory else DefaultCodegenFactory
|
||||||
|
).build()
|
||||||
if (analysisResult.shouldGenerateCode) {
|
if (analysisResult.shouldGenerateCode) {
|
||||||
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION)
|
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,14 +187,14 @@ class KotlinDebuggerCaches(project: Project) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createTypeMapper(file: KtFile, analysisResult: AnalysisResult): KotlinTypeMapper {
|
private fun createTypeMapper(file: KtFile, analysisResult: AnalysisResult): KotlinTypeMapper {
|
||||||
val state = GenerationState(
|
val state = GenerationState.Builder(
|
||||||
file.project,
|
file.project,
|
||||||
ClassBuilderFactories.THROW_EXCEPTION,
|
ClassBuilderFactories.THROW_EXCEPTION,
|
||||||
analysisResult.moduleDescriptor,
|
analysisResult.moduleDescriptor,
|
||||||
analysisResult.bindingContext,
|
analysisResult.bindingContext,
|
||||||
listOf(file),
|
listOf(file),
|
||||||
CompilerConfiguration.EMPTY
|
CompilerConfiguration.EMPTY
|
||||||
)
|
).build()
|
||||||
state.beforeCompile()
|
state.beforeCompile()
|
||||||
return state.typeMapper
|
return state.typeMapper
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -469,15 +469,14 @@ class KotlinEvaluator(val codeFragment: KtCodeFragment, val sourcePosition: Sour
|
|||||||
override fun shouldGenerateScript(script: KtScript) = false
|
override fun shouldGenerateScript(script: KtScript) = false
|
||||||
}
|
}
|
||||||
|
|
||||||
val state = GenerationState(
|
val state = GenerationState.Builder(
|
||||||
fileForDebugger.project,
|
fileForDebugger.project,
|
||||||
if (!DEBUG_MODE) ClassBuilderFactories.binaries(false) else ClassBuilderFactories.TEST,
|
if (!DEBUG_MODE) ClassBuilderFactories.binaries(false) else ClassBuilderFactories.TEST,
|
||||||
moduleDescriptor,
|
moduleDescriptor,
|
||||||
bindingContext,
|
bindingContext,
|
||||||
files,
|
files,
|
||||||
CompilerConfiguration.EMPTY,
|
CompilerConfiguration.EMPTY
|
||||||
generateClassFilter
|
).generateDeclaredClassFilter(generateClassFilter).build()
|
||||||
)
|
|
||||||
|
|
||||||
val frameVisitor = FrameVisitor(context)
|
val frameVisitor = FrameVisitor(context)
|
||||||
|
|
||||||
|
|||||||
@@ -324,11 +324,15 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GenerationState state = new GenerationState(
|
GenerationState state = new GenerationState.Builder(
|
||||||
ktFile.getProject(), ClassBuilderFactories.TEST, resolutionFacade.getModuleDescriptor(), bindingContext, toProcess,
|
ktFile.getProject(), ClassBuilderFactories.TEST, resolutionFacade.getModuleDescriptor(), bindingContext, toProcess,
|
||||||
configuration, generateClassFilter,
|
configuration
|
||||||
configuration.getBoolean(JVMConfigurationKeys.IR) ? JvmIrCodegenFactory.INSTANCE : DefaultCodegenFactory.INSTANCE
|
)
|
||||||
);
|
.generateDeclaredClassFilter(generateClassFilter)
|
||||||
|
.codegenFactory(configuration.getBoolean(JVMConfigurationKeys.IR)
|
||||||
|
? JvmIrCodegenFactory.INSTANCE
|
||||||
|
: DefaultCodegenFactory.INSTANCE)
|
||||||
|
.build();
|
||||||
|
|
||||||
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -43,13 +43,14 @@ class StubProducerExtension(
|
|||||||
bindingTrace: BindingTrace,
|
bindingTrace: BindingTrace,
|
||||||
files: Collection<KtFile>
|
files: Collection<KtFile>
|
||||||
): AnalysisResult? {
|
): AnalysisResult? {
|
||||||
val generationState = GenerationState(
|
val generationState = GenerationState.Builder(
|
||||||
project,
|
project,
|
||||||
StubClassBuilderFactory(),
|
StubClassBuilderFactory(),
|
||||||
module,
|
module,
|
||||||
bindingTrace.bindingContext,
|
bindingTrace.bindingContext,
|
||||||
files.toList(),
|
files.toList(),
|
||||||
CompilerConfiguration.EMPTY)
|
CompilerConfiguration.EMPTY
|
||||||
|
).build()
|
||||||
|
|
||||||
KotlinCodegenFacade.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION)
|
KotlinCodegenFacade.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION)
|
||||||
|
|
||||||
|
|||||||
@@ -222,14 +222,14 @@ abstract class AbstractKapt3Extension(
|
|||||||
name = compilerConfiguration[CommonConfigurationKeys.MODULE_NAME] ?: module.name.asString(),
|
name = compilerConfiguration[CommonConfigurationKeys.MODULE_NAME] ?: module.name.asString(),
|
||||||
type = "java-production")
|
type = "java-production")
|
||||||
|
|
||||||
val generationState = GenerationState(
|
val generationState = GenerationState.Builder(
|
||||||
project,
|
project,
|
||||||
builderFactory,
|
builderFactory,
|
||||||
module,
|
module,
|
||||||
bindingContext,
|
bindingContext,
|
||||||
files,
|
files,
|
||||||
compilerConfiguration,
|
compilerConfiguration
|
||||||
targetId = targetId)
|
).targetId(targetId).build()
|
||||||
|
|
||||||
val (classFilesCompilationTime) = measureTimeMillis {
|
val (classFilesCompilationTime) = measureTimeMillis {
|
||||||
KotlinCodegenFacade.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION)
|
KotlinCodegenFacade.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION)
|
||||||
|
|||||||
Reference in New Issue
Block a user