From 64e9f2a2f7f9095f081e18634454e47d4cbf8b7e Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Thu, 27 Oct 2016 12:45:54 +0300 Subject: [PATCH] minor refactoring in native to frontend interface --- .../bc/analyze/TopDownAnalyzerFacadeForBC.kt | 19 ---- .../org/jetbrains/kotlin/cli/bc/K2Native.kt | 89 +++++++++++-------- 2 files changed, 50 insertions(+), 58 deletions(-) delete mode 100644 backend.native/bc.frontend/src/org/jetbrains/kotlin/bc/analyze/TopDownAnalyzerFacadeForBC.kt diff --git a/backend.native/bc.frontend/src/org/jetbrains/kotlin/bc/analyze/TopDownAnalyzerFacadeForBC.kt b/backend.native/bc.frontend/src/org/jetbrains/kotlin/bc/analyze/TopDownAnalyzerFacadeForBC.kt deleted file mode 100644 index 841c05a961c..00000000000 --- a/backend.native/bc.frontend/src/org/jetbrains/kotlin/bc/analyze/TopDownAnalyzerFacadeForBC.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.jetbrains.kotlin.bc.analyze - -import org.jetbrains.kotlin.analyzer.AnalysisResult -import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.context.ModuleContext -import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.resolve.BindingTrace -import kotlin.reflect.jvm.internal.impl.descriptors.PackagePartProvider - - -fun analyze(moduleContext: ModuleContext, - files:Collection, - trace:BindingTrace, - configuration: CompilerConfiguration, - packagePartProvider: PackagePartProvider) : AnalysisResult { - val bindingContext = trace.bindingContext - val module = moduleContext.module - return AnalysisResult.success(bindingContext, module) -} diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt index 62bfbde9fa2..8b228bdc2b2 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt @@ -20,53 +20,64 @@ import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM import java.lang.System.out import java.util.* -import java.util.Collections.emptyList import kotlin.reflect.jvm.internal.impl.load.java.JvmAbi +class NativeAnalyzer(val environment: KotlinCoreEnvironment) : + AnalyzerWithCompilerReport.Analyzer { + override fun analyze(): AnalysisResult { + val sharedTrace = + CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace() + + TopDownAnalyzerFacadeForJVM.createContextWithSealedModule( + environment.project, environment.configuration) + return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( + environment.project, + environment.getSourceFiles(), + sharedTrace, + environment.configuration, + { scope -> JvmPackagePartProvider(environment, scope) } + ) + } + + override fun reportEnvironmentErrors() { + } +} + class K2Native : CLICompiler() { - override fun doExecute(arguments: K2NativeCompilerArguments, configuration: CompilerConfiguration, rootDisposable: Disposable): ExitCode { - configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME) + override fun doExecute(arguments : K2NativeCompilerArguments, + configuration : CompilerConfiguration, + rootDisposable: Disposable): ExitCode { + configuration.put(CommonConfigurationKeys.MODULE_NAME, + JvmAbi.DEFAULT_MODULE_NAME) + configuration.addKotlinSourceRoots(arguments.freeArgs) + val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, + configuration, Arrays.asList("extensions/common.xml")) - configuration.addKotlinSourceRoots(arguments.freeArgs) - val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, Arrays.asList("extensions/common.xml")) - val collector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) - val analyzerWithCompilerReport = AnalyzerWithCompilerReport(collector) + 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() + // Build AST and binding info. + analyzerWithCompilerReport.analyzeAndReport(environment.getSourceFiles(), + NativeAnalyzer(environment)) - /* replace this with native specific */ - val moduleContext = - TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project, environment.configuration) + // Translate AST to high level IR. + val translator = Psi2IrTranslator(Psi2IrConfiguration(false)) + val module = translator.generateModule( + analyzerWithCompilerReport.analysisResult.moduleDescriptor, + environment.getSourceFiles(), + analyzerWithCompilerReport.analysisResult.bindingContext) + // Emit LLVM code. + module.accept(DumpIrTreeVisitor(out), "") + emitLLVM(module, arguments.runtimeFile, arguments.outputFile) + return ExitCode.OK + } - return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( - environment.project, - environment.getSourceFiles(), - sharedTrace, - environment.configuration, - { scope -> JvmPackagePartProvider(environment, scope) } - ) - } - override fun reportEnvironmentErrors() { - - //TODO("implement me") - } - }) - - val translator = Psi2IrTranslator(Psi2IrConfiguration(false)) - val module = translator.generateModule(analyzerWithCompilerReport.analysisResult.moduleDescriptor, - environment.getSourceFiles(), - analyzerWithCompilerReport.analysisResult.bindingContext) - - module.accept(DumpIrTreeVisitor(out), "") - emitLLVM(module, arguments.runtimeFile, arguments.outputFile) - return ExitCode.OK - } - - override fun setupPlatformSpecificArgumentsAndServices(configuration: CompilerConfiguration, arguments: K2NativeCompilerArguments, services: Services) {} + override fun setupPlatformSpecificArgumentsAndServices( + configuration: CompilerConfiguration, + arguments : K2NativeCompilerArguments, + services : Services) {} override fun createArguments(): K2NativeCompilerArguments { return K2NativeCompilerArguments()