From 96cfa903e231af351a4f19170b148252cf1c6c7e Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Thu, 15 Sep 2016 15:23:49 +0300 Subject: [PATCH] minimal compiler to get ir from the source --- .../kotlin/cli/student/K2StudentLlvm.kt | 68 ++++++++++++++++--- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/experiments/student-backend/src/org/jetbrains/kotlin/cli/student/K2StudentLlvm.kt b/experiments/student-backend/src/org/jetbrains/kotlin/cli/student/K2StudentLlvm.kt index 010ef4c4c06..75f8494e51a 100644 --- a/experiments/student-backend/src/org/jetbrains/kotlin/cli/student/K2StudentLlvm.kt +++ b/experiments/student-backend/src/org/jetbrains/kotlin/cli/student/K2StudentLlvm.kt @@ -1,23 +1,73 @@ package org.jetbrains.kotlin.cli.student +import com.intellij.openapi.Disposable +import org.jetbrains.kotlin.analyzer.AnalysisResult import org.jetbrains.kotlin.cli.common.CLICompiler +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport +import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport +import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles +import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.config.CommonConfigurationKeys +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.config.Services +import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration +import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM /** * Created by minamoto on 14/09/16. */ -public class K2StudentLlvm : CLICompiler { +class K2StudentLlvm : CLICompiler() { + override fun setupPlatformSpecificArgumentsAndServices(configuration: CompilerConfiguration, arguments: K2StudentLlvmArguments, services: Services) {} + + override fun doExecute(arguments: K2StudentLlvmArguments, configuration: CompilerConfiguration, rootDisposable: Disposable): ExitCode { + configuration.put(CommonConfigurationKeys.MODULE_NAME, /*arguments.moduleName ?: */ JvmAbi.DEFAULT_MODULE_NAME) + val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, emptyList()) + val collector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) + val analyzerWithCompilerReport = AnalyzerWithCompilerReport(collector) + analyzerWithCompilerReport.analyzeAndReport( + environment.getSourceFiles(), object : AnalyzerWithCompilerReport.Analyzer { + override fun analyze(): AnalysisResult { + val sharedTrace = CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace() + val moduleContext = + TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project, environment.configuration) + + return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( + moduleContext, + environment.getSourceFiles(), + sharedTrace, + environment.configuration, + JvmPackagePartProvider(environment) + ) + } + override fun reportEnvironmentErrors() { + TODO(/* implement me*/) + //KotlinToJVMBytecodeCompiler.reportRuntimeConflicts(collector, environment.configuration.jvmClasspathRoots) + } + }) + + val translator = Psi2IrTranslator(Psi2IrConfiguration(false)) + val module = translator.generateModule(analyzerWithCompilerReport.analysisResult.moduleDescriptor, + environment.getSourceFiles(), + BindingContext.EMPTY) + return ExitCode.OK + } + override fun createArguments(): K2StudentLlvmArguments { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return K2StudentLlvmArguments() } - override fun setupPlatformSpecificArgumentsAndServices(configuration: CompilerConfiguration?, arguments: K2StudentLlvmArguments?, services: Services?) { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + companion object { + @JvmStatic fun main(args: Array) { + CLICompiler.doMain(K2StudentLlvm(), args) + } } +} - override fun doExecute(arguments: K2StudentLlvmArguments?, configuration: CompilerConfiguration?, rootDisposable: MediaDisposer.Disposable?): ExitCode { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } - -} \ No newline at end of file +fun main(args: Array) = K2StudentLlvm.main(args)