From 978a4db07b189e7f21e153f12578516a69b37718 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Tue, 20 Sep 2016 10:27:25 +0300 Subject: [PATCH] Introduced IR CodegenFactory, added configuration key to enable it --- compiler/backend/backend.iml | 1 + .../kotlin/codegen/state/GenerationState.kt | 3 +- .../kotlin/config/JVMConfigurationKeys.java | 3 ++ .../kotlin/backend/jvm/JvmBackendFacade.kt | 2 +- .../kotlin/backend/jvm/JvmIrCodegenFactory.kt | 53 +++++++++++++++++++ .../internal/KotlinBytecodeToolWindow.java | 21 ++++---- .../internal/KotlinDecompilerServiceImpl.kt | 2 +- .../AbstractBytecodeToolWindowTest.kt | 2 +- 8 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt diff --git a/compiler/backend/backend.iml b/compiler/backend/backend.iml index 6ee674ea183..337b0497f71 100644 --- a/compiler/backend/backend.iml +++ b/compiler/backend/backend.iml @@ -13,5 +13,6 @@ + \ No newline at end of file diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt index 8ec27b30031..021a8c8dfa6 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.state import com.intellij.openapi.project.Project import com.intellij.openapi.util.ModificationTracker import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory import org.jetbrains.kotlin.builtins.ReflectionTypes import org.jetbrains.kotlin.codegen.* import org.jetbrains.kotlin.codegen.`when`.MappingsClassesForWhenByEnum @@ -69,7 +70,7 @@ class GenerationState @JvmOverloads constructor( // TODO: get rid of it with the proper module infrastructure val outDirectory: File? = null, private val onIndependentPartCompilationEnd: GenerationStateEventCallback = GenerationStateEventCallback.DO_NOTHING, - val codegenFactory: CodegenFactory = DefaultCodegenFactory, + val codegenFactory: CodegenFactory = if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory else DefaultCodegenFactory, wantsDiagnostics: Boolean = true ) { abstract class GenerateClassFilter { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConfigurationKeys.java b/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConfigurationKeys.java index e44195247a6..e9cf1019b03 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConfigurationKeys.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConfigurationKeys.java @@ -82,4 +82,7 @@ public class JVMConfigurationKeys { public static final CompilerConfigurationKey> FRIEND_PATHS = CompilerConfigurationKey.create("friend module paths"); + + public static final CompilerConfigurationKey IR = + CompilerConfigurationKey.create("IR"); } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt index 2da7de16a19..eb4df96faa0 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt @@ -54,7 +54,7 @@ object JvmBackendFacade { } } - private fun createJvmBackendContext(psi2ir: Psi2IrTranslator, state: GenerationState): JvmBackendContext { + fun createJvmBackendContext(psi2ir: Psi2IrTranslator, state: GenerationState): JvmBackendContext { val jvmFileClassProvider = JvmFileClassProvider() psi2ir.add(jvmFileClassProvider) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt new file mode 100644 index 00000000000..1769624f02d --- /dev/null +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.backend.jvm + +import org.jetbrains.kotlin.codegen.* +import org.jetbrains.kotlin.codegen.context.PackageContext +import org.jetbrains.kotlin.codegen.state.GenerationState +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtFile + +object JvmIrCodegenFactory : CodegenFactory { + override fun createPackageCodegen(state: GenerationState, files: Collection, fqName: FqName, registry: PackagePartRegistry): PackageCodegen { + val impl = PackageCodegenImpl(state, files, fqName, registry) +// val psi2ir = Psi2IrTranslator() +// val psi2irContext = psi2ir.createGeneratorContext(state.module, state.bindingContext) +// val jvmBackendContext = JvmBackendFacade.createJvmBackendContext(psi2ir, state) + + + return object : PackageCodegen { + override fun generate(errorHandler: CompilationErrorHandler) { + JvmBackendFacade.doGenerateFiles(files, state, errorHandler) + } + + override fun generateClassOrObject(classOrObject: KtClassOrObject, packagePartContext: PackageContext) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getPackageFragment(): PackageFragmentDescriptor { + return impl.packageFragment + } + } + } + + override fun createMultifileClassCodegen(state: GenerationState, files: Collection, fqName: FqName, registry: PackagePartRegistry): MultifileClassCodegen { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java index 2cddd7705c7..5d5c4edd92e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java +++ b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java @@ -133,7 +133,11 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable { configuration.put(JVMConfigurationKeys.JVM_TARGET, JvmTarget.JVM_1_8); } - return getBytecodeForFile(ktFile, configuration, ir.isSelected()); + if (ir.isSelected()) { + configuration.put(JVMConfigurationKeys.IR, true); + } + + return getBytecodeForFile(ktFile, configuration); } @Override @@ -241,10 +245,10 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable { // public for tests @NotNull - public static String getBytecodeForFile(@NotNull KtFile ktFile, @NotNull CompilerConfiguration configuration, boolean newIRGenerator) { + public static String getBytecodeForFile(@NotNull KtFile ktFile, @NotNull CompilerConfiguration configuration) { GenerationState state; try { - state = compileSingleFile(ktFile, configuration, newIRGenerator); + state = compileSingleFile(ktFile, configuration); } catch (ProcessCanceledException e) { throw e; @@ -284,8 +288,7 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable { @NotNull public static GenerationState compileSingleFile( @NotNull final KtFile ktFile, - @NotNull CompilerConfiguration configuration, - boolean newIRGenerator + @NotNull CompilerConfiguration configuration ) { ResolutionFacade resolutionFacade = ResolutionUtils.getResolutionFacade(ktFile); @@ -324,11 +327,9 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable { ktFile.getProject(), ClassBuilderFactories.TEST, resolutionFacade.getModuleDescriptor(), bindingContext, toProcess, configuration, generateClassFilter ); - if (newIRGenerator) { - JvmBackendFacade.INSTANCE.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION); - } else { - KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION); - } + + KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION); + return state; } diff --git a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinDecompilerServiceImpl.kt b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinDecompilerServiceImpl.kt index b02382d55da..d4122f69d7a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinDecompilerServiceImpl.kt +++ b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinDecompilerServiceImpl.kt @@ -72,7 +72,7 @@ class KotlinDecompilerServiceImpl : KotlinDecompilerService { } fun bytecodeMapForSourceFile(file: KtFile): Map ByteArray> { - val generationState = KotlinBytecodeToolWindow.compileSingleFile(file, CompilerConfiguration.EMPTY, false) + val generationState = KotlinBytecodeToolWindow.compileSingleFile(file, CompilerConfiguration.EMPTY) val bytecodeMap = hashMapOf ByteArray>() generationState.factory.asList().filter { FileUtilRt.extensionEquals(it.relativePath, "class") }.forEach { diff --git a/idea/tests/org/jetbrains/kotlin/idea/internal/AbstractBytecodeToolWindowTest.kt b/idea/tests/org/jetbrains/kotlin/idea/internal/AbstractBytecodeToolWindowTest.kt index 9103472e9f8..d0dd589ea30 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/internal/AbstractBytecodeToolWindowTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/internal/AbstractBytecodeToolWindowTest.kt @@ -44,7 +44,7 @@ abstract class AbstractBytecodeToolWindowTest: KotlinLightCodeInsightFixtureTest if (InTextDirectivesUtils.getPrefixedBoolean(mainFileText, "// INLINE:") == false) { configuration.put(CommonConfigurationKeys.DISABLE_INLINE, true) } - val bytecodes = KotlinBytecodeToolWindow.getBytecodeForFile(file, configuration, false) + val bytecodes = KotlinBytecodeToolWindow.getBytecodeForFile(file, configuration) assert(bytecodes.contains("// ================")) { "The header \"// ================\" is missing.\n This means that there is an exception failed during compilation:\n$bytecodes" }