translator: translation state simplification
This commit is contained in:
Generated
+2
-2
@@ -19,8 +19,8 @@
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
<bytecodeTargetLevel>
|
||||
<module name="ast-kotlin_main" target="1.5" />
|
||||
<module name="ast-kotlin_test" target="1.5" />
|
||||
<module name="ast-kotlin_main" target="1.8" />
|
||||
<module name="ast-kotlin_test" target="1.8" />
|
||||
</bytecodeTargetLevel>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+1
-1
@@ -13,7 +13,7 @@
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
</project>
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id="ast-kotlin:main" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="ast-kotlin" external.system.module.type="sourceSet" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/../../build/classes/main" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../src/main">
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id="ast-kotlin:test" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="ast-kotlin" external.system.module.type="sourceSet" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
|
||||
<output-test url="file://$MODULE_DIR$/../../build/classes/test" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../src/test">
|
||||
|
||||
@@ -13,8 +13,6 @@ buildscript {
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'kotlin'
|
||||
|
||||
sourceCompatibility = 1.5
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import org.kotlinnative.translator.FileTranslator
|
||||
import org.kotlinnative.translator.TranslationState
|
||||
import org.kotlinnative.translator.parseAndAnalyze
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
@@ -10,7 +10,7 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
|
||||
val disposer = Disposer.newDisposable()
|
||||
val state = TranslationState(args.asList(), disposer)
|
||||
val state = parseAndAnalyze(args.asList(), disposer)
|
||||
|
||||
val files = state.environment.getSourceFiles()
|
||||
if (files.isEmpty()) {
|
||||
|
||||
@@ -20,65 +20,64 @@ import org.jetbrains.kotlin.utils.PathUtil
|
||||
import org.kotlinnative.translator.exceptions.TranslationException
|
||||
import java.util.*
|
||||
|
||||
class TranslationState(val environment: KotlinCoreEnvironment, val bindingContext: BindingContext) {
|
||||
|
||||
class TranslationState(sources: List<String>, disposer: Disposable) {
|
||||
|
||||
val environment: KotlinCoreEnvironment
|
||||
val bindingContext: BindingContext
|
||||
var functions = HashMap<String, FunctionCodegen>()
|
||||
var classes = HashMap<String, ClassCodegen>()
|
||||
|
||||
val variableManager = VariableManager()
|
||||
}
|
||||
|
||||
init {
|
||||
val configuration = CompilerConfiguration()
|
||||
val messageCollector = GroupingMessageCollector(object : MessageCollector {
|
||||
private var hasError = false
|
||||
fun parseAndAnalyze(sources: List<String>, disposer: Disposable): TranslationState {
|
||||
|
||||
override fun hasErrors(): Boolean = hasError
|
||||
val configuration = CompilerConfiguration()
|
||||
val messageCollector = GroupingMessageCollector(object : MessageCollector {
|
||||
private var hasError = false
|
||||
|
||||
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
|
||||
println("[report] $message")
|
||||
hasError = severity.isError || hasError
|
||||
}
|
||||
})
|
||||
override fun hasErrors(): Boolean = hasError
|
||||
|
||||
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
||||
configuration.put(JVMConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME)
|
||||
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
|
||||
println("[report] $message")
|
||||
hasError = severity.isError || hasError
|
||||
}
|
||||
})
|
||||
|
||||
configuration.addKotlinSourceRoots(sources)
|
||||
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
||||
configuration.put(JVMConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME)
|
||||
|
||||
environment = KotlinCoreEnvironment.createForProduction(disposer, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
bindingContext = analyze(environment)?.bindingContext ?: throw TranslationException()
|
||||
}
|
||||
configuration.addKotlinSourceRoots(sources)
|
||||
|
||||
fun analyze(environment: KotlinCoreEnvironment): AnalysisResult? {
|
||||
val collector = environment.configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
|
||||
val environment = KotlinCoreEnvironment.createForProduction(disposer, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
val bindingContext = analyze(environment)?.bindingContext ?: throw TranslationException()
|
||||
|
||||
val analyzer = AnalyzerWithCompilerReport(collector)
|
||||
analyzer.analyzeAndReport(environment.getSourceFiles(), object : AnalyzerWithCompilerReport.Analyzer {
|
||||
override fun analyze(): AnalysisResult {
|
||||
val sharedTrace = CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace()
|
||||
val moduleContext = TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project, environment.getModuleName())
|
||||
return TranslationState(environment, bindingContext)
|
||||
}
|
||||
|
||||
return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationWithCustomContext(
|
||||
moduleContext,
|
||||
environment.getSourceFiles(),
|
||||
sharedTrace,
|
||||
environment.configuration.get(JVMConfigurationKeys.MODULES),
|
||||
environment.configuration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS),
|
||||
JvmPackagePartProvider(environment))
|
||||
}
|
||||
fun analyze(environment: KotlinCoreEnvironment): AnalysisResult? {
|
||||
val collector = environment.configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
|
||||
|
||||
override fun reportEnvironmentErrors() {
|
||||
val files = environment.configuration.jvmClasspathRoots
|
||||
val runtimes = files.map { it.canonicalFile }.filter { it.name == PathUtil.KOTLIN_JAVA_RUNTIME_JAR && it.exists() }
|
||||
collector.report(CompilerMessageSeverity.ERROR, runtimes.joinToString { it.path }, CompilerMessageLocation.NO_LOCATION)
|
||||
println(runtimes.joinToString { it.toString() })
|
||||
}
|
||||
})
|
||||
val analyzer = AnalyzerWithCompilerReport(collector)
|
||||
analyzer.analyzeAndReport(environment.getSourceFiles(), object : AnalyzerWithCompilerReport.Analyzer {
|
||||
override fun analyze(): AnalysisResult {
|
||||
val sharedTrace = CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace()
|
||||
val moduleContext = TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project, environment.getModuleName())
|
||||
|
||||
return if (analyzer.hasErrors()) null else analyzer.analysisResult
|
||||
}
|
||||
return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationWithCustomContext(
|
||||
moduleContext,
|
||||
environment.getSourceFiles(),
|
||||
sharedTrace,
|
||||
environment.configuration.get(JVMConfigurationKeys.MODULES),
|
||||
environment.configuration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS),
|
||||
JvmPackagePartProvider(environment))
|
||||
}
|
||||
|
||||
override fun reportEnvironmentErrors() {
|
||||
val files = environment.configuration.jvmClasspathRoots
|
||||
val runtimes = files.map { it.canonicalFile }.filter { it.name == PathUtil.KOTLIN_JAVA_RUNTIME_JAR && it.exists() }
|
||||
collector.report(CompilerMessageSeverity.ERROR, runtimes.joinToString { it.path }, CompilerMessageLocation.NO_LOCATION)
|
||||
println(runtimes.joinToString { it.toString() })
|
||||
}
|
||||
})
|
||||
|
||||
return if (analyzer.hasErrors()) null else analyzer.analysisResult
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user